Commit Graph

42 Commits

Author SHA1 Message Date
suleshahid 462335af96 Remove unused BUILD load (#2397)
BUG=[318738218](https://b.corp.google.com/issues/318738218)
2026-04-20 14:47:58 +08:00
Steven Toribio 2ab85f2f7e 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)
2026-04-20 14:47:57 +08: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
Pauline Sho f8baced726
Support custom ops on TFLM Python interpreter (#1302)
* Support custom ops on TFLM Python interpreter

BUG=b/236154921

* code style
2022-08-02 23:44:12 +00:00
Pauline Sho c478145e83
Fix Python interpreter destructor bug and disable invoke exception (#1254)
BUG=b/239225101

In the current Python interpreter, we do Py_DECREF(model_) in the body destructor of
InterpreterWrapper, which will be called first before member variables' destructors. This causes
an issue because destructing the MicroInterpreter (namely FreeSubgraphs) relies on some parts of
model_. This means that when the destructor of MicroInterpreter is called, the model may have
already been released, causing a segfault.

We can bypass this by not using a unique pointer for MicroInterpreter, and simply delete
interpreter_ before we call Py_DECREF(model_).

BUG=b/238459185

Some error codes are expected, such as cycles_until_run in the circular buffer op. The interpreter
wrapper shouldn't throw any exception in Python when invoke returns an error.
2022-07-15 22:12:21 +00:00
Pauline Sho 82f441328b
Disable ubsan for interpreter_test (#1185)
Asan and msan are already disabled, ubsan should be as well but was
initially undetected because ubsan tests are already disabled on GitHub
CI. This was caught during the internal sync.

BUG=b/234879912
2022-06-06 21:40:17 +00:00
Pauline Sho 569b80df95
Move model generation to each test in Python interpreter test (#1183)
BUG=b/234883019
2022-06-06 18:14:23 +00:00
Pauline Sho 90d62ab628
Implement Python interpreter for TFLM (#1147)
* First pass working with float input/output

* Numpy buffer input works. Got a sine graph for hello world output.

Copyright notice

* Remove static references

* Output tensors work

* Output tensors work

* bazel test works

bazel test tensorflow/lite/micro/tools:interpreter_test
--test_output=all

* Successfully built extension with Python setuptools

cd tensorflow/lite/micro/tools/interpreter_pypi
python -m build
pip install dist/example_tflm_interpreter_psho-0.0.3-cp39-cp39-linux_x86_64.whl --force-reinstall
python tests/test-interpreter.py

TestPyPi:
pip uninstall example-tflm-interpreter-psho
python -m twine upload --repository testpypi dist/example-tflm-interpreter-psho-0.0.3.tar.gz
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple example-tflm-interpreter-psho==0.0.3
python tests/test-interpreter.py

* Refactored some

* Cleaned for production and tested with conv model

* Fixed formatting

* Fixed numpy header issue for Docker. Need to fix double registration issue that's somehow only present in Docker.

* Test fix with changes in ci.yml

* Added sudo

* Added nomsan and noasan tags to python test

* Added -layering-check, used py_library instead of py_binary to generate conv model, added micro namespace

* Removed extra debug code

* Added new tests

* Changed reinterpret_steal to cast

* Addressed PR comments

* size log refactor (#1156)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Addressed PR and review comments

* adding documentation to log_binary* workflows. (#1161)

* Automated binary size log update (#1160)

* Create an allocator to manage persistent arena (#1174)

* Create an allocator to manage persistent arena

This PR corresponds to internal cl/452380512.

Create an allocator to manage persistent arena.
This is another step towards the feature of enabling the client to
have two separate memory arenas.

BUG=https://b/226971240

* Manually patch due to Makefile difference

* Fix virtual environment when building Vela for Arm Ethos-U (#1177)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Removed print statement

Co-authored-by: jwithers <jpwithers@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: TFLM-bot <tflm-github-bot@google.com>
Co-authored-by: deqiangc <86809673+deqiangc@users.noreply.github.com>
Co-authored-by: Måns Nilsson <mans.nilsson@arm.com>
2022-06-03 19:09:54 +00:00