コード例 #1
0
 def __init__(self, block, num_blocks, params):
     self.in_planes = 16
     self.params = dict()
     self.params["conv1"] = hcl.const_tensor(params[0], "w_conv1",
                                             qtype_float)
     self.params["bn1"] = [
         hcl.const_tensor(params[i], "w_bn1_{}".format(i), qtype_float)
         for i in range(1, 5)
     ]
     self.params["layer1"] = params[5:59]
     self.params["layer2"] = params[59:113]
     self.params["layer3"] = params[113:167]
     self.params["linear"] = [
         hcl.const_tensor(params[i], "w_fc_{}".format(i), qtype_float)
         for i in range(167, 169)
     ]
     self.layer1 = self._make_layer(block,
                                    16,
                                    num_blocks[0],
                                    stride=1,
                                    params=self.params["layer1"],
                                    id=0)
     self.layer2 = self._make_layer(block,
                                    32,
                                    num_blocks[1],
                                    stride=2,
                                    params=self.params["layer2"],
                                    id=1)
     self.layer3 = self._make_layer(block,
                                    64,
                                    num_blocks[2],
                                    stride=2,
                                    params=self.params["layer3"],
                                    id=2)
コード例 #2
0
ファイル: test_conv.py プロジェクト: chhzh123/heterocl-demo
 def kernel(A):
     r = hcl.reduce_axis(0, KERNEL_SIZE)
     c = hcl.reduce_axis(0, KERNEL_SIZE)
     F = hcl.const_tensor(
         np.random.randint(0, 10, (KERNEL_SIZE, KERNEL_SIZE)), "F")
     return hcl.compute(
         (SIZE - KERNEL_SIZE + 1, SIZE - KERNEL_SIZE + 1),
         lambda y, x: hcl.sum(A[y + r, x + c] * F[r, c], axis=[r, c]), "B")
コード例 #3
0
def build_packed_bnn(input_image):
    w_conv1 = hcl.const_tensor(packed_params["w_conv1"], "w_conv1", qtype_bit)
    bn_t1 = hcl.const_tensor(packed_params["bn_t1"], "bn_t1", qtype_float)
    w_conv2 = hcl.const_tensor(packed_params["w_conv2"], "w_conv2",
                               hcl.UInt(16))
    bn_t2 = hcl.const_tensor(packed_params["bn_t2"], "bn_t2", qtype_float)
    w_fc1 = hcl.const_tensor(packed_params["w_fc1"], "w_fc1", qtype_packed)
    b_fc1 = hcl.const_tensor(packed_params["b_fc1"], "b_fc1", qtype_float)
    w_fc2 = hcl.const_tensor(packed_params["w_fc2"], "w_fc2", qtype_packed)
    b_fc2 = hcl.const_tensor(packed_params["b_fc2"], "b_fc2", qtype_float)

    conv1 = bnn.packed_conv2d_nhwc(input_image,
                                   w_conv1,
                                   padding=[1, 1],
                                   name="conv1",
                                   out_dtype=qtype_int)
    bn1 = bnn.packed_batch_norm_threshold_nhwc(conv1, bn_t1, name="bn1")
    if not args.stream:
        maxpool1 = bnn.packed_max_pool2d_nhwc(bn1, [2, 2], [2, 2],
                                              name="maxpool1")
    else:
        maxpool1 = bnn.packed_max_pool2d_nhwc_LB(bn1, [2, 2], [2, 2],
                                                 name="maxpool1")

    conv2 = bnn.packed_conv2d_nhwc(maxpool1,
                                   w_conv2,
                                   padding=[1, 1],
                                   name="conv2",
                                   out_dtype=qtype_int)
    bn2 = bnn.packed_batch_norm_threshold_nhwc(conv2, bn_t2, name="bn2")
    if not args.stream:
        maxpool2 = bnn.packed_max_pool2d_nhwc(bn2, [2, 2], [2, 2],
                                              name="maxpool2")  # 32*4*4=512
    else:
        maxpool2 = bnn.packed_max_pool2d_nhwc_LB(bn2, [2, 2], [2, 2],
                                                 name="maxpool2")  # 32*4*4=512

    pack = bnn.packed_flatten_nhwc(maxpool2, name="packed_flatten")
    fc1 = bnn.packed_dense(pack, w_fc1, b_fc1, True,
                           name="fc1")  # 512/32->256/32
    fc2 = bnn.packed_dense(fc1,
                           w_fc2,
                           b_fc2,
                           False,
                           name="fc2",
                           dtype=dtype_out)  # 256/32->10
    return fc2
コード例 #4
0
def build_packed_bnn(input_image):  # 1*16*16
    w_conv1 = hcl.const_tensor(packed_params["w_conv1"], "w_conv1", qtype_bit)
    bn_t1 = hcl.const_tensor(packed_params["bn_t1"], "bn_t1", qtype_float)
    w_conv2 = hcl.const_tensor(packed_params["w_conv2"], "w_conv2",
                               hcl.UInt(16))
    bn_t2 = hcl.const_tensor(packed_params["bn_t2"], "bn_t2", qtype_float)
    w_fc1 = hcl.const_tensor(packed_params["w_fc1"], "w_fc1", qtype_packed)
    b_fc1 = hcl.const_tensor(packed_params["b_fc1"], "b_fc1", qtype_float)
    w_fc2 = hcl.const_tensor(packed_params["w_fc2"], "w_fc2", qtype_packed)
    b_fc2 = hcl.const_tensor(packed_params["b_fc2"], "b_fc2", qtype_float)

    conv1 = bnn.packed_conv2d_nchw(input_image,
                                   w_conv1,
                                   padding=[1, 1],
                                   name="conv1",
                                   out_dtype=qtype_int,
                                   bitwidth=1)  # 16*16*16
    bn1 = bnn.packed_batch_norm_threshold(conv1, bn_t1, name="bn1")
    maxpool1 = bnn.packed_max_pool2d_LB(bn1, [2, 2], [2, 2],
                                        name="maxpool1")  # 16*8*8

    conv2 = bnn.packed_conv2d_nchw(maxpool1,
                                   w_conv2,
                                   padding=[1, 1],
                                   name="conv2",
                                   out_dtype=qtype_int,
                                   bitwidth=16)  # 32*8*8
    bn2 = bnn.packed_batch_norm_threshold(conv2, bn_t2, name="bn2")
    maxpool2 = bnn.packed_max_pool2d_LB(bn2, [2, 2], [2, 2],
                                        name="maxpool2")  # 32*4*4=512

    pack = bnn.packed_flatten(maxpool2, name="packed_flatten")
    fc1 = bnn.packed_dense(pack, w_fc1, b_fc1, True,
                           name="fc1")  # 512/32->256/32
    fc2 = bnn.packed_dense(fc1,
                           w_fc2,
                           b_fc2,
                           False,
                           name="fc2",
                           dtype=dtype_out)  # 256/32->10
    return fc2
コード例 #5
0
 def __init__(self, in_planes, planes, stride, params, name="bb"):
     self.params = dict()
     self.params["rprelu1"] = [
         hcl.const_tensor(params[i], "w_{}_rprelu1_{}".format(name, i),
                          qtype_float) for i in range(3)
     ]
     self.params["rprelu2"] = [
         hcl.const_tensor(params[i], "w_{}_rprelu2_{}".format(name, i),
                          qtype_float) for i in range(3, 6)
     ]
     self.params["rsign1"] = hcl.const_tensor(params[6],
                                              "w_{}_rsign1".format(name),
                                              qtype_float)
     self.params["rsign2"] = hcl.const_tensor(params[7],
                                              "w_{}_rsign2".format(name),
                                              qtype_float)
     if name.split("layer")[1][0] == "1":
         bitwidth = 16
     elif name.split("layer")[1][0] == "2":
         bitwidth = 32
     else:
         bitwidth = 32  # do NOT use 64 bitwidth!
     dtype = hcl.UInt(bitwidth)  # remember to set
     self.params["conv1"] = hcl.const_tensor(params[8],
                                             "w_{}_conv1".format(name),
                                             hcl.UInt(bitwidth))
     self.params["bn1"] = [
         hcl.const_tensor(params[i], "w_{}_bn1_{}".format(name, i),
                          qtype_float) for i in range(9, 13)
     ]
     self.params["conv2"] = hcl.const_tensor(params[13],
                                             "w_{}_conv2".format(name),
                                             hcl.UInt(bitwidth))
     self.params["bn2"] = [
         hcl.const_tensor(params[i], "w_{}_bn2_{}".format(name, i),
                          qtype_float) for i in range(14, 18)
     ]
     self.stride = stride
     self.flag = in_planes != planes
     self.name = name
コード例 #6
0
 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],
                        dtype=hcl.Float())
コード例 #7
0
 def kernel():
     A = hcl.const_tensor(np.random.random((10, 10)), "A", dtype)
     return hcl.compute(A.shape, lambda x, y: A[x, y] + 1, "B", dtype)