Commit graph

40 commits

Author SHA1 Message Date
Ryan Kuester
6c67d478e3
feat(compression): add Huffman and Pruning compression support (#3612)
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
2026-06-30 22:53:59 +00:00
Ryan Kuester
9bfaeadf48
feat(compression): add LUT compression plugin (#3608)
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
2026-06-24 22:44:48 +00:00
Ryan Kuester
074b75f8ec
feat(compression): add Compressor protocol (#3590)
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
2026-06-18 20:15:56 +00:00
Ryan Kuester
e0b2c281f2
feat(compression): add DECODE operator types and metadata (#3589)
Add decode module with DecodeType constants and DecodeCommonMetadata,
per the TFLM DECODE Operator Design document.

BUG=part of #3256
2026-06-06 01:31:13 +00:00
Ryan Kuester
eea46d3d61
chore(compression): remove test_models.py (#3587)
Remove test_models module and its tests, now superseded by
model_editor.

BUG=part of #3256
2026-06-04 18:44:14 +00:00
Ryan Kuester
ac1fae3619
refactor(compression): replace test_models with model_editor in compress_test (#3586)
Replace dictionary-based test_models.build() with model_editor's
declarative API for building test models.

BUG=part of #3256
2026-06-02 16:09:54 +00:00
Ryan Kuester
730449fef1
chore(compression): remove model_facade.py (#3585)
Remove model_facade module and its tests, now superseded by
model_editor.

BUG=part of #3256
2026-06-01 23:03:06 +00:00
Ryan Kuester
c2accf5c8f
refactor(compression): migrate compress.py from model_facade to model_editor (#3580)
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
2026-06-01 16:36:48 +00:00
Ryan Kuester
7c26381de5
feat(compression): implement model_editor for TFLite model manipulation (#3575)
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
2026-05-29 21:42:17 +00:00
Ryan Kuester
db550c38c9
refactor(compression): hoist numpy dtype map into tensor_type (#3578)
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
2026-05-28 21:56:37 +00:00
Ryan Kuester
068c6a59b6
build(bazel): inject tflite_micro shim via tflm_py_* wrappers (#3573)
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.
2026-05-26 19:51:58 +00:00
Esun Kim
9f5ac257ee
No tensorflow 2 (#3548) 2026-05-06 21:17:46 +00:00
Esun Kim
807c35d6a9
Bazel Module (#3364)
* Work

* Review update

* Enabled tests

* Bazel: Suppress warnings from external repositories

* Fixed whl_test

* Formatted

* More format
2026-02-11 14:11:47 -08:00
Esun Kim
183da3e302
Typo in code (#3447)
* Typo in code

* Format
2026-02-11 20:34:42 +00:00
Esun Kim
3e1185c715
[T2] Refactor Group 4 (#3371)
* Refactor t2-4

* Fix
2026-01-30 17:29:19 +00:00
Esun Kim
41a80afaa4
[Dep] Upgraded Flatbuffer to 25.9.23 (#3296)
* flatbuffer-25.9.23

* Patch 1

* No dynamic allocation patch to Flatbuffer

* Individual ModelBuilder

* Fix

* Patch

* One more removal
2026-01-27 22:12:21 +00:00
Esun Kim
b46e68e79f
Bazel clean-up (#3359) 2026-01-26 17:20:18 -08:00
Esun Kim
2aa6e12979
[CI] Reformat (#3347)
* Clean-up BUILD

* Added pyproject.toml

* Format gen

* Fix

* Fix format

* Fix
2026-01-24 21:13:38 -08:00
Esun Kim
233ff4715c
[CI] Windows Fixes (#3344)
* Fixes

* Exclude
2026-01-24 17:57:50 -08:00
Esun Kim
8abd791179
[CI] windows 2 (#3341)
* Fix

* More fix

* More fix
2026-01-24 16:33:58 -08:00
Esun Kim
ce41d286c3
[CI] windows 2 (#3340)
* Fix

* More fix
2026-01-24 14:18:07 -08:00
Esun Kim
b1592615eb
Bazel windows (#3336) 2026-01-24 11:52:08 -08:00
Esun Kim
fd33340e06
Revert "[CI] Replaced tensorflow with tensorflow-cpu (#3261)" (#3290)
This reverts commit 9cbf6e0403.
2026-01-12 11:19:37 -08:00
Esun Kim
9cbf6e0403
[CI] Replaced tensorflow with tensorflow-cpu (#3261)
* Replaced tensorflow with tensorflow-cpu

* Use tensorflow-cpu for bazel targets
2025-12-03 13:43:14 -08:00
Ryan Kuester
c3c78cb7ea
docs(compression): add MNIST compression tutorial (#3224)
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>
2025-10-17 10:01:13 -07:00
Esun Kim
858f5a3edd
[CI] Removed six, updated absl_py (#3203)
* Removed ci/install_qemu.sh

* Removed six from direct deps

* Removed six from tflm-ci docker

* Use requirement(absl_py)

* New qemu-user
2025-09-25 14:37:39 -07:00
Esun Kim
ba753cef3e
[CI] Fix build (#3194)
- 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
2025-09-19 20:48:22 +00:00
Ryan Kuester
93ebaa9ca4
feat(compression): integrate automatic FlatBuffer alignment (#3177)
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
2025-09-03 22:19:19 +00:00
Ryan Kuester
2fa2e03ab0
feat: add tool for visualizing compressed (and uncompressed) models (#3099)
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
2025-08-04 20:09:49 +00:00
Ryan Kuester
bbf70db499
feat(compression): add SpecBuilder for programmatic compression specs (#3133)
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>
2025-07-10 16:32:57 -04:00
Ryan Kuester
f50a6eaafd
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
2025-07-08 20:18:04 +00:00
Shlomi Regev
15b55b7afd
Revert back from C/C++17 to C/C++11 (#3095)
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
2025-05-13 04:37:10 +00:00
Ryan Kuester
01e31b7493
fix(compress.py): use single value table for per-tensor quantized tensors (#3025)
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
2024-12-19 18:20:46 +00:00
Ryan Kuester
4c7b7bf18d
fix: explicitly give type parameter to help some compilers (#2983)
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
2024-12-04 22:49:22 +00:00
Ryan Kuester
e2cc052c4f
test(compression): remove the test log hexdump of the Python metadata (#2941)
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
2024-11-27 00:12:18 +00:00
Ryan Kuester
233e936fdf
feat(compression): add model compression tool (#2939)
Add a library and CLI for compressing models.

Add bitarray to the Python third-party package dependencies.

BUG=#2636
2024-11-26 22:12:48 +00:00
Ryan Kuester
cac6c98a26
feat(compression): add facade for working with tflite.Model (#2821)
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
2024-11-19 20:37:26 +00:00
Ryan Kuester
dac389ed14
feat(compression): add module for defining test .tflite models (#2798)
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
2024-11-19 19:55:53 +00:00
Ryan Kuester
77f707106e
feat(compression): add YAML format for specifying compression (#2795)
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
2024-11-19 19:20:03 +00:00
Ryan Kuester
0c3a93b8f2
feat: add compression metadata flatbuffer schema and tests (#2774)
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
2024-11-19 04:12:49 +00:00