-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Relax][Frontend][TFLite] Add HASHTABLE_LOOKUP converter #19654
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
LudovicoYIN
wants to merge
1
commit into
apache:main
Choose a base branch
from
LudovicoYIN:lukeyin/tflite-hashtable-lookup
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.
+172
−0
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 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4053,6 +4053,18 @@ def _get_builtin_operator(builtin_name): | |||||||||||||||||||||||||||||||||||||
| return getattr(_tfl_builtin_operator, builtin_name) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _run_module(mod, *inputs): | ||||||||||||||||||||||||||||||||||||||
| tgt = tvm.target.Target("c") | ||||||||||||||||||||||||||||||||||||||
| ex = tvm.compile(mod, tgt) | ||||||||||||||||||||||||||||||||||||||
| vm = relax.VirtualMachine(ex, tvm.cpu()) | ||||||||||||||||||||||||||||||||||||||
| vm.set_input("main", *inputs) | ||||||||||||||||||||||||||||||||||||||
| vm.invoke_stateful("main") | ||||||||||||||||||||||||||||||||||||||
| outputs = vm.get_outputs("main") | ||||||||||||||||||||||||||||||||||||||
| if hasattr(outputs, "numpy"): | ||||||||||||||||||||||||||||||||||||||
| return outputs.numpy() | ||||||||||||||||||||||||||||||||||||||
| return tuple(output.numpy() for output in outputs) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _build_tflite_call_model( | ||||||||||||||||||||||||||||||||||||||
| call_subgraph_index=1, | ||||||||||||||||||||||||||||||||||||||
| callee_inputs=None, | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -5844,6 +5856,36 @@ def _build_tflite_hashtable_size_uninitialized_model(): | |||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _build_tflite_hashtable_lookup_model(*, value_shape, value_type=None): | ||||||||||||||||||||||||||||||||||||||
| """Build a model containing one HASHTABLE_LOOKUP operator.""" | ||||||||||||||||||||||||||||||||||||||
| builder = flatbuffers.Builder(1024) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| value_type = _tfl_tensor_type.FLOAT32 if value_type is None else value_type | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| lookup_tensor = _build_tensor(builder, 0, [4], tensor_type=_tfl_tensor_type.INT32) | ||||||||||||||||||||||||||||||||||||||
| key_tensor = _build_tensor(builder, 1, [3], tensor_type=_tfl_tensor_type.INT32) | ||||||||||||||||||||||||||||||||||||||
| value_tensor = _build_tensor(builder, 2, value_shape, tensor_type=value_type) | ||||||||||||||||||||||||||||||||||||||
| output_tensor = _build_tensor(builder, 3, [4, *value_shape[1:]], tensor_type=value_type) | ||||||||||||||||||||||||||||||||||||||
| hits_tensor = _build_tensor(builder, 4, [4], tensor_type=_tfl_tensor_type.UINT8) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| hashtable_lookup = _build_operator(builder, 0, [0, 1, 2], [3, 4]) | ||||||||||||||||||||||||||||||||||||||
| main_subgraph = _build_subgraph( | ||||||||||||||||||||||||||||||||||||||
| builder, | ||||||||||||||||||||||||||||||||||||||
| tensors=[lookup_tensor, key_tensor, value_tensor, output_tensor, hits_tensor], | ||||||||||||||||||||||||||||||||||||||
| operators=[hashtable_lookup], | ||||||||||||||||||||||||||||||||||||||
| inputs=[0, 1, 2], | ||||||||||||||||||||||||||||||||||||||
| outputs=[3, 4], | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| operator_codes = [_build_operator_code(builder, _get_builtin_operator("HASHTABLE_LOOKUP"))] | ||||||||||||||||||||||||||||||||||||||
| buffers = [_build_buffer(builder) for _ in range(5)] | ||||||||||||||||||||||||||||||||||||||
| return _finish_tflite_model( | ||||||||||||||||||||||||||||||||||||||
| builder, | ||||||||||||||||||||||||||||||||||||||
| subgraph=main_subgraph, | ||||||||||||||||||||||||||||||||||||||
| operator_codes=operator_codes, | ||||||||||||||||||||||||||||||||||||||
| buffers=buffers, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def test_resource_variable_call_once_init_read(): | ||||||||||||||||||||||||||||||||||||||
| """Test reading a resource variable initialized by a supported CALL_ONCE subgraph.""" | ||||||||||||||||||||||||||||||||||||||
| mod = _load_model_from_buffer(_build_tflite_resource_variable_model()) | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -5908,6 +5950,53 @@ def test_hashtable_size_uninitialized_unsupported(): | |||||||||||||||||||||||||||||||||||||
| _load_model_from_buffer(_build_tflite_hashtable_size_uninitialized_model()) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def test_hashtable_lookup_1d_value(): | ||||||||||||||||||||||||||||||||||||||
| mod = _load_model_from_buffer(_build_tflite_hashtable_lookup_model(value_shape=[3])) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| output, hits = _run_module( | ||||||||||||||||||||||||||||||||||||||
| mod, | ||||||||||||||||||||||||||||||||||||||
| np.array([1234, -292, -11, 0], dtype=np.int32), | ||||||||||||||||||||||||||||||||||||||
| np.array([-11, 0, 1234], dtype=np.int32), | ||||||||||||||||||||||||||||||||||||||
| np.array([0.0, 0.1, 0.4], dtype=np.float32), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(output, np.array([0.4, 0.0, 0.0, 0.1], dtype=np.float32)) | ||||||||||||||||||||||||||||||||||||||
| np.testing.assert_array_equal(hits, np.array([1, 0, 1, 1], dtype=np.uint8)) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5956
to
+5964
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since TFLite hashtable keys are not guaranteed to be sorted, we should update the test case to use unsorted keys to ensure correctness and prevent regressions.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def test_hashtable_lookup_2d_value(): | ||||||||||||||||||||||||||||||||||||||
| mod = _load_model_from_buffer(_build_tflite_hashtable_lookup_model(value_shape=[3, 2])) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| output, hits = _run_module( | ||||||||||||||||||||||||||||||||||||||
| mod, | ||||||||||||||||||||||||||||||||||||||
| np.array([1234, -292, -11, 0], dtype=np.int32), | ||||||||||||||||||||||||||||||||||||||
| np.array([-11, 0, 1234], dtype=np.int32), | ||||||||||||||||||||||||||||||||||||||
| np.array([[0.0, 0.1], [1.0, 1.1], [2.0, 2.1]], dtype=np.float32), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose( | ||||||||||||||||||||||||||||||||||||||
| output, | ||||||||||||||||||||||||||||||||||||||
| np.array( | ||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||
| [2.0, 2.1], | ||||||||||||||||||||||||||||||||||||||
| [0.0, 0.0], | ||||||||||||||||||||||||||||||||||||||
| [0.0, 0.1], | ||||||||||||||||||||||||||||||||||||||
| [1.0, 1.1], | ||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||
| dtype=np.float32, | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| np.testing.assert_array_equal(hits, np.array([1, 0, 1, 1], dtype=np.uint8)) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def test_hashtable_lookup_string_value_unsupported(): | ||||||||||||||||||||||||||||||||||||||
| string_type = _get_string_tensor_type() | ||||||||||||||||||||||||||||||||||||||
| with pytest.raises(ValueError, match="unknown dtype `string`"): | ||||||||||||||||||||||||||||||||||||||
| _load_model_from_buffer( | ||||||||||||||||||||||||||||||||||||||
| _build_tflite_hashtable_lookup_model(value_shape=[3], value_type=string_type) | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _get_stablehlo_builtin_operator(builtin_name): | ||||||||||||||||||||||||||||||||||||||
| if not hasattr(_tfl_builtin_operator, builtin_name): | ||||||||||||||||||||||||||||||||||||||
| pytest.skip(f"TFLite schema does not provide BuiltinOperator.{builtin_name}") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation uses
relax.op.bucketizeto find the lookup positions. However,bucketizeassumes that the boundaries (thekeytensor) are sorted. In TFLite,HASHTABLE_LOOKUPkeys are not guaranteed to be sorted. If the keys are unsorted,bucketizewill perform an incorrect binary search and return wrong indices.To support unsorted keys correctly, we can use a broadcasted comparison to find the matching indices using
relax.op.equal,relax.op.argmax, andrelax.op.max.