-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Lower cache_read and cache_write to Hexagon DMA via tensorize #10365
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fe24883
Lower cache_read and cache_write to Hexagon DMA via tensorize
adstraw 459dbf8
Merge branch 'main' into hexagon-tir-to-dma-lower
adstraw 2fc7b5f
rework test to be compatible with launcher
adstraw 7c1ed6f
remove cpu device api mem_copy implementation and test
adstraw 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
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
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
183 changes: 183 additions & 0 deletions
183
tests/python/contrib/test_hexagon/test_cache_read_write.py
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,183 @@ | ||
| # 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 pytest | ||
| import numpy as np | ||
|
|
||
| import tvm.testing | ||
| from tvm import te | ||
| from tvm.contrib import utils | ||
| from tvm.contrib.hexagon.build import HexagonLauncher | ||
| import tvm.contrib.hexagon.hexagon as hexagon | ||
|
|
||
| from .conftest import requires_hexagon_toolchain | ||
|
|
||
|
|
||
| def intrin_mem_copy(shape, dtype, dst_scope, src_scope): | ||
| assert len(shape) == 1 | ||
| src = te.placeholder(shape=shape, dtype=dtype, name="src") | ||
| dst = te.compute(shape, lambda i: src[i], name="dst") | ||
| size = shape[0] * np.dtype(dtype).itemsize | ||
|
|
||
| src_buffer = tvm.tir.decl_buffer( | ||
| shape, | ||
| dtype, | ||
| scope=src_scope, | ||
| offset_factor=1, | ||
| ) | ||
|
|
||
| dst_buffer = tvm.tir.decl_buffer( | ||
| shape, | ||
| dtype, | ||
| scope=dst_scope, | ||
| offset_factor=1, | ||
| ) | ||
|
|
||
| def intrin_func(ins, outs): | ||
| ib = tvm.tir.ir_builder.create() | ||
|
|
||
| _src = ins[0] | ||
| _dst = outs[0] | ||
| ib.emit( | ||
| tvm.tir.call_intrin( | ||
| "handle", "tir.mem_copy", _dst.access_ptr("w"), _src.access_ptr("r"), size | ||
| ) | ||
| ) | ||
| return ib.get() | ||
|
|
||
| return te.decl_tensor_intrin(dst.op, intrin_func, binds={src: src_buffer, dst: dst_buffer}) | ||
|
|
||
|
|
||
| @requires_hexagon_toolchain | ||
| def test_hexagon(android_serial_number, tvm_tracker_host, tvm_tracker_port): | ||
| size = 128 | ||
| outer_shape = (size,) | ||
| factor = 16 | ||
| inner_shape = (factor,) | ||
| dtype = "int8" | ||
|
|
||
| x = te.placeholder(shape=outer_shape, dtype=dtype, name="x") | ||
| y = te.placeholder(shape=outer_shape, dtype=dtype, name="y") | ||
| z = te.compute(outer_shape, lambda i: x[i] + y[i], name="z") | ||
| s = te.create_schedule(z.op) | ||
|
|
||
| x_global = s.cache_read(x, "global.vtcm", [z]) | ||
| y_global = s.cache_read(y, "global.vtcm", [z]) | ||
| z_global = s.cache_write(z, "global.vtcm") | ||
|
|
||
| zouter, zinner = s[z_global].split(z_global.op.axis[0], factor=factor) | ||
|
|
||
| s[x_global].compute_at(s[z_global], zouter) | ||
| s[y_global].compute_at(s[z_global], zouter) | ||
|
|
||
| mem_copy_read = intrin_mem_copy(inner_shape, dtype, "global.vtcm", "global") | ||
|
|
||
| (cache_read_x,) = s[x_global].op.axis | ||
| s[x_global].tensorize(cache_read_x, mem_copy_read) | ||
|
|
||
| (cache_read_y,) = s[y_global].op.axis | ||
| s[y_global].tensorize(cache_read_y, mem_copy_read) | ||
|
|
||
| mem_copy_write = intrin_mem_copy(outer_shape, dtype, "global", "global.vtcm") | ||
|
|
||
| (cache_write_z,) = s[z].op.axis | ||
| s[z].tensorize(cache_write_z, mem_copy_write) | ||
|
|
||
| print(tvm.lower(s, [x, y, z])) | ||
|
|
||
| target_hexagon = tvm.target.hexagon("v68", link_params=True) | ||
| func = tvm.build( | ||
| s, [x, y, z], tvm.target.Target(target_hexagon, host=target_hexagon), name="dmacpy" | ||
| ) | ||
| temp = utils.tempdir() | ||
| dso_binary = "test_binary.so" | ||
| dso_binary_path = temp.relpath(dso_binary) | ||
| func.save(dso_binary_path) | ||
|
|
||
| if not android_serial_number: | ||
| pytest.skip("Skip hardware test since ANDROID_SERIAL_NUMBER is not set.") | ||
|
|
||
| launcher = HexagonLauncher(serial_number=android_serial_number) | ||
| launcher.android_run_rpc(rpc_tracker_host=tvm_tracker_host, rpc_tracker_port=tvm_tracker_port) | ||
| launcher.hexagon_setup() | ||
| remote_kw = { | ||
| "host": tvm_tracker_host, | ||
| "port": tvm_tracker_port, | ||
| "priority": 0, | ||
| "timeout": 60, | ||
| } | ||
| launcher.hexagon_session_setup(remote_kw) | ||
| launcher.upload(dso_binary_path, dso_binary) | ||
|
|
||
| with launcher.session as sess: | ||
| mod = launcher.get_module(dso_binary) | ||
| xt = tvm.nd.array(np.random.uniform(size=size).astype(x.dtype), device=sess.device) | ||
| yt = tvm.nd.array(np.random.uniform(size=size).astype(y.dtype), device=sess.device) | ||
| zt = tvm.nd.array(np.random.uniform(size=size).astype(z.dtype), device=sess.device) | ||
| mod["dmacpy"](xt, yt, zt) | ||
| launcher.close() | ||
|
|
||
| ref = xt.numpy() + yt.numpy() | ||
| np.testing.assert_equal(zt.numpy(), ref) | ||
|
|
||
|
|
||
| def test_cpu(): | ||
adstraw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| size = 128 | ||
| outer_shape = (size,) | ||
| factor = 16 | ||
| inner_shape = (factor,) | ||
| dtype = "int8" | ||
|
|
||
| x = te.placeholder(shape=outer_shape, dtype=dtype, name="x") | ||
| y = te.placeholder(shape=outer_shape, dtype=dtype, name="y") | ||
| z = te.compute(outer_shape, lambda i: x[i] + y[i], name="z") | ||
| s = te.create_schedule(z.op) | ||
|
|
||
| x_global = s.cache_read(x, "global", [z]) | ||
| y_global = s.cache_read(y, "global", [z]) | ||
| z_global = s.cache_write(z, "global") | ||
|
|
||
| zouter, zinner = s[z_global].split(z_global.op.axis[0], factor=factor) | ||
|
|
||
| s[x_global].compute_at(s[z_global], zouter) | ||
| s[y_global].compute_at(s[z_global], zouter) | ||
|
|
||
| mem_copy_read = intrin_mem_copy(inner_shape, dtype, "global", "global") | ||
|
|
||
| (cache_read_x,) = s[x_global].op.axis | ||
| s[x_global].tensorize(cache_read_x, mem_copy_read) | ||
|
|
||
| (cache_read_y,) = s[y_global].op.axis | ||
| s[y_global].tensorize(cache_read_y, mem_copy_read) | ||
|
|
||
| mem_copy_write = intrin_mem_copy(outer_shape, dtype, "global", "global") | ||
|
|
||
| (cache_write_z,) = s[z].op.axis | ||
| s[z].tensorize(cache_write_z, mem_copy_write) | ||
|
|
||
| print(tvm.lower(s, [x, y, z])) | ||
| func = tvm.build(s, [x, y, z], target="llvm") | ||
|
|
||
| dev = tvm.device("llvm", 0) | ||
|
|
||
| xt = tvm.nd.array(np.random.uniform(size=size).astype(x.dtype), dev) | ||
| yt = tvm.nd.array(np.random.uniform(size=size).astype(y.dtype), dev) | ||
| zt = tvm.nd.array(np.random.uniform(size=size).astype(z.dtype), dev) | ||
| func(xt, yt, zt) | ||
|
|
||
| ref = xt.numpy() + yt.numpy() | ||
| np.testing.assert_equal(zt.numpy(), ref) | ||
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.
cc @tqchen @junrushao1994 if they want a better name here.
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.
Is it possible to make it more specific? We have a few examples above like texture2d_load/store. What about dma_mem_copy() or any hexagon-specific name?
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 was thinking about something with "DMA" in the name but I realized that DMA is more of an implementation detail. I ended up referencing DMA in the comments instead. Happy to take suggestions including
dma_mem_copy.Uh oh!
There was an error while loading. Please reload this page.
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 thought about:
mem_copy_local(to emphasize the local nature of the intended usage, unlikeTVMArrayCopyFromTo@junrushao1994 mentioned which might involve RPC)mem_copy_1d(if we intend to add 2d rectangular memcpy later)But both of them don't particularly sound better.