|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pytest |
| 19 | +import numpy as np |
| 20 | + |
| 21 | +import tvm.testing |
| 22 | +from tvm import te |
| 23 | +from tvm.contrib import utils |
| 24 | +from tvm.contrib.hexagon.build import HexagonLauncher |
| 25 | +import tvm.contrib.hexagon.hexagon as hexagon |
| 26 | + |
| 27 | +from .conftest import requires_hexagon_toolchain |
| 28 | + |
| 29 | + |
| 30 | +def intrin_mem_copy(shape, dtype, dst_scope, src_scope): |
| 31 | + assert len(shape) == 1 |
| 32 | + src = te.placeholder(shape=shape, dtype=dtype, name="src") |
| 33 | + dst = te.compute(shape, lambda i: src[i], name="dst") |
| 34 | + size = shape[0] * np.dtype(dtype).itemsize |
| 35 | + |
| 36 | + src_buffer = tvm.tir.decl_buffer( |
| 37 | + shape, |
| 38 | + dtype, |
| 39 | + scope=src_scope, |
| 40 | + offset_factor=1, |
| 41 | + ) |
| 42 | + |
| 43 | + dst_buffer = tvm.tir.decl_buffer( |
| 44 | + shape, |
| 45 | + dtype, |
| 46 | + scope=dst_scope, |
| 47 | + offset_factor=1, |
| 48 | + ) |
| 49 | + |
| 50 | + def intrin_func(ins, outs): |
| 51 | + ib = tvm.tir.ir_builder.create() |
| 52 | + |
| 53 | + _src = ins[0] |
| 54 | + _dst = outs[0] |
| 55 | + ib.emit( |
| 56 | + tvm.tir.call_intrin( |
| 57 | + "handle", "tir.mem_copy", _dst.access_ptr("w"), _src.access_ptr("r"), size |
| 58 | + ) |
| 59 | + ) |
| 60 | + return ib.get() |
| 61 | + |
| 62 | + return te.decl_tensor_intrin(dst.op, intrin_func, binds={src: src_buffer, dst: dst_buffer}) |
| 63 | + |
| 64 | + |
| 65 | +@requires_hexagon_toolchain |
| 66 | +def test_cache_read_write(android_serial_number, tvm_tracker_host, tvm_tracker_port): |
| 67 | + size = 128 |
| 68 | + outer_shape = (size,) |
| 69 | + factor = 16 |
| 70 | + inner_shape = (factor,) |
| 71 | + dtype = "int8" |
| 72 | + |
| 73 | + x = te.placeholder(shape=outer_shape, dtype=dtype, name="x") |
| 74 | + y = te.placeholder(shape=outer_shape, dtype=dtype, name="y") |
| 75 | + z = te.compute(outer_shape, lambda i: x[i] + y[i], name="z") |
| 76 | + s = te.create_schedule(z.op) |
| 77 | + |
| 78 | + x_global = s.cache_read(x, "global.vtcm", [z]) |
| 79 | + y_global = s.cache_read(y, "global.vtcm", [z]) |
| 80 | + z_global = s.cache_write(z, "global.vtcm") |
| 81 | + |
| 82 | + zouter, zinner = s[z_global].split(z_global.op.axis[0], factor=factor) |
| 83 | + |
| 84 | + s[x_global].compute_at(s[z_global], zouter) |
| 85 | + s[y_global].compute_at(s[z_global], zouter) |
| 86 | + |
| 87 | + mem_copy_read = intrin_mem_copy(inner_shape, dtype, "global.vtcm", "global") |
| 88 | + |
| 89 | + (cache_read_x,) = s[x_global].op.axis |
| 90 | + s[x_global].tensorize(cache_read_x, mem_copy_read) |
| 91 | + |
| 92 | + (cache_read_y,) = s[y_global].op.axis |
| 93 | + s[y_global].tensorize(cache_read_y, mem_copy_read) |
| 94 | + |
| 95 | + mem_copy_write = intrin_mem_copy(outer_shape, dtype, "global", "global.vtcm") |
| 96 | + |
| 97 | + (cache_write_z,) = s[z].op.axis |
| 98 | + s[z].tensorize(cache_write_z, mem_copy_write) |
| 99 | + |
| 100 | + print(tvm.lower(s, [x, y, z])) |
| 101 | + |
| 102 | + target_hexagon = tvm.target.hexagon("v68", link_params=True) |
| 103 | + func = tvm.build( |
| 104 | + s, [x, y, z], tvm.target.Target(target_hexagon, host=target_hexagon), name="dmacpy" |
| 105 | + ) |
| 106 | + temp = utils.tempdir() |
| 107 | + dso_binary = "test_binary.so" |
| 108 | + dso_binary_path = temp.relpath(dso_binary) |
| 109 | + func.save(dso_binary_path) |
| 110 | + |
| 111 | + if not android_serial_number: |
| 112 | + pytest.skip("Skip hardware test since ANDROID_SERIAL_NUMBER is not set.") |
| 113 | + |
| 114 | + launcher = HexagonLauncher(serial_number=android_serial_number) |
| 115 | + launcher.android_run_rpc(rpc_tracker_host=tvm_tracker_host, rpc_tracker_port=tvm_tracker_port) |
| 116 | + launcher.hexagon_setup() |
| 117 | + remote_kw = { |
| 118 | + "host": tvm_tracker_host, |
| 119 | + "port": tvm_tracker_port, |
| 120 | + "priority": 0, |
| 121 | + "timeout": 60, |
| 122 | + } |
| 123 | + launcher.hexagon_session_setup(remote_kw) |
| 124 | + launcher.upload(dso_binary_path, dso_binary) |
| 125 | + |
| 126 | + with launcher.session as sess: |
| 127 | + mod = launcher.get_module(dso_binary) |
| 128 | + xt = tvm.nd.array(np.random.uniform(size=size).astype(x.dtype), device=sess.device) |
| 129 | + yt = tvm.nd.array(np.random.uniform(size=size).astype(y.dtype), device=sess.device) |
| 130 | + zt = tvm.nd.array(np.random.uniform(size=size).astype(z.dtype), device=sess.device) |
| 131 | + mod["dmacpy"](xt, yt, zt) |
| 132 | + launcher.close() |
| 133 | + |
| 134 | + ref = xt.numpy() + yt.numpy() |
| 135 | + np.testing.assert_equal(zt.numpy(), ref) |
0 commit comments