Exemple #1
0
def test_elif():
    def kernel(A):
        with hcl.if_(A[0] > 5):
            A[0] = 5
        with hcl.elif_(A[0] > 3):
            A[0] = 3

    A = hcl.placeholder((1, ))
    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)

    np_A = np.random.randint(10, size=(1, ))
    golden_A = [5 if np_A[0] > 5 else (3 if np_A[0] > 3 else np_A[0])]

    hcl_A = hcl.asarray(np_A)

    f(hcl_A)

    ret_A = hcl_A.asnumpy()
Exemple #2
0
def test_reuse_before_streaming():
    hcl.init()
    A = hcl.placeholder((10, 10), name="A")

    def kernel(A):
        B = hcl.compute((10, 8),
                        lambda y, x: A[y, x] + A[y, x + 1] + A[y, x + 2],
                        name="B")
        C = hcl.compute((10, 8), lambda y, x: B[y, x], name="C")
        return C

    s = hcl.create_schedule([A], kernel)
    kernel_B = kernel.B
    RB = s.reuse_at(A, s[kernel_B], kernel_B.axis[1])
    target = hcl.platform.zc706
    target.config(compile="vivado_hls", mode="csim")
    s.to(kernel.B, target.xcel)
    s.to(kernel.C, target.host)
    f = hcl.build(s, target)
Exemple #3
0
def top(target=None):
    points = hcl.placeholder((N, dim))
    means = hcl.placeholder((K, dim))

    def kmeans(points, means):
        def loop_kernel(labels):
            # assign cluster
            with hcl.for_(0, N, name="N") as n:
                min_dist = hcl.local(100000)
                with hcl.for_(0, K) as k:
                    dist = hcl.local(0)
                    with hcl.for_(0, dim) as d:
                        dist_ = points[n, d] - means[k, d]
                        dist[0] += dist_ * dist_
                    with hcl.if_(dist[0] < min_dist[0]):
                        min_dist[0] = dist[0]
                        labels[n] = k
            # update mean
            num_k = hcl.compute((K, ), lambda x: 0)
            sum_k = hcl.compute((K, dim), lambda x, y: 0)

            def calc_sum(n):
                num_k[labels[n]] += 1
                with hcl.for_(0, dim) as d:
                    sum_k[labels[n], d] += points[n, d]

            hcl.mutate((N, ), lambda n: calc_sum(n), "calc_sum")
            hcl.update(means, lambda k, d: sum_k[k, d] // num_k[k],
                       "update_mean")

        labels = hcl.compute((N, ), lambda x: 0)
        hcl.mutate((niter, ), lambda _: loop_kernel(labels), "main_loop")
        return labels

    # create schedule and apply compute customization
    s = hcl.create_schedule([points, means], kmeans)
    main_loop = kmeans.main_loop
    update_mean = main_loop.update_mean
    s[main_loop].pipeline(main_loop.N)
    s[main_loop.calc_sum].unroll(main_loop.calc_sum.axis[0])
    fused = s[update_mean].fuse(update_mean.axis[0], update_mean.axis[1])
    s[update_mean].unroll(fused)
    return hcl.build(s, target=target)
def test_mixed_stream():
    A = hcl.placeholder((10, 32), "A")
    B = hcl.placeholder((10, 32), "B")

    def kernel(A, B):
        C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], "C")
        D = hcl.compute(C.shape, lambda i, j: C[i][j], "D")
        return D

    target = hcl.Platform.aws_f1
    s = hcl.create_schedule([A, B], kernel)

    s.to([A, B], target.xcel)
    s.to(kernel.D, target.host)
    s.to(kernel.C, s[kernel.D])

    code = str(hcl.lower(s))
    assert "test(A, B, D)" in code
    assert "allocate C.pipe.1[int32 * 10 * 32]" in code
Exemple #5
0
def test_schedule_return():
    A = hcl.placeholder((10, ))

    def algorithm(A):
        return hcl.compute(A.shape, lambda x: A[x] + 1)

    s = hcl.create_schedule([A], algorithm)
    f = hcl.build(s)

    _A = hcl.asarray(np.random.randint(100, size=(10, )), dtype=hcl.Int(32))
    _B = hcl.asarray(np.zeros(10), dtype=hcl.Int(32))

    f(_A, _B)

    _A = _A.asnumpy()
    _B = _B.asnumpy()

    for i in range(10):
        assert (_B[i] == _A[i] + 1)
    def test_simple():
        hcl.init()
        A = hcl.placeholder((10, 10), name="A")

        def kernel(A):
            B = hcl.compute((10, 8),
                            lambda y, x: A[y, x] + A[y, x + 1] + A[y, x + 2],
                            name="B")
            return B

        s = hcl.create_schedule([A], kernel)
        target = hcl.platform.zc706
        target.config(compile="vivado_hls", mode="debug")

        s.to(A, target.xcel, local_buffer=False)
        s.to(kernel.B, target.host, local_buffer=False)

        code = str((hcl.build(s, target)))
        assert ("test(A, B)" in code) or ("test(B, A)" in code)
    def test_merge_kernel_stages():
        hcl.init()
        A = hcl.placeholder((10, 32), "A")
        B = hcl.placeholder((10, 32), "B")

        def kernel(A, B):
            C = hcl.compute(A.shape, lambda i, j: 0, "C")
            hcl.update(C, lambda i, j: A[i, j] + 1, "s1")
            hcl.update(C, lambda i, j: B[i, j] * 2, "s2")
            return hcl.compute(C.shape, lambda *args: C[args] + 3, "ret")

        target = hcl.Platform.aws_f1
        s = hcl.create_schedule([A, B], kernel)

        s.to([A, B], target.xcel)
        s.to(kernel.ret, target.host)
        tops = s.subgraph()[0]
        dev_body = str(tops.op.body)
        assert "device_scope = \"fpga\"" in dev_body
Exemple #8
0
    def test_comm_intf():
        hcl.init()
        A = hcl.placeholder((10, 32), "A")
        B = hcl.placeholder((10, 32), "B")

        def kernel(A, B):
            C = hcl.compute(A.shape, lambda i, j: A[i, j] + B[i, j], "C")
            D = hcl.compute(C.shape, lambda i, j: C[i, j] + 1, "D")
            return D

        target = hcl.platform.aws_f1
        target.config(compile="vitis", mode="debug")
        s = hcl.create_schedule([A, B], kernel)
        s.to(A, target.xcel, stream_type=hcl.Stream.FIFO)
        s.to(B, target.xcel, stream_type=hcl.Stream.Copy)
        s.to(kernel.D, target.host, stream_type=hcl.Stream.FIFO)
        code = hcl.build(s, target)
        assert "hls::stream<pkt_b32> &A" in code
        assert "hls::stream<pkt_b32> &D" in code
Exemple #9
0
    def test_move_inputs():
        hcl.init()
        A = hcl.placeholder((10, 32), "A")
        B = hcl.placeholder((10, 32), "B")
        C = hcl.placeholder((10, 32), "C")
        D = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], "D")
        E = hcl.compute(C.shape, lambda i, j: C[i][j] * D[i][j], "E")
        F = hcl.compute(C.shape, lambda i, j: E[i][j] + 1, "F")

        target = hcl.platform.aws_f1
        s = hcl.create_schedule([A, B, C, D, E, F])
        s.to([A, B, C], target.xcel)
        s.to(E, target.host)
        code = str(hcl.lower(s))
        pattern = "test({}.channel, {}.channel, {}.channel, E.channel)"
        combination = [
            pattern.format(*_) for _ in list(permutations(["A", "B", "C"]))
        ]
        assert any([_ in code for _ in combination])
Exemple #10
0
def test_toy_nn():
    dtype = hcl.Float(32)
    hcl.init(dtype)

    input_1 = hcl.placeholder((16, ), dtype=dtype, name="input_1")
    output_1 = hcl.placeholder((5, ), dtype=dtype, name="output_1")

    def math_func(input_1, output_1):
        toynn_vhls_ip(input_1, output_1)

    target = hcl.Platform.aws_f1
    s = hcl.create_schedule([input_1, output_1], math_func)
    s.to(input_1, target.xcel)
    s.to(output_1, target.host)

    target.config(compiler="vitis", mode="debug")
    code = hcl.build(s, target)
    assert "nnet::softmax" in code, code
    os.system("rm -rf firmware toynn.tar.gz")
Exemple #11
0
def test_extern_op_multicast():
    A = hcl.placeholder((10, 32), "A")
    B = hcl.placeholder((10, 32), "B")

    def kernel(A, B):
        C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], "C")
        D = hcl.compute(C.shape, lambda i, j: C[i][j] + 1, "D")
        E = hcl.compute(C.shape, lambda i, j: C[i][j] * 2, "E")
        return D, E

    target = hcl.platform.aws_f1
    s = hcl.create_schedule([A, B], kernel)
    s.to(kernel.C, s[kernel.D])
    s.to(kernel.C, s[kernel.E])
    code = str(hcl.lower(s))
    assert "C.pipe1.write" in code
    assert "C.pipe1.read" in code
    assert "C.pipe2.write" in code
    assert "C.pipe2.read" in code
def test_float():
    hcl.init(hcl.Float())

    a = hcl.placeholder((10, ))
    b = hcl.compute(a.shape, lambda x: hcl.power(2.0, a[x]))

    s = hcl.create_schedule([a, b])
    f = hcl.build(s)

    np_a = np.random.rand(10)
    np_b = np.zeros(10)

    hcl_a = hcl.asarray(np_a)
    hcl_b = hcl.asarray(np_b)

    f(hcl_a, hcl_b)

    np_golden = np.power(2, np_a)
    assert np.allclose(np_golden, hcl_b.asnumpy())
def test_sobel_vivado_hls():
    width, height = 224, 224
    A = hcl.placeholder((height, width, 3), "A")
    Gx = hcl.placeholder((3, 3), "Gx")
    Gy = hcl.placeholder((3, 3), "Gy")

    def sobel(A, Gx, Gy):
        B = hcl.compute((height, width),
                        lambda x, y: A[x][y][0] + A[x][y][1] + A[x][y][2], "B")
        r = hcl.reduce_axis(0, 3)
        c = hcl.reduce_axis(0, 3)
        D = hcl.compute((height - 2, width - 2), lambda x, y: hcl.sum(
            B[x + r, y + c] * Gx[r, c], axis=[r, c], name="sum1"), "xx")

        t = hcl.reduce_axis(0, 3)
        g = hcl.reduce_axis(0, 3)
        E = hcl.compute(
            (height - 2, width - 2),
            lambda x, y: hcl.sum(B[x + t, y + g] * Gy[t, g], axis=[t, g]),
            "yy")
        return hcl.compute((height - 2, width - 2), lambda x, y: hcl.sqrt(D[x][
            y] * D[x][y] + E[x][y] * E[x][y]) * 0.05891867, "Fimg")

    s = hcl.create_schedule([A, Gx, Gy], sobel)
    LBX = s.reuse_at(sobel.B._op, s[sobel.xx], sobel.xx.axis[0], "LBX")
    LBY = s.reuse_at(sobel.B._op, s[sobel.yy], sobel.yy.axis[0], "LBY")
    WBX = s.reuse_at(LBX, s[sobel.xx], sobel.xx.axis[1], "WBX")
    WBY = s.reuse_at(LBY, s[sobel.yy], sobel.yy.axis[1], "WBY")
    s.partition(LBX)
    s.partition(LBY)
    s.partition(WBX)
    s.partition(WBY)
    s.partition(Gx)
    s.partition(Gy)
    s[sobel.xx].pipeline(sobel.xx.axis[1])
    s[sobel.yy].pipeline(sobel.yy.axis[1])

    target = hcl.Platform.xilinx_zc706
    s.to([A, Gx, Gy], target.xcel)
    s.to(sobel.Fimg, target.host)

    target.config(compiler="vivado_hls", mode="debug")
    print(hcl.build(s, target))
def test_stages_one_to_many():
    A = hcl.placeholder((10, 32), "A")
    B = hcl.placeholder((10, 32), "B")

    def kernel(A, B):
        C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], "C")
        D = hcl.compute(C.shape, lambda i, j: C[i][j] + 1, "D")
        E = hcl.compute(C.shape, lambda i, j: C[i][j] * 2, "E")
        return D, E

    target = hcl.Platform.aws_f1
    s = hcl.create_schedule([A, B], kernel)
    s.to(kernel.C, s[kernel.D])
    s.to(kernel.C, s[kernel.E])

    code = str(hcl.lower(s))
    print(code)
    assert "allocate C.pipe.1[int32 * 10 * 32]" in code
    assert "allocate C.pipe.2[int32 * 10 * 32]" in code
def test_multi_stage():
    hcl.init()

    def test(A):
        r = hcl.reduce_axis(0, 10)
        B = hcl.compute((10, ), lambda x: hcl.sum(A[x, r], axis=r), "B")
        return B

    A = hcl.placeholder((10, 10))
    s = hcl.create_schedule([A], test)
    s[test.B].split(test.B.axis[0], 5)
    f = hcl.build(s)
    a_np = np.random.randint(0, 10, size=(10, 10))
    b_np = np.zeros(shape=(10, ), dtype="int")
    a_hcl = hcl.asarray(a_np)
    b_hcl = hcl.asarray(b_np)
    f(a_hcl, b_hcl)
    d_np = np.sum(a_np, axis=1)
    np.testing.assert_array_equal(d_np, b_hcl.asnumpy())
def test_compute_at_no_dep_diff_shape_larger():
    hcl.init()
    A = hcl.compute((12, 12), lambda y, x: y + x, "A")
    B = hcl.compute((10, 10), lambda y, x: y - x, "B")
    s = hcl.create_schedule([A, B])
    # the outer one will be truncated
    s[A].compute_at(s[B], B.axis[1])
    f = hcl.build(s)
    a_hcl = hcl.asarray(np.zeros(A.shape, dtype="int"))
    b_hcl = hcl.asarray(np.zeros(B.shape, dtype="int"))
    f(a_hcl, b_hcl)
    a_np = np.fromfunction(lambda i, j: i + j, A.shape, dtype="int")
    b_np = np.fromfunction(lambda i, j: i - j, B.shape, dtype="int")
    for i in range(0, 12):
        for j in range(0, 12):
            if (i >= 10 or j >= 10):
                a_np[i][j] = 0
    np.testing.assert_array_equal(a_np, a_hcl.asnumpy())
    np.testing.assert_array_equal(b_np, b_hcl.asnumpy())
def test_compute_at_with_reuse_2D():
    hcl.init()
    A = hcl.compute((10, 10), lambda y, x: x + y, "A")
    B = hcl.compute(
        (8, 8), lambda y, x: A[y, x] + A[y + 1, x + 1] + A[y + 2, x + 2], "B")
    s = hcl.create_schedule([B])
    s[A].compute_at(s[B], B.axis[1])
    ir = hcl.lower(s)
    assert "allocate A[int32 * 3 * 3]" in str(ir)
    f = hcl.build(s)
    a_np = np.fromfunction(lambda i, j: i + j, A.shape, dtype="int")
    b_np = np.zeros(B.shape, dtype="int")
    c_np = np.zeros(B.shape, dtype="int")
    for y in range(0, 8):
        for x in range(0, 8):
            c_np[y][x] = a_np[y][x] + a_np[y + 1][x + 1] + a_np[y + 2][x + 2]
    b_hcl = hcl.asarray(b_np)
    f(b_hcl)
    np.testing.assert_array_equal(c_np, b_hcl.asnumpy())
def test_binary_conv():
    hcl.init()
    A = hcl.placeholder((1, 32, 14, 14), dtype=hcl.UInt(1), name="A")
    B = hcl.placeholder((64, 32, 3, 3), dtype=hcl.UInt(1), name="B")
    rc = hcl.reduce_axis(0, 32)
    ry = hcl.reduce_axis(0, 3)
    rx = hcl.reduce_axis(0, 3)
    C = hcl.compute((1, 64, 12, 12),
                    lambda nn, ff, yy, xx: hcl.sum(A[nn, rc, yy + ry, xx + rx]
                                                   * B[ff, rc, ry, rx],
                                                   axis=[rc, ry, rx]),
                    dtype=hcl.UInt(8),
                    name="C")
    s = hcl.create_schedule([A, B, C])
    s[C].split(C.axis[1], factor=5)
    code = hcl.build(s, target='aocl')
    assert "for (int ff_outer = 0; ff_outer < 13; ++ff_outer)" in code
    assert "for (int ff_inner = 0; ff_inner < 5; ++ff_inner)" in code
    assert "if (ff_inner < (64 - (ff_outer * 5)))" in code
Exemple #19
0
def systolic(m=16, k=16, n=16, dtype=hcl.Int(), target=None):
    hcl.init(dtype)

    dim_x, dim_y = 16, 16
    A = hcl.placeholder((m, k), dtype=dtype, name="A")
    B = hcl.placeholder((k, n), dtype=dtype, name="B")

    def kernel(A, B):

        localA = hcl.compute((m, k - 1), lambda *args: 0, "localA")
        localB = hcl.compute((k - 1, n), lambda *args: 0, "localB")
        output = hcl.compute((m, n), lambda *args: 0, "output")

        def update(k, y, x):

            localA[y, x] = hcl.select(x > 0, localA[y, x - 1], A[y, k])
            localB[y, x] = hcl.select(y > 0, localB[y - 1, x], B[k, x])
            output[y, x] = hcl.select(
                k == 0, 0, output[y, x]) + localA[y, x] * localB[y, x]

        hcl.mutate((m, dim_y, dim_x),
                   lambda k, y, x: update(k, y, x),
                   name="update")
        return output

    s = hcl.create_schedule([A, B], kernel)

    k = kernel.update
    s[k].pipeline(k.axis[0])

    # self loopback streaming
    s.to(k.localA, kernel.update)
    s.to(k.localB, kernel.update)

    # move to xcel scope
    if not host_only:
        s.to([A, B], target.xcel)
        s.to(k.output, target.host)

    print(hcl.lower(s))
    f = hcl.build(s, target=target)
    return f
Exemple #20
0
def test_dtype_struct():
    hcl.init()
    A = hcl.placeholder((100, ), dtype=hcl.Int(8))
    B = hcl.placeholder((100, ), dtype=hcl.Fixed(13, 11))
    C = hcl.placeholder((100, ), dtype=hcl.Float())

    def kernel(A, B, C):
        stype = hcl.Struct({
            "fa": hcl.Int(8),
            "fb": hcl.Fixed(13, 11),
            "fc": hcl.Float()
        })
        D = hcl.compute(A.shape, lambda x: (A[x], B[x], C[x]), dtype=stype)
        E = hcl.compute(A.shape, lambda x: D[x].fa, dtype=hcl.Int(8))
        F = hcl.compute(A.shape, lambda x: D[x].fb, dtype=hcl.Fixed(13, 11))
        G = hcl.compute(A.shape, lambda x: D[x].fc, dtype=hcl.Float())
        # Check the data type
        assert D[0].fa.dtype == "int8"
        assert D[0].fb.dtype == "fixed13_11"
        assert D[0].fc.dtype == "float32"
        return E, F, G

    s = hcl.create_schedule([A, B, C], kernel)
    print(hcl.lower(s))
    f = hcl.build(s)
    np_A = np.random.randint(0, 500, size=100) - 250
    np_B = np.random.rand(100) - 0.5
    np_C = np.random.rand(100) - 0.5
    np_E = np.zeros(100)
    np_F = np.zeros(100)
    np_G = np.zeros(100)
    hcl_A = hcl.asarray(np_A, dtype=hcl.Int(8))
    hcl_B = hcl.asarray(np_B, dtype=hcl.Fixed(13, 11))
    hcl_C = hcl.asarray(np_C, dtype=hcl.Float())
    hcl_E = hcl.asarray(np_E, dtype=hcl.Int(8))
    hcl_F = hcl.asarray(np_F, dtype=hcl.Fixed(13, 11))
    hcl_G = hcl.asarray(np_G, dtype=hcl.Float())
    f(hcl_A, hcl_B, hcl_C, hcl_E, hcl_F, hcl_G)

    assert np.allclose(hcl_A.asnumpy(), hcl_E.asnumpy())
    assert np.allclose(hcl_B.asnumpy(), hcl_F.asnumpy())
    assert np.allclose(hcl_C.asnumpy(), hcl_G.asnumpy())
Exemple #21
0
def test_for_step_negative():

    def kernel(A):
        with hcl.Stage():
            with hcl.for_(9, -1, -1) as i:
                A[i] = i

    A = hcl.placeholder((10,))
    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)

    np_A = np.random.randint(10, size=(10,))
    golden_A = [i for i in range(0, 10)]

    hcl_A = hcl.asarray(np_A)

    f(hcl_A)

    ret_A = hcl_A.asnumpy()
    assert np.array_equal(golden_A, ret_A)
Exemple #22
0
def test_for_irregular_bound():
    def kernel(A):
        with hcl.for_(4, 8) as i:
            A[i] = i

    A = hcl.placeholder((10, ))
    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)

    np_A = np.random.randint(10, size=(10, ))
    golden_A = np.copy(np_A)
    for i in range(4, 8):
        golden_A[i] = i

    hcl_A = hcl.asarray(np_A)

    f(hcl_A)

    ret_A = hcl_A.asnumpy()
    assert np.array_equal(golden_A, ret_A)
Exemple #23
0
def test_dtype_long_int():
    # the longest we can support right now is 255-bit
    hcl.init(hcl.UInt(32))
    A = hcl.placeholder((100, ))

    def kernel(A):
        B = hcl.compute(A.shape,
                        lambda x: hcl.cast(hcl.UInt(255), A[x]) << 200,
                        dtype=hcl.UInt(255))
        C = hcl.compute(A.shape, lambda x: B[x] >> 200)
        return C

    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)
    np_A = np.random.randint(0, 1 << 31, 100)
    hcl_A = hcl.asarray(np_A)
    hcl_C = hcl.asarray(np.zeros(A.shape))
    f(hcl_A, hcl_C)

    assert np.array_equal(np_A, hcl_C.asnumpy())
Exemple #24
0
def bias_add_test(d_shape, b_shape, axis=1):
    hcl.init()
    data = hcl.placeholder(d_shape)
    bias = hcl.placeholder(b_shape)

    def func(data, bias, axis=axis):
        return hlib.op.nn.bias_add(data, bias, axis=axis)
    s = hcl.create_schedule([data, bias], func)
    f = hcl.build(s)
    _in = np.random.randint(10, size=d_shape)
    b = np.random.randint(10, size=b_shape)
    out = hcl.asarray(np.zeros(d_shape))
    f(hcl.asarray(_in), hcl.asarray(b), out)

    def add(a):
        return np.add(a, b)
    if axis < 0:
        axis += len(d_shape)
    t_out = np.apply_along_axis(add, axis, _in)
    tst.assert_almost_equal(t_out, out.asnumpy())
Exemple #25
0
def test_assert_exception():
    hcl.init()

    A = hcl.placeholder((10, ))

    def kernel(A):
        hcl.assert_(5 == 6)
        return hcl.compute(A.shape, lambda x: A[x])

    s = hcl.create_schedule([A], kernel)
    f = hcl.build(s)

    hclA = hcl.asarray(np.zeros(A.shape))
    hclO = hcl.asarray(np.zeros(A.shape))

    try:
        f(hclA, hclO)
    except hcl.debug.AssertError:
        return
    assert False
Exemple #26
0
def test_cast_removal():
    hcl.init()

    A = hcl.placeholder((10, 10), dtype=hcl.UInt(16), name="A")
    B = hcl.placeholder((10, 10), dtype=hcl.Int(16), name="B")

    def algo(A, B):
        def f_mutate(i, j):
            factor = hcl.scalar(B[0][0][13:11], name="factor")
            idx = hcl.scalar(B[0][0][11:0], dtype=hcl.UInt(16), name="idx")
            idx += i * hcl.cast(hcl.UInt(16), factor.v)
            A[idx][j] = B[idx][j]

        bound = hcl.scalar(5, dtype=hcl.Int(32))
        domain = (hcl.cast(hcl.UInt(32),
                           bound.v), hcl.cast(hcl.UInt(32), bound.v))
        hcl.mutate(domain, f_mutate)

    s = hcl.create_schedule([A, B], algo)
    f = hcl.build(s, target="vhls")
Exemple #27
0
def test_while_basic():
    def kernel(A):
        a = hcl.scalar(0)
        with hcl.while_(a[0] < 10):
            A[a[0]] = a[0]
            a[0] += 1

    A = hcl.placeholder((10, ))
    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)

    np_A = np.random.randint(10, size=(10, ))
    golden_A = [i for i in range(0, 10)]

    hcl_A = hcl.asarray(np_A)

    f(hcl_A)

    ret_A = hcl_A.asnumpy()
    assert np.array_equal(golden_A, ret_A)
Exemple #28
0
def test_mem_if():
    hcl.init(raise_assert_exception=False)
    matrix_1 = hcl.placeholder((m, k))
    matrix_2 = hcl.placeholder((k, n))
    def kernel(matrix_1, matrix_2):
        return_matrix = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y], "return_matrix")

        with hcl.if_(matrix_2[0, 0] == 0):
            matrix_A = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y], "matrix_A")
        with hcl.else_():
            matrix_B = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 2, "matrix_B")

        hcl.assert_(matrix_1[0, 0] != 0, "customized assert message 1") #result is false

        matrix_C = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y], "matrix_C")
        hcl.print(0, "this shouldn't be printed")
        return return_matrix

    s = hcl.create_schedule([matrix_1, matrix_2], kernel)
    return s
Exemple #29
0
def test_if():

    def kernel(A):
        with hcl.Stage():
            with hcl.if_(A[0] > 5):
                A[0] = 5

    A = hcl.placeholder((1,))
    s = hcl.create_schedule(A, kernel)
    f = hcl.build(s)

    np_A = np.random.randint(10, size=(1,))
    golden_A = [5 if np_A[0]>5 else np_A[0]]

    hcl_A = hcl.asarray(np_A)

    f(hcl_A)

    ret_A = hcl_A.asnumpy()
    assert np.array_equal(golden_A, ret_A)
    def inter_stage_join():
        hcl.init()
        A = hcl.placeholder((10, 32), "A")
        B = hcl.placeholder((10, 32), "B")

        def kernel(A, B):
            C = hcl.compute(A.shape, lambda i, j: 0, "C")
            hcl.update(C, lambda i, j: A[i, j] + 1, "s1")
            hcl.update(C, lambda i, j: B[i, j] * 2, "s2")
            return hcl.compute(C.shape, lambda *args: C[args] + 3, "ret")

        target = hcl.Platform.aws_f1
        s = hcl.create_schedule([A, B], kernel)

        s.to(kernel.s1.C, kernel.ret.C)
        s.to(kernel.s2.C, kernel.ret.C)

        code = str(hcl.lower(s))
        assert "allocate C.pipe.2[int32 * 10 * 32]" in code
        assert "allocate C.pipe.1[int32 * 10 * 32]" in code