|
| 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 | +from functools import partial |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import pytest |
| 21 | + |
| 22 | +import tvm |
| 23 | +import tvm.testing |
| 24 | +from tvm import te |
| 25 | + |
| 26 | + |
| 27 | +@tvm.testing.requires_gpu |
| 28 | +@tvm.testing.parametrize_targets("cuda", "metal", "vulkan -supports_int64=1", "opencl") |
| 29 | +@pytest.mark.parametrize("dtype", ["int32", "uint32", "int64", "uint64"]) |
| 30 | +def test_int_intrin(target, dev, dtype): |
| 31 | + test_funcs = [ |
| 32 | + (tvm.tir.clz, lambda x, dtype: int(dtype[-2:]) - (len(bin(x)) - 2)), |
| 33 | + ] |
| 34 | + |
| 35 | + def run_test(tvm_intrin, np_func, dtype): |
| 36 | + n = 128 |
| 37 | + A = te.placeholder((n,), name="A", dtype=dtype) |
| 38 | + B = te.compute(A.shape, lambda *i: tvm_intrin(A(*i)), name="B") |
| 39 | + func = te.create_prim_func([A, B]) |
| 40 | + sch = tvm.tir.Schedule(func) |
| 41 | + (x,) = sch.get_loops(sch.get_block("B")) |
| 42 | + sch.bind(x, "threadIdx.x") |
| 43 | + f = tvm.build(sch.mod, target=target) |
| 44 | + a = tvm.nd.array(np.random.randint(0, 100000, size=n).astype(A.dtype), dev) |
| 45 | + b = tvm.nd.array(np.zeros(shape=(n,)).astype(B.dtype), dev) |
| 46 | + f(a, b) |
| 47 | + ref = np.vectorize(partial(np_func, dtype=dtype))(a.numpy()) |
| 48 | + tvm.testing.assert_allclose(b.numpy(), ref) |
| 49 | + |
| 50 | + for func in test_funcs: |
| 51 | + run_test(*func, dtype) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + tvm.testing.main() |
0 commit comments