Skip to content

Commit f50a6ea

Browse files
authored
feat(python): add compression module to tflite_micro Python package (#3129)
feat(python): add compression module to tflite_micro Python package Integrate the TFLM compression tools into the tflite_micro Python package, allowing users to compress models directly from Python code that imports the package. Usage: from tflite_micro import compression Details: - Add compress_lib py_library target in compression BUILD - Create compression package with __init__.py exposing public API - Include compression module in Python package dependencies - Add compression dependencies to wheel requirements BUG=see description
1 parent f60e4fa commit f50a6ea

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

python/tflite_micro/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ py_package(
130130
# in the tflm tree.
131131
packages = [
132132
"python.tflite_micro",
133+
"tensorflow.lite.micro.compression",
133134
"tensorflow.lite.micro.tools.generate_test_for_model",
134135
"tensorflow.lite.python",
135136
"tensorflow.lite.tools.flatbuffer_utils",
@@ -138,6 +139,7 @@ py_package(
138139
":postinstall_check",
139140
":runtime",
140141
":version",
142+
"//tensorflow/lite/micro/compression",
141143
],
142144
)
143145

@@ -223,8 +225,10 @@ py_wheel(
223225
":local": "py3",
224226
}),
225227
requires = [
228+
"bitarray",
226229
"flatbuffers",
227230
"numpy",
231+
"pyyaml",
228232
"tensorflow",
229233
],
230234
stamp = 1, # 1 == always stamp

python/tflite_micro/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
# Unambiguously identify the source used to build the package.
2525
from tflite_micro.python.tflite_micro._version import __version__
2626

27-
# Ordered after `runtime` to avoid a circular dependency
27+
# Provide a convenient alias for the compression module
28+
from tflite_micro.tensorflow.lite.micro import compression
29+
30+
# Ordered after `runtime` and `compression` to avoid circular dependencies
2831
from tflite_micro.python.tflite_micro import postinstall_check

python/tflite_micro/postinstall_check.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
# in the Python installation environment rather than to locations in the tflm
2020
# source tree.
2121
from tflite_micro import runtime
22+
from tflite_micro import compression
2223

2324
import numpy as np
2425
import pkg_resources
2526
import sys
27+
import tempfile
28+
import os
2629

2730

28-
def passed():
31+
def runtime_test():
32+
"""Test the runtime interpreter functionality."""
2933
# Create an interpreter with a sine model
3034
model = pkg_resources.resource_filename(__name__, "sine_float.tflite")
3135
interpreter = runtime.Interpreter.from_file(model)
@@ -49,5 +53,23 @@ def infer(x):
4953
return np.allclose(outputs, goldens, atol=0.05)
5054

5155

56+
def compression_test():
57+
"""Test that the compression module is available and functional."""
58+
59+
# Test that compress function is available
60+
# We don't actually compress here as it requires a properly structured model
61+
# with compressible tensors, but we verify the function is importable
62+
assert callable(compression.compress)
63+
64+
return True
65+
66+
67+
def passed():
68+
"""Run all postinstall checks."""
69+
runtime_passed = runtime_test()
70+
compression_passed = compression_test()
71+
return runtime_passed and compression_passed
72+
73+
5274
if __name__ == "__main__":
5375
sys.exit(0 if passed() else 1)

tensorflow/lite/micro/compression/BUILD

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ load(
88
"flatbuffer_cc_library",
99
"flatbuffer_py_library",
1010
)
11-
load("@rules_python//python:defs.bzl", "py_test")
11+
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
1212
load("@tflm_pip_deps//:requirements.bzl", "requirement")
1313

1414
package(
@@ -17,6 +17,15 @@ package(
1717
],
1818
)
1919

20+
py_library(
21+
name = "compression",
22+
srcs = ["__init__.py"],
23+
deps = [
24+
":compress_lib",
25+
":spec",
26+
],
27+
)
28+
2029
flatbuffer_cc_library(
2130
# Generates the header-only library "metadata_generated.h", used to read
2231
# the metadata flatbuffer.
@@ -96,8 +105,8 @@ py_test(
96105
],
97106
)
98107

99-
py_binary(
100-
name = "compress",
108+
py_library(
109+
name = "compress_lib",
101110
srcs = [
102111
"compress.py",
103112
],
@@ -113,6 +122,16 @@ py_binary(
113122
],
114123
)
115124

125+
py_binary(
126+
name = "compress",
127+
srcs = [
128+
"compress.py",
129+
],
130+
deps = [
131+
":compress_lib",
132+
],
133+
)
134+
116135
py_test(
117136
name = "compress_test",
118137
size = "small",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2025 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""TensorFlow Lite for Microcontrollers compression module."""
15+
16+
# This __init__.py file exists to make compression features available as part
17+
# of the tflite_micro Python package.
18+
#
19+
# Usage example:
20+
# from tflite_micro import compression
21+
# ...
22+
23+
from .compress import compress
24+
from .spec import parse_yaml
25+
26+
__all__ = ["compress", "parse_yaml"]

0 commit comments

Comments
 (0)