test_scan.py 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.
17 18 19 20
import tvm
import numpy as np

def test_scan():
21 22
    m = tvm.var("m")
    n = tvm.var("n")
23 24 25
    X = tvm.placeholder((m, n), name="X")
    s_state = tvm.placeholder((m, n))
    s_init = tvm.compute((1, n), lambda _, i: X[0, i])
26 27
    s_update = tvm.compute((m, n), lambda t, i: s_state[t-1, i] + X[t, i])
    res = tvm.scan(s_init, s_update, s_state)
28 29

    # schedule
30
    s = tvm.create_schedule(res.op)
31
    num_thread = 256
32 33
    block_x = tvm.thread_axis(None, "blockIdx.x")
    thread_x = tvm.thread_axis((0, num_thread), "threadIdx.x")
34 35 36 37 38 39
    xo, xi = s[s_init].split(s_init.op.axis[1], factor=num_thread)
    s[s_init].bind(xo, block_x)
    s[s_init].bind(xi, thread_x)
    xo, xi = s[s_update].split(s_update.op.axis[1], factor=num_thread)
    s[s_update].bind(xo, block_x)
    s[s_update].bind(xi, thread_x)
40 41

    # one line to build the function.
42
    def check_device(device):
43 44
        ctx = tvm.context(device, 0)
        if not ctx.exist:
45
            print("skip because %s is not enabled.." % device)
46
            return
47
        fscan = tvm.build(s, [X, res],
48
                          device,
49 50 51 52 53 54 55 56
                          name="myscan")
        # launch the kernel.
        n = 1024
        m = 10
        a_np = np.random.uniform(size=(m, n)).astype(res.dtype)
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros((m, n), dtype=res.dtype), ctx)
        fscan(a, b)
57
        tvm.testing.assert_allclose(
58 59
            b.asnumpy(), np.cumsum(a_np, axis=0))

60
    check_device("vulkan")
61
    check_device("cuda")
62
    check_device("metal")
63
    check_device("opencl")
64 65 66 67


if __name__ == "__main__":
    test_scan()