test_ewise_fpga.py 3.71 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
import tvm
18
from tvm import te
19 20 21 22
import numpy as np
import os

os.environ["XCL_EMULATION_MODE"] = "1"
23
os.environ["CL_CONTEXT_EMULATOR_DEVICE_INTELFPGA"] = "1"
24 25 26 27 28 29 30 31 32

@tvm.register_func
def tvm_callback_vhls_postproc(code):
    """Hook to inspect the Vivado HLS code before actually run it"""
    print(code)
    return code

def test_exp():
    # graph
33 34 35 36
    n = tvm.runtime.convert(1024)
    A = te.placeholder((n,), name='A')
    B = te.compute(A.shape, lambda *i: te.exp(A(*i)), name='B')
    s = te.create_schedule(B.op)
37 38
    # create iter var and assign them tags.
    px, x = s[B].split(B.op.axis[0], nparts=1)
39
    s[B].bind(px, te.thread_axis("pipeline"))
40 41

    # one line to build the function.
42
    def check_device(device, host="llvm"):
43
        if not tvm.runtime.enabled(host):
44 45 46 47 48 49 50 51 52 53 54 55 56
            return
        ctx = tvm.context(device, 0)
        if not ctx.exist:
            return
        fexp = tvm.build(s, [A, B],
                         device, host,
                         name="myexp")
        ctx = tvm.context(device, 0)
        # launch the kernel.
        n = 1024
        a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
        b = tvm.nd.array(np.zeros(n, dtype=B.dtype), ctx)
        fexp(a, b)
57
        tvm.testing.assert_allclose(
58 59 60
            b.asnumpy(), np.exp(a.asnumpy()), rtol=1e-5)

    check_device("sdaccel")
61 62
    if "AWS_PLATFORM" in os.environ:
        check_device("sdaccel -device=" + os.environ.get("AWS_PLATFORM"))
63

64
    check_device("aocl_sw_emu")
65

66 67
def test_multi_kernel():
    # graph
68 69 70 71 72 73
    n = tvm.runtime.convert(1024)
    A = te.placeholder((n,), name='A')
    B = te.placeholder((n,), name='B')
    C = te.compute(A.shape, lambda *i: A(*i) + B(*i), name='C')
    D = te.compute(A.shape, lambda *i: A(*i) + C(*i), name='D')
    s = te.create_schedule(D.op)
74 75
    # create iter var and assign them tags.
    px, x = s[C].split(C.op.axis[0], nparts=1)
76
    s[C].bind(px, te.thread_axis("pipeline"))
77
    px, x = s[D].split(D.op.axis[0], nparts=1)
78
    s[D].bind(px, te.thread_axis("pipeline"))
79 80 81

    # one line to build the function.
    def check_device(device, host="llvm"):
82
        if not tvm.runtime.enabled(host):
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            return
        ctx = tvm.context(device, 0)
        if not ctx.exist:
            return
        fadd = tvm.build(s, [A, B, C, D],
                         device, host,
                         name="myadd")
        ctx = tvm.context(device, 0)
        # launch the kernel.
        n = 1024
        a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
        b = tvm.nd.array(np.random.uniform(size=n).astype(B.dtype), ctx)
        c = tvm.nd.array(np.random.uniform(size=n).astype(C.dtype), ctx)
        d = tvm.nd.array(np.random.uniform(size=n).astype(D.dtype), ctx)
        fadd(a, b, c, d)
98
        tvm.testing.assert_allclose(
99 100 101
            d.asnumpy(), a.asnumpy() * 2 + b.asnumpy(), rtol=1e-5)

    check_device("sdaccel")
102
    check_device("aocl_sw_emu")
103 104


105 106
if __name__ == "__main__":
    test_exp()
107
    test_multi_kernel()