Commit graph

29 commits

Author SHA1 Message Date
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
suleshahid
1e74475fde
Update Flatbuffer version to 2.0.6 and generate schema local. (#1374) 2022-08-25 18:01:35 +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
TANMAY DAS
0e825b99b3
Removed a simple typo. (#1247)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2022-07-12 23:57:25 +00:00
deqiangc
24c08505df
Fix to tflite_size (#1248)
1. Make variable name c-style following python style guide
2. Move file IO out of convert_tflite_to_html function. File io make
testing this function hard
3. Avoid file write in test and other complaints discovered in importing
   to internal

BUG=discovery these when importing
2022-07-08 21:55:20 +00:00
deqiangc
b45edd7086
Update documentation for tflite-size (#1238)
Since the html is not directly renderable by github, insert a png
to show the tool's output.

Also beautify the output slightly: calls out the number is for size
explicitly.

BUG=
2022-07-06 14:48:21 +00:00
deqiangc
2cd28c7fbc
A tool to visualize tflite file with size info in html format (#1215)
* A tool to visualize tflite file with size info in html format

* remove redundant build target

* Add main to tflite_size and this allows the following usage:

bazel run flatbuffer_size -- input.tflite output.html

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2022-06-30 18:28:52 +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