Add spec types, YAML parser support, and plugin stubs for Huffman and
Pruning compression methods. The plugins raise CompressionError when
invoked, to be replaced with working implementations later.
BUG=part of #3256
Implement LutCompressor using the Compressor protocol. Lookup table
compression replaces tensor values with indices into a table of unique
values, producing packed indices and ancillary data in the format
expected by the TFLM DECODE kernel.
Supports per-tensor and per-channel compression, sizes value tables to
actual unique count, and handles unquantized tensors.
BUG=part of #3256
Define the plugin interface for compression methods. Each compressor
implements the Compressor protocol with a compress() method that returns
encoded data and ancillary data.
BUG=part of #3256
Replace model_facade with model_editor in compress.py and tests.
model_editor provides a cleaner API with better buffer and metadata
handling.
Update BUILD dependencies accordingly.
BUG=part of #3256
Implement unified module for creating, reading, and modifying TFLite
models with a clean API. The module eliminates manual index tracking
and buffer management through automatic bookkeeping, supporting both
declarative and imperative construction styles.
Wrapper classes (Tensor, Operator, Subgraph, Model) hold the underlying
flatbuffer T objects as backing storage rather than copying fields into
dataclasses. This ensures all schema fields are preserved during
read-modify-write cycles, even fields not explicitly handled by
model_editor. Future schema additions will be preserved automatically.
Add comprehensive test coverage including field preservation tests that
verify unhandled schema fields survive read-modify-write.
BUG=part of #3256
Add a tensor_type module that holds the single mapping from a TFLite
TensorType to a numpy dtype, and convert view.py to use it. The mapping
was inlined in view.py; centralizing it gives the compression tooling
one place to maintain as more callers need to read tensor buffers as
numpy arrays.
tensor_type.to_numpy() raises ValueError for types with no clean numpy
equivalent (STRING, RESOURCE, VARIANT, BFLOAT16, and the sub-byte
integer types) instead of silently returning a wrong dtype. Only types
with an unambiguous little-endian numpy representation are mapped.
BUG=part of #3256
Python targets in this repository import one another under the
"tflite_micro" package namespace, which //:tflite_micro_shim synthesizes
at import time. The shim is required under Bzlmod, where the main
repository's runfiles root is the fixed name "_main" rather than the
module name, so the "tflite_micro" prefix no longer resolves on its own.
Every such target therefore had to list //:tflite_micro_shim in its
deps, which was repetitive and easy to forget.
Add tflm_py_library, tflm_py_test, and tflm_py_binary wrappers in a new
//python:py_rules.bzl that inject the shim dependency automatically,
following the naming convention of the existing tflm_cc_* wrappers, and
document the shim's rationale there. Convert every target that
previously listed the shim to the corresponding wrapper and drop the
explicit dependency. The dependency graph is unchanged; only the means
by which the shim is attached differs.
Add a comprehensive Jupyter notebook tutorial demonstrating TFLM's
compression pipeline using the MNIST dataset. The tutorial covers weight
clustering with TensorFlow Model Optimization toolkit, post-training
quantization, and TFLM's LUT-based compression.
Update documentation to reference the new tutorial from the main README,
Python interpreter guide, and compression documentation.
BUG=#2636
Co-authored-by: Esun Kim <veblush@google.com>
- Upgraded the Bazel BuildTool to the latest version (8.2.1).
- Updated the `tflm-ci` Docker image and pushed the new tag (0.6.1) to the `ghcr.io/tflm-bot/tflm-ci registry`.
- Updated the WORKSPACE to load `rules_cc` and `rules_shell` explicitly
- Ran `buildifier` to ensure all BUILD files to have all the fixes.
BUG=Clean-up
Enhance compress() function to automatically apply proper
FlatBuffer alignment after compression, eliminating the need for
users to manually run tflite_flatbuffer_align as a separate step.
Use the C++ alignment wrapper internally, as the Python
flatbuffers library doesn't respect force_align schema
attributes.
Keep the API unchanged - compress() still returns a bytearray,
but now the output is properly aligned for the TFLM interpreter.
Update documentation, and build dependencies of the Python
package.
BUG=#3125
Add a development tool that prints compressed and uncompressed
.tflite models to stdout in a human-readable, searchable,
structured, text format. Helpful annotations (indexes of lists,
names of operators, etc.) derived from the model are added as
virtual fields with names beginning with an _underscore.
Add a unit test which simply ensures the viewer does not crash
when run on several models found in the source tree.
BUG=see description
Add a fluent builder API for creating compression specifications
without writing YAML strings. This is useful in scripts and
Jupyter notebooks.
Example usage:
spec = (compression.SpecBuilder()
.add_tensor(subgraph=0, tensor=2)
.with_lut(index_bitwidth=4)
.build())
BUG=#3125
Co-authored-by: suleshahid <110432064+suleshahid@users.noreply.github.com>
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
There are older toolchains that don't support C++17 and are still used in shipping products.
Rewrite hexdump.cc to not use C++17 features (std::byte and Class Template Argument Deduction). Also remove forcing of -std=libc for xtensa targets, which was added because stdlib isn't supported with -std=c++17.
BUG=410831256
Compress using a single value table when a tensor is per-tensor
quantized, as indicated by the presence of only one quantization
scale and zero point. Update unit tests accordingly and augment
`test_models` to accommodate additional quantization fields.
Abandon the logic that a tensor should be compressed along the
NHWC channel dimension if the quantization parameters do not
specify an axis. Instead, fail with an error if the compression
axis cannot be inferred from the quantization parameters.
The interpreter already expects a single value table when a
tensor is per-tensor quantized.
BUG=part of #2636
Explicitly give a type to an instantiation of tflite::Span<T>
rather than let the compiler infer it. The inference breaks on
some internal compilers.
BUG=see description
Remove the hexdump of the compression metadata to the test log
during the Python compression metadata test, and the requirement
for the hexdump Python package. This dump was helpful during
development of the compression metadata flatbuffer schema to
assess how changes were represented, affecting the size and the
scaling of the size.
Unfortunately, requiring the hexdump module creates problems when
Google imports this code internally, so remove it for now.
The C++ metadata test still prints a hexdump; however, the C++
flatbuffer library writes the flatbuffer slightly differently.
Within the bounds of the flatbuffer specifications, different
library writers can make different, compatible implementation
choices.
BUG=see description
Add a module that provides convenient navigation, data type
conversions, and utilities for working with a tflite.Model, which
can be tedious and verbose to work with directly.
BUG=#2636
Add a module that provides tools for constructing .tflite
flatbuffers from a Python dictionary representation of a model.
This is useful for declaratively defining model flatbuffers for
testing, which is faster and clearer than building up model
flatbuffers programmatically.
The implementation should stay low-level and independent from any
helpers in this project which make constructing model and
flatbuffers easier, because this module is used to define tests
for those helpers.
BUG=#2636
Add a module for reading YAML strings that specify how a model
should be compressed. Put an example in spec.EXAMPLE_YAML_SPEC.
specfiles are written during model development to specify which
tensors should be compressed, by what method, and according to
what parameters. specfiles are read by the compression tool. They
are not used by the TFLM interpreter.
Add a YAML parser to the python third-party package dependencies.
BUG=#2636
Add a flatbuffer schema for describing compressed models.
Flatbuffers with this schema are to be used as the value in a
.tflite model flatbuffer metadata field, and contain the extra
information necessary to describe a compressed model.
Include tests to ensure basic functionality and demonstrate
integration with C++, Python, and Bazel.
BUG=#2636