Commit graph

60 commits

Author SHA1 Message Date
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
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
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
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
suleshahid
bc68d362d6
Remove deprecated BUILD tags (#3091)
These tags have been deprecated and are unused now. Syncing with upstream and removing them from the BUILD files.

BUG=remove deprecated tags
2025-04-08 19:21:25 +00:00
Ryan Kuester
11b15b3a81
build(bazel): introduce tflm_cc_* macros, refactoring away micro_copts (#2765)
Remove micro_copts() by replacing every cc_* target that used
them with a tflm_cc_* equivalent, and setting those common copts
in one place, inside the tflm_cc_* macro.

This is the first of several commits introducing tflm_cc_* macros
in place of cc_binary, cc_library, and cc_test. Motivated by the
upcoming need to support conditional compilation, the objective
is to centralize build configuration rather than requiring (and
remembering that) each cc_* target in the project add the same
common attributes such as compiler options and select()ed

Alternatives such as setting global options on the command line
or in .bazelrc, even if simplified with a --config option, fail
to preserve flags and hooks for configuration in the case TFLM is
used as an external repository by an application project. Nor is
it easy in that case for individual targets to override an
otherwise global setting.

BUG=#2636
2024-11-14 23:16:28 +00:00
RJ Ascani
29526e413b
Replace tensorflow-cpu usage with tensorflow (#2396)
The tensorflow-cpu package does not support MacOS or non-x86 hardware. Replacing the tensorflow-cpu python package requirement with the tensorflow meta package should enable the bazel build and the dependent python scripts to be used on those platforms.

BUG=#2367, #1781
2024-02-23 00:35:22 +00:00
suleshahid
fdf7876bbf
Remove unused BUILD load (#2397)
BUG=[318738218](https://b.corp.google.com/issues/318738218)
2024-01-17 18:20:19 +00:00
RJ Ascani
7a63bce432
Revert "Compute output shapes for some kernels (#2356)" (#2390)
This change broke some internal models, so reverting this until we can better understand why.

This also reverts PR #2383. When relanded, both should go back in together.

BUG=b/318738218
2024-01-11 22:24:58 +00:00
Steven Toribio
95467d2066
removing usage of tflm_runtime and replacing usage with new runtime target target in g3 TFLM repo (#2375)
removing usage of `tflm_runtime` and replacing usage with new runtime target target in g3 TFLM repo

[g3 cl](https://critique.corp.google.com/cl/595884672)

BUG=[b/286456378](https://b.corp.google.com/issues/286456378)
2024-01-05 17:01:02 +00:00
David Davis
bb54330fda
Compute output shapes for some kernels (#2356)
@tensorflow/micro

Update the output tensor shape during prepare phase when the computed shape does not match the shape in the flatbuffer.

Kernels:
- BATCH_TO_SPACE_ND
- SPACE_TO_BATCH_ND
- CONV
- RESHAPE
- EXPAND_DIMS
- DEPTHWISE_CONV

Update CMSIS_NN and ARC_MLI optimized kernels.
Add additional tests from TfLite for BATCH_TO_SPACE_ND and SPACE_TO_BATCH_ND.
Update existing tests.
Add tests for Keras model using convolution with dilation > 1.

Update memory_arena_threshold_test to increase total, tail, and persistent allocation sizes:
- Add 20 bytes for CONV output shape
- Add 15 bytes for arena allocation alignment
- x2 convolution layers

Update micro_speech_test arena size as per description in C++ code.

See #2319 for additional details.

Resolves [b/317362237](https://issuetracker.google.com/317362237)

bug=fixes #2368 #1646 #1629 #1231 #2338 #2319
2024-01-02 22:22:36 +00:00
suleshahid
127f88fe3a
remove unused visibility (#2230)
from cl/565864667
BUG=cleanup
2023-09-18 17:50:23 +00:00
Steven Toribio
80cb11b131
load()s are being added in preparation for changes being made to Blaze/Bazel (#2160)
`load()`s are being added in preparation for changes being made to Blaze/Bazel !

corresponding [google3 cl](https://critique.corp.google.com/cl/553849494)

BUG=[b/295216390](https://b.corp.google.com/issues/295216390)
2023-08-10 03:30:46 +00:00
Ryan Kuester
b5da836410
refactor(python): move runtime to package-friendly directory structure (#2051)
To package the module `runtime` as `tflite_micro.runtime`, put `runtime` under
a directory representing the Python namespace package `tflite_micro`. For
organization's sake, move it all to the top-level directory `python/`. Adjust
tests and docs to match.

Some code outside of the Python extension module has come to depend on
`python/tflite_micro:python_ops_resolver` as a replacement for
`all_ops_resolver` (e.g.:`t/l/m/integration_tests/seanet/add/integration_tests.cc`).
`python_ops_resolver` is intended to be a private implementation detail of the
Python extension module. For now, grandfather in the dependent code by updating
its references to the resolver's location; however, soon the dependent code
should be migrated away to a different resolver. (#2033,
https://issuetracker.google.com/286508251)

BUG=part of #1484
2023-06-15 06:25:19 +00:00
suleshahid
c306ff6084
Adds Signal Library Window Op (#2019)
First Op for the TFLM Signal Processing Ops library.
Doc linked in bug.

The Window OP is a custom signal processing OP similar to what is found in tf.signal library, but specific for integer (int16) purposes.

You can directly use this as a builtin op via the op resolver as:
* `op_resolver.AddWindow()` 
* or via python as shown in `window_op_test.py`

3 testing options are provided:
* Micro(C++): `bazel run signal/micro/kernels:window_test`
* Tensorflow/Micro(Python): `bazel run python/tflite_micro/signal:window_op_test`
* Makefile(C++): `make -f tensorflow/lite/micro/tools/make/Makefile test_kernel_window_test`  

BUG=[259145369](http://b/259145369)
2023-06-14 00:25:02 +00:00
Ryan Kuester
96c4b86214
feat(python): rename interpreter_test to runtime_test (#2045)
Rename interpreter_test to runtime_test to match the naming of the runtime
module, as is convention.

BUG=part of #1484
2023-06-13 21:40:43 +00:00
Ryan Kuester
e84914d881
refactor(python): rename extension module to _runtime (#2040)
Rename extension module `interpreter_wrapper_pybind` to `_runtime`. It is
conventional for an extension module to use the same name as its corresponding
pure-Python wrapper module, but with a leading underscore.

BUG=part of #1484
2023-06-13 16:42:17 +00:00
Advait Jain
e39d319ff6
remove unused pybind_library. (#2034)
Internal checks caught this unused import in the BUILD file.

BUG=cleanup
2023-06-12 15:07:58 +00:00
Advait Jain
43541be261
Shim tflm_runtime.py to not break existing code after #2030 (#2032)
BUG=http://b/286456378
2023-06-09 21:35:52 +00:00
Ryan Kuester
8913f03377
python: rename package tflm_runtime to runtime (#2030)
Rename the Python package `tflm_runtime` to simply `runtime` in preparation for adding it under the new namespace package `tflite_micro`. Its full name will then be `tflite_micro.runtime`.

We have kept the `tflm_runtime` target as an alias in order to stage this change. More details in http://b/286456378

BUG=part of #1484
2023-06-08 23:26:13 +00:00
Ryan Kuester
e95532675c
build: remove intermediate pybind_library target (#2028)
Remove the pybind_library() used only as a dependency to the
pybind_extension(), and instead simply make the pybind_extension() directly. A
pybind_library() may be useful if used in other palces in addition to a
pybind_extension(), but with no such use in our tree, it just adds
complication.

BUG=part of #1484
2023-06-08 17:21:13 +00:00
Steven Toribio
f469180396
python op resolver visibility change (#1978)
changes python op resolver visibility to allow for nightly build generated integration test to be able to use it as well. Currently only seanet integration_tests have visibility 

[test via running workflow on forked repo](https://github.com/turbotoribio/tflite-micro/actions/runs/4997829269)

BUG=[b/276918689 ](https://b.corp.google.com/issues/276918689#comment17)
2023-05-17 06:31:33 +00:00
Steven Toribio
e344f4b6bb
Regenerates Integration_Test and depricates AllOpsResolver (#1969)
This PR does the following: 

This cl finishes the depreciation of AllOpsResolver in TFLM and then regenerate generate_per_layer test using PythonOpsResolver instead of AllOpsResolver after adding it to the MakoTemplate. 

[command used to regenerate integration_test](https://b.corp.google.com/issues/245770006#comment1)

[doc for changes](https://docs.google.com/document/d/1vafCGaFGGBauAssAdFABtmgpiRju2V3isioZfLMgeEc/edit?resourcekey=0-pc-aJl3TPZ7NcO9RxrNsrA#)

BUG=[b/276918689](https://b.corp.google.com/issues/276918689)
2023-05-15 17:36:16 +00:00
Steven Toribio
207dde4da9
adds PythonOpsResolver (#1965)
This PR does the following: 

1. Introduces PythonOpsResolver Target 
2. removes a reference to AllOpsResolver in remaing places in TFLM repo outside of integration_tests

[doc for changes](https://docs.google.com/document/d/1vafCGaFGGBauAssAdFABtmgpiRju2V3isioZfLMgeEc/edit?resourcekey=0-pc-aJl3TPZ7NcO9RxrNsrA#)

[google3 cl](https://critique.corp.google.com/cl/531013669)

BUG=[b/276918689](https://b.corp.google.com/issues/276918689)
2023-05-12 18:29:53 +00:00
TANMAY DAS
8503d790d5
Replace snprintf with std::string in interpreter_wrapper. (#1939)
BUG=http://b/279061457
2023-04-27 00:32:38 +00:00
Advait Jain
cfeb9a5284
Return the correct TfLiteStatus from interpreter->Invoke() (#1919)
We would like to throw and exception on kTfLiteError (http://b/273622999) and pass-through all the other TfLiteStatus returned by calling interpreter->Invoke()

https://github.com/tensorflow/tflite-micro/pull/1907 changed the behavior to throw an exception on kTfLiteError but did not pass through all the other status.

In particular, this is important because streaming implementation with circular_buffers relies on kTfLiteAbort being appropriately propagated.

BUG=http://b/279055226
2023-04-20 20:16:51 +00:00
TANMAY DAS
beb3acc15c
Invoke failure throws ValueError. (#1907)
BUG=http://b/273622999
2023-04-18 06:21:34 +00:00
Ryan Kuester
17aae9e35d
build: add means of linking with C libs in Py packages (#1668)
Extend the Python repository_rule used to create external repositories, adding targets for C-language binary libraries shipped inside Python packages; e.g., that shipped in package tensorflow-gpu.

These targets are to be used as dependencies by C-language targets.

Note: when debugging the build, it can be helpful to examine the repository directory and BUILD file this repository_rule generates in the bazel cache.

Begin using a python/ directory at the root of the project for code that is specific to Python.

Upgrade to the latest version of rules_python first. Note that the unit test to keep requirements.in and requirements.txt is disabled (specifically with 
ec6bdc5d4443285d28a44076f06c203d9582e4a1)

Add a unit test for this feature.

BUG=see description
2023-03-05 06:25:58 +00:00
suleshahid
37e27ed08b
Remove deprecated parameter from TFLM python interpreter (#1799)
Cleanup of automatically calculating the number of resource variables in the python interpreter.
Removes the parameters from the two function calls to initialize the interpreter.
BUG=[251851084](https://b.corp.google.com/issues/251851084)
2023-03-01 22:01:49 +00:00
suleshahid
f5772962ef
Deprecate parameter for number of resource variables (#1738)
Continued work on automating resource variables from python. After removing usage of the parameter, this PR removes it completely from the function call.

In addition, we now print the number of resource variables if it is greater than zero, so the user can use it to get the count when doing C++.

BUG=[251851084](https://b.corp.google.com/issues/251851084)
2023-02-18 01:02:54 +00:00
suleshahid
634398344a
Python interpreter automatically count number of resource variables (#1716) 2023-02-07 14:37:15 -08:00
Pauline Sho
e2531de516
Update Python interpreter README.md
BUG=To be in sync with cl/491979522
2022-11-30 22:21:58 +00:00
Renjie
b4279b999d
Enable TAP for python interpreter test
BUG=http://b/247808903

Enable TAP for python interpreter test. Previous decision on disabling is due to the randomness of model parameters introduced by the training process. This issue will be addressed by b/248061370. This PR only relaxes the tolerance for test coverage considerations. 

This PR reverts https://github.com/tensorflow/tflite-micro/pull/1436.
2022-11-29 23:18:45 +00:00
Renjie
a00ecea566
Forcing reference kernel usage for TFLite interpreter during testing
BUG=http://b/259572815

Currently, the default kernel usage of the TFlite interpreter is optimized ones. This creates test inconsistencies when comparing TFLM results (reference kernel) with TFL's. Inference results comparison is logically valid if both are using reference kernels.
2022-11-21 20:40:49 +00:00
Pauline Sho
e72435ce4b
Add missing dependencies for some Python targets
BUG=b/257314384

Tested with copybara changes in cl/485911587 and a forced copybara import. Passed all presubmits.
2022-11-04 18:44:44 +00:00
Pauline Sho
4b419e9150
Add README for Python interpreter
BUG=b/256650563
2022-11-04 17:30:47 +00:00
TANMAY DAS
dde75de483
Add RecordingMicroAllocator to python interpreter (#1485) 2022-10-21 11:46:49 -07:00
suleshahid
83b711876d
Clean up python support for resource variables (#1480) 2022-10-18 21:45:59 +00:00
suleshahid
81f5208ef7
add support for MicroResourceVariables from python wrapper (#1462) 2022-10-13 22:56:06 +00:00
Pauline Sho
bea0931154
Manual sync from upstream
Manual sync after cl/480442151
Manually added the change in `numpy_utils.cc`

BUG=b/246606168
NO_CHECK_TFLITE_FILES=manually ran sync_from_upstream.sh
2022-10-12 01:37:55 +00:00
Ryan Kuester
f050eec7e3
python: fix problems with extension module build
Fix problems with the Python extension module build:

- Explicitly build against downloaded NumPy headers instead of relying on the build OS implicitly via the compiler's built-in paths, fixing #1283.
- Use tagged, verified downloads of pybind11_bazel and pybind to silence noise in the bazel output and improve tamper resistance, reproducibility, and caching.

The underlying issue was that the extension module depends on header files from numpy but that dependency was not captured in the BUILD rules.

Upstream Tensorflow's solution is too complicated to import. They manage dependencies on Python runtimes and libraries via much more extensive system of [custom rules](https://github.com/tensorflow/tensorflow/tree/master/third_party/py), designed to meet their many additional requirements.

Background:
- [pybind/pybind11](https://github.com/pybind/pybind11) is a C++ library that helps in creating our extension's interface to CPython.
- [pybind/pybind11_bazel](https://github.com/pybind/pybind11_bazel) provides Bazel rules for building pybind-based extensions, but doesn't cover the unusual case of using C headers provided in another Python module (in our case, NumPy).
- Bazel's own [bazelbuild/rules_python](https://github.com/bazelbuild/rules_python) provides rules for fetching Python dependencies from PyPI for use when *running* Python code via Bazel, but doesn't directly help with the unusual case of *building* code using headers provided by Python packages from PyPI (again, in our case, NumPy).
- The standard way, in Bazel, to build against third-party code is to create an [external repository](https://bazel.build/docs/external) by writing and calling [repository rules](https://bazel.build/extending/repo).

This PR supplies the C headers from NumPy to the build of our extension by adding a repository (via the new repository rule `tflm_py_cc_headers`) that reuses the PyPI package downloaded by rules_python and wraps it in a `cc_library` target on which the build of the extension can depend.

BUG=fixes #1283
2022-10-03 22:22:08 +00:00
Vamsi Krishna Manchala
89d554d791
Suppress the failing test from TAP Presubmit
This PR will suppress the `python/interpreter/tests:interpreter_test` temporarily as this needs some redesign in terms of making sure to test the right aspects added in https://github.com/tensorflow/tflite-micro/pull/1421.

The following bug will take care of the test changes- 

BUG=http://b/247808903
2022-09-22 02:09:02 +00:00
Renjie
282bcef7e6
Add random seed to the conv network training used in testing
BUG=b/248060722

Every random operation should be seeded to help debugging (deterministic outputs). Set the seed inside the conv net training code since random data is used as training inputs. 

Also, testing criteria on the quantization parameters are relaxed before b/248061370 is ready.
2022-09-22 01:20:35 +00:00
Vamsi Krishna Manchala
6fdade1aed
Update TFLM examples, kernels and integration tests to clean-up the usage of ErrorReporter
This PR updates remainder of the TFLM examples, all the kernels and integration tests to remove the usage of ErrorReporter as per the changes made to the TFLM Framework API in https://github.com/tensorflow/tflite-micro/pull/1415; replaces TF_LITE_REPORT_ERROR with MicroPrintf.

BUG=http://b/192091017, http://b/245802069
2022-09-20 20:46:26 +00:00
Renjie
5606c89583
Access input/output tensor details from python
Inference on a quantized model requires information about 
* input tensor quantization parameters (dtype, scale, zero point): to convert the input float data into the quantized one
* output tensor quantization parameters: convert the quantized result back to float (i.e., sanity check with normal model output)

This PR allows users to access these information from the python interpreter. 

It also refactored the error handling mechanism following TF and TFLite.  

BUG=b/245831075, b/246651364
2022-09-19 23:15:09 +00:00
rewu93
7af29235f5
Add reset function into the python wrapper
Some models such as LSTM are stateful, which requires state reset after each inference. This PR adds the model reset function, which has already been implemented in C++, into the TFLM python interpreter. 

BUG=http://b/244330968
2022-09-01 00:26:23 +00:00