def test_set_slice(): A = hcl.placeholder((10,), "A") def kernel(A): with hcl.Stage("S"): A[0][5:1] = 1 s = hcl.create_schedule([A], kernel) code = hcl.build(s, target="ihls") assert "A[0].set_slc(1, ((ac_int<4, false>)1))" in code
def test_split_fuse(): hcl.init() A = hcl.placeholder((10, 100), "A") def two_stage(A): B = hcl.compute(A.shape, lambda x, y : A[x, y] + 1, "B") C = hcl.compute(A.shape, lambda x, y : B[x, y] + 1, 'C') return C s = hcl.create_schedule([A], two_stage) s_B = two_stage.B x_out, x_in = s[s_B].split(s_B.axis[0], 5) code = hcl.build(s, target='aocl') s2 = hcl.create_schedule([A], two_stage) s2_B = two_stage.B x_y = s[s_B].fuse(s2_B.axis[0], s2_B.axis[1]) code2 = hcl.build(s2, target='aocl')
def _verify_build(sch): f = hcl.build(sch) a_np = np.random.randint(low=0, high=100, size=(10, 20, 30)) a_hcl = hcl.asarray(a_np) c_hcl = hcl.asarray(np.zeros(a_np.shape), dtype="int32") f(a_hcl, c_hcl) c_np = a_np * 2 + 1 np.testing.assert_allclose(c_np, c_hcl.asnumpy())
def build_resnet20_inf(params, target=target): if isinstance(target, hcl.platform): s.to([input_image] + hcl_ph, target.xcel) #s.to(build_resnet20.fc, target.host) s.to(build_resnet20.bn1, target.host) return hcl.build(s, target=target)
def test_pragma(): hcl.init() A = hcl.placeholder((10, 32), "A") B = hcl.placeholder((10, 32)) C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j]) # unroll s1 = hcl.create_schedule([A, B, C]) s1[C].unroll(C.axis[1], factor=4) code1 = hcl.build(s1, target='aocl') assert "#pragma unroll 4" in code1 # pipeline s2 = hcl.create_schedule([A, B, C]) s2[C].pipeline(C.axis[0], initiation_interval=2) code2 = hcl.build(s2, target='aocl') assert "#pragma ii 2" in code2
def test_index_split(): hcl.init() A = hcl.placeholder((10, 10), "A") B = hcl.compute(A.shape, lambda y, x: A[y][x], "B") s = hcl.create_schedule([A, B]) s[B].split(B.axis[0], 5) code = hcl.build(s, target="vhls") assert "B[(y_inner + (y_outer * 5))][x]" in code
def test_index_fuse(): hcl.init() A = hcl.placeholder((10, 10), "A") B = hcl.compute(A.shape, lambda y, x: A[y][x], "B") s = hcl.create_schedule([A, B]) s[B].fuse(B.axis[0], B.axis[1]) code = hcl.build(s, target=target) assert "for (sc_int<32> y_x_fused = 0; y_x_fused < 100; ++y_x_fused)" in code
def _test_dataflow_region_in_func(): s = hcl.create_schedule([A, B], kernel) s.to([A, B], target.xcel) s.to(kernel.Super.C, target.host) top = s.subgraph()[0] s[top].dataflow() code = str(hcl.build(s, target="vhls")) assert "#pragma HLS dataflow" in code, code
def test_index_split(): hcl.init() A = hcl.placeholder((10, 10), "A") B = hcl.compute(A.shape, lambda y, x: A[y][x], "B") s = hcl.create_schedule([A, B]) s[B].split(B.axis[0], 5) code = hcl.build(s, target=target) assert "for (sc_int<32> y_inner = 0; y_inner < 5; ++y_inner)" in code
def test_index_fuse(): hcl.init() A = hcl.placeholder((10, 10), "A") B = hcl.compute(A.shape, lambda y, x: A[y][x], "B") s = hcl.create_schedule([A, B]) s[B].fuse(B.axis[0], B.axis[1]) code = hcl.build(s, target="vhls") assert "B[(y_x_fused / 10)][(y_x_fused % 10)]" in code
def test_set_bit(): A = hcl.placeholder((10,), "A") def kernel(A): with hcl.Stage("S"): A[0][4] = 1 s = hcl.create_schedule([A], kernel) code = hcl.build(s, target="ihls") assert "A[0][4] = 1" in code
def test_vhls_debug(): target = hcl.Platform.xilinx_zc706 s = hcl.create_schedule([A], kernel) s.to(kernel.B, target.xcel) s.to(kernel.C, target.host) target.config(compiler="vivado_hls", mode="debug") code = hcl.build(s, target) print(code) assert "test(int B[10][32], int C[10][32])" in code
def test(): def func(A): hcl.assert_(A[0] > 0, "") A = hcl.placeholder((2,), "A", dtype=hcl.UInt(16)) s = hcl.create_schedule([A], func) code = hcl.build(s, "shls") assert "SC_ASSERT" in code assert "uint32" not in code
def top_func(dtype = hcl.Int()): def random(number): number[0] = 78 number = hcl.placeholder((64,), "number") s = hcl.create_schedule([number], random) return hcl.build(s)
def test_vhls_debug(): target = hcl.platform.zc706 s = hcl.create_schedule([A], kernel) s.to(kernel.B, target.xcel) s.to(kernel.C, target.host) target.config(compile="vivado_hls", mode="debug") code = hcl.build(s, target) print(code) assert "test(hls::stream<ap_int<32> >& B_channel, hls::stream<ap_int<32> >& C_channel)" in code
def test_uint_imm_ops(): A = hcl.placeholder((10, 10), "A", dtype=hcl.UInt(1)) def kernel(A): return hcl.compute((8, 8), lambda y, x: hcl.select(x < 4, A[y][x], 0), "B") s = hcl.create_scheme(A, kernel) s = hcl.create_schedule_from_scheme(s) code = hcl.build(s, target="vhls") assert "(unsigned int)0U)" in code
def test_pack(): def pack(A): return hcl.pack(A, factor=5) A = hcl.placeholder((40, ), "A", dtype=hcl.UInt(3)) s = hcl.create_schedule([A], pack) code = hcl.build(s, target="vhls") slice_range = "(((i * 3) + 2), (i * 3))" assert slice_range in code
def test_sdaccel_debug(): target = hcl.platform.aws_f1 s = hcl.create_schedule([A], kernel) s.to(kernel.B, target.xcel) s.to(kernel.C, target.host) target.config(compile="sdaccel", mode="debug", backend="vhls") code = hcl.build(s, target) print(code) assert "cl::Kernel kernel(program, \"test\", &err)" in code
def test_dtye_strcut_complex(): hcl.init() A = hcl.placeholder((100, )) B = hcl.placeholder((100, )) C = hcl.placeholder((100, )) O = hcl.placeholder((100, 6)) def kernel(A, B, C, O): dtype_xyz = hcl.Struct({ "x": hcl.Int(), "y": hcl.Int(), "z": hcl.Int() }) dtype_out = hcl.Struct({ "v0": hcl.Int(), "v1": hcl.Int(), "v2": hcl.Int(), "v3": hcl.Int(), "v4": hcl.Int(), "v5": hcl.Int() }) D = hcl.compute(A.shape, lambda x: (A[x], B[x], C[x]), dtype=dtype_xyz) E = hcl.compute(A.shape, lambda x: (D[x].x * D[x].x, D[x].y * D[x].y, D[x].z * D[x].z, D[ x].x * D[x].y, D[x].y * D[x].z, D[x].x * D[x].z), dtype=dtype_out) with hcl.Stage(): with hcl.for_(0, 100) as i: for j in range(0, 6): O[i][j] = E[i].__getattr__("v" + str(j)) s = hcl.create_schedule([A, B, C, O], kernel) f = hcl.build(s) np_A = np.random.randint(10, size=100) np_B = np.random.randint(10, size=100) np_C = np.random.randint(10, size=100) np_O = np.zeros((100, 6)) np_G = np.zeros((100, 6)).astype("int") for i in range(0, 100): np_G[i][0] = np_A[i] * np_A[i] np_G[i][1] = np_B[i] * np_B[i] np_G[i][2] = np_C[i] * np_C[i] np_G[i][3] = np_A[i] * np_B[i] np_G[i][4] = np_B[i] * np_C[i] np_G[i][5] = np_A[i] * np_C[i] hcl_A = hcl.asarray(np_A) hcl_B = hcl.asarray(np_B) hcl_C = hcl.asarray(np_C) hcl_O = hcl.asarray(np_O) f(hcl_A, hcl_B, hcl_C, hcl_O) assert np.array_equal(hcl_O.asnumpy(), np_G)
def test_legacy_interface(): hcl.init() A = hcl.placeholder((10, 10), "A") B = hcl.compute(A.shape, lambda y, x: A[y][x], "B") s = hcl.create_schedule([A, B]) s[B].fuse(B.axis[0], B.axis[1]) code = hcl.build(s, target="vhls") assert "A[10][10]" in code assert "B[10][10]" in code
def test_get_slice(): A = hcl.placeholder((10,), "A") def kernel(A): with hcl.Stage("S"): A[0] = A[0][5:1] s = hcl.create_schedule([A], kernel) code = hcl.build(s, target="ihls") assert "A[0].slc<4>(1)" in code
def test_ac_fixed(): hcl.init() A = hcl.placeholder((1, 32), dtype=hcl.Fixed(5, 3)) B = hcl.placeholder((1, 32), dtype=hcl.UFixed(5, 3)) C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], dtype=hcl.Fixed(7, 4)) s = hcl.create_schedule([A, B, C]) code = hcl.build(s, target='ihls') assert "ac_fixed<5, 2, true>" in code assert "ac_fixed<5, 2, false>" in code assert "ac_fixed<7, 3, true>" in code
def test_reuse_compute(): hcl.init() A = hcl.placeholder((10, 10), name="A") B = hcl.compute((10, 10), lambda y, x: A[y, x], "B") C = hcl.compute((10, 8), lambda y, x: B[y, x] + B[y, x + 1] + B[y, x + 2], "C") s = hcl.create_schedule([A, B, C]) RB = s.reuse_at(B, s[C], C.axis[1]) print(hcl.lower(s)) f = hcl.build(s)
def test_reuse_compute_sum(): hcl.init() rx = hcl.reduce_axis(0, 3, name="rx") A = hcl.placeholder((10, 10), name="A") B = hcl.compute((10, 10), lambda y, x: A[y, x], "B") C = hcl.compute((10, 8), lambda y, x: hcl.sum(B[y, x + rx], axis=rx), "C") s = hcl.create_schedule([A, B, C]) RB = s.reuse_at(B, s[C], C.axis[1]) print(hcl.lower(s)) f = hcl.build(s)
def test_ac_int(): hcl.init() A = hcl.placeholder((1, 32), dtype=hcl.Int(3)) B = hcl.placeholder((1, 32), dtype=hcl.UInt(3)) C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], dtype=hcl.Int(8)) s = hcl.create_schedule([A, B, C]) code = hcl.build(s, target='ihls') assert "ac_int<3, true>" in code assert "ac_int<3, false>" in code assert "ac_int<8, true>" in code
def test2(): hcl.init() A = hcl.placeholder((10, 10), "A") def kernel(A): B = hcl.compute(A.shape, lambda x, y: A[x][y] + 1, "B") return B s = hcl.create_schedule(A, kernel) s[kernel.B].dataflow(kernel.B.axis[0]) f = hcl.build(s,"vhls") print(f)
def test_print(target): hcl.init() A = hcl.placeholder((10, 32)) def kernel(A): hcl.print(A[0]) return hcl.compute(A.shape, lambda *args: A[args]) s = hcl.create_schedule([A], kernel) code = hcl.build(s, target=target)
def test_ap_int(): hcl.init(); A = hcl.placeholder((1, 32), dtype=hcl.Int(3)) B = hcl.placeholder((1, 32), dtype=hcl.UInt(3)) C = hcl.compute(A.shape, lambda i, j: A[i][j] + B[i][j], dtype=hcl.Int(8)) s = hcl.create_schedule([A, B, C]) code = hcl.build(s, target='aocl') assert "ap_int<3>" in code assert "ap_uint<3>" in code assert "int8" in code
def test(): def func(A, B): B[0] = reduce(A[0], A[1], A[2]) A = hcl.placeholder((5, ), "A", dtype=hcl.UInt(32)) B = hcl.placeholder((2, ), "B", dtype=hcl.UInt(32)) s = hcl.create_schedule([A, B], func) code = hcl.build(s, "shls") assert "(sc_biguint<131>)" in code
def test_imm_ops(): A = hcl.placeholder((10, 10), "A") def kernel(A): return hcl.compute((8, 8), lambda y, x: hcl.select(x < 4, A[y][x] + A[y+2][x+2], 0), "B") s = hcl.create_scheme(A, kernel) s = hcl.create_schedule_from_scheme(s) code = hcl.build(s, target="vhls") assert "((ap_int<33>)0)" in code assert "((ap_int<33>)(((ap_int<33>)A" in code