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.
- 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
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
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
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
@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
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
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)
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
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
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
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
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
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)
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)
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.
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.
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
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
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.
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
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