From 7806b65c5c3241360902d855f79b342a3398d3d2 Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Fri, 11 Sep 2026 14:09:16 -0700 Subject: [PATCH 1/2] Remove tensorflow dependency from flatbuffer_utils, schema_util, and debug tools --- .../lite/micro/tools/generate_test_for_model.py | 8 +------- .../lite/micro/tools/layer_by_layer_debugger.py | 9 +-------- tensorflow/lite/python/BUILD | 4 ---- tensorflow/lite/python/schema_util.py | 11 +++-------- tensorflow/lite/tools/BUILD | 1 - tensorflow/lite/tools/flatbuffer_utils.py | 8 ++++---- 6 files changed, 9 insertions(+), 32 deletions(-) diff --git a/tensorflow/lite/micro/tools/generate_test_for_model.py b/tensorflow/lite/micro/tools/generate_test_for_model.py index a694ebbe9f4..adaadd60839 100644 --- a/tensorflow/lite/micro/tools/generate_test_for_model.py +++ b/tensorflow/lite/micro/tools/generate_test_for_model.py @@ -28,13 +28,7 @@ import tflite_runtime.interpreter as tflite_interp from tflite_runtime.interpreter import OpResolverType except ImportError: - try: - import tensorflow.lite as tflite_interp - from tensorflow.lite.experimental import OpResolverType - except ImportError: - raise ImportError( - "Could not import ai_edge_litert, tflite_runtime, or tensorflow." - ) + raise ImportError("Could not import ai_edge_litert or tflite_runtime.") class TestDataGenerator: diff --git a/tensorflow/lite/micro/tools/layer_by_layer_debugger.py b/tensorflow/lite/micro/tools/layer_by_layer_debugger.py index c755064404f..a388fe8a575 100644 --- a/tensorflow/lite/micro/tools/layer_by_layer_debugger.py +++ b/tensorflow/lite/micro/tools/layer_by_layer_debugger.py @@ -44,14 +44,7 @@ except ImportError: pass except ImportError: - try: - import tensorflow.lite as tflite_interp - - OpResolverType = tflite_interp.experimental.OpResolverType - except ImportError: - raise ImportError( - "Could not import ai_edge_litert, tflite_runtime, or tensorflow." - ) + raise ImportError("Could not import ai_edge_litert or tflite_runtime.") np.set_printoptions(threshold=sys.maxsize) diff --git a/tensorflow/lite/python/BUILD b/tensorflow/lite/python/BUILD index f7a662782ed..1e819f59f0e 100644 --- a/tensorflow/lite/python/BUILD +++ b/tensorflow/lite/python/BUILD @@ -18,8 +18,4 @@ py_library( name = "schema_util", srcs = ["schema_util.py"], visibility = ["//:__subpackages__"], - deps = [ - requirement("flatbuffers"), - requirement("tensorflow"), - ], ) diff --git a/tensorflow/lite/python/schema_util.py b/tensorflow/lite/python/schema_util.py index e898a47318d..cb4cd6716e2 100644 --- a/tensorflow/lite/python/schema_util.py +++ b/tensorflow/lite/python/schema_util.py @@ -14,7 +14,9 @@ # ============================================================================== """Schema utilities to get builtin code from operator code.""" -from tensorflow.python.util import all_util +__all__ = [ + 'get_builtin_code_from_operator_code', +] def get_builtin_code_from_operator_code(opcode): @@ -36,10 +38,3 @@ def get_builtin_code_from_operator_code(opcode): return max(opcode.BuiltinCode(), opcode.DeprecatedBuiltinCode()) return max(opcode.builtinCode, opcode.deprecatedBuiltinCode) - - -_allowed_symbols = [ - 'get_builtin_code_from_operator_code', -] - -all_util.remove_undocumented(__name__, _allowed_symbols) diff --git a/tensorflow/lite/tools/BUILD b/tensorflow/lite/tools/BUILD index 093e9c863c4..dc6a8c1c525 100644 --- a/tensorflow/lite/tools/BUILD +++ b/tensorflow/lite/tools/BUILD @@ -8,7 +8,6 @@ tflm_py_library( visibility = ["//:__subpackages__"], deps = [ requirement("flatbuffers"), - requirement("tensorflow"), "//tensorflow/lite/python:schema_py", "//tensorflow/lite/python:schema_util", ], diff --git a/tensorflow/lite/tools/flatbuffer_utils.py b/tensorflow/lite/tools/flatbuffer_utils.py index 71e1afed6b5..f7ea6e7c6a8 100644 --- a/tensorflow/lite/tools/flatbuffer_utils.py +++ b/tensorflow/lite/tools/flatbuffer_utils.py @@ -21,6 +21,7 @@ """ import copy +import os import random import re import struct @@ -31,7 +32,6 @@ from tflite_micro.tensorflow.lite.python import schema_py_generated as schema_fb from tflite_micro.tensorflow.lite.python import schema_util -from tensorflow.python.platform import gfile _TFLITE_FILE_IDENTIFIER = b'TFL3' @@ -55,9 +55,9 @@ def read_model(input_tflite_file): Returns: A python object corresponding to the input tflite file. """ - if not gfile.Exists(input_tflite_file): + if not os.path.exists(input_tflite_file): raise RuntimeError('Input file not found at %r\n' % input_tflite_file) - with gfile.GFile(input_tflite_file, 'rb') as input_file_handle: + with open(input_tflite_file, 'rb') as input_file_handle: model_bytearray = bytearray(input_file_handle.read()) return read_model_from_bytearray(model_bytearray) @@ -144,7 +144,7 @@ def write_model(model_object, output_tflite_file): model_object = copy.deepcopy(model_object) byte_swap_tflite_model_obj(model_object, 'big', 'little') model_bytearray = convert_object_to_bytearray(model_object) - with gfile.GFile(output_tflite_file, 'wb') as output_file_handle: + with open(output_tflite_file, 'wb') as output_file_handle: output_file_handle.write(model_bytearray) From 189d302fda62c63cd686512d672918e56a43955f Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Fri, 11 Sep 2026 14:59:30 -0700 Subject: [PATCH 2/2] Revert changes to tensorflow/lite/{tools,python} files synced from upstream --- tensorflow/lite/python/BUILD | 4 ++++ tensorflow/lite/python/schema_util.py | 11 ++++++++--- tensorflow/lite/tools/BUILD | 1 + tensorflow/lite/tools/flatbuffer_utils.py | 8 ++++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tensorflow/lite/python/BUILD b/tensorflow/lite/python/BUILD index 1e819f59f0e..f7a662782ed 100644 --- a/tensorflow/lite/python/BUILD +++ b/tensorflow/lite/python/BUILD @@ -18,4 +18,8 @@ py_library( name = "schema_util", srcs = ["schema_util.py"], visibility = ["//:__subpackages__"], + deps = [ + requirement("flatbuffers"), + requirement("tensorflow"), + ], ) diff --git a/tensorflow/lite/python/schema_util.py b/tensorflow/lite/python/schema_util.py index cb4cd6716e2..e898a47318d 100644 --- a/tensorflow/lite/python/schema_util.py +++ b/tensorflow/lite/python/schema_util.py @@ -14,9 +14,7 @@ # ============================================================================== """Schema utilities to get builtin code from operator code.""" -__all__ = [ - 'get_builtin_code_from_operator_code', -] +from tensorflow.python.util import all_util def get_builtin_code_from_operator_code(opcode): @@ -38,3 +36,10 @@ def get_builtin_code_from_operator_code(opcode): return max(opcode.BuiltinCode(), opcode.DeprecatedBuiltinCode()) return max(opcode.builtinCode, opcode.deprecatedBuiltinCode) + + +_allowed_symbols = [ + 'get_builtin_code_from_operator_code', +] + +all_util.remove_undocumented(__name__, _allowed_symbols) diff --git a/tensorflow/lite/tools/BUILD b/tensorflow/lite/tools/BUILD index dc6a8c1c525..093e9c863c4 100644 --- a/tensorflow/lite/tools/BUILD +++ b/tensorflow/lite/tools/BUILD @@ -8,6 +8,7 @@ tflm_py_library( visibility = ["//:__subpackages__"], deps = [ requirement("flatbuffers"), + requirement("tensorflow"), "//tensorflow/lite/python:schema_py", "//tensorflow/lite/python:schema_util", ], diff --git a/tensorflow/lite/tools/flatbuffer_utils.py b/tensorflow/lite/tools/flatbuffer_utils.py index f7ea6e7c6a8..71e1afed6b5 100644 --- a/tensorflow/lite/tools/flatbuffer_utils.py +++ b/tensorflow/lite/tools/flatbuffer_utils.py @@ -21,7 +21,6 @@ """ import copy -import os import random import re import struct @@ -32,6 +31,7 @@ from tflite_micro.tensorflow.lite.python import schema_py_generated as schema_fb from tflite_micro.tensorflow.lite.python import schema_util +from tensorflow.python.platform import gfile _TFLITE_FILE_IDENTIFIER = b'TFL3' @@ -55,9 +55,9 @@ def read_model(input_tflite_file): Returns: A python object corresponding to the input tflite file. """ - if not os.path.exists(input_tflite_file): + if not gfile.Exists(input_tflite_file): raise RuntimeError('Input file not found at %r\n' % input_tflite_file) - with open(input_tflite_file, 'rb') as input_file_handle: + with gfile.GFile(input_tflite_file, 'rb') as input_file_handle: model_bytearray = bytearray(input_file_handle.read()) return read_model_from_bytearray(model_bytearray) @@ -144,7 +144,7 @@ def write_model(model_object, output_tflite_file): model_object = copy.deepcopy(model_object) byte_swap_tflite_model_obj(model_object, 'big', 'little') model_bytearray = convert_object_to_bytearray(model_object) - with open(output_tflite_file, 'wb') as output_file_handle: + with gfile.GFile(output_tflite_file, 'wb') as output_file_handle: output_file_handle.write(model_bytearray)