-
Notifications
You must be signed in to change notification settings - Fork 1k
refactor(compression): hoist numpy dtype map into tensor_type #3578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkuester
wants to merge
1
commit into
tensorflow:main
Choose a base branch
from
rkuester:feat-tensor-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Copyright 2026 The TensorFlow Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Single source of truth for mapping a TFLite TensorType to a numpy dtype. | ||
|
|
||
| Compression tooling reads tensor buffer bytes as numpy arrays, so it needs to | ||
| know the element type. Only the TensorTypes with a clean numpy equivalent are | ||
| mapped; anything else raises rather than silently guessing a type. | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| from tflite_micro.tensorflow.lite.python import schema_py_generated as tflite | ||
|
|
||
| # TFLite buffers are little-endian, so the dtypes are pinned to little-endian | ||
| # byte order to keep np.frombuffer correct on any host. | ||
| _TO_NUMPY = { | ||
| tflite.TensorType.FLOAT16: np.dtype("<f2"), | ||
| tflite.TensorType.FLOAT32: np.dtype("<f4"), | ||
| tflite.TensorType.FLOAT64: np.dtype("<f8"), | ||
| tflite.TensorType.INT8: np.dtype("<i1"), | ||
| tflite.TensorType.INT16: np.dtype("<i2"), | ||
| tflite.TensorType.INT32: np.dtype("<i4"), | ||
| tflite.TensorType.INT64: np.dtype("<i8"), | ||
| tflite.TensorType.UINT8: np.dtype("<u1"), | ||
| tflite.TensorType.UINT16: np.dtype("<u2"), | ||
| tflite.TensorType.UINT32: np.dtype("<u4"), | ||
| tflite.TensorType.UINT64: np.dtype("<u8"), | ||
| } | ||
|
|
||
| # TensorType value -> name, for readable error messages. | ||
| _NAMES = { | ||
| value: name | ||
| for name, value in vars(tflite.TensorType).items() | ||
| if not name.startswith("_") | ||
| } | ||
|
|
||
|
|
||
| def to_numpy(tensor_type: int) -> np.dtype: | ||
| """Return the little-endian numpy dtype for a TFLite TensorType. | ||
|
|
||
| Raises: | ||
| ValueError: if the type has no clean numpy equivalent (e.g. STRING, | ||
| RESOURCE, VARIANT, BFLOAT16, or the sub-byte INT4/UINT4/INT2 types). | ||
| """ | ||
| try: | ||
| return _TO_NUMPY[tensor_type] | ||
| except KeyError: | ||
| name = _NAMES.get(tensor_type, "?") | ||
| raise ValueError( | ||
| f"no numpy dtype for TFLite TensorType {name} ({tensor_type})") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright 2026 The TensorFlow Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from tflite_micro.tensorflow.lite.python import schema_py_generated as tflite | ||
| from tflite_micro.tensorflow.lite.micro.compression import tensor_type | ||
|
|
||
|
|
||
| class TensorTypeTest(unittest.TestCase): | ||
|
|
||
| def test_maps_known_types_to_little_endian_dtypes(self): | ||
| self.assertEqual(tensor_type.to_numpy(tflite.TensorType.INT8), | ||
| np.dtype("<i1")) | ||
| self.assertEqual(tensor_type.to_numpy(tflite.TensorType.UINT32), | ||
| np.dtype("<u4")) | ||
| self.assertEqual(tensor_type.to_numpy(tflite.TensorType.FLOAT32), | ||
| np.dtype("<f4")) | ||
|
|
||
| def test_dtype_itemsize_matches_type_width(self): | ||
| # Reading buffers depends on the dtype having the right element size. | ||
| self.assertEqual(tensor_type.to_numpy(tflite.TensorType.INT16).itemsize, 2) | ||
| self.assertEqual( | ||
| tensor_type.to_numpy(tflite.TensorType.FLOAT64).itemsize, 8) | ||
|
|
||
| def test_raises_on_type_without_numpy_equivalent(self): | ||
| with self.assertRaises(ValueError): | ||
| tensor_type.to_numpy(tflite.TensorType.STRING) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.