-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[2/3][AOT][DeviceAPI] Add Hooks for Activate/Deactivate/Open/Close #9500
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| from collections import OrderedDict | ||
| import sys | ||
| import re | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
@@ -693,5 +694,108 @@ def @main(%data: Tensor[(1, 4, 4, 4), float32], %weight: Tensor[(4, 4, 3, 3), fl | |
| assert source.count("TVMBackendAllocWorkspace") == 3 | ||
|
|
||
|
|
||
| def test_device_api_hooks(): | ||
| """Check for Device API hooks""" | ||
|
|
||
| # Ideally we should have a sample Target registered here | ||
| # but we're going to re-use this for now | ||
| pytest.importorskip("ethosu.vela") | ||
| import tensorflow as tf | ||
| import tflite.Model | ||
|
|
||
| from tests.python.contrib.test_ethosu import infra | ||
| from tvm.relay.op.contrib.ethosu import partition_for_ethosu | ||
|
|
||
| def create_tflite_graph(): | ||
| tf.config.run_functions_eagerly(True) | ||
|
|
||
| class Model(tf.Module): | ||
| @tf.function | ||
| def tf_function(self, x): | ||
| return tf.nn.max_pool(x, [1, 2], [1, 2], "SAME") | ||
|
|
||
| def representative_dataset(): | ||
| for _ in range(100): | ||
| data = np.random.rand(*tuple([1, 3, 4, 3])) | ||
|
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. @Mousius Could the unpack here be directly from the list? Like:
Member
Author
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. Oh, nice spot @gromero, by the same token I don't think we even need to unpack the list? This is the same as
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. @Mousius Hi! Yeah, I thought of that too (avoiding the unpack too), however I assumed you would like to keep it as |
||
| yield [data.astype(np.float32)] | ||
|
|
||
| model = Model() | ||
| concrete_func = model.tf_function.get_concrete_function( | ||
| tf.TensorSpec([1, 3, 4, 3], dtype=tf.float32) | ||
| ) | ||
|
|
||
| converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) | ||
| converter.optimizations = [tf.lite.Optimize.DEFAULT] | ||
| converter.representative_dataset = representative_dataset | ||
| converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] | ||
| converter.inference_input_type = tf.int8 | ||
| converter.inference_output_type = tf.int8 | ||
| tflite_model = converter.convert() | ||
| return tflite_model | ||
|
|
||
| tflite_graph = create_tflite_graph() | ||
| tflite_model = tflite.Model.Model.GetRootAsModel(tflite_graph, 0) | ||
|
|
||
| relay_module, params = relay.frontend.from_tflite( | ||
| tflite_model, | ||
| shape_dict={"x": [1, 3, 4, 3]}, | ||
| dtype_dict={"x": "int8"}, | ||
| ) | ||
| mod = partition_for_ethosu(relay_module, params) | ||
|
|
||
| # Generate reference data | ||
| input_data, output_data = infra.generate_ref_data_tflite(tflite_graph) | ||
|
|
||
| compiled_models = infra.build_source( | ||
| mod, | ||
| input_data, | ||
| output_data, | ||
| ) | ||
| main_ir_module = list(compiled_models[0].executor_factory.lowered_ir_mods.values())[0] | ||
| main_func = main_ir_module["run_model"] | ||
|
|
||
| # Activate Device | ||
| assert ( | ||
| str(main_func.body[0][0].value) | ||
| == "@tir.call_extern(" | ||
| + '"TVMDeviceEthosUActivate",' | ||
| + " device_context_ethos_u: handle," | ||
| + " dtype=int32)" | ||
| ) | ||
| # Open Device | ||
| assert ( | ||
| str(main_func.body[1].body.body[0][0][0].value) | ||
| == "@tir.call_extern(" | ||
| + '"TVMDeviceEthosUOpen",' | ||
| + " device_context_ethos_u: handle," | ||
| + " dtype=int32)" | ||
| ) | ||
| # Device Call | ||
| assert ( | ||
| str(main_func.body[1].body.body[0][0][1].value) | ||
| == "@tir.call_extern(" | ||
| + '"tvmgen_default_ethos_u_main_0",' | ||
| + " input: handle, output: handle," | ||
| + " device_context_ethos_u: handle," | ||
| + " dtype=int32)" | ||
| ) | ||
| # Close Device | ||
| assert ( | ||
| str(main_func.body[1].body.body[0][0][2].value) | ||
| == "@tir.call_extern(" | ||
| + '"TVMDeviceEthosUClose",' | ||
| + " device_context_ethos_u: handle," | ||
| + " dtype=int32)" | ||
| ) | ||
| # Deactivate Device | ||
| assert ( | ||
| str(main_func.body[2][0].value) | ||
| == "@tir.call_extern(" | ||
| + '"TVMDeviceEthosUDeactivate",' | ||
| + " device_context_ethos_u: handle," | ||
| + " dtype=int32)" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(pytest.main([__file__] + sys.argv[1:])) | ||
Uh oh!
There was an error while loading. Please reload this page.