add BUILD file for python/util and refactor python/BUILD

PiperOrigin-RevId: 347652147
Change-Id: I97aa24d6a109ed5aec8fca9a9b5e3b2b1a96b760
diff --git a/tensorflow/core/framework/BUILD b/tensorflow/core/framework/BUILD
index a0c8c57..eec4af3 100644
--- a/tensorflow/core/framework/BUILD
+++ b/tensorflow/core/framework/BUILD
@@ -1245,6 +1245,7 @@
     visibility = [
         "//tensorflow/core:__pkg__",
         "//tensorflow/python:__pkg__",
+        "//tensorflow/python/util:__pkg__",
     ],
 )
 
diff --git a/tensorflow/core/util/BUILD b/tensorflow/core/util/BUILD
index e3e7d6e..fb40248 100644
--- a/tensorflow/core/util/BUILD
+++ b/tensorflow/core/util/BUILD
@@ -274,6 +274,7 @@
     visibility = [
         "//tensorflow/core:__pkg__",
         "//tensorflow/python:__pkg__",
+        "//tensorflow/python/util:__pkg__",
     ],
 )
 
@@ -460,6 +461,7 @@
     visibility = [
         "//tensorflow/core:__pkg__",
         "//tensorflow/python:__pkg__",
+        "//tensorflow/python/util:__pkg__",
     ],
     alwayslink = 1,
 )
@@ -474,6 +476,7 @@
         "//tensorflow/core/platform:__pkg__",
         "//tensorflow/python:__pkg__",
         "//tensorflow/python/eager:__pkg__",
+        "//tensorflow/python/util:__pkg__",
     ],
     deps = [
         "//tensorflow/core/platform:status",
diff --git a/tensorflow/python/BUILD b/tensorflow/python/BUILD
index 2c481f7..d141220 100644
--- a/tensorflow/python/BUILD
+++ b/tensorflow/python/BUILD
@@ -124,16 +124,10 @@
         "//third_party/py/tensorflow_core:__subpackages__",
     ],
     deps = [
-        ":_pywrap_checkpoint_reader",
         ":_pywrap_events_writer",
-        ":_pywrap_kernel_registry",
         ":_pywrap_py_exception_registry",
         ":_pywrap_python_op_gen",
         ":_pywrap_quantize_training",
-        ":_pywrap_stat_summarizer",
-        ":_pywrap_tfprof",
-        ":_pywrap_transform_graph",
-        ":_pywrap_util_port",
         ":_pywrap_utils",
         ":array_ops",
         ":audio_ops_gen",
@@ -198,7 +192,6 @@
         ":tf_item",
         ":tf_optimizer",
         ":training",
-        ":util",
         ":weights_broadcast_ops",
         ":while_v2",
         "//tensorflow/core:protos_all_py",
@@ -239,6 +232,13 @@
         "//tensorflow/python/tpu:tpu_noestimator",
         "//tensorflow/python/training:saver_test_utils",
         "//tensorflow/python/types",
+        "//tensorflow/python/util",
+        "//tensorflow/python/util:_pywrap_checkpoint_reader",
+        "//tensorflow/python/util:_pywrap_kernel_registry",
+        "//tensorflow/python/util:_pywrap_stat_summarizer",
+        "//tensorflow/python/util:_pywrap_tfprof",
+        "//tensorflow/python/util:_pywrap_transform_graph",
+        "//tensorflow/python/util:_pywrap_util_port",
         "//third_party/py/numpy",
     ],
 )
@@ -250,8 +250,8 @@
     ],
     deps = [
         ":tf_decorator",
-        ":tf_stack",
         "//tensorflow/python/types",
+        "//tensorflow/python/util:tf_stack",
     ],
 )
 
@@ -357,6 +357,22 @@
 )
 
 alias(
+    name = "util",
+    actual = "//tensorflow/python/util:util",
+    visibility = visibility + [
+        "//tensorflow:__pkg__",
+        "//third_party/py/tensorflow_core:__subpackages__",
+        "//third_party/py/tf_agents:__subpackages__",
+        "//third_party/py/tfx:__subpackages__",
+    ],
+)
+
+alias(
+    name = "tf_decorator",
+    actual = "//tensorflow/python/util:tf_decorator",
+)
+
+alias(
     name = "bfloat16_lib",
     actual = "//tensorflow/python/lib/core:bfloat16_lib",
 )
@@ -386,18 +402,6 @@
     ],
 )
 
-cc_library(
-    name = "kernel_registry",
-    srcs = ["util/kernel_registry.cc"],
-    hdrs = ["util/kernel_registry.h"],
-    deps = [
-        "//tensorflow/core:framework",
-        "//tensorflow/core:lib",
-        "//tensorflow/core:protos_all_cc",
-    ],
-    alwayslink = 1,
-)
-
 py_library(
     name = "pywrap_tf_session",
     srcs = ["client/pywrap_tf_session.py"],
@@ -454,46 +458,9 @@
     ),
 )
 
-tf_python_pybind_extension(
-    name = "_pywrap_tfprof",
-    srcs = ["util/tfprof_wrapper.cc"],
-    module_name = "_pywrap_tfprof",
-    deps = [
-        "//tensorflow/core:framework_headers_lib",
-        "//tensorflow/core:lib_headers_for_pybind",
-        "//tensorflow/core/profiler/internal:print_model_analysis_hdr",
-        "//third_party/eigen3",
-        "//third_party/python_runtime:headers",
-        "@com_google_absl//absl/strings",
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
+alias(
     name = "_pywrap_utils",
-    srcs = ["util/util_wrapper.cc"],
-    hdrs = ["util/util.h"],
-    module_name = "_pywrap_utils",
-    deps = [
-        "//tensorflow/python/lib/core:pybind11_lib",
-        "//third_party/python_runtime:headers",
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
-    name = "_pywrap_kernel_registry",
-    srcs = ["util/kernel_registry_wrapper.cc"],
-    hdrs = ["util/kernel_registry.h"],
-    module_name = "_pywrap_kernel_registry",
-    deps = [
-        "//tensorflow/core:framework_headers_lib",
-        "//tensorflow/core:lib_headers_for_pybind",
-        "//tensorflow/core:protos_all_cc",
-        "//tensorflow/python/lib/core:pybind11_lib",
-        "//third_party/python_runtime:headers",
-        "@pybind11",
-    ],
+    actual = "//tensorflow/python/util:_pywrap_utils",
 )
 
 tf_python_pybind_extension(
@@ -518,43 +485,6 @@
 )
 
 tf_python_pybind_extension(
-    name = "_pywrap_stat_summarizer",
-    srcs = ["util/stat_summarizer_wrapper.cc"],
-    module_name = "_pywrap_stat_summarizer",
-    deps = [
-        "//tensorflow/core:framework_headers_lib",
-        "//tensorflow/core:protos_all_cc",
-        "//third_party/eigen3",
-        "//third_party/python_runtime:headers",
-        "@com_google_absl//absl/memory",
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
-    name = "_pywrap_tensor_float_32_execution",
-    srcs = ["util/tensor_float_32.cc"],
-    hdrs = ["//tensorflow/core/platform:tensor_float_32_hdr"],
-    compatible_with = get_compatible_with_portable(),
-    module_name = "_pywrap_tensor_float_32_execution",
-    deps = [
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
-    name = "_pywrap_util_port",
-    srcs = ["util/port_wrapper.cc"],
-    hdrs = ["//tensorflow/core/util:port_hdrs"],
-    module_name = "_pywrap_util_port",
-    deps = [
-        "//tensorflow/core/util:port",
-        "//third_party/python_runtime:headers",
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
     name = "_pywrap_debug_events_writer",
     srcs = ["client/debug_events_writer_wrapper.cc"],
     module_name = "_pywrap_debug_events_writer",
@@ -588,48 +518,6 @@
     ],
 )
 
-tf_python_pybind_extension(
-    name = "_pywrap_transform_graph",
-    srcs = ["util/transform_graph_wrapper.cc"],
-    hdrs = ["//tensorflow/tools/graph_transforms:transform_graph_hdrs"],
-    module_name = "_pywrap_transform_graph",
-    deps = [
-        "//tensorflow/core:framework_headers_lib",
-        "//tensorflow/core:lib_headers_for_pybind",
-        "//tensorflow/core:protos_all_cc",
-        "//tensorflow/python/lib/core:pybind11_status",
-        "//third_party/python_runtime:headers",
-        "@pybind11",
-    ],
-)
-
-tf_python_pybind_extension(
-    name = "_pywrap_checkpoint_reader",
-    srcs = ["util/py_checkpoint_reader_wrapper.cc"],
-    hdrs = [
-        "//tensorflow/c:checkpoint_reader_hdrs",
-        "//tensorflow/c:headers",
-        "//tensorflow/c/eager:headers",
-        "//tensorflow/python/lib/core:ndarray_tensor_hdr",
-        "//tensorflow/python/lib/core:py_exception_registry_hdr",
-        "//tensorflow/python/lib/core:safe_ptr_hdr",
-    ],
-    module_name = "_pywrap_checkpoint_reader",
-    deps = [
-        "//tensorflow/core:lib_headers_for_pybind",
-        "//tensorflow/core:op_gen_lib",
-        "//tensorflow/core:protos_all_cc",
-        "//tensorflow/core/util/tensor_bundle:tensor_bundle_headers_lib",
-        "//tensorflow/python/lib/core:pybind11_lib",
-        "//tensorflow/python/lib/core:pybind11_status",
-        "//tensorflow/python/lib/core:safe_pyobject_ptr",
-        "//third_party/py/numpy:headers",
-        "//third_party/python_runtime:headers",
-        "@com_google_absl//absl/strings",
-        "@pybind11",
-    ],
-)
-
 # TODO(yanhuasun): Move this back and the source file back to lib/core directory.
 tf_python_pybind_extension(
     name = "_pywrap_py_exception_registry",
@@ -667,20 +555,6 @@
 # targets that depend are relying on cpp_python_util to pull in safe_ptr's
 # third_party/tensorflow/c:c_api_no_xla dependency, which registers
 # ops/gradients, rather than depending on it themselves.)
-cc_library(
-    name = "cpp_python_util",
-    srcs = ["util/util.cc"],
-    hdrs = ["util/util.h"],
-    deps = [
-        ":safe_ptr",
-        "//tensorflow/core:lib",
-        "//tensorflow/core:lib_internal",
-        "//tensorflow/python/lib/core:safe_pyobject_ptr",
-        "//third_party/python_runtime:headers",
-        "@com_google_absl//absl/memory",
-    ],
-)
-
 cc_header_only_library(
     name = "py_func_headers_lib",
     features = ["-parse_headers"],
@@ -727,50 +601,7 @@
         ":framework_for_generated_wrappers",
         ":io_ops",
         ":platform",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "decorator_utils_test",
-    srcs = ["util/decorator_utils_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":platform",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "deprecation_test",
-    srcs = ["util/deprecation_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":platform",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "dispatch_test",
-    srcs = ["util/dispatch_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":platform",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "keyword_args_test",
-    srcs = ["util/keyword_args_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -916,10 +747,9 @@
     ],
     srcs_version = "PY2AND3",
     deps = [
-        ":_pywrap_checkpoint_reader",
         ":_pywrap_debug_events_writer",
         ":_pywrap_events_writer",
-        ":_pywrap_kernel_registry",
+        "//tensorflow/python/util:_pywrap_kernel_registry",
         ":_pywrap_py_exception_registry",
         "//tensorflow/python/lib/core:_pywrap_py_func",  # TODO(b/142001480): remove once the bug is fixed.
         ":_pywrap_python_api_dispatcher",
@@ -928,10 +758,11 @@
         ":_pywrap_python_op_gen",
         ":_pywrap_quantize_training",
         "//tensorflow/python/platform:_pywrap_stacktrace_handler",
-        ":_pywrap_stat_summarizer",
-        ":_pywrap_tfprof",
-        ":_pywrap_transform_graph",
-        ":_pywrap_util_port",
+        "//tensorflow/python/util:_pywrap_checkpoint_reader",
+        "//tensorflow/python/util:_pywrap_stat_summarizer",
+        "//tensorflow/python/util:_pywrap_tfprof",
+        "//tensorflow/python/util:_pywrap_transform_graph",
+        "//tensorflow/python/util:_pywrap_util_port",
         ":_pywrap_utils",
         ":composite_tensor",
         ":config",
@@ -953,7 +784,7 @@
         ":tensor_spec",
         ":tensor_util",
         ":type_spec",
-        ":util",
+        "//tensorflow/python/util:util",
         "//third_party/py/numpy",
         "@six_archive//:six",
         "//tensorflow/python/eager:context",
@@ -1000,7 +831,7 @@
     srcs = ["framework/device_spec.py"],
     srcs_version = "PY2AND3",
     deps = [
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1046,7 +877,7 @@
         ":c_api_util",
         ":error_interpolation",
         ":pywrap_tf_session",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1072,10 +903,10 @@
         ":graph_to_function_def",
         ":op_def_registry",
         ":pywrap_tf_session",
-        ":util",
         ":variable_scope",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1166,8 +997,8 @@
     srcs_version = "PY2AND3",
     deps = [
         ":pywrap_tf_session",
-        ":util",
         "//tensorflow/core:protos_all_py",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1182,8 +1013,8 @@
         ":op_def_registry",
         ":platform",
         ":tensor_shape",
-        ":util",
         "//tensorflow/core:protos_all_py",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -1248,9 +1079,9 @@
     srcs = ["framework/op_def_util.cc"],
     hdrs = ["framework/op_def_util.h"],
     deps = [
-        ":cpp_python_util",
         "//tensorflow/core:protos_all_cc",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",  # buildcleaner: keep
         "@com_google_absl//absl/strings",
     ],
@@ -1271,10 +1102,10 @@
     ],
     hdrs = [
         "framework/op_def_util.h",
-        "util/util.h",
         "//tensorflow/c:headers",
         "//tensorflow/c/eager:headers",
         "//tensorflow/python/lib/core:safe_ptr_hdr",
+        "//tensorflow/python/util:util_hdr",
     ],
     module_name = "_op_def_util",
     deps = [
@@ -1300,7 +1131,6 @@
     srcs = ["framework/python_api_parameter_converter.cc"],
     hdrs = ["framework/python_api_parameter_converter.h"],
     deps = [
-        ":cpp_python_util",
         ":op_def_util_cc",
         ":python_api_info",
         ":python_tensor_converter",
@@ -1310,6 +1140,7 @@
         "//tensorflow/core/platform:status",
         "//tensorflow/python/eager:pywrap_tfe_lib",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",  # buildcleaner: keep
         "@com_google_absl//absl/strings",
     ],
@@ -1382,7 +1213,6 @@
     srcs = ["framework/python_api_info.cc"],
     hdrs = ["framework/python_api_info.h"],
     deps = [
-        ":cpp_python_util",
         ":op_def_util_cc",
         ":python_tensor_converter",
         "//tensorflow/core:framework",
@@ -1391,6 +1221,7 @@
         "//tensorflow/core/platform:status",
         "//tensorflow/python/eager:pywrap_tfe_lib",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",  # buildcleaner: keep
         "@com_google_absl//absl/strings",
     ],
@@ -1462,9 +1293,9 @@
     srcs = ["framework/python_api_dispatcher.cc"],
     hdrs = ["framework/python_api_dispatcher.h"],
     deps = [
-        ":cpp_python_util",
         "//tensorflow/core/platform:logging",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",  # buildcleaner: keep
         "@com_google_absl//absl/container:inlined_vector",
         "@com_google_absl//absl/strings",
@@ -1501,11 +1332,11 @@
     srcs = ["framework/python_tensor_converter.cc"],
     hdrs = ["framework/python_tensor_converter.h"],
     deps = [
-        ":cpp_python_util",
         "//tensorflow/c/eager:c_api",
         "//tensorflow/core:protos_all_cc",
         "//tensorflow/python/eager:pywrap_tfe_lib",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",  # buildcleaner: keep
         "@com_google_absl//absl/strings",
     ],
@@ -1589,7 +1420,6 @@
         ":tf2",
         ":traceable_stack",
         ":type_spec",
-        ":util",
         ":versions",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
@@ -1597,6 +1427,7 @@
         "//tensorflow/python/eager:monitoring",
         "//tensorflow/python/eager:tape",
         "//tensorflow/python/profiler:traceme",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -1634,9 +1465,9 @@
         ":tensor_conversion_registry",
         ":tensor_shape",
         ":type_spec",
-        ":util",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/types",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1661,9 +1492,9 @@
         ":sparse_tensor",
         ":tensor_array_ops",
         ":tensor_shape",
-        ":util",
         ":variable_scope",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1693,7 +1524,7 @@
         ":framework_ops",
         ":sparse_tensor",
         ":tensor_array_ops",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1724,8 +1555,7 @@
     srcs_version = "PY2AND3",
     deps = [
         ":framework_ops",
-        ":util",
-        "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1741,7 +1571,7 @@
         ":constant_op",
         ":platform",
         ":test_ops",
-        ":util",
+        "//tensorflow/python/util:util",
     ] + tf_additional_xla_deps_py(),
 )
 
@@ -1760,8 +1590,8 @@
     srcs_version = "PY2AND3",
     deps = [
         ":platform",
-        ":tf_stack",
-        ":util",
+        "//tensorflow/python/util",
+        "//tensorflow/python/util:tf_stack",
     ],
 )
 
@@ -1839,9 +1669,9 @@
     deps = [
         ":dtypes",
         ":tf2",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:monitoring",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1853,7 +1683,7 @@
     deps = [
         ":dtypes",
         ":tensor_shape",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -1867,7 +1697,7 @@
         ":dtypes",
         ":tensor_shape",
         ":type_spec",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -1879,9 +1709,9 @@
     deps = [
         ":errors",
         ":tensor_shape",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/types",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1891,7 +1721,7 @@
     srcs_version = "PY2AND3",
     visibility = ["//visibility:public"],
     deps = [
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -1941,7 +1771,6 @@
         ":session",
         ":tensor_array_ops",
         ":training",
-        ":util",
         ":variables",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:backprop",
@@ -1950,6 +1779,7 @@
         "//tensorflow/python/ops/ragged:ragged_tensor",
         "//tensorflow/python/ops/ragged:ragged_tensor_value",
         "//tensorflow/python/platform:_pywrap_stacktrace_handler",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@absl_py//absl/testing:parameterized",
         "@six_archive//:six",
@@ -2011,8 +1841,8 @@
         ":framework_ops",
         ":framework_test_combinations_lib",
         ":tf2",
-        ":util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -2021,7 +1851,7 @@
     srcs = ["framework/test_combinations.py"],
     srcs_version = "PY2AND3",
     deps = [
-        ":util",
+        "//tensorflow/python/util",
         "@absl_py//absl/testing:parameterized",
     ],
 )
@@ -2159,10 +1989,10 @@
         ":sparse_tensor",
         ":tensor_array_ops",
         ":tensor_shape",
-        ":util",
         ":variable_scope",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -2297,7 +2127,7 @@
         ":platform_test",
         ":test_ops",
         ":traceable_stack",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -2375,13 +2205,13 @@
         ":resources",
         ":test_ops",
         ":test_ops_2",
-        ":util",
         ":variable_scope",
         ":variables",
         ":while_v2",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3066,7 +2896,7 @@
         ":sparse_tensor",
         ":tensor_shape",
         ":tensor_util",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
@@ -3079,7 +2909,7 @@
     deps = [
         ":bitwise_ops_gen",
         ":framework",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3129,7 +2959,7 @@
         ":framework",
         ":framework_for_generated_wrappers",
         ":set_ops_gen",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3156,7 +2986,7 @@
         ":math_ops",
         ":sparse_tensor",
         ":tensor_util",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -3314,9 +3144,9 @@
         ":tensor_array_ops",
         ":tensor_shape",
         ":tf2",
-        ":tf_should_use",
-        ":util",
         "//tensorflow/core:protos_all_py",
+        "//tensorflow/python/util",
+        "//tensorflow/python/util:tf_should_use",
         "@six_archive//:six",
     ],
 )
@@ -3338,11 +3168,11 @@
         ":control_flow_util",
         ":control_flow_v2_func_graphs",
         ":framework_ops",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/distribute:distribute_lib",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3363,7 +3193,7 @@
         ":control_flow_util",
         ":control_flow_util_v2",
         ":framework_ops",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3434,10 +3264,10 @@
         ":graph_to_function_def",
         ":handle_data_util",
         ":pywrap_tensorflow",
-        ":util",
         "//tensorflow/python/compat",
         "//tensorflow/python/data/ops:dataset_ops",
         "//tensorflow/python/eager:function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3466,8 +3296,8 @@
         ":tensor_array_ops",
         ":tensor_shape",
         ":tensor_util",
-        ":util",
         "//tensorflow/python/eager:function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3577,7 +3407,7 @@
         ":framework_ops",
         ":protos_all_py",
         ":pywrap_tf_session",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3630,8 +3460,8 @@
         ":random_grad",
         ":tensor_array_ops",
         ":unconnected_gradients",
-        ":util",
         "//tensorflow/python/ops/linalg/sparse",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3656,11 +3486,11 @@
         ":resource_variable_ops",
         ":tensor_util",
         ":unconnected_gradients",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:backprop",
         "//tensorflow/python/eager:backprop_util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
@@ -3702,7 +3532,7 @@
     srcs = ["ops/unconnected_gradients.py"],
     srcs_version = "PY2AND3",
     deps = [
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3750,8 +3580,8 @@
         ":nn_ops_gen",
         ":random_ops",
         ":string_ops",
-        ":util",
         ":variables",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -3768,7 +3598,7 @@
         ":linalg_ops_impl",
         ":math_ops",
         ":random_ops",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -3786,7 +3616,7 @@
         ":math_ops",
         ":random_ops",
         ":stateless_random_ops",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -3887,8 +3717,8 @@
         ":logging_ops_gen",
         ":platform",
         ":string_ops",
-        ":util",
         "//tensorflow/python/compat",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -3905,8 +3735,8 @@
         ":math_ops",
         ":sparse_tensor",
         ":string_ops",
-        ":util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -3956,9 +3786,9 @@
         ":state_ops",
         ":state_ops_gen",
         ":tensor_shape",
-        ":util",
         "//tensorflow/python/compat",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -3972,7 +3802,7 @@
         ":control_flow_ops",
         ":framework_for_generated_wrappers",
         ":math_ops",
-        ":tf_should_use",
+        "//tensorflow/python/util:tf_should_use",
     ],
 )
 
@@ -3990,11 +3820,11 @@
         ":pywrap_tf_session",
         ":resource_variable_ops_gen",
         ":tensor_shape",
-        ":util",
         ":variables",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:tape",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4009,8 +3839,8 @@
         ":framework_ops",
         ":resource_variable_ops_gen",
         ":tensor_array_ops",
-        ":util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4054,9 +3884,9 @@
         ":nn_ops",
         ":nn_ops_gen",
         ":sparse_ops",
-        ":util",
         ":variables",
         "//tensorflow/python/platform:device_context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4234,9 +4064,9 @@
         ":math_ops",
         ":rnn_cell",
         ":tensor_array_ops",
-        ":util",
         ":variable_scope",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4257,11 +4087,11 @@
         ":nn_ops",
         ":partitioned_variables",
         ":random_ops",
-        ":util",
         ":variable_scope",
         ":variables",
         "//tensorflow/python/keras/layers/legacy_rnn:rnn_cell_impl",
         "//tensorflow/python/keras/layers/legacy_rnn:rnn_cell_wrapper_impl",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4297,7 +4127,7 @@
         ":array_ops",
         ":data_flow_ops_gen",
         ":framework_for_generated_wrappers",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4327,7 +4157,7 @@
         ":framework_for_generated_wrappers",
         ":math_ops",
         ":sparse_ops_gen",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -4425,11 +4255,11 @@
         ":sets",
         ":sparse_ops",
         ":state_ops",
-        ":util",
         ":variable_scope",
         ":variables",
         ":weights_broadcast_ops",
         "//tensorflow/python/distribute:distribute_lib",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4542,7 +4372,6 @@
         ":template",
         ":tensor_array_grad",
         ":tensor_array_ops",
-        ":util",
         ":variable_scope",
         ":variables",
         "//tensorflow/python/compiler",
@@ -4553,6 +4382,7 @@
         "//tensorflow/python/ops/ragged",
         "//tensorflow/python/ops/structured",
         "//tensorflow/python/training/experimental:loss_scaling_gradient_tape",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4574,7 +4404,7 @@
         ":resource_variable_ops_gen",
         ":state_ops_gen",
         ":tensor_shape",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4586,7 +4416,7 @@
         ":framework",
         ":framework_for_generated_wrappers",
         ":string_ops_gen",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4608,10 +4438,10 @@
         ":summary_ops_gen",
         ":tensor_util",
         ":training_util",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:profiler",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -4623,10 +4453,10 @@
     deps = [
         ":framework_for_generated_wrappers",
         ":platform",
-        ":util",
         ":variable_scope",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -4657,8 +4487,8 @@
         ":tensor_shape",
         ":tensor_util",
         ":tf2",
-        ":tf_should_use",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_should_use",
     ],
 )
 
@@ -4675,10 +4505,10 @@
         ":resource_variable_ops",
         ":tensor_shape",
         ":tf2",
-        ":util",
         ":variables",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:monitoring",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -4695,11 +4525,11 @@
         ":math_ops",
         ":state_ops",
         ":tensor_shape",
-        ":tf_should_use",
-        ":util",
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/training/tracking:base",
+        "//tensorflow/python/util",
+        "//tensorflow/python/util:tf_should_use",
     ],
 )
 
@@ -4736,7 +4566,7 @@
     srcs_version = "PY2AND3",
     deps = [
         ":user_ops_gen",
-        ":util",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -4783,11 +4613,11 @@
         ":tensor_array_grad",
         ":tensor_array_ops",
         ":training",
-        ":util",
         ":variable_scope",
         ":variables",
         ":while_v2",
         "//tensorflow/python/eager:def_function",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -5202,174 +5032,15 @@
         ":platform",
         ":session",
         ":session_ops",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
 )
 
-# Leaf library: may not depend on anything else inside TensorFlow.
-py_strict_library(
-    name = "tf_export",
-    srcs = ["util/tf_export.py"],
-    compatible_with = get_compatible_with_portable(),
-    srcs_version = "PY2AND3",
-    visibility = ["//tensorflow:__subpackages__"],
-    deps = [
-        ":tf_decorator",
-    ],
-)
-
-tf_py_test(
-    name = "tf_export_test",
-    srcs = ["util/tf_export_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":platform",
-        ":util",
-    ],
-)
-
-# Leaf library: may not depend on anything else inside TensorFlow.
-# TODO(mdan): Move this utility outside of TF.
-py_strict_library(
-    name = "tf_decorator",
-    srcs = [
-        "util/tf_contextlib.py",
-        "util/tf_decorator.py",
-        "util/tf_inspect.py",
-    ],
-    compatible_with = get_compatible_with_portable(),
-    srcs_version = "PY2AND3",
-    visibility = [
-        "//tensorflow:__subpackages__",
-        # TODO(mdan): Remove these dependencies.
-        "//third_party/py/tf_slim:__subpackages__",
-        "//learning/deepmind/research/language/translation/lm:__subpackages__",
-    ],
-    deps = [
-        "@six_archive//:six",
-    ],
-)
-
 # Note: this is a heavyweight library specialized for TensorFlow graphs. Do not use for
 # other purposes.
 
-tf_python_pybind_extension(
-    name = "_tf_stack",
-    srcs = ["util/tf_stack.cc"],
-    hdrs = [
-        "//tensorflow/c:headers",
-        "//tensorflow/c/eager:headers",
-        # Using header directly is required to avoid ODR violations.
-        "util/stack_trace.h",
-    ],
-    # TODO(b/138203821): change to "util._tf_stack" once the bug is fixed.
-    module_name = "_tf_stack",
-    deps = [
-        "@com_google_absl//absl/algorithm:container",
-        "@com_google_absl//absl/strings",
-        "@com_google_absl//absl/strings:str_format",
-        "@com_google_absl//absl/types:span",
-        "@pybind11",
-        "//third_party/python_runtime:headers",  # buildcleaner: keep
-        "//tensorflow/c:pywrap_required_hdrs",
-        "//tensorflow/core/common_runtime:core_cpu_headers_lib",
-        "//tensorflow/core/framework:pywrap_required_hdrs",
-        "//tensorflow/core/platform:path",
-    ] + if_static([
-        ":stack_trace",
-    ]),
-)
-
-py_strict_library(
-    name = "tf_stack",
-    srcs = ["util/tf_stack.py"],
-    srcs_version = "PY2AND3",
-    # TODO(mdan): Remove public visibility.
-    visibility = ["//visibility:public"],
-    deps = [
-        ":_tf_stack",
-        "@six_archive//:six",
-    ],
-)
-
-tf_py_test(
-    name = "tf_stack_test",
-    srcs = ["util/tf_stack_test.py"],
-    python_version = "PY3",
-    tags = ["no_windows"],  # TODO(b/175394927): enable the test for all oss windows build
-    deps = [
-        ":client_testlib",
-        ":tf_export",
-        ":tf_stack",
-    ],
-)
-
-cc_library(
-    name = "stack_trace",
-    srcs = ["util/stack_trace.cc"],
-    hdrs = ["util/stack_trace.h"],
-    deps = [
-        "//tensorflow/core/platform:str_util",
-        "//tensorflow/core/platform:stringpiece",
-        "//tensorflow/core/util:abstract_stack_trace",
-        "//third_party/python_runtime:headers",  # buildcleaner: keep
-        "@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/container:inlined_vector",
-        "@com_google_absl//absl/types:optional",
-    ],
-)
-
-cc_library(
-    name = "function_parameter_canonicalizer",
-    srcs = ["util/function_parameter_canonicalizer.cc"],
-    hdrs = ["util/function_parameter_canonicalizer.h"],
-    deps = [
-        "//tensorflow/core/platform:logging",
-        "//tensorflow/core/platform:macros",
-        "//tensorflow/python/lib/core:py_util",
-        "//tensorflow/python/lib/core:safe_pyobject_ptr",
-        "//third_party/python_runtime:headers",  # buildcleaner: keep
-        "@com_google_absl//absl/container:flat_hash_set",
-        "@com_google_absl//absl/types:span",
-    ],
-)
-
-tf_python_pybind_extension(
-    name = "_function_parameter_canonicalizer_binding_for_test",
-    testonly = True,
-    srcs = ["util/function_parameter_canonicalizer_binding_for_test.cc"],
-    hdrs = [
-        "util/function_parameter_canonicalizer.h",
-    ],
-    module_name = "_function_parameter_canonicalizer_binding_for_test",
-    deps = [
-        "//tensorflow/core:lib",
-        "//tensorflow/python/lib/core:safe_pyobject_ptr_required_hdrs",
-        "//third_party/python_runtime:headers",  # buildcleaner: keep
-        "@com_google_absl//absl/types:span",
-        "@pybind11",
-    ],
-)
-
-tf_py_test(
-    name = "function_parameter_canonicalizer_test",
-    srcs = ["util/function_parameter_canonicalizer_test.py"],
-    python_version = "PY3",
-    tags = [
-        "no_pip",  # b/168621686
-        "no_windows",  # b/169275019
-    ],
-    deps = [
-        ":_function_parameter_canonicalizer_binding_for_test",
-        ":client_testlib",
-    ],
-)
-
 py_library(
     name = "global_test_configuration",
     compatible_with = get_compatible_with_portable(),
@@ -5377,201 +5048,15 @@
            tf_enable_mlir_bridge(),
 )
 
-py_library(
-    name = "util",
-    srcs = glob(
-        ["util/**/*.py"],
-        exclude = [
-            "util/example_parser*",
-            "util/tf_contextlib.py",
-            "util/tf_should_use.py",
-            "util/tf_export.py",
-            "util/tf_stack.py",
-            "util/tf_decorator.py",
-            "util/**/*_test.py",
-        ],
-    ),
-    compatible_with = get_compatible_with_portable(),
-    srcs_version = "PY2AND3",
-    visibility = visibility + [
-        "//tensorflow:__pkg__",
-        "//third_party/py/tensorflow_core:__subpackages__",
-        "//third_party/py/tf_agents:__subpackages__",
-        "//third_party/py/tfx:__subpackages__",
-    ],
-    deps = [
-        ":_pywrap_tensor_float_32_execution",
-        # global_test_configuration is added here because all major tests depend on this
-        # library. It isn't possible to add these test dependencies via tensorflow.bzl's
-        # py_test because not all tensorflow tests use tensorflow.bzl's py_test.
-        ":global_test_configuration",
-        ":tf_decorator",
-        ":tf_export",
-        "@org_python_pypi_backports_weakref",
-        "@com_google_protobuf//:protobuf_python",
-        "//third_party/py/numpy",
-        "@six_archive//:six",
-        "@wrapt",
-        "//tensorflow/tools/docs:doc_controls",
-        "//tensorflow/tools/compatibility:all_renames_v2",
-    ],
-)
-
-tf_py_test(
-    name = "object_identity_test",
-    size = "small",
-    srcs = ["util/object_identity_test.py"],
-    python_version = "PY3",
-)
-
-# Placeholder for intenal nest_test comments.
-tf_py_test(
-    name = "util_nest_test",
-    size = "small",
-    srcs = ["util/nest_test.py"],
-    main = "util/nest_test.py",
-    python_version = "PY3",
-    deps = [":util_nest_test_main_lib"],
-)
-
-py_library(
-    name = "util_nest_test_main_lib",
-    testonly = True,
-    srcs = ["util/nest_test.py"],
-    deps = [
-        ":array_ops",
-        ":client_testlib",
-        ":framework",
-        ":framework_for_generated_wrappers",
-        ":math_ops",
-        ":util",
-        "//third_party/py/numpy",
-        "@absl_py//absl/testing:parameterized",
-    ],
-)
-
-tf_py_test(
-    name = "util_serialization_test",
-    size = "small",
-    srcs = ["util/serialization_test.py"],
-    main = "util/serialization_test.py",
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "function_utils_test",
-    srcs = ["util/function_utils_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "tf_contextlib_test",
-    size = "small",
-    srcs = ["util/tf_contextlib_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-    ],
-)
-
-tf_py_test(
-    name = "tf_decorator_test",
-    size = "small",
-    srcs = ["util/tf_decorator_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-    ],
-)
-
-py_library(
-    name = "tf_should_use",
-    srcs = ["util/tf_should_use.py"],
-    srcs_version = "PY2AND3",
-    deps = [
-        ":framework_ops",
-        ":util",
-        "//tensorflow/python/eager:context",
-        "@six_archive//:six",
-    ],
-)
-
-tf_py_test(
-    name = "tf_should_use_test",
-    size = "small",
-    srcs = ["util/tf_should_use_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":tf_should_use",
-    ],
-)
-
-tf_py_test(
-    name = "tf_inspect_test",
-    size = "small",
-    srcs = ["util/tf_inspect_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-    ],
-)
-
-py_library(
-    name = "util_example_parser_configuration",
-    srcs = ["util/example_parser_configuration.py"],
-    srcs_version = "PY2AND3",
-    visibility = ["//visibility:public"],
-    deps = [
-        ":framework",
-        ":framework_for_generated_wrappers",
-        "//tensorflow/core:protos_all_py",
-    ],
-)
-
-tf_py_test(
-    name = "lock_util_test",
-    size = "small",
-    srcs = ["util/lock_util_test.py"],
-    main = "util/lock_util_test.py",
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-        "@absl_py//absl/testing:parameterized",
-    ],
-)
-
-tf_py_test(
-    name = "module_wrapper_test",
-    size = "small",
-    srcs = ["util/module_wrapper_test.py"],
-    python_version = "PY3",
-    deps = [
-        ":client_testlib",
-        ":util",
-        "//tensorflow/tools/compatibility:all_renames_v2",
-        "@six_archive//:six",
-    ],
-)
+# `tree.compat` requires visibility exception to test against `nest_test`
+# to facilitate convergence between `tree.compat` and `nest`.
 
 tf_proto_library(
     name = "protos_all",
     srcs = glob(
         ["**/*.proto"],
         exclude = [
-            "util/protobuf/compare_test.proto",
+            "//tensorflow/python/util:compare_test_proto_src",
             "framework/cpp_shape_inference.proto",
         ],
     ),
@@ -5580,13 +5065,6 @@
 )
 
 tf_proto_library(
-    name = "compare_test_proto",
-    testonly = 1,
-    srcs = ["util/protobuf/compare_test.proto"],
-    cc_api_version = 2,
-)
-
-tf_proto_library(
     name = "cpp_shape_inference_proto",
     srcs = ["framework/cpp_shape_inference.proto"],
     cc_api_version = 2,
@@ -5596,37 +5074,6 @@
 )
 
 tf_py_test(
-    name = "protobuf_compare_test",
-    size = "small",
-    srcs = ["util/protobuf/compare_test.py"],
-    main = "util/protobuf/compare_test.py",
-    python_version = "PY3",
-    tags = ["no_pip"],  # compare_test_pb2 proto is not available in pip.
-    deps = [
-        ":compare_test_proto_py",
-        ":platform_test",
-        ":util",
-        "@six_archive//:six",
-    ],
-)
-
-tf_py_test(
-    name = "util_example_parser_configuration_test",
-    size = "small",
-    srcs = ["util/example_parser_configuration_test.py"],
-    main = "util/example_parser_configuration_test.py",
-    python_version = "PY3",
-    deps = [
-        ":array_ops",
-        ":client",
-        ":client_testlib",
-        ":framework_for_generated_wrappers",
-        ":parsing_ops",
-        ":util_example_parser_configuration",
-    ],
-)
-
-tf_py_test(
     name = "events_writer_test",
     size = "small",
     srcs = ["client/events_writer_test.py"],
@@ -5636,7 +5083,7 @@
         ":framework_test_lib",
         ":lib",
         ":platform_test",
-        ":util",
+        "//tensorflow/python/util",
     ],
 )
 
@@ -5749,15 +5196,15 @@
         ":bfloat16_lib",
         ":cost_analyzer_lib",
         ":model_analyzer_lib",
-        ":cpp_python_util",
-        ":function_parameter_canonicalizer",
-        ":kernel_registry",
-        ":numpy_lib",
-        ":safe_ptr",
-        ":py_exception_registry",
         "//tensorflow/core/distributed_runtime/rpc:grpc_rpc_factory_registration",
         "//tensorflow/core/distributed_runtime/rpc:grpc_server_lib",
         "//tensorflow/core/distributed_runtime/rpc:grpc_session",
+        "//tensorflow/python/util:cpp_python_util",
+        "//tensorflow/python/util:function_parameter_canonicalizer",
+        "//tensorflow/python/util:kernel_registry",
+        ":numpy_lib",
+        ":safe_ptr",
+        ":py_exception_registry",
         "//tensorflow/python/lib/core:py_func_lib",
         "//tensorflow/python/lib/io:py_record_reader_lib",
         "//tensorflow/python/lib/core:pybind11_absl",
@@ -5826,8 +5273,8 @@
     srcs = [
         ":bfloat16_lib",  # bfloat16
         ":cost_analyzer_lib",  # cost_analyzer
-        ":cpp_python_util",  # util
-        ":kernel_registry",  # kernel_registry
+        "//tensorflow/python/util:cpp_python_util",
+        "//tensorflow/python/util:kernel_registry",
         ":model_analyzer_lib",  # model_analyzer
         ":ndarray_tensor",  # checkpoint_reader
         ":numpy_lib",  # checkpoint_reader
@@ -6005,7 +5452,7 @@
         ":platform",
         ":pywrap_tensorflow",
         ":session_ops",
-        ":util",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@wrapt",
     ],
@@ -6059,8 +5506,8 @@
         ":platform_test",
         ":state_ops",
         ":training",
-        ":util",
         ":variables",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
@@ -6090,8 +5537,8 @@
         ":platform_test",
         ":state_ops",
         ":training",
-        ":util",
         ":variables",
+        "//tensorflow/python/util",
         "//third_party/py/numpy",
     ],
 )
@@ -6136,7 +5583,7 @@
         ":math_ops",
         ":platform_test",
         ":training",
-        ":util",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -6260,9 +5707,9 @@
         ":summary_op_util",
         ":summary_ops_gen",
         ":summary_ops_v2",
-        ":util",
         "//tensorflow/python/distribute:summary_op_util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util",
         "@six_archive//:six",
     ],
 )
@@ -7204,7 +6651,6 @@
     name = "_pywrap_tfe",
     srcs = ["tfe_wrapper.cc"],
     hdrs = [
-        "util/util.h",
         "//tensorflow/c:headers",
         "//tensorflow/c/eager:headers",
         "//tensorflow/c/eager:pywrap_required_hdrs",
@@ -7216,6 +6662,7 @@
         "//tensorflow/python/lib/core:numpy_hdr",
         "//tensorflow/python/lib/core:py_exception_registry_hdr",
         "//tensorflow/python/lib/core:safe_ptr_hdr",
+        "//tensorflow/python/util:util_hdr",
     ],
     module_name = "_pywrap_tfe",
     # Only include TensorFlow header-only targets here.
diff --git a/tensorflow/python/compiler/mlir/BUILD b/tensorflow/python/compiler/mlir/BUILD
index 7e19379..1e4316d 100644
--- a/tensorflow/python/compiler/mlir/BUILD
+++ b/tensorflow/python/compiler/mlir/BUILD
@@ -11,7 +11,7 @@
     srcs_version = "PY2AND3",
     deps = [
         "//tensorflow/python:pywrap_mlir",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/data/ops/BUILD b/tensorflow/python/data/ops/BUILD
index d69bf6a..fae8051 100644
--- a/tensorflow/python/data/ops/BUILD
+++ b/tensorflow/python/data/ops/BUILD
@@ -56,9 +56,9 @@
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_spec",
         "//tensorflow/python:tf2",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/data/util:convert",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/data/util/nest.py b/tensorflow/python/data/util/nest.py
index 9f9bcde..69f4da5 100644
--- a/tensorflow/python/data/util/nest.py
+++ b/tensorflow/python/data/util/nest.py
@@ -37,8 +37,8 @@
 
 import six as _six
 
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.framework import sparse_tensor as _sparse_tensor
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util.compat import collections_abc as _collections_abc
 
 
diff --git a/tensorflow/python/distribute/BUILD b/tensorflow/python/distribute/BUILD
index 6fb015e..5198b3f 100644
--- a/tensorflow/python/distribute/BUILD
+++ b/tensorflow/python/distribute/BUILD
@@ -48,10 +48,10 @@
         "//tensorflow/python:platform",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:def_function",
         "//tensorflow/python/eager:executor",
+        "//tensorflow/python/util:tf_export",
         "//tensorflow/tools/docs:doc_controls",
         "@enum34_archive//:enum",
         "@six_archive//:six",
@@ -255,7 +255,6 @@
         "//tensorflow/python:pywrap_tfe",
         "//tensorflow/python:summary_ops_v2",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
@@ -263,6 +262,7 @@
         "//tensorflow/python/autograph/impl",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:def_function",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -286,7 +286,6 @@
         "//tensorflow/python:pywrap_tfe",
         "//tensorflow/python:summary_ops_v2",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
@@ -294,6 +293,7 @@
         "//tensorflow/python/autograph/impl",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:def_function",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -345,12 +345,12 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:resource_variable_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python/distribute/cluster_resolver:cluster_resolver_lib",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -408,11 +408,11 @@
         "//tensorflow/python:errors",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training",
         "//tensorflow/python:util",
         "//tensorflow/python/distribute/cluster_resolver:cluster_resolver_lib",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -715,7 +715,6 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:resource_variable_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:type_spec",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
@@ -725,6 +724,7 @@
         "//tensorflow/python/training/saving:saveable_object_util",
         "//tensorflow/python/training/tracking:base",
         "//tensorflow/python/types",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -836,12 +836,12 @@
         ":tpu_strategy",
         "//tensorflow/python:platform",
         "//tensorflow/python:tf2",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/distribute/cluster_resolver:cluster_resolver_lib",
         "//tensorflow/python/distribute/cluster_resolver:tpu_cluster_resolver_py",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/eager:remote",
         "//tensorflow/python/tpu:tpu_lib",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -1087,13 +1087,13 @@
         "//tensorflow/python:partitioned_variables",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:tensor_shape",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:type_spec",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/saved_model:save_context",
         "//tensorflow/python/training/saving:saveable_object_util",
         "//tensorflow/python/training/tracking:base",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/eager/BUILD b/tensorflow/python/eager/BUILD
index 621ae55..cf9f2da 100644
--- a/tensorflow/python/eager/BUILD
+++ b/tensorflow/python/eager/BUILD
@@ -64,16 +64,16 @@
         "//tensorflow/core/platform:types",
         "//tensorflow/core/profiler/lib:traceme",
         "//tensorflow/core/util:abstract_stack_trace",
-        "//tensorflow/python:cpp_python_util",
         "//tensorflow/python:ndarray_tensor",
         "//tensorflow/python:ndarray_tensor_bridge",
         "//tensorflow/python:numpy_lib",
         "//tensorflow/python:py_exception_registry",
-        "//tensorflow/python:stack_trace",
         "//tensorflow/python/lib/core:py_seq_tensor",
         "//tensorflow/python/lib/core:py_util",
         "//tensorflow/python/lib/core:safe_ptr",
         "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//tensorflow/python/util:cpp_python_util",
+        "//tensorflow/python/util:stack_trace",
         "//third_party/py/numpy:headers",
         "//third_party/python_runtime:headers",
         "@com_google_absl//absl/container:flat_hash_map",
@@ -232,10 +232,10 @@
         "//tensorflow/c/eager:c_api",
         "//tensorflow/c/eager:c_api_experimental",
         "//tensorflow/c/eager:custom_device_testutil",
-        "//tensorflow/python:cpp_python_util",
         "//tensorflow/python:pybind11_lib",
         "//tensorflow/python/lib/core:pybind11_status",
         "//tensorflow/python/lib/core:safe_ptr",
+        "//tensorflow/python/util:cpp_python_util",
         "//third_party/python_runtime:headers",
         "@pybind11",
     ],
diff --git a/tensorflow/python/eager/backprop.py b/tensorflow/python/eager/backprop.py
index f079943..3d6faf2 100644
--- a/tensorflow/python/eager/backprop.py
+++ b/tensorflow/python/eager/backprop.py
@@ -28,7 +28,6 @@
 import six
 
 from tensorflow.python import pywrap_tfe
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.eager import backprop_util
 from tensorflow.python.eager import context
 from tensorflow.python.eager import execute
@@ -49,6 +48,7 @@
 from tensorflow.python.ops import resource_variable_ops
 from tensorflow.python.ops.unconnected_gradients import UnconnectedGradients
 from tensorflow.python.platform import tf_logging as logging
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import nest
 from tensorflow.python.util import tf_contextlib
 from tensorflow.python.util import tf_inspect
diff --git a/tensorflow/python/eager/function.py b/tensorflow/python/eager/function.py
index a41dc8e..828af8f 100644
--- a/tensorflow/python/eager/function.py
+++ b/tensorflow/python/eager/function.py
@@ -33,7 +33,6 @@
 
 from tensorflow.core.framework import attr_value_pb2
 from tensorflow.core.framework import function_pb2
-from tensorflow.python import _pywrap_utils
 from tensorflow.python import pywrap_tfe
 from tensorflow.python.client import pywrap_tf_session
 from tensorflow.python.eager import backprop
@@ -67,6 +66,7 @@
 from tensorflow.python.platform import tf_logging as logging
 from tensorflow.python.profiler import trace
 from tensorflow.python.saved_model import save_context
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import compat
 from tensorflow.python.util import function_utils
 from tensorflow.python.util import lazy_loader
diff --git a/tensorflow/python/feature_column/BUILD b/tensorflow/python/feature_column/BUILD
index b3c6e06..e1293a6 100644
--- a/tensorflow/python/feature_column/BUILD
+++ b/tensorflow/python/feature_column/BUILD
@@ -84,7 +84,6 @@
         "//tensorflow/python:sparse_tensor",
         "//tensorflow/python:string_ops",
         "//tensorflow/python:tensor_shape",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
@@ -92,6 +91,7 @@
         "//tensorflow/python/eager:context",
         "//tensorflow/python/training/tracking",
         "//tensorflow/python/training/tracking:data_structures",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
diff --git a/tensorflow/python/framework/composite_tensor.py b/tensorflow/python/framework/composite_tensor.py
index 22dbe7c..531a8a5 100644
--- a/tensorflow/python/framework/composite_tensor.py
+++ b/tensorflow/python/framework/composite_tensor.py
@@ -22,8 +22,8 @@
 
 import six
 
-from tensorflow.python import _pywrap_utils
 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import nest
 from tensorflow.python.util.tf_export import tf_export
 
diff --git a/tensorflow/python/framework/config.py b/tensorflow/python/framework/config.py
index 2691665..a9bb996 100644
--- a/tensorflow/python/framework/config.py
+++ b/tensorflow/python/framework/config.py
@@ -18,8 +18,8 @@
 from __future__ import division
 from __future__ import print_function
 
-from tensorflow.python import _pywrap_tensor_float_32_execution
 from tensorflow.python.eager import context
+from tensorflow.python.util import _pywrap_tensor_float_32_execution
 from tensorflow.python.util import deprecation
 from tensorflow.python.util.tf_export import tf_export
 
diff --git a/tensorflow/python/framework/op_def_library.py b/tensorflow/python/framework/op_def_library.py
index 016af65..f6d93e2 100644
--- a/tensorflow/python/framework/op_def_library.py
+++ b/tensorflow/python/framework/op_def_library.py
@@ -26,13 +26,13 @@
 from tensorflow.core.framework import tensor_pb2
 from tensorflow.core.framework import tensor_shape_pb2
 from tensorflow.core.framework import types_pb2
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.framework import dtypes
 from tensorflow.python.framework import op_callbacks
 from tensorflow.python.framework import op_def_registry
 from tensorflow.python.framework import ops
 from tensorflow.python.framework import tensor_shape
 from tensorflow.python.platform import tf_logging as logging
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import compat
 from tensorflow.python.util import tf_contextlib
 
diff --git a/tensorflow/python/framework/sparse_tensor.py b/tensorflow/python/framework/sparse_tensor.py
index 28e21a8..d96a137 100644
--- a/tensorflow/python/framework/sparse_tensor.py
+++ b/tensorflow/python/framework/sparse_tensor.py
@@ -23,7 +23,6 @@
 import numpy as np
 
 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
-from tensorflow.python import _pywrap_utils
 from tensorflow.python import tf2
 from tensorflow.python.framework import composite_tensor
 from tensorflow.python.framework import constant_op
@@ -35,6 +34,7 @@
 from tensorflow.python.framework import type_spec
 from tensorflow.python.ops import gen_sparse_ops
 from tensorflow.python.types import internal
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util.tf_export import tf_export
 
 # pylint: disable=protected-access
diff --git a/tensorflow/python/framework/tensor_spec.py b/tensorflow/python/framework/tensor_spec.py
index 20b776d..68c2320 100644
--- a/tensorflow/python/framework/tensor_spec.py
+++ b/tensorflow/python/framework/tensor_spec.py
@@ -20,12 +20,12 @@
 
 import numpy as np
 
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.framework import common_shapes
 from tensorflow.python.framework import dtypes
 from tensorflow.python.framework import ops
 from tensorflow.python.framework import tensor_shape
 from tensorflow.python.framework import type_spec
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util.tf_export import tf_export
 
 
diff --git a/tensorflow/python/framework/test_util.py b/tensorflow/python/framework/test_util.py
index d7772f0..6d550e27 100644
--- a/tensorflow/python/framework/test_util.py
+++ b/tensorflow/python/framework/test_util.py
@@ -43,7 +43,6 @@
 
 from tensorflow.core.framework import graph_pb2
 from tensorflow.core.protobuf import rewriter_config_pb2
-from tensorflow.python import _pywrap_util_port
 from tensorflow.python import tf2
 from tensorflow.python.client import device_lib
 from tensorflow.python.client import pywrap_tf_session
@@ -82,6 +81,7 @@
 from tensorflow.python.platform import googletest
 from tensorflow.python.platform import tf_logging as logging
 from tensorflow.python.training import server_lib
+from tensorflow.python.util import _pywrap_util_port
 from tensorflow.python.util import compat
 from tensorflow.python.util import deprecation
 from tensorflow.python.util import nest
diff --git a/tensorflow/python/framework/type_spec.py b/tensorflow/python/framework/type_spec.py
index fa48ca8..80b6933 100644
--- a/tensorflow/python/framework/type_spec.py
+++ b/tensorflow/python/framework/type_spec.py
@@ -20,16 +20,16 @@
 
 import abc
 import collections
-
 import re
+
 import numpy as np
 import six
 
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.framework import composite_tensor
 from tensorflow.python.framework import dtypes
 from tensorflow.python.framework import tensor_shape
 from tensorflow.python.platform import tf_logging as logging
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import compat
 from tensorflow.python.util import nest
 from tensorflow.python.util import tf_decorator
diff --git a/tensorflow/python/keras/applications/BUILD b/tensorflow/python/keras/applications/BUILD
index 4d23718..fbd9de7 100644
--- a/tensorflow/python/keras/applications/BUILD
+++ b/tensorflow/python/keras/applications/BUILD
@@ -41,7 +41,6 @@
     deps = [
         "//tensorflow/python:lib",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras:activations",
         "//tensorflow/python/keras:backend",
         "//tensorflow/python/keras:models",
@@ -49,6 +48,7 @@
         "//tensorflow/python/keras/layers",
         "//tensorflow/python/keras/utils:data_utils",
         "//tensorflow/python/keras/utils:layer_utils",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
diff --git a/tensorflow/python/keras/distribute/BUILD b/tensorflow/python/keras/distribute/BUILD
index 98f30e7..ec450c3 100644
--- a/tensorflow/python/keras/distribute/BUILD
+++ b/tensorflow/python/keras/distribute/BUILD
@@ -897,11 +897,11 @@
         "//tensorflow/python:dtypes",
         "//tensorflow/python:errors",
         "//tensorflow/python:summary_ops_v2",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:training_lib",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/training/tracking:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/keras/engine/BUILD b/tensorflow/python/keras/engine/BUILD
index ef26a14..e6b7dc5 100644
--- a/tensorflow/python/keras/engine/BUILD
+++ b/tensorflow/python/keras/engine/BUILD
@@ -1,7 +1,10 @@
 # Description:
 #   Contains the Keras engine API (internal TensorFlow version).
 
+# buildifier: disable=same-origin-load
 load("//tensorflow:tensorflow.bzl", "tf_py_test")
+
+# buildifier: disable=same-origin-load
 load("//tensorflow:tensorflow.bzl", "cuda_py_test")
 
 package(
@@ -135,7 +138,7 @@
         "//tensorflow/python:tensor_spec",
         "//tensorflow/python:tensor_util",
         "//tensorflow/python:tf2",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/autograph/core",
diff --git a/tensorflow/python/keras/feature_column/BUILD b/tensorflow/python/keras/feature_column/BUILD
index 8266173..e736913 100644
--- a/tensorflow/python/keras/feature_column/BUILD
+++ b/tensorflow/python/keras/feature_column/BUILD
@@ -45,10 +45,10 @@
     deps = [
         ":base_feature_layer",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/feature_column:feature_column_v2",
         "//tensorflow/python/keras:backend",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -61,8 +61,8 @@
         ":base_feature_layer",
         ":dense_features",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/feature_column:feature_column_v2",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -125,9 +125,9 @@
         "//tensorflow/python:array_ops",
         "//tensorflow/python:check_ops",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/feature_column:feature_column_v2",
         "//tensorflow/python/keras:backend",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/keras/layers/preprocessing/BUILD b/tensorflow/python/keras/layers/preprocessing/BUILD
index 33d201a..d13473a 100644
--- a/tensorflow/python/keras/layers/preprocessing/BUILD
+++ b/tensorflow/python/keras/layers/preprocessing/BUILD
@@ -58,11 +58,11 @@
         "//tensorflow/python:resources",
         "//tensorflow/python:sparse_tensor",
         "//tensorflow/python:tensor_spec",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras/engine",
         "//tensorflow/python/keras/utils:tf_utils",
         "//tensorflow/python/ops/parallel_for:control_flow_ops",
         "//tensorflow/python/ops/ragged:ragged_functional_ops",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -81,11 +81,11 @@
         "//tensorflow/python:sparse_tensor",
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_spec",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras/engine",
         "//tensorflow/python/keras/utils:tf_utils",
         "//tensorflow/python/ops/ragged:ragged_array_ops",
         "//tensorflow/python/ops/ragged:ragged_tensor",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -105,11 +105,11 @@
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_spec",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras/engine",
         "//tensorflow/python/keras/utils:tf_utils",
         "//tensorflow/python/ops/ragged:ragged_functional_ops",
         "//tensorflow/python/ops/ragged:ragged_tensor",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -132,7 +132,6 @@
         "//tensorflow/python:stateless_random_ops",
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variables",
         "//tensorflow/python/compat",
         "//tensorflow/python/eager:context",
@@ -140,6 +139,7 @@
         "//tensorflow/python/keras/engine",
         "//tensorflow/python/keras/engine:input_spec",
         "//tensorflow/python/keras/utils:control_flow_util",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -179,10 +179,10 @@
         "//tensorflow/python:init_ops",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:tensor_shape",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/keras:backend",
         "//tensorflow/python/keras/engine",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -198,8 +198,8 @@
         ":index_lookup",
         ":table_utils",
         "//tensorflow/python:dtypes",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras/engine",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -242,7 +242,6 @@
         "//tensorflow/python:string_ops",
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_spec",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/data/ops:dataset_ops",
         "//tensorflow/python/keras:backend",
         "//tensorflow/python/keras/engine",
@@ -250,6 +249,7 @@
         "//tensorflow/python/keras/utils:tf_utils",
         "//tensorflow/python/ops/ragged:ragged_functional_ops",
         "//tensorflow/python/ops/ragged:ragged_string_ops",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -272,13 +272,13 @@
         "//tensorflow/python:sparse_tensor",
         "//tensorflow/python:tensor_shape",
         "//tensorflow/python:tensor_spec",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/keras:backend",
         "//tensorflow/python/keras/engine",
         "//tensorflow/python/keras/engine:input_spec",
         "//tensorflow/python/keras/utils:layer_utils",
         "//tensorflow/python/ops/ragged:ragged_tensor",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -308,8 +308,8 @@
         ":index_lookup",
         ":table_utils",
         "//tensorflow/python:dtypes",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/keras/engine",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/keras/optimizer_v2/BUILD b/tensorflow/python/keras/optimizer_v2/BUILD
index 2c5660c..138741d 100644
--- a/tensorflow/python/keras/optimizer_v2/BUILD
+++ b/tensorflow/python/keras/optimizer_v2/BUILD
@@ -83,8 +83,8 @@
         "//tensorflow/python:dtypes",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/ops/resource_variable_ops.py b/tensorflow/python/ops/resource_variable_ops.py
index 6cda36d..f58dd5c 100644
--- a/tensorflow/python/ops/resource_variable_ops.py
+++ b/tensorflow/python/ops/resource_variable_ops.py
@@ -27,7 +27,6 @@
 
 from tensorflow.core.framework import attr_value_pb2
 from tensorflow.core.framework import variable_pb2
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.client import pywrap_tf_session
 from tensorflow.python.eager import context
 from tensorflow.python.eager import tape
@@ -53,6 +52,7 @@
 # pylint: enable=wildcard-import
 from tensorflow.python.training.tracking import base as trackable
 from tensorflow.python.types import core
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import compat
 from tensorflow.python.util.deprecation import deprecated
 
diff --git a/tensorflow/python/ops/variables.py b/tensorflow/python/ops/variables.py
index 4e79ec9..387cde1 100644
--- a/tensorflow/python/ops/variables.py
+++ b/tensorflow/python/ops/variables.py
@@ -27,7 +27,6 @@
 from tensorflow.core.framework import attr_value_pb2
 from tensorflow.core.framework import variable_pb2
 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
-from tensorflow.python import _pywrap_utils
 from tensorflow.python.eager import context
 from tensorflow.python.framework import dtypes
 from tensorflow.python.framework import ops
@@ -41,6 +40,7 @@
 from tensorflow.python.ops import state_ops
 from tensorflow.python.platform import tf_logging as logging
 from tensorflow.python.training.tracking import base as trackable
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util import compat
 from tensorflow.python.util import object_identity
 from tensorflow.python.util import tf_should_use
diff --git a/tensorflow/python/platform/BUILD b/tensorflow/python/platform/BUILD
index f9f8424..024fcbc 100644
--- a/tensorflow/python/platform/BUILD
+++ b/tensorflow/python/platform/BUILD
@@ -46,10 +46,10 @@
     deps = [
         ":build_info",
         "//tensorflow/core:protos_all_py",
-        "//tensorflow/python:_pywrap_util_port",
         "//tensorflow/python:pywrap_tfe",
         "//tensorflow/python:util",
         "//tensorflow/python/lib/io:lib",
+        "//tensorflow/python/util:_pywrap_util_port",
         "@absl_py//absl/flags",
         "@rules_python//python/runfiles",
         "@six_archive//:six",
diff --git a/tensorflow/python/profiler/BUILD b/tensorflow/python/profiler/BUILD
index 9996f5c..42e542c 100644
--- a/tensorflow/python/profiler/BUILD
+++ b/tensorflow/python/profiler/BUILD
@@ -88,8 +88,8 @@
         ":option_builder",
         ":tfprof_logger",
         "//tensorflow/core/profiler:protos_all_py",
-        "//tensorflow/python:_pywrap_tfprof",
         "//tensorflow/python:errors",
+        "//tensorflow/python/util:_pywrap_tfprof",
         "@six_archive//:six",
     ],
 )
@@ -229,8 +229,8 @@
         "//tensorflow:internal",
     ],
     deps = [
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/profiler/internal:_pywrap_traceme",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/profiler/internal/model_analyzer_testlib.py b/tensorflow/python/profiler/internal/model_analyzer_testlib.py
index 459822c..a9b03b1 100644
--- a/tensorflow/python/profiler/internal/model_analyzer_testlib.py
+++ b/tensorflow/python/profiler/internal/model_analyzer_testlib.py
@@ -19,7 +19,6 @@
 
 import contextlib
 
-from tensorflow.python import _pywrap_tfprof as print_mdl
 from tensorflow.python.framework import dtypes
 from tensorflow.python.ops import array_ops
 from tensorflow.python.ops import init_ops
@@ -32,6 +31,7 @@
 from tensorflow.python.ops import variable_scope
 from tensorflow.python.profiler import model_analyzer
 from tensorflow.python.training import gradient_descent
+from tensorflow.python.util import _pywrap_tfprof as print_mdl
 from tensorflow.python.util import compat
 
 
diff --git a/tensorflow/python/profiler/model_analyzer.py b/tensorflow/python/profiler/model_analyzer.py
index 12ef107..4d8f097 100644
--- a/tensorflow/python/profiler/model_analyzer.py
+++ b/tensorflow/python/profiler/model_analyzer.py
@@ -27,12 +27,12 @@
 from google.protobuf import message
 from tensorflow.core.profiler import tfprof_options_pb2
 from tensorflow.core.profiler import tfprof_output_pb2
-from tensorflow.python import _pywrap_tfprof as print_mdl
 from tensorflow.python.eager import context
 from tensorflow.python.framework import errors
 from tensorflow.python.framework import ops
 from tensorflow.python.profiler import option_builder
 from tensorflow.python.profiler import tfprof_logger
+from tensorflow.python.util import _pywrap_tfprof as print_mdl
 from tensorflow.python.util.tf_export import tf_export
 
 _DEFAULT_PROFILE_OPTIONS = 0
diff --git a/tensorflow/python/profiler/profile_context.py b/tensorflow/python/profiler/profile_context.py
index e8e9ebd..6566550 100644
--- a/tensorflow/python/profiler/profile_context.py
+++ b/tensorflow/python/profiler/profile_context.py
@@ -25,12 +25,12 @@
 import threading
 
 from tensorflow.core.protobuf import config_pb2
-from tensorflow.python import _pywrap_tfprof as print_mdl
 from tensorflow.python.client import session
 from tensorflow.python.framework import errors
 from tensorflow.python.framework import ops
 from tensorflow.python.platform import gfile
 from tensorflow.python.profiler import model_analyzer
+from tensorflow.python.util import _pywrap_tfprof as print_mdl
 from tensorflow.python.util import compat
 
 WARMUP_STEPS = 10
diff --git a/tensorflow/python/saved_model/BUILD b/tensorflow/python/saved_model/BUILD
index 1043d51..772ec7b 100644
--- a/tensorflow/python/saved_model/BUILD
+++ b/tensorflow/python/saved_model/BUILD
@@ -42,21 +42,21 @@
     name = "constants",
     srcs = ["constants.py"],
     srcs_version = "PY2AND3",
-    deps = ["//tensorflow/python:tf_export"],
+    deps = ["//tensorflow/python/util:tf_export"],
 )
 
 py_strict_library(
     name = "signature_constants",
     srcs = ["signature_constants.py"],
     srcs_version = "PY2AND3",
-    deps = ["//tensorflow/python:tf_export"],
+    deps = ["//tensorflow/python/util:tf_export"],
 )
 
 py_strict_library(
     name = "tag_constants",
     srcs = ["tag_constants.py"],
     srcs_version = "PY2AND3",
-    deps = ["//tensorflow/python:tf_export"],
+    deps = ["//tensorflow/python/util:tf_export"],
 )
 
 py_strict_library(
@@ -75,9 +75,9 @@
         "//tensorflow/python:lib",
         "//tensorflow/python:platform",
         "//tensorflow/python:saver",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -97,9 +97,9 @@
         "//tensorflow/python:lib",
         "//tensorflow/python:platform",
         "//tensorflow/python:saver",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -137,8 +137,8 @@
         ":signature_def_utils",
         ":tag_constants",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -153,9 +153,9 @@
         "//tensorflow/python:control_flow_ops",
         "//tensorflow/python:framework_for_generated_wrappers",
         "//tensorflow/python:lookup_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -205,9 +205,9 @@
         "//tensorflow/python:framework_for_generated_wrappers",
         "//tensorflow/python:lib",
         "//tensorflow/python:sparse_tensor",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -238,8 +238,8 @@
         "//tensorflow/core:protos_all_py",
         "//tensorflow/python:errors",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -347,7 +347,6 @@
         "//tensorflow/python:platform",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:versions",
         "//tensorflow/python/eager:context",
@@ -360,6 +359,7 @@
         "//tensorflow/python/training/tracking:base",
         "//tensorflow/python/training/tracking:graph_view",
         "//tensorflow/python/training/tracking:util",
+        "//tensorflow/python/util:tf_export",
         "@absl_py//absl/logging",
     ],
 )
@@ -407,7 +407,6 @@
         "//tensorflow/python:lookup_ops",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:tensor_util",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/distribute:distribute_lib",
@@ -421,6 +420,7 @@
         "//tensorflow/python/training/tracking:base",
         "//tensorflow/python/training/tracking:graph_view",
         "//tensorflow/python/training/tracking:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -604,8 +604,8 @@
     name = "save_options",
     srcs = ["save_options.py"],
     deps = [
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
         "@enum34_archive//:enum",
         "@six_archive//:six",
     ],
@@ -615,7 +615,7 @@
     name = "load_options",
     srcs = ["load_options.py"],
     deps = [
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -628,8 +628,8 @@
         ":loader",
         "//tensorflow/python:lib",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/tools/BUILD b/tensorflow/python/tools/BUILD
index 8e40d51..dc126a1 100644
--- a/tensorflow/python/tools/BUILD
+++ b/tensorflow/python/tools/BUILD
@@ -274,8 +274,8 @@
     visibility = ["//visibility:public"],
     deps = [
         "//tensorflow/python",  # TODO(b/34059704): remove when fixed
-        "//tensorflow/python:_pywrap_kernel_registry",
         "//tensorflow/python:platform",
+        "//tensorflow/python/util:_pywrap_kernel_registry",
     ],
 )
 
diff --git a/tensorflow/python/tools/selective_registration_header_lib.py b/tensorflow/python/tools/selective_registration_header_lib.py
index ff2b3db..a6fbd2a 100644
--- a/tensorflow/python/tools/selective_registration_header_lib.py
+++ b/tensorflow/python/tools/selective_registration_header_lib.py
@@ -28,9 +28,9 @@
 
 from google.protobuf import text_format
 from tensorflow.core.framework import graph_pb2
-from tensorflow.python import _pywrap_kernel_registry
 from tensorflow.python.platform import gfile
 from tensorflow.python.platform import tf_logging
+from tensorflow.python.util import _pywrap_kernel_registry
 
 # Usually, we use each graph node to induce registration of an op and
 # corresponding kernel; nodes without a corresponding kernel (perhaps due to
diff --git a/tensorflow/python/tpu/BUILD b/tensorflow/python/tpu/BUILD
index 234ca52..0d5f30d 100644
--- a/tensorflow/python/tpu/BUILD
+++ b/tensorflow/python/tpu/BUILD
@@ -97,7 +97,7 @@
     deps = [
         ":topology",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -171,7 +171,7 @@
     srcs_version = "PY2AND3",
     deps = [
         "//tensorflow/core/protobuf/tpu:topology_proto_py",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -288,12 +288,12 @@
         "//tensorflow/python:function",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:tensor_shape",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python/compiler/xla",
         "//tensorflow/python/distribute:device_util",
         "//tensorflow/python/distribute:distribute_lib",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
         "@absl_py//absl/logging",
         "@enum34_archive//:enum",
@@ -478,7 +478,7 @@
     name = "tpu_name_util",
     srcs = ["tpu_name_util.py"],
     deps = [
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/training/BUILD b/tensorflow/python/training/BUILD
index 0c7dbb1..543e3d2 100644
--- a/tensorflow/python/training/BUILD
+++ b/tensorflow/python/training/BUILD
@@ -113,9 +113,9 @@
         ":warm_starting_util",
         "//tensorflow/python:learning_rate_decay",
         "//tensorflow/python:sdca_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/training/experimental:loss_scale_optimizer",
         "//tensorflow/python/training/experimental:mixed_precision",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -139,7 +139,7 @@
         ":training_ops",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -154,7 +154,7 @@
         "//tensorflow/python:constant_op",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -170,7 +170,7 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:init_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -186,8 +186,8 @@
         "//tensorflow/python:math_ops",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -197,7 +197,7 @@
     srcs_version = "PY2AND3",
     deps = [
         "//tensorflow/python:errors",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -226,11 +226,11 @@
         "//tensorflow/python:io_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:resource_variable_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
         "//tensorflow/python/distribute:distribute_lib",
         "//tensorflow/python/training/saving:saveable_object_util",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -242,8 +242,8 @@
     deps = [
         "//tensorflow/python:errors",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -256,7 +256,7 @@
         ":server_lib",
         "//tensorflow/python:device",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -297,7 +297,7 @@
         "//tensorflow/python:constant_op",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -311,7 +311,7 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:resource_variable_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -335,10 +335,10 @@
         "//tensorflow/python:sparse_tensor",
         "//tensorflow/python:summary",
         "//tensorflow/python:tensor_shape",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -352,7 +352,7 @@
         ":training_ops",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -368,11 +368,11 @@
         "//tensorflow/python:init_ops",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
         "//tensorflow/python/distribute:distribute_lib",
         "//tensorflow/python/distribute:reduce_util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -390,7 +390,6 @@
         "//tensorflow/python:math_ops",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
@@ -399,6 +398,7 @@
         "//tensorflow/python/eager:backprop",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/training/tracking:base",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -413,7 +413,7 @@
         "//tensorflow/python:constant_op",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -426,7 +426,7 @@
         ":training_ops",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -436,8 +436,8 @@
     srcs_version = "PY2AND3",
     deps = [
         "//tensorflow/python:_pywrap_quantize_training",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -450,9 +450,9 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:session",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -474,7 +474,7 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:init_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -488,8 +488,8 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:session",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/distribute:distribute_lib",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -532,11 +532,11 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
         "//tensorflow/python/distribute:distribute_lib",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -573,10 +573,10 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
         "//tensorflow/python/training/saving:saveable_object_util",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -795,11 +795,11 @@
     name = "py_checkpoint_reader",
     srcs = ["py_checkpoint_reader.py"],
     deps = [
-        "//tensorflow/python:_pywrap_checkpoint_reader",
         "//tensorflow/python:dtypes",
         "//tensorflow/python:errors",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:_pywrap_checkpoint_reader",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -818,10 +818,10 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:lib",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -867,13 +867,13 @@
         "//tensorflow/python:platform",
         "//tensorflow/python:session",
         "//tensorflow/python:string_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/training/saving:saveable_object",
         "//tensorflow/python/training/saving:saveable_object_util",
         "//tensorflow/python/training/tracking:base",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
     ],
 )
@@ -996,8 +996,8 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:init_ops",
         "//tensorflow/python:platform",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variable_scope",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
         "@six_archive//:six",
     ],
@@ -1007,7 +1007,7 @@
     name = "session_run_hook",
     srcs = ["session_run_hook.py"],
     srcs_version = "PY2AND3",
-    deps = ["//tensorflow/python:tf_export"],
+    deps = ["//tensorflow/python/util:tf_export"],
 )
 
 py_library(
@@ -1025,10 +1025,10 @@
         "//tensorflow/python:lookup_ops",
         "//tensorflow/python:platform",
         "//tensorflow/python:summary",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -1065,8 +1065,8 @@
     deps = [
         "//tensorflow/python:errors",
         "//tensorflow/python:pywrap_tf_session",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -1082,10 +1082,10 @@
         "//tensorflow/python:platform",
         "//tensorflow/python:resource_variable_ops",
         "//tensorflow/python:state_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
         "//tensorflow/python/eager:context",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
@@ -1351,10 +1351,10 @@
         "//tensorflow/python:platform",
         "//tensorflow/python:resources",
         "//tensorflow/python:summary",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variables",
         "//tensorflow/python/distribute:distribute_coordinator_context",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
diff --git a/tensorflow/python/training/experimental/BUILD b/tensorflow/python/training/experimental/BUILD
index 0e43766..239ea2b 100644
--- a/tensorflow/python/training/experimental/BUILD
+++ b/tensorflow/python/training/experimental/BUILD
@@ -15,7 +15,6 @@
         "//tensorflow/python:dtypes",
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
         "//tensorflow/python:variable_scope",
         "//tensorflow/python:variables",
@@ -23,6 +22,7 @@
         "//tensorflow/python/distribute:reduce_util",
         "//tensorflow/python/eager:context",
         "//tensorflow/python/training/tracking:base",
+        "//tensorflow/python/util:tf_export",
         "@six_archive//:six",
     ],
 )
@@ -37,9 +37,9 @@
         "//tensorflow/python:framework_ops",
         "//tensorflow/python:math_ops",
         "//tensorflow/python:smart_cond",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python/distribute:distribute_lib",
         "//tensorflow/python/training:optimizer",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/training/py_checkpoint_reader.py b/tensorflow/python/training/py_checkpoint_reader.py
index 83ab6e2..e3165e2 100644
--- a/tensorflow/python/training/py_checkpoint_reader.py
+++ b/tensorflow/python/training/py_checkpoint_reader.py
@@ -17,10 +17,10 @@
 from __future__ import division
 from __future__ import print_function
 
-from tensorflow.python._pywrap_checkpoint_reader import CheckpointReader
 from tensorflow.python.framework import dtypes
 from tensorflow.python.framework import errors_impl
 from tensorflow.python.util import compat
+from tensorflow.python.util._pywrap_checkpoint_reader import CheckpointReader
 from tensorflow.python.util.tf_export import tf_export
 
 
diff --git a/tensorflow/python/training/saving/BUILD b/tensorflow/python/training/saving/BUILD
index 218ce9d..1edad74 100644
--- a/tensorflow/python/training/saving/BUILD
+++ b/tensorflow/python/training/saving/BUILD
@@ -16,7 +16,7 @@
     name = "checkpoint_options",
     srcs = ["checkpoint_options.py"],
     deps = [
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
     ],
 )
 
diff --git a/tensorflow/python/types/BUILD b/tensorflow/python/types/BUILD
index d48f066..ed08419 100644
--- a/tensorflow/python/types/BUILD
+++ b/tensorflow/python/types/BUILD
@@ -33,7 +33,7 @@
     ],
     deps = [
         ":doc_typealias",
-        "//tensorflow/python:tf_export",
+        "//tensorflow/python/util:tf_export",
         "//third_party/py/numpy",
         "@typing_extensions_archive//:typing_extensions",
     ],
diff --git a/tensorflow/python/util/BUILD b/tensorflow/python/util/BUILD
new file mode 100644
index 0000000..292e4d1
--- /dev/null
+++ b/tensorflow/python/util/BUILD
@@ -0,0 +1,627 @@
+# Tensorflow util package
+
+load("//tensorflow:tensorflow.bzl", "py_strict_library")
+
+# buildifier: disable=same-origin-load
+load("//tensorflow:tensorflow.bzl", "get_compatible_with_portable")
+
+# buildifier: disable=same-origin-load
+load("//tensorflow:tensorflow.bzl", "tf_python_pybind_extension")
+
+# buildifier: disable=same-origin-load
+load("//tensorflow:tensorflow.bzl", "tf_py_test")
+
+# buildifier: disable=same-origin-load
+load("//tensorflow/core/platform:build_config.bzl", "tf_proto_library")  # @unused
+load("//tensorflow/core/platform:build_config_root.bzl", "if_static")
+
+visibility = [
+    "//engedu/ml/tf_from_scratch:__pkg__",
+    "//third_party/cloud_tpu/convergence_tools:__subpackages__",
+    "//third_party/mlperf:__subpackages__",
+    "//tensorflow:internal",
+    "//tensorflow/lite/toco/python:__pkg__",
+    "//tensorflow_models:__subpackages__",
+    "//tensorflow_model_optimization:__subpackages__",
+    "//third_party/py/cleverhans:__subpackages__",
+    "//third_party/py/launchpad:__subpackages__",
+    "//third_party/py/reverb:__subpackages__",
+    "//third_party/py/neural_structured_learning:__subpackages__",
+    "//third_party/py/tensorflow_examples:__subpackages__",
+    "//third_party/py/tf_agents:__subpackages__",  # For benchmarks.
+    "//third_party/py/tf_slim:__subpackages__",
+    "//third_party/py/tensorflow_docs:__subpackages__",
+    "//third_party/py/keras:__subpackages__",
+]
+
+package(
+    default_visibility = visibility,
+    licenses = ["notice"],  # Apache 2.0
+)
+
+# TODO(mdan): Move this utility outside of TF.
+cc_library(
+    name = "kernel_registry",
+    srcs = ["kernel_registry.cc"],
+    hdrs = ["kernel_registry.h"],
+    deps = [
+        "//tensorflow/core:framework",
+        "//tensorflow/core:lib",
+        "//tensorflow/core:protos_all_cc",
+    ],
+    alwayslink = 1,
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_tfprof",
+    srcs = ["tfprof_wrapper.cc"],
+    module_name = "_pywrap_tfprof",
+    deps = [
+        "//tensorflow/core:framework_headers_lib",
+        "//tensorflow/core:lib_headers_for_pybind",
+        "//tensorflow/core/profiler/internal:print_model_analysis_hdr",
+        "//third_party/eigen3",
+        "//third_party/python_runtime:headers",
+        "@com_google_absl//absl/strings",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_utils",
+    srcs = ["util_wrapper.cc"],
+    hdrs = ["util.h"],
+    module_name = "_pywrap_utils",
+    deps = [
+        "//tensorflow/python:pybind11_lib",
+        "//third_party/python_runtime:headers",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_kernel_registry",
+    srcs = ["kernel_registry_wrapper.cc"],
+    hdrs = ["kernel_registry.h"],
+    module_name = "_pywrap_kernel_registry",
+    deps = [
+        "//tensorflow/core:framework_headers_lib",
+        "//tensorflow/core:lib_headers_for_pybind",
+        "//tensorflow/core:protos_all_cc",
+        "//tensorflow/python:pybind11_lib",
+        "//third_party/python_runtime:headers",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_stat_summarizer",
+    srcs = ["stat_summarizer_wrapper.cc"],
+    module_name = "_pywrap_stat_summarizer",
+    deps = [
+        "//tensorflow/core:framework_headers_lib",
+        "//tensorflow/core:protos_all_cc",
+        "//third_party/eigen3",
+        "//third_party/python_runtime:headers",
+        "@com_google_absl//absl/memory",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_tensor_float_32_execution",
+    srcs = ["tensor_float_32.cc"],
+    hdrs = ["//tensorflow/core/platform:tensor_float_32_hdr"],
+    compatible_with = get_compatible_with_portable(),
+    module_name = "_pywrap_tensor_float_32_execution",
+    deps = [
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_util_port",
+    srcs = ["port_wrapper.cc"],
+    hdrs = ["//tensorflow/core/util:port_hdrs"],
+    module_name = "_pywrap_util_port",
+    deps = [
+        "//tensorflow/core/util:port",
+        "//third_party/python_runtime:headers",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_transform_graph",
+    srcs = ["transform_graph_wrapper.cc"],
+    hdrs = ["//tensorflow/tools/graph_transforms:transform_graph_hdrs"],
+    module_name = "_pywrap_transform_graph",
+    deps = [
+        "//tensorflow/core:framework_headers_lib",
+        "//tensorflow/core:lib_headers_for_pybind",
+        "//tensorflow/core:protos_all_cc",
+        "//tensorflow/python/lib/core:pybind11_status",
+        "//third_party/python_runtime:headers",
+        "@pybind11",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_pywrap_checkpoint_reader",
+    srcs = ["py_checkpoint_reader_wrapper.cc"],
+    hdrs = [
+        "//tensorflow/c:checkpoint_reader_hdrs",
+        "//tensorflow/c:headers",
+        "//tensorflow/c/eager:headers",
+        "//tensorflow/python/lib/core:ndarray_tensor_hdr",
+        "//tensorflow/python/lib/core:py_exception_registry_hdr",
+        "//tensorflow/python/lib/core:safe_ptr_hdr",
+    ],
+    module_name = "_pywrap_checkpoint_reader",
+    deps = [
+        "//tensorflow/core:lib_headers_for_pybind",
+        "//tensorflow/core:op_gen_lib",
+        "//tensorflow/core:protos_all_cc",
+        "//tensorflow/core/util/tensor_bundle:tensor_bundle_headers_lib",
+        "//tensorflow/python:pybind11_lib",
+        "//tensorflow/python:pybind11_status",
+        "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//third_party/py/numpy:headers",
+        "//third_party/python_runtime:headers",
+        "@com_google_absl//absl/strings",
+        "@pybind11",
+    ],
+)
+
+cc_library(
+    name = "cpp_python_util",
+    srcs = ["util.cc"],
+    hdrs = ["util.h"],
+    deps = [
+        "//tensorflow/core:lib",
+        "//tensorflow/core:lib_internal",
+        "//tensorflow/python/lib/core:safe_ptr",
+        "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//third_party/python_runtime:headers",
+        "@com_google_absl//absl/memory",
+    ],
+)
+
+tf_py_test(
+    name = "decorator_utils_test",
+    srcs = ["decorator_utils_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:platform",
+    ],
+)
+
+tf_py_test(
+    name = "deprecation_test",
+    srcs = ["deprecation_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:platform",
+    ],
+)
+
+tf_py_test(
+    name = "dispatch_test",
+    srcs = ["dispatch_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:platform",
+    ],
+)
+
+tf_py_test(
+    name = "keyword_args_test",
+    srcs = ["keyword_args_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+py_strict_library(
+    name = "tf_export",
+    srcs = ["tf_export.py"],
+    compatible_with = get_compatible_with_portable(),
+    srcs_version = "PY2AND3",
+    visibility = ["//tensorflow:__subpackages__"],
+    deps = [
+        ":tf_decorator",
+    ],
+)
+
+tf_py_test(
+    name = "tf_export_test",
+    srcs = ["tf_export_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:platform",
+    ],
+)
+
+# Leaf library: may not depend on anything else inside TensorFlow.
+# TODO(mdan): Move this utility outside of TF.
+py_strict_library(
+    name = "tf_decorator",
+    srcs = [
+        "tf_contextlib.py",
+        "tf_decorator.py",
+        "tf_inspect.py",
+    ],
+    compatible_with = get_compatible_with_portable(),
+    srcs_version = "PY2AND3",
+    visibility = [
+        "//tensorflow:__subpackages__",
+        # TODO(mdan): Remove these dependencies.
+        "//third_party/py/tf_slim:__subpackages__",
+        "//learning/deepmind/research/language/translation/lm:__subpackages__",
+    ],
+    deps = [
+        "@six_archive//:six",
+    ],
+)
+
+# Note: this is a heavyweight library specialized for TensorFlow graphs. Do not use for
+# other purposes.
+py_strict_library(
+    name = "tf_stack",
+    srcs = ["tf_stack.py"],
+    srcs_version = "PY2AND3",
+    # TODO(mdan): Remove public visibility.
+    visibility = ["//visibility:public"],
+    deps = [
+        ":_tf_stack",
+        "@six_archive//:six",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_tf_stack",
+    srcs = ["tf_stack.cc"],
+    hdrs = [
+        "//tensorflow/c:headers",
+        "//tensorflow/c/eager:headers",
+        # Using header directly is required to avoid ODR violations.
+        "stack_trace.h",
+    ],
+    # TODO(b/138203821): change to "util._tf_stack" once the bug is fixed.
+    module_name = "_tf_stack",
+    deps = [
+        "@com_google_absl//absl/algorithm:container",
+        "@com_google_absl//absl/strings",
+        "@com_google_absl//absl/strings:str_format",
+        "@com_google_absl//absl/types:span",
+        "@pybind11",
+        "//third_party/python_runtime:headers",  # buildcleaner: keep
+        "//tensorflow/c:pywrap_required_hdrs",
+        "//tensorflow/core/common_runtime:core_cpu_headers_lib",
+        "//tensorflow/core/framework:pywrap_required_hdrs",
+        "//tensorflow/core/platform:path",
+    ] + if_static([
+        ":stack_trace",
+    ]),
+)
+
+tf_py_test(
+    name = "tf_stack_test",
+    srcs = ["tf_stack_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":tf_export",
+        ":tf_stack",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+cc_library(
+    name = "stack_trace",
+    srcs = ["stack_trace.cc"],
+    hdrs = ["stack_trace.h"],
+    deps = [
+        "//tensorflow/core/platform:str_util",
+        "//tensorflow/core/platform:stringpiece",
+        "//tensorflow/core/util:abstract_stack_trace",
+        "//third_party/python_runtime:headers",  # buildcleaner: keep
+        "@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/container:inlined_vector",
+        "@com_google_absl//absl/types:optional",
+    ],
+)
+
+cc_library(
+    name = "function_parameter_canonicalizer",
+    srcs = ["function_parameter_canonicalizer.cc"],
+    hdrs = ["function_parameter_canonicalizer.h"],
+    deps = [
+        "//tensorflow/core/platform:logging",
+        "//tensorflow/core/platform:macros",
+        "//tensorflow/python/lib/core:py_util",
+        "//tensorflow/python/lib/core:safe_pyobject_ptr",
+        "//third_party/python_runtime:headers",  # buildcleaner: keep
+        "@com_google_absl//absl/container:flat_hash_set",
+        "@com_google_absl//absl/types:span",
+    ],
+)
+
+tf_python_pybind_extension(
+    name = "_function_parameter_canonicalizer_binding_for_test",
+    testonly = True,
+    srcs = ["function_parameter_canonicalizer_binding_for_test.cc"],
+    hdrs = [
+        "function_parameter_canonicalizer.h",
+    ],
+    module_name = "_function_parameter_canonicalizer_binding_for_test",
+    deps = [
+        "//tensorflow/core:lib",
+        "//tensorflow/python/lib/core:safe_pyobject_ptr_required_hdrs",
+        "//third_party/python_runtime:headers",  # buildcleaner: keep
+        "@com_google_absl//absl/types:span",
+        "@pybind11",
+    ],
+)
+
+tf_py_test(
+    name = "function_parameter_canonicalizer_test",
+    srcs = ["function_parameter_canonicalizer_test.py"],
+    python_version = "PY3",
+    tags = [
+        "no_pip",  # b/168621686
+        "no_windows",  # b/169275019
+    ],
+    deps = [
+        ":_function_parameter_canonicalizer_binding_for_test",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+py_library(
+    name = "util",
+    srcs = glob(
+        ["**/*.py"],
+        exclude = [
+            "example_parser*",
+            "tf_contextlib.py",
+            "tf_should_use.py",
+            "tf_export.py",
+            "tf_stack.py",
+            "tf_decorator.py",
+            "**/*_test.py",
+        ],
+    ),
+    compatible_with = get_compatible_with_portable(),
+    srcs_version = "PY2AND3",
+    visibility = visibility + [
+        "//tensorflow:__pkg__",
+        "//third_party/py/tensorflow_core:__subpackages__",
+        "//third_party/py/tf_agents:__subpackages__",
+        "//third_party/py/tfx:__subpackages__",
+    ],
+    deps = [
+        ":_pywrap_tensor_float_32_execution",
+        # global_test_configuration is added here because all major tests depend on this
+        # library. It isn't possible to add these test dependencies via tensorflow.bzl's
+        # py_test because not all tensorflow tests use tensorflow.bzl's py_test.
+        "//tensorflow/python:global_test_configuration",
+        ":tf_decorator",
+        ":tf_export",
+        "@org_python_pypi_backports_weakref",
+        "@com_google_protobuf//:protobuf_python",
+        "//third_party/py/numpy",
+        "@six_archive//:six",
+        "@wrapt",
+        "//tensorflow/tools/docs:doc_controls",
+        "//tensorflow/tools/compatibility:all_renames_v2",
+    ],
+)
+
+tf_py_test(
+    name = "object_identity_test",
+    size = "small",
+    srcs = ["object_identity_test.py"],
+    python_version = "PY3",
+)
+
+# Placeholder for intenal nest_test comments.
+tf_py_test(
+    name = "nest_test",
+    size = "small",
+    srcs = ["nest_test.py"],
+    main = "nest_test.py",
+    python_version = "PY3",
+    deps = [":nest_test_main_lib"],
+)
+
+py_library(
+    name = "nest_test_main_lib",
+    testonly = True,
+    srcs = ["nest_test.py"],
+    deps = [
+        ":util",
+        "//tensorflow/python:array_ops",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:framework",
+        "//tensorflow/python:framework_for_generated_wrappers",
+        "//tensorflow/python:math_ops",
+        "//third_party/py/numpy",
+        "@absl_py//absl/testing:parameterized",
+    ],
+)
+
+tf_py_test(
+    name = "serialization_test",
+    size = "small",
+    srcs = ["serialization_test.py"],
+    main = "serialization_test.py",
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+tf_py_test(
+    name = "function_utils_test",
+    srcs = ["function_utils_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+tf_py_test(
+    name = "tf_contextlib_test",
+    size = "small",
+    srcs = ["tf_contextlib_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+tf_py_test(
+    name = "tf_decorator_test",
+    size = "small",
+    srcs = ["tf_decorator_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+py_library(
+    name = "tf_should_use",
+    srcs = ["tf_should_use.py"],
+    srcs_version = "PY2AND3",
+    deps = [
+        ":util",
+        "//tensorflow/python:framework_ops",
+        "//tensorflow/python/eager:context",
+        "@six_archive//:six",
+    ],
+)
+
+tf_py_test(
+    name = "tf_should_use_test",
+    size = "small",
+    srcs = ["tf_should_use_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":tf_should_use",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+tf_py_test(
+    name = "tf_inspect_test",
+    size = "small",
+    srcs = ["tf_inspect_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+    ],
+)
+
+py_library(
+    name = "example_parser_configuration",
+    srcs = ["example_parser_configuration.py"],
+    srcs_version = "PY2AND3",
+    visibility = ["//visibility:public"],
+    deps = [
+        "//tensorflow/core:protos_all_py",
+        "//tensorflow/python:framework",
+        "//tensorflow/python:framework_for_generated_wrappers",
+    ],
+)
+
+tf_py_test(
+    name = "lock_util_test",
+    size = "small",
+    srcs = ["lock_util_test.py"],
+    main = "lock_util_test.py",
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "@absl_py//absl/testing:parameterized",
+    ],
+)
+
+tf_py_test(
+    name = "module_wrapper_test",
+    size = "small",
+    srcs = ["module_wrapper_test.py"],
+    python_version = "PY3",
+    deps = [
+        ":util",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/tools/compatibility:all_renames_v2",
+        "@six_archive//:six",
+    ],
+)
+
+tf_proto_library(
+    name = "compare_test_proto",
+    testonly = 1,
+    srcs = ["protobuf/compare_test.proto"],
+    cc_api_version = 2,
+)
+
+tf_py_test(
+    name = "protobuf_compare_test",
+    size = "small",
+    srcs = ["protobuf/compare_test.py"],
+    main = "protobuf/compare_test.py",
+    python_version = "PY3",
+    tags = ["no_pip"],  # compare_test_pb2 proto is not available in pip.
+    deps = [
+        ":compare_test_proto_py",
+        ":util",
+        "//tensorflow/python:platform_test",
+        "@six_archive//:six",
+    ],
+)
+
+tf_py_test(
+    name = "example_parser_configuration_test",
+    size = "small",
+    srcs = ["example_parser_configuration_test.py"],
+    main = "example_parser_configuration_test.py",
+    python_version = "PY3",
+    deps = [
+        ":example_parser_configuration",
+        "//tensorflow/python:array_ops",
+        "//tensorflow/python:client",
+        "//tensorflow/python:client_testlib",
+        "//tensorflow/python:framework_for_generated_wrappers",
+        "//tensorflow/python:parsing_ops",
+    ],
+)
+
+filegroup(
+    name = "util_hdr",
+    srcs = ["util.h"],
+)
+
+filegroup(
+    name = "compare_test_proto_src",
+    srcs = ["protobuf/compare_test.proto"],
+)
diff --git a/tensorflow/python/util/function_parameter_canonicalizer_test.py b/tensorflow/python/util/function_parameter_canonicalizer_test.py
index 968265f..5dc87b5 100644
--- a/tensorflow/python/util/function_parameter_canonicalizer_test.py
+++ b/tensorflow/python/util/function_parameter_canonicalizer_test.py
@@ -18,8 +18,8 @@
 from __future__ import division
 from __future__ import print_function
 
-from tensorflow.python import _function_parameter_canonicalizer_binding_for_test
 from tensorflow.python.platform import test
+from tensorflow.python.util import _function_parameter_canonicalizer_binding_for_test
 
 
 class FunctionParameterCanonicalizerTest(test.TestCase):
diff --git a/tensorflow/python/util/nest.py b/tensorflow/python/util/nest.py
index 21a61e4..9c83158 100644
--- a/tensorflow/python/util/nest.py
+++ b/tensorflow/python/util/nest.py
@@ -47,10 +47,10 @@
 import six as _six
 import wrapt as _wrapt
 
-from tensorflow.python import _pywrap_utils
+from tensorflow.python.platform import tf_logging
+from tensorflow.python.util import _pywrap_utils
 from tensorflow.python.util.compat import collections_abc as _collections_abc
 from tensorflow.python.util.tf_export import tf_export
-from tensorflow.python.platform import tf_logging
 
 
 _SHALLOW_TREE_HAS_INVALID_KEYS = (
diff --git a/tensorflow/python/util/tf_stack.py b/tensorflow/python/util/tf_stack.py
index aad0a0f..a9a8848 100644
--- a/tensorflow/python/util/tf_stack.py
+++ b/tensorflow/python/util/tf_stack.py
@@ -25,7 +25,7 @@
 import six
 
 # TODO(b/138203821): change to from ...util import ... once the bug is fixed.
-from tensorflow.python import _tf_stack
+from tensorflow.python.util import _tf_stack
 
 # Generally such lookups should be done using `threading.local()`. See
 # https://blogs.gnome.org/jamesh/2008/06/11/tls-python/ for a detailed
diff --git a/tensorflow/tools/def_file_filter/symbols_pybind.txt b/tensorflow/tools/def_file_filter/symbols_pybind.txt
index 1ceb0b3..959bc57 100644
--- a/tensorflow/tools/def_file_filter/symbols_pybind.txt
+++ b/tensorflow/tools/def_file_filter/symbols_pybind.txt
@@ -1,4 +1,4 @@
-[//tensorflow/python:cpp_python_util] # util tfe
+[//tensorflow/python/util:cpp_python_util] # util tfe
 tensorflow::swig::IsSequence
 tensorflow::swig::IsSequenceOrComposite
 tensorflow::swig::IsCompositeTensor
diff --git a/tensorflow/tools/docs/BUILD b/tensorflow/tools/docs/BUILD
index 72374a8..6d74df3 100644
--- a/tensorflow/tools/docs/BUILD
+++ b/tensorflow/tools/docs/BUILD
@@ -183,8 +183,8 @@
         ":base_dir_oss",
         "//tensorflow:tensorflow_py",
         "//tensorflow/python:framework_ops",
-        "//tensorflow/python:tf_export",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:tf_export",
         "@absl_py//absl:app",
         "@absl_py//absl/flags",
     ],
diff --git a/tensorflow/tools/graph_transforms/BUILD b/tensorflow/tools/graph_transforms/BUILD
index 2f5eceb..3aeaf86 100644
--- a/tensorflow/tools/graph_transforms/BUILD
+++ b/tensorflow/tools/graph_transforms/BUILD
@@ -223,6 +223,7 @@
     visibility = [
         "//tensorflow/core:__pkg__",
         "//tensorflow/python:__pkg__",
+        "//tensorflow/python/util:__pkg__",
     ],
 )
 
@@ -336,9 +337,9 @@
     srcs_version = "PY2AND3",
     deps = [
         "//tensorflow/core:protos_all_py",
-        "//tensorflow/python:_pywrap_transform_graph",
         "//tensorflow/python:errors",
         "//tensorflow/python:util",
+        "//tensorflow/python/util:_pywrap_transform_graph",
     ],
 )
 
diff --git a/tensorflow/tools/graph_transforms/__init__.py b/tensorflow/tools/graph_transforms/__init__.py
index 8746567..84f7ea0 100644
--- a/tensorflow/tools/graph_transforms/__init__.py
+++ b/tensorflow/tools/graph_transforms/__init__.py
@@ -19,8 +19,8 @@
 
 # pylint: disable=unused-import,wildcard-import, line-too-long
 from tensorflow.core.framework import graph_pb2
-from tensorflow.python._pywrap_transform_graph import TransformGraphWithStringInputs
 from tensorflow.python.util import compat
+from tensorflow.python.util._pywrap_transform_graph import TransformGraphWithStringInputs
 
 
 def TransformGraph(input_graph_def, inputs, outputs, transforms):
diff --git a/tensorflow/tools/pip_package/BUILD b/tensorflow/tools/pip_package/BUILD
index 40416a0..1252c01 100644
--- a/tensorflow/tools/pip_package/BUILD
+++ b/tensorflow/tools/pip_package/BUILD
@@ -46,8 +46,6 @@
         # Code that relies on these headers should dynamically link to
         # _pywrap_tensorflow_internal.so as well.
         "//tensorflow/python:model_analyzer_lib",
-        "//tensorflow/python:kernel_registry",
-        "//tensorflow/python:cpp_python_util",
         "//tensorflow/python/lib/core:py_exception_registry",
         "//tensorflow/python/lib/core:pybind11_proto",
         "//tensorflow/python/lib/core:pybind11_absl",
@@ -57,6 +55,8 @@
         "//tensorflow/python/lib/core:py_seq_tensor",
         "//tensorflow/python/lib/core:py_util",
         "//tensorflow/python/lib/io:py_record_reader_lib",
+        "//tensorflow/python/util:cpp_python_util",
+        "//tensorflow/python/util:kernel_registry",
         "//tensorflow/python:python_op_gen",
         "//tensorflow/python:tf_session_helper",
         "//third_party/eigen3",
@@ -116,7 +116,6 @@
     "//tensorflow/python/training/experimental:loss_scale_optimizer",
     "//tensorflow/python:memory_checker",
     "//tensorflow/python:meta_graph_testdata",
-    "//tensorflow/python:util_example_parser_configuration",
     "//tensorflow/python/data/benchmarks:benchmark_base",
     "//tensorflow/python/data/experimental/kernel_tests/serialization:dataset_serialization_test_base",
     "//tensorflow/python/data/experimental/kernel_tests:data_service_test_base",
@@ -147,6 +146,7 @@
     "//tensorflow/python/tools:tools_pip",
     "//tensorflow/python/tools/api/generator:create_python_api",
     "//tensorflow/python/tpu",
+    "//tensorflow/python/util:example_parser_configuration",
     "//tensorflow/python:image_grad_test_base",
     "//tensorflow/python:test_ops",
     "//tensorflow/python:while_v2",
diff --git a/tensorflow/tools/pip_package/pip_smoke_test.py b/tensorflow/tools/pip_package/pip_smoke_test.py
index 60e1ae5..a862d6d 100644
--- a/tensorflow/tools/pip_package/pip_smoke_test.py
+++ b/tensorflow/tools/pip_package/pip_smoke_test.py
@@ -87,7 +87,7 @@
     "//tensorflow/python/debug:grpc_tensorflow_server.par",
     "//tensorflow/python/feature_column:vocabulary_testdata",
     "//tensorflow/python:framework/test_file_system.so",
-    "//tensorflow/python:util_nest_test_main_lib",
+    "//tensorflow/python/util:nest_test_main_lib",
     # lite
     "//tensorflow/lite/experimental/examples/lstm:rnn_cell",
     "//tensorflow/lite/experimental/examples/lstm:rnn_cell.py",