Пример #1
0
def test_const_tensor_int():
    def test_kernel(dtype, size):
        hcl.init(dtype)

        np_A = numpy.random.randint(10, size=size)
        py_A = np_A.tolist()

        def kernel():
            cp1 = hcl.const_tensor(np_A)
            cp2 = hcl.const_tensor(py_A)
            return hcl.compute(np_A.shape, lambda *x: cp1[x] + cp2[x])

        O = hcl.placeholder(np_A.shape)
        s = hcl.create_schedule([], kernel)
        f = hcl.build(s)

        np_O = numpy.zeros(np_A.shape)
        hcl_O = hcl.asarray(np_O, dtype=dtype)

        f(hcl_O)

        assert numpy.array_equal(hcl_O.asnumpy(), np_A * 2)

    for i in range(0, 5):
        bit = numpy.random.randint(6, 60)
        test_kernel(hcl.Int(bit), (8, 8))
        test_kernel(hcl.UInt(bit), (8, 8))
        test_kernel(hcl.Int(bit), (20, 20, 3))
        test_kernel(hcl.UInt(bit), (20, 20, 3))
Пример #2
0
    def test_scalar(self):
        hcl.init()
        host = '''\
int main(int argc, char **argv) {
  unsigned int data[] = {1,2,3};
  unsigned int out[3] = {0};
  unsigned int ref[] = {1,1,2};
  default_function(data, out);
  for (int i = 0; i < 3; ++i)
    if (ref[i] != out[i])
        return 1;
  return 0;
}
'''

        def bitcount(v):
            out = hcl.scalar(0, "out", dtype=hcl.UInt(32))
            with hcl.for_(0, 3) as i:
                out[0] += v[i]
            return out[0]

        A = hcl.placeholder((3, ), "A", dtype=hcl.UInt(32))
        B = hcl.compute(A.shape,
                        lambda x: bitcount(A[x]),
                        "B",
                        dtype=hcl.UInt(32))
        s = hcl.create_schedule([A, B])
        code = hcl.build(s, target='merlinc')
        with open('tmp.cpp', 'w') as f:
            f.write(code)
            f.write('\n')
            f.write(host)
        self.assertEqual(run_gcc_test('tmp.cpp'), 0)
Пример #3
0
def partition_test():
    if os.system("which vivado_hls >> /dev/null") != 0:
        return

    A = hcl.placeholder((10, 10), "A", dtype=hcl.UInt(8))

    def kernel(A):
        B = hcl.compute(A.shape,
                        lambda *args: A[args] + 1,
                        "B",
                        dtype=hcl.UInt(8))
        return B

    target = hcl.platform.zc706
    s = hcl.create_schedule([A], kernel)
    s.to(kernel.B, target.host)
    A_ = s.to(A, target.xcel)
    s.partition(A_, hcl.Partition.Block, dim=1, factor=2)
    target.config(compile="vivado_hls", mode="csim")
    f = hcl.build(s, target)

    np_A = np.random.randint(10, size=(10, 10))
    np_B = np.zeros((10, 10))

    hcl_A = hcl.asarray(np_A, dtype=hcl.UInt(8))
    hcl_B = hcl.asarray(np_B, dtype=hcl.UInt(8))
    f(hcl_A, hcl_B)
    ret_B = hcl_B.asnumpy()
def test_unpack_dtype():
    def unpack(A, B):
        C = hcl.unpack(A, name="C", dtype=B.dtype)
        hcl.update(B, lambda x: C[x])

    for i in range(4, 36, 4):
        A = hcl.placeholder((10, ), "A", dtype=hcl.UInt(i))
        B = hcl.placeholder((40, ), "B", dtype=hcl.UInt(i // 4))

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

        _A = hcl.asarray(np.random.randint(1000, size=(10, )),
                         dtype=hcl.UInt(i))
        _B = hcl.asarray(np.zeros(40), dtype=hcl.UInt(i // 4))

        f(_A, _B)

        __A = _A.asnumpy()
        __B = _B.asnumpy()

        for j in range(0, 10):
            for k in range(0, 4):
                numA = __A[j]
                numB = __B[j * 4 + k]
                golden = (numA >> (i // 4 * k)) % (1 << (i // 4))
                assert numB == golden
def test_pack_multi_dimension():
    def pack(A):
        return hcl.pack(A, axis=1, factor=4)

    for i in range(4, 36, 4):
        A = hcl.placeholder((10, 40), "A", dtype=hcl.UInt(i // 4))

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

        _A = hcl.asarray(np.random.randint(1000, size=(10, 40)),
                         dtype=hcl.UInt(i // 4))
        _B = hcl.asarray(np.zeros((10, 10)), dtype=hcl.UInt(i))

        f(_A, _B)

        __A = _A.asnumpy()
        __B = _B.asnumpy()

        for j in range(0, 10):
            for k in range(0, 10):
                golden = 0
                numB = __B[j, k]
                for l in range(0, 4):
                    numA = __A[j, k * 4 + l]
                    golden += numA << (l * i // 4)
                assert numB == golden
def test_unpack():
    def unpack(A):
        return hcl.unpack(A, factor=4, name="B")

    for i in range(4, 36, 4):
        A = hcl.placeholder((10, ), "A", dtype=hcl.UInt(i))

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

        _A = hcl.asarray(np.random.randint(1000, size=(10, )),
                         dtype=hcl.UInt(i))
        _B = hcl.asarray(np.zeros(40), dtype=hcl.UInt(i // 4))

        f(_A, _B)

        __A = _A.asnumpy()
        __B = _B.asnumpy()

        for j in range(0, 10):
            for k in range(0, 4):
                numA = __A[j]
                numB = __B[j * 4 + k]
                golden = (numA >> (i // 4 * k)) % (1 << (i // 4))
                assert numB == golden
Пример #7
0
    def test_hls(target_mode="csyn"):
        input_image = hcl.placeholder((1, 1, 16, 16), "input_image",dtype=hcl.UInt(1))
        w_conv1 = hcl.placeholder((1, 16, 3, 3), "w_conv1",dtype=hcl.UInt(1))
        bn_t1 = hcl.placeholder((16, 16, 16), "bn_t1",dtype=hcl.Float())
        def kernel(input_image, w_conv1):
            conv1 = bnn.conv2d_nchw(input_image, w_conv1,
                                    padding=[1,1], name="conv1", out_dtype=hcl.Int(8))
            return conv1
            bn1 = bnn.batch_norm_threshold(conv1, bn_t1, name="bn1")
            return bn1
        
        target = hcl.platform.aws_f1
        s = hcl.create_schedule([input_image, w_conv1], kernel)
        s.to([input_image, w_conv1], target.xcel)
        s.to(kernel.conv1, target.host)
        target.config(compile="vivado_hls", mode=target_mode)
        # f = hcl.build(s, target=None)
        f = hcl.build(s, target)

        np_input_image = np.random.randint(0, 2, size=(1,1,16,16))
        np_w_conv1 = np.random.randint(0, 2, size=(1,16,3,3))
        np_bn_t1 = np.random.randint(0, 10, size=(16,16,16))
        np_out = np.zeros((1,16,16,16))

        hcl_input_image = hcl.asarray(np_input_image,dtype=hcl.UInt(1))
        hcl_w_conv1 = hcl.asarray(np_w_conv1,dtype=hcl.UInt(1))
        hcl_bn_t1 = hcl.asarray(np_bn_t1,dtype=hcl.Float())
        hcl_out = hcl.asarray(np_out,dtype=hcl.Int(8))
        f(hcl_input_image, hcl_w_conv1, hcl_out)
Пример #8
0
def conv6():
    dtype = hcl.UInt(32)
    A = hcl.placeholder((1, 1, 16, 16), name="layer3_0_rsign1", dtype=dtype)
    F = hcl.placeholder((64, 1, 3, 3), name="w_layer3_0_conv1", dtype=dtype)

    def kernel(A, F):
        conv = bnn.packed_conv2d_nchw(A,
                                      F,
                                      padding=[1, 1],
                                      strides=[1, 1],
                                      name="layer3_0_conv1",
                                      out_dtype=hcl.UInt(16),
                                      mac=False)
        print(conv.shape, conv.dtype)
        return conv

    s = hcl.create_schedule([A, F], kernel)
    s_conv = kernel.layer3_0_conv1
    LB = s.reuse_at(kernel.layer3_0_conv1_pad._op, s[s_conv], s_conv.axis[2],
                    "layer3_0_conv1_LB")
    WB = s.reuse_at(LB, s[s_conv], s_conv.axis[3], "layer3_0_conv1_WB")
    s[kernel.layer3_0_conv1].pipeline(kernel.layer3_0_conv1.axis[2])

    target = hcl.platform.zc706
    target.config(compile="vivado_hls", mode="csim", project="conv6")
    s.to([A, F], target.xcel)
    s.to(kernel.layer3_0_conv1, target.host)
    f = hcl.build(s, target=target)
    hcl_A = hcl.asarray(np.random.randint(0, 10, A.shape), dtype)
    hcl_F = hcl.asarray(np.random.randint(0, 10, F.shape), dtype)
    hcl_B = hcl.asarray(np.zeros((1, 64, 16, 16)), hcl.UInt(16))
    f(hcl_A, hcl_F, hcl_B)
Пример #9
0
def conv4():
    dtype = hcl.UInt(4)
    A = hcl.placeholder((1, 1, 8, 8), name="A", dtype=dtype)
    F = hcl.placeholder((16, 1, 3, 3), name="F", dtype=dtype)

    def kernel(A, F):
        return bnn.conv2d_nchw(A,
                               F,
                               padding=[1, 1],
                               name="conv1",
                               out_dtype=hcl.UInt(16))

    s = hcl.create_schedule([A, F], kernel)
    s_conv = kernel.conv1
    LB = s.reuse_at(kernel.conv1_pad._op, s[s_conv], s_conv.axis[2], "LB")
    WB = s.reuse_at(LB, s[s_conv], s_conv.axis[3], "WB")
    s[kernel.B].pipeline(kernel.B.axis[2])

    target = hcl.platform.zc706
    target.config(compile="vivado_hls", mode="csim|csyn|cosim", project="conv")
    s.to([A, F], target.xcel)
    s.to(kernel.conv1, target.host)
    f = hcl.build(s, target=target)
    hcl_A = hcl.asarray(np.random.randint(0, 10, A.shape), dtype)
    hcl_F = hcl.asarray(np.random.randint(0, 10, F.shape), dtype)
    hcl_B = hcl.asarray(np.zeros((1, 16, 8, 8)), hcl.UInt(16))
    f(hcl_A, hcl_F, hcl_B)
Пример #10
0
def test_module_quantize_args():

    hcl.init()

    def algorithm(A, B):
        @hcl.def_([A.shape, B.shape, ()])
        def add(A, B, x):
            hcl.return_(A[x] + B[x])

        return hcl.compute(A.shape, lambda x: add(A, B, x), "C")

    A = hcl.placeholder((10, ), dtype=hcl.UInt(2))
    B = hcl.placeholder((10, ))

    s = hcl.create_scheme([A, B], algorithm)
    s.downsize([algorithm.add.A], hcl.UInt(2))
    s = hcl.create_schedule_from_scheme(s)
    f = hcl.build(s)

    a = np.random.randint(100, size=(10, ))
    b = np.random.randint(100, size=(10, ))
    c = np.zeros(10)
    _A = hcl.asarray(a, hcl.UInt(2))
    _B = hcl.asarray(b)
    _C = hcl.asarray(c)

    f(_A, _B, _C)

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

    for i in range(0, 10):
        assert (_C[i] == a[i] % 4 + b[i])
Пример #11
0
    def test_array_partition():
        if os.system("which vivado_hls >> /dev/null") != 0:
            return

        hcl.init()
        A = hcl.placeholder((10, 10), "A", dtype=hcl.UInt(8))

        def kernel(A):
            B = hcl.compute(A.shape,
                            lambda *args: A[args] + 1,
                            name="B",
                            dtype=hcl.UInt(8))
            return B

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

        s.to(A, target.xcel)
        s.to(kernel.B, target.host)
        s.partition(A, hcl.Partition.Block, dim=1, factor=2)
        s.partition(kernel.B, hcl.Partition.Block, dim=1, factor=2)

        target.config(compiler="vivado_hls", mode="csyn")
        f = hcl.build(s, target)

        np_A = np.random.randint(10, size=(10, 10))
        np_B = np.zeros((10, 10))

        hcl_A = hcl.asarray(np_A, dtype=hcl.UInt(8))
        hcl_B = hcl.asarray(np_B, dtype=hcl.UInt(8))
        print(hcl.lower(s))
Пример #12
0
 def kernel(A, B):
     C = hcl.compute(A.shape,
                     lambda x: hcl.cast(hcl.UInt(bw), A[x]) << sl,
                     dtype=hcl.UInt(bw))
     D = hcl.compute(A.shape, lambda x: B[x] + C[x], dtype=hcl.UInt(bw))
     E = hcl.compute(A.shape, lambda x: A[x])
     return E
def test_pack():
    def pack(A):
        return hcl.pack(A, factor=4)

    for i in range(4, 36, 4):
        A = hcl.placeholder((40, ), "A", dtype=hcl.UInt(i // 4))

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

        _A = hcl.asarray(np.random.randint(1000, size=(40, )),
                         dtype=hcl.UInt(i // 4))
        _B = hcl.asarray(np.zeros(10), dtype=hcl.UInt(i))

        f(_A, _B)

        __A = _A.asnumpy()
        __B = _B.asnumpy()

        for j in range(0, 10):
            golden = 0
            numB = __B[j]
            for k in range(0, 4):
                numA = __A[j * 4 + k]
                golden += numA << (k * i // 4)
            assert numB == golden
Пример #14
0
 def pack(A):
     rk = hcl.reduce_axis(0, 4, name='rk')
     genpack = hcl.reducer(0, lambda x, y: y * 2 + x,
                           dtype=hcl.UInt(4))  # y is accumulator
     pack = hcl.compute((2, ),
                        lambda x: genpack(A[x * 4 + (3 - rk)], axis=rk),
                        dtype=hcl.UInt(4))
     # pack = hcl.pack(A, axis=0, factor=4, dtype=hcl.UInt(4))
     return pack
Пример #15
0
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
Пример #16
0
def top(input, ):
    final_total_extent_1 = (
        hcl.cast(dtype=hcl.Int(bits=64), expr=final_extent_1) *
        hcl.cast(dtype=hcl.Int(bits=64), expr=final_extent_0))
    max_local = hcl.compute((final_extent_0, final_extent_1),
                            lambda x, y: 0,
                            name="max_local",
                            dtype=hcl.UInt(bits=16))
    with hcl.Stage("max_local"):
        with hcl.for_(final_min_1, final_extent_1,
                      name="max_local_s0_y") as max_local_s0_y:
            with hcl.for_(final_min_0, final_extent_0,
                          name="max_local_s0_x") as max_local_s0_x:
                maximum = hcl.compute((1, 1),
                                      lambda x, y: 0,
                                      name="maximum",
                                      dtype=hcl.UInt(bits=16))
                with hcl.Stage("maximum"):
                    maximum[max_local_s0_x,
                            max_local_s0_y] = hcl.cast(dtype=hcl.UInt(bits=16),
                                                       expr=0)
                    with hcl.for_(
                            0, 3,
                            name="maximum_s1_box__y") as maximum_s1_box__y:
                        with hcl.for_(
                                0, 3,
                                name="maximum_s1_box__x") as maximum_s1_box__x:
                            maximum[max_local_s0_x,
                                    max_local_s0_y] = hcl.select(
                                        maximum[max_local_s0_x, max_local_s0_y]
                                        > input[(max_local_s0_x +
                                                 maximum_s1_box__x),
                                                (max_local_s0_y +
                                                 maximum_s1_box__y)],
                                        maximum[max_local_s0_x,
                                                max_local_s0_y],
                                        input[(max_local_s0_x +
                                               maximum_s1_box__x),
                                              (max_local_s0_y +
                                               maximum_s1_box__y)])
                max_local[max_local_s0_x,
                          max_local_s0_y] = maximum[max_local_s0_x,
                                                    max_local_s0_y]
    final = hcl.compute((640, 480),
                        lambda x, y: 0,
                        name="final",
                        dtype=hcl.UInt(bits=16))
    with hcl.Stage("final"):
        with hcl.for_(final_min_1, final_extent_1,
                      name="final_s0_y") as final_s0_y:
            with hcl.for_(final_min_0, final_extent_0,
                          name="final_s0_x") as final_s0_x:
                final[final_s0_x, final_s0_y] = max_local[final_s0_x,
                                                          final_s0_y]
    return final
Пример #17
0
    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)
Пример #18
0
def test3():
    A = hcl.placeholder((8, 8), "A", dtype=hcl.UInt(2))
    B = hcl.placeholder((8, 8), "B", dtype=hcl.UInt(2))

    def kernel(A, B):
        return hcl.compute((8, 8),
                           lambda y, x: hcl.select(x < 4, A[y, x][0], 0), "C")

    s = hcl.create_scheme([A, B], kernel)
    s = hcl.create_schedule_from_scheme(s)
    f = hcl.build(s, "vhls")
    print(f)
Пример #19
0
    def test_gemm_binary():
        hcl.init()
        data = hcl.placeholder((64, 64), 'd', dtype=hcl.UInt(1))
        weight = hcl.placeholder((64, 64), 'w', dtype=hcl.UInt(1))

        def kernel(d, w):
            return hlib.ppac.gemm_binary(d, w, 'res')

        s = hcl.create_schedule([data, weight], kernel)
        f = hcl.build(s, target='rv64_ppac')
        code = str(f)
        assert 'PPACFunc_GeMMBin' in code
Пример #20
0
    def test_hdc_accu(proto, pack_data, labels, type):
        #pack the prototype
        pack_proto = hcl.pack(proto,
                              axis=1,
                              dtype=hcl.UInt(bw),
                              name="pack_proto")

        ###data preparation
        distance1 = hcl.compute((pack_data.shape[1], ),
                                lambda x: 0,
                                'distance1',
                                dtype=hcl.UInt(bw))
        pre_hamming = hcl.compute((pack_data.shape[1], ), lambda x: 0,
                                  "pre_hamming")
        hamming_dist1 = hcl.compute((numClasses, ), lambda x: 0,
                                    "hamming_dist1")
        m1 = hcl.reduce_axis(0, pack_data.shape[1], "m1")
        correct1 = hcl.scalar(0, 'correct1')
        ###

        with hcl.for_(0, pack_data.shape[0]) as i:
            hcl.print((i), "%d suc\n")
            with hcl.for_(0, numClasses) as n:
                #Do hdc multiplication(XOR) on sample[i]'s hdv and prototype[n]'s hdv (elementwise on the high-bit data)
                hcl.update(distance1,
                           lambda x: pack_data[i][x] ^ pack_proto[n][x])
                #Calculate the hamming distance of the two vectors by adding 1s
                hcl.update(pre_hamming, lambda x: popcount(distance1[x]))
                hcl.print((), "sum of 1s suc")
                ###########################seg fault
                hamming_dist1[n] = hcl.sum(pre_hamming[m1], axis=m1)

            #Find the one having the least hamming distance and choose it's label as the predicted label
            pred1 = hcl.scalar(0, 'pred1')
            with hcl.for_(0, hamming_dist1.shape[0]) as j:
                with hcl.if_(hamming_dist1[j] < hamming_dist1[pred1]):
                    pred1.v = j

            with hcl.if_(pred1.v == labels[i]):
                correct1.v += 1

        #Print the accuracy
        all1 = hcl.scalar(pack_data.shape[0], "all1", dtype=hcl.Float(32))
        accuracy1 = hcl.compute((1, ),
                                lambda x: correct1.v / all1.v * 100,
                                "accuracy1",
                                dtype=hcl.Float(32))
        with hcl.if_(type == 1):
            hcl.print((correct1, pack_data.shape[0], accuracy1[0]),
                      "Training accu: %d/%d (%.2f%%)\n")
        with hcl.else_():
            hcl.print((correct1, pack_data.shape[0], accuracy1[0]),
                      "Testing accu: %d/%d (%.2f%%)\n")
Пример #21
0
    def test_hmm_sim():
        hcl.init()
        x = hcl.placeholder((1, ), 'x', dtype=hcl.UInt(64))
        y = hcl.placeholder((64, ), 'y', dtype=hcl.UInt(64))

        def kernel(X, Y):
            return hlib.ppac.hmm_sim(X, Y, name='Z')

        s = hcl.create_schedule([x, y], kernel)
        f = hcl.build(s, target='rv64_ppac')
        code = str(f)
        assert 'PPACFunc_HmmSim' in code
Пример #22
0
    def test_gemm_multi_bit_unsigned():
        hcl.init()
        data = hcl.placeholder((32, 32), 'd', dtype=hcl.UInt(8))
        weight = hcl.placeholder((32, 32), 'w', dtype=hcl.UInt(8))

        def kernel(d, w):
            return hlib.ppac.gemm_multi_bit(d, w, 'res')

        s = hcl.create_schedule([data, weight], kernel)
        f = hcl.build(s, target='rv64_ppac')
        code = str(f)
        assert 'PPACFunc_GeMMUInt' in code
Пример #23
0
def test1():
    hcl.init()
    a = hcl.placeholder((3,), dtype=hcl.UInt(8), name="a")
    out = hcl.compute((3,),
        lambda x: tvm.intrin.popcount(a[x]),
        dtype=hcl.UInt(32))
    s = hcl.create_schedule([a, out])
    f = hcl.build(s)
    hcl_a = hcl.asarray(np.array([9, 7, 31]), dtype=hcl.UInt(8))
    hcl_out = hcl.asarray(np.array([0, 0, 0]), dtype=hcl.UInt(32))
    f(hcl_a, hcl_out)
    print("Input : {}".format(hcl_a.asnumpy()))
    print("Output : {}".format(hcl_out.asnumpy()))
Пример #24
0
def test2():
    hcl.init()
    A = hcl.placeholder((3, ), dtype=hcl.UInt(8), name="A")
    B = hcl.placeholder((3, ), dtype=hcl.UInt(8), name="B")
    rb = hcl.reduce_axis(0, 8, name="rb")
    out = hcl.compute(
        (3, ),
        lambda x:
        # popcnt(A[x] ^ B[x]))
        hcl.sum((A[x] ^ B[x])[rb], axis=rb))
    s = hcl.create_schedule([A, B, out])
    f = hcl.build(s, "vhls")
    print(f)
Пример #25
0
 def kernel(A):
     B = hcl.compute(A.shape,
                     lambda *args: A[args] + 1,
                     "B",
                     dtype=hcl.UInt(8))
     C = hcl.compute(A.shape,
                     lambda *args: B[args] + 1,
                     "B",
                     dtype=hcl.UInt(8))
     D = hcl.compute(A.shape,
                     lambda *args: C[args] + 1,
                     "D",
                     dtype=hcl.UInt(8))
     return D
Пример #26
0
def test_mask():

    def mask(A):
        return hcl.compute(A.shape, lambda x: (A[x] & 0xFFFF), "mask", dtype=A.dtype)

    A = hcl.placeholder((2,), "A", dtype=hcl.UInt(16))
    s = hcl.create_schedule([A], mask)

    m = hcl.build(s)

    hcl_A = hcl.asarray([10,5], dtype=A.dtype)
    hcl_R = hcl.asarray([99,99], dtype=hcl.UInt(16))
    m(hcl_A, hcl_R)
    assert np.array_equal(hcl_A.asnumpy(), [10, 5])
Пример #27
0
    def test_hmm_sim():
        hcl.init()
        b_n = 10
        d_n = 256
        X = hcl.placeholder((b_n, ), 'X', dtype=hcl.UInt(64))
        Y = hcl.placeholder((d_n, ), 'Y', dtype=hcl.UInt(64))

        def kernel(X, Y):
            return hlib.ppac.hmm_sim(X, Y, name='Z')

        s = hcl.create_schedule([X, Y], kernel)
        ir = str(hcl.lower(s))
        assert ('\"_batch_num\"=' + str(b_n)) in ir
        assert ('\"_in_block_num\"=' + str(1)) in ir
        assert ('\"_out_channel_num\"=' + str(d_n)) in ir
Пример #28
0
    def test_gemm_binary():
        hcl.init()
        b_n, i_c, o_c = 64, 256, 256
        ppac_config = hlib.ppac.PPAC_config(multi_bit=False)
        data = hcl.placeholder((b_n, i_c), 'd', dtype=hcl.UInt(1))
        weight = hcl.placeholder((o_c, i_c), 'w', dtype=hcl.UInt(1))

        def kernel(d, w):
            return hlib.ppac.gemm_binary(d, w, 'res')

        s = hcl.create_schedule([data, weight], kernel)
        ir = str(hcl.lower(s))
        assert ('\"_batch_num\"=' + str(b_n)) in ir
        assert ('\"_in_block_num\"=' + str(i_c // ppac_config.elem_num)) in ir
        assert ('\"_out_channel_num\"=' + str(o_c)) in ir
Пример #29
0
def test2():
    def pack(A):
        return hcl.pack(A, axis=1, dtype=hcl.UInt(2))

    hcl.init()
    a = hcl.placeholder((2, 4), dtype=hcl.UInt(1), name="A")
    s = hcl.create_schedule([a], pack)
    f = hcl.build(s)

    in_array = hcl.asarray(np.array([[0, 1, 1, 1], [1, 0, 0, 1]]),
                           dtype=hcl.UInt(1))
    out_array = hcl.asarray(np.zeros((2, 2)), dtype=hcl.UInt(2))
    f(in_array, out_array)
    print("Input: {}".format(in_array.asnumpy()))
    print("Output: {}".format(out_array.asnumpy()))
Пример #30
0
def blur_func_gen(target='soda', burst_width=512, unroll_factor=8):
    hcl.init()
    img_i = hcl.placeholder((2335, 235), "img_i", dtype=hcl.UInt(16))
    def blur_x(y, x):
        return (img_i[y, x-1] + img_i[y, x] + img_i[y, x+1])/3
    def blur_y(y, x):
        return (img_t[y-1, x] + img_t[y, x] + img_t[y+1, x])/3
    img_t = hcl.compute((2335, 233), blur_x, "img_t", dtype=hcl.UInt(16))
    img_o = hcl.compute((2333, 233), blur_y, "img_o", dtype=hcl.UInt(16))
    hcl_schedule = hcl.create_schedule([img_i, img_o])
    hcl_schedule[img_t].stencil(
        burst_width=burst_width, unroll_factor=unroll_factor)
    hcl_schedule[img_o].stencil(
        burst_width=burst_width, unroll_factor=unroll_factor)
    return hcl.build(hcl_schedule, target=target)