-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Hexagon] Add USMP tests #11279
Merged
Merged
[Hexagon] Add USMP tests #11279
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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,35 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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. | ||
""" This file contains USMP tests harnesses.""" | ||
|
||
import tvm | ||
|
||
|
||
def check_for_no_tvm_backendallocworkspace_calls(mod: tvm.runtime.module): | ||
"""This checker checks whether any c-source produced has TVMBackendAllocWorkspace calls. | ||
If USMP is invoked, none of them should have TVMBAW calls""" | ||
dso_modules = mod._collect_dso_modules() | ||
for dso_mod in dso_modules: | ||
if dso_mod.type_key not in ["c", "llvm"]: | ||
assert ( | ||
False | ||
), 'Current AoT codegen flow should only produce type "c" or "llvm" runtime modules' | ||
|
||
source = dso_mod.get_source() | ||
assert ( | ||
source.count("TVMBackendAllocWorkspace") == 0 | ||
), "This is failing because USMP was unable to plan for every tir.allocate node" |
This file contains 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 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 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 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 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,110 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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 sys | ||
import pytest | ||
import numpy as np | ||
|
||
import tvm.testing | ||
from tvm import te | ||
from tvm import relay | ||
from tvm.relay.backend import Executor, Runtime | ||
from tvm.contrib.hexagon.session import Session | ||
from tvm.testing.usmp import check_for_no_tvm_backendallocworkspace_calls | ||
|
||
from .conftest import requires_hexagon_toolchain | ||
|
||
|
||
@requires_hexagon_toolchain | ||
def test_conv2d(hexagon_session: Session, aot_host_target, aot_target): | ||
dtype = "float32" | ||
input_shape = (1, 8, 8, 3) | ||
w1_shape = (5, 5, 3, 1) | ||
w2_shape = (5, 5, 1, 3) | ||
data = relay.var("data", relay.TensorType(input_shape, dtype)) | ||
weight1 = relay.var("weight1", relay.TensorType(w1_shape, dtype)) | ||
weight2 = relay.var("weight2", relay.TensorType(w2_shape, dtype)) | ||
y1 = relay.nn.conv2d( | ||
data, | ||
weight1, | ||
padding=(2, 2), | ||
kernel_size=(5, 5), | ||
data_layout="NHWC", | ||
kernel_layout="HWIO", | ||
out_dtype="float32", | ||
) | ||
y2 = relay.nn.conv2d( | ||
y1, | ||
weight2, | ||
padding=(2, 2), | ||
kernel_size=(5, 5), | ||
data_layout="NHWC", | ||
kernel_layout="HWIO", | ||
out_dtype="float32", | ||
) | ||
f = relay.Function([data, weight1, weight2], y2) | ||
relay_mod = tvm.IRModule.from_expr(f) | ||
relay_mod = relay.transform.InferType()(relay_mod) | ||
|
||
weight1_data = np.random.rand(w1_shape[0], w1_shape[1], w1_shape[2], w1_shape[3]).astype( | ||
dtype=dtype | ||
) | ||
weight2_data = np.random.rand(w2_shape[0], w2_shape[1], w2_shape[2], w2_shape[3]).astype( | ||
dtype=dtype | ||
) | ||
input_data = np.random.rand( | ||
input_shape[0], input_shape[1], input_shape[2], input_shape[3] | ||
).astype(dtype=dtype) | ||
|
||
params = {"weight1": weight1_data, "weight2": weight2_data} | ||
inputs = {"data": input_data} | ||
|
||
with tvm.transform.PassContext(opt_level=3, config={"tir.usmp.enable": True}): | ||
lowered = tvm.relay.build( | ||
relay_mod, | ||
params=params, | ||
target=tvm.target.Target(aot_target, host=aot_host_target), | ||
runtime=Runtime("cpp"), | ||
executor=Executor("aot", {"unpacked-api": False, "interface-api": "packed"}), | ||
) | ||
|
||
check_for_no_tvm_backendallocworkspace_calls(lowered.lib) | ||
|
||
aot_mod = hexagon_session.get_executor_from_factory(lowered) | ||
aot_mod.set_input(**inputs) | ||
aot_mod.run() | ||
hexagon_output = aot_mod.get_output(0).numpy() | ||
|
||
target_llvm = tvm.target.Target("llvm") | ||
with tvm.transform.PassContext(opt_level=3): | ||
llvm_lowered = tvm.relay.build( | ||
relay_mod, | ||
tvm.target.Target(target_llvm, host=target_llvm), | ||
runtime=Runtime("cpp"), | ||
executor=Executor("graph"), | ||
) | ||
|
||
llvm_graph_mod = tvm.contrib.graph_executor.GraphModule(llvm_lowered["default"](tvm.cpu(0))) | ||
llvm_graph_mod.set_input(**params) | ||
llvm_graph_mod.run(**inputs) | ||
expected_output = llvm_graph_mod.get_output(0).numpy() | ||
|
||
tvm.testing.assert_allclose(hexagon_output, expected_output, rtol=1e-4, atol=1e-5) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(sys.argv)) |
This file contains 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.
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.
Can we also verify in this test that without this pass we do detect TVMBackendAllocWorkspace and then with this pass we detect that they no longer are present?
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.
I suspect we should because of the padding in conv2d as long as it isn't inlined, but worth checking just in case.
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.
yes, will add that.