|
| 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 | +"""Test code for matmul""" |
| 18 | +import numpy as np |
| 19 | +import pytest |
| 20 | +import sys |
| 21 | + |
| 22 | +import tvm |
| 23 | +from tvm import topi |
| 24 | +from tvm import te |
| 25 | +import tvm.topi.testing |
| 26 | +from tvm.topi.utils import get_const_tuple |
| 27 | + |
| 28 | +from .conftest import requires_hexagon_toolchain |
| 29 | + |
| 30 | +dtype = tvm.testing.parameter( |
| 31 | + "float32", |
| 32 | + "float16", |
| 33 | +) |
| 34 | + |
| 35 | +x_batch, y_batch, M, N, K = tvm.testing.parameters( |
| 36 | + (1, 1, 16, 16, 32), |
| 37 | + (5, 5, 16, 16, 32), |
| 38 | + (5, 5, 16, 20, 32), |
| 39 | + (30, 30, 16, 20, 32), |
| 40 | + # Test batch broadcasting. |
| 41 | + (1, 5, 16, 16, 32), |
| 42 | + (5, 1, 16, 16, 32), |
| 43 | +) |
| 44 | + |
| 45 | +# TODO(mehrdadh): add dynamic testing |
| 46 | +@requires_hexagon_toolchain |
| 47 | +def test_batch_matmul(hexagon_session, x_batch, y_batch, M, N, K, dtype): |
| 48 | + if dtype == "float16": |
| 49 | + pytest.xfail("float16 is not supported.") |
| 50 | + |
| 51 | + x = te.placeholder((x_batch, M, K), name="x") |
| 52 | + y = te.placeholder((y_batch, N, K), name="y") |
| 53 | + |
| 54 | + def get_ref_data(): |
| 55 | + a_np = np.random.uniform(size=(x_batch, M, K)).astype(dtype) |
| 56 | + b_np = np.random.uniform(size=(y_batch, N, K)).astype(dtype) |
| 57 | + c_np = tvm.topi.testing.batch_matmul(a_np, b_np) |
| 58 | + return (a_np, b_np, c_np) |
| 59 | + |
| 60 | + # get the test data |
| 61 | + a_np, b_np, c_np = get_ref_data() |
| 62 | + |
| 63 | + target_hexagon = tvm.target.hexagon("v68") |
| 64 | + with tvm.target.Target(target_hexagon): |
| 65 | + fcompute = topi.nn.batch_matmul |
| 66 | + fschedule = topi.hexagon.schedule_batch_matmul |
| 67 | + out = fcompute(x, y) |
| 68 | + s = fschedule([out]) |
| 69 | + out_shape = out.shape |
| 70 | + |
| 71 | + func = tvm.build( |
| 72 | + s, [x, y, out], tvm.target.Target(target_hexagon, host=target_hexagon), name="batch_matmul" |
| 73 | + ) |
| 74 | + mod = hexagon_session.load_module(func) |
| 75 | + |
| 76 | + dev = hexagon_session.device |
| 77 | + a = tvm.nd.array(a_np, dev) |
| 78 | + b = tvm.nd.array(b_np, dev) |
| 79 | + c = tvm.nd.array(np.zeros(get_const_tuple(out_shape), dtype=dtype), dev) |
| 80 | + mod["batch_matmul"](a, b, c) |
| 81 | + |
| 82 | + tvm.testing.assert_allclose(c.numpy(), c_np, rtol=1e-5) |
| 83 | + |
| 84 | + |
| 85 | +x_batch_1, y_batch_1, M_1, N_1, K_1 = tvm.testing.parameters( |
| 86 | + (1, 1, 2, 3, 1), |
| 87 | + (1, 1, 16, 24, 32), |
| 88 | + (5, 5, 24, 16, 32), |
| 89 | + (30, 30, 16, 20, 32), |
| 90 | + (1, 5, 16, 16, 32), |
| 91 | + (5, 1, 16, 16, 32), |
| 92 | +) |
| 93 | + |
| 94 | + |
| 95 | +@requires_hexagon_toolchain |
| 96 | +def test_batch_matmul_int8(hexagon_session, x_batch_1, y_batch_1, M_1, N_1, K_1): |
| 97 | + dtype = "int8" |
| 98 | + out_dtype = "int8" |
| 99 | + assert x_batch_1 == y_batch_1 or x_batch_1 == 1 or y_batch_1 == 1 |
| 100 | + x = te.placeholder((x_batch_1, M_1, K_1), name="x", dtype=dtype) |
| 101 | + y = te.placeholder((y_batch_1, N_1, K_1), name="y", dtype=dtype) |
| 102 | + |
| 103 | + def get_ref_data(): |
| 104 | + a_np = np.random.randint(low=-128, high=127, size=(x_batch_1, M_1, K_1)).astype(dtype) |
| 105 | + b_np = np.random.randint(low=-128, high=127, size=(y_batch_1, N_1, K_1)).astype(dtype) |
| 106 | + c_np = tvm.topi.testing.batch_matmul(a_np, b_np, out_dtype=out_dtype) |
| 107 | + return (a_np, b_np, c_np) |
| 108 | + |
| 109 | + # get the test data |
| 110 | + a_np, b_np, c_np = get_ref_data() |
| 111 | + |
| 112 | + target_hexagon = tvm.target.hexagon("v68") |
| 113 | + with tvm.target.Target(target_hexagon): |
| 114 | + fcompute = topi.nn.batch_matmul |
| 115 | + fschedule = topi.hexagon.schedule_batch_matmul |
| 116 | + out = fcompute(x, y) |
| 117 | + s = fschedule([out]) |
| 118 | + |
| 119 | + func = tvm.build( |
| 120 | + s, |
| 121 | + [x, y, out], |
| 122 | + tvm.target.Target(target_hexagon, host=target_hexagon), |
| 123 | + name="batch_matmul_int8", |
| 124 | + ) |
| 125 | + mod = hexagon_session.load_module(func) |
| 126 | + |
| 127 | + dev = hexagon_session.device |
| 128 | + a = tvm.nd.array(a_np, dev) |
| 129 | + b = tvm.nd.array(b_np, dev) |
| 130 | + c = tvm.nd.array(np.zeros(get_const_tuple(out.shape), dtype=out_dtype), dev) |
| 131 | + mod["batch_matmul_int8"](a, b, c) |
| 132 | + tvm.testing.assert_allclose(c.numpy(), c_np, rtol=1e-5) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + sys.exit(pytest.main(sys.argv)) |
0 commit comments