Пример #1
0
def verify_upsampling():
    batch = IntIter(list_constraint([1, 4, 8, 16]))
    channel = IntIter(list_constraint([1, 3, 4]))
    h = IntIter(range_constraint(1, 9, 3))
    w = IntIter(range_constraint(1, 9, 3))
    dshp = opg.ExtendIter(batch, channel, h, w)
    datas = []
    for i in range(len(dshp)):
        size = np.product(dshp[i])
        arr1 = ConstantIter(rand_constraint(-127, 127, size), shape=dshp[i])
        arr2 = ConstantIter(rand_constraint(0, 127, size), shape=dshp[i])
        datas.extend([arr1, arr2])
    data = ConcatIter(*datas)

    scale = IntIter(iter_constraint(3), name="scale")

    def upsampling(data, scale):
        if scale == 0:
            raise ValueError("scale must > 0 vs. " + str(scale))
        a_np = np.array(data)
        b_np = topi.testing.upsampling_python(a_np, scale, "NCHW")
        return [b_np]

    op_units = opg.OpUnitIter([data, scale], 1)
    op_units.eval_data("upsampling", upsampling, is_dump=True)
Пример #2
0
def verify_dense():
    batch = IntIter(list_constraint([1, 4, 8, 16]))
    channel = IntIter(list_constraint([1, 3, 32, 64]))
    dshp = opg.ExtendIter(batch, channel)
    datas = []
    for i in range(len(dshp)):
        size = np.product(dshp[i])
        arr1 = ConstantIter(rand_constraint(-127, 127, size), shape=dshp[i])
        arr2 = ConstantIter(rand_constraint(0, 127, size), shape=dshp[i])
        arr3 = ConstantIter(rand_constraint(1, 127, size), shape=dshp[i])
        datas.extend([arr1, arr2, arr3])
    data = ConcatIter(
        *datas, ConstantIter(rand_constraint(-127, 127, 112), shape=(4, 4, 7)))
    print(len(data))

    units = IntIter(list_constraint([1, 32, 64]), name="units")
    use_bias = BoolIter(name="use_bias")
    op_units = opg.OpUnitIter([data, units, use_bias], 1)
    for i in range(len(op_units)):
        data, units, use_bias = op_units[i]
        data_nd = nd.array(data)
        batch, ic = data_nd.shape[:2]
        wshp = (units, ic)
        wsize = np.product(wshp)
        weight = ConstantIter(rand_constraint(-127, 127, wsize), shape=wshp)
        weight_nd = nd.array(weight[0])
        bshp = (units, )
        bias = ConstantIter(rand_constraint(-127, 127, units), shape=bshp)
        bias_nd = nd.array(bias[0])
        attr = {
            'units': units,
            'use_bias': use_bias,
        }
        ins = [data_nd, weight_nd, bias_nd
               ] if use_bias else [data_nd, weight_nd]
        outs, err = None, None
        try:
            if use_bias:
                out = nd.FullyConnected(data_nd, weight_nd, bias_nd, units,
                                        (not use_bias))
            else:
                out = nd.FullyConnected(data_nd, weight_nd, None, units,
                                        (not use_bias))
            outs = [out]
        except Exception as e:
            err = "Error:\n" + str(e)
        print(data_nd.shape, wshp, bshp, attr, outs[0].shape if outs else None,
              err.replace("\n", "") if err else None)
        opg.dump("dense", attr, ins, outs, err)
Пример #3
0
def verify_get_valid_counts():
    dshp = ConcatIter(PermutationIter(list_constraint([4, 5, 6])),
                      [[1, 2, 3], [3, 1, 2], [2, 3, 1]])
    data_arr = []
    for i in range(len(dshp)):
        shp = dshp[i]
        size = np.product(shp)
        arr = ConstantIter(rand_constraint(0, 10, size), shape=shp)
        data_arr.append(arr)
    data = ConcatIter(*data_arr)
    score = IntIter(list_constraint([-1, 0, 1, 5, 7]), name="score_threshold")

    def get_valid_counts(data, score_threshold):
        np_data = np.array(data)
        dshp = np_data.shape
        if len(dshp) != 3 or (dshp[2] <= 2):
            raise ValueError("data shape error: " + str(dshp))
        batch_size, num_anchor, elem_length = dshp
        np_out1 = np.zeros(shape=(batch_size, ))
        np_out2 = np.zeros(shape=dshp, dtype=INT32)
        for i in range(batch_size):
            np_out1[i] = 0
            inter_idx = 0
            for j in range(num_anchor):
                score = np_data[i, j, 1]
                if score > score_threshold:
                    for k in range(elem_length):
                        np_out2[i, inter_idx, k] = np_data[i, j, k]
                    np_out1[i] += 1
                    inter_idx += 1
                if j >= np_out1[i]:
                    for k in range(elem_length):
                        np_out2[i, j, k] = -1.0
        return [np_out1, np_out2]

    op_units = opg.OpUnitIter([data, score], 1)
    op_units.eval_data("get_valid_counts", get_valid_counts, is_dump=True)
Пример #4
0
def verify_non_max_suppression():
    # batch = np.random.randint(low=1, high=10)
    # n = np.random.randint(low=10, high=11)
    # k = 6
    # dshape = (batch, n, k)
    # data = tvm.placeholder(dshape, name="data")
    # valid_count = tvm.placeholder((dshape[0],), dtype="int32", name="valid_count")
    # nms_threshold = np.random.randint(low=1, high=10)
    # force_suppress = True if np.random.randint(low=0, high=1) == 1 else False
    # nms_topk = np.random.randint(low=1, high=9)
    # params = {'iou_threshold':nms_threshold*10, 'coord_start':2, 'score_index':1, 'id_index':0,
    #         'force_suppress':force_suppress, 'top_k': nms_topk, 'return_indices':False}
    # save_dict(params, case_dir + '/attr.txt')

    # np_data = np.random.randint(low=-(2**31-1), high=(2**31-1), size=dshape).astype(data.dtype)
    # np_valid_count = np.random.randint(low=1, high=10, size=(batch)).astype(valid_count.dtype)

    # device = 'llvm'
    # ctx = tvm.context(device, 0)
    # with tvm.target.create(device):
    #     out = non_max_suppression(data, valid_count, -1, nms_threshold, force_suppress, nms_topk, return_indices=False)
    #     s = topi.generic.schedule_nms(out)

    # tvm_data = tvm.nd.array(np_data, ctx)
    # tvm_valid_count = tvm.nd.array(np_valid_count, ctx)
    # save_txt(tvm_data, case_dir + "/in_0.txt")
    # save_txt(tvm_valid_count, case_dir + "/in_1.txt")

    # tvm_out = tvm.nd.array(np.zeros(dshape, dtype=data.dtype), ctx)
    # f = tvm.build(s, [data, valid_count, out], device)
    # f(tvm_data, tvm_valid_count, tvm_out)

    # save_txt(tvm_out, case_dir + "/out_0.txt")

    batch = IntIter(range_constraint(1, 2))
    n = IntIter(rand_constraint(1, 32, 40))
    k = IntIter(list_constraint([6]))
    dshp = opg.ExtendIter(batch, n, k)
    datas = []
    for i in range(len(dshp)):
        shp = dshp[i]
        data = []
        for n in range(shp[1]):
            elem = rand_constraint(-20, 20, 6)()
            elem[0] = random.randint(-1, 10)
            data.append(elem)
        datas.append([[data]])
    data = ConcatIter(*datas)
    valid_count = RandomVectorIter(1, 32, 1, 10)
    iou = ConcatIter(IntIter(rand_constraint(0, 100, 20)),
                     IntIter(list_constraint([110])),
                     name="iou_threshold")
    force_suppress = BoolIter(name="force_suppress")
    top_k = ConcatIter(IntIter(list_constraint([-1])),
                       IntIter(rand_constraint(1, 32, 6)),
                       name="top_k")
    id_index = IntIter(list_constraint([0]), name="id_index")
    score_index = IntIter(list_constraint([1]), name="score_index")
    coord_start = IntIter(list_constraint([2]), name="coord_start")
    max_output_size = ConcatIter(IntIter(list_constraint([-1])),
                                 IntIter(rand_constraint(1, 32, 6)),
                                 name="max_output_size")
    return_indices = BoolIter(const=False, name="return_indices")
    invalid_to_bottom = BoolIter(const=True, name="invalid_to_bottom")
    inputs = [
        data, valid_count, iou, force_suppress, top_k, id_index, score_index,
        coord_start, max_output_size, return_indices, invalid_to_bottom
    ]

    def cstr_func(data, valid_count, *args):
        data_np = np.array([data])
        count = valid_count[0]
        if count > data_np.shape[1]:
            return False
        return True

    op_units = opg.OpUnitIter([
        data, valid_count, iou, force_suppress, top_k, id_index, score_index,
        coord_start, max_output_size, return_indices, invalid_to_bottom
    ], 2, [cstr_func])

    def non_max_suppression(data, valid_count, iou, force_suppress, top_k,
                            id_index, score_index, coord_start,
                            max_output_size, return_indices,
                            invalid_to_bottom):
        device = 'llvm'
        ctx = tvm.context(device, 0)
        data_np, valid_count_np = np.array(data, dtype="float32"), np.array(
            valid_count, dtype="int32")
        data_nd, valid_count_nd = tvm.nd.array(data_np, ctx), tvm.nd.array(
            valid_count_np, ctx)
        dshp = data_nd.shape
        data_tvm = tvm.placeholder(dshp, name="data", dtype="float32")
        valid_count_tvm = tvm.placeholder((dshp[0], ),
                                          dtype="int32",
                                          name="valid_count")
        with tvm.target.create(device):
            out = topi.vision.non_max_suppression(data_tvm, valid_count_tvm,
                                                  max_output_size, iou / 100,
                                                  force_suppress, top_k,
                                                  coord_start, score_index,
                                                  id_index, return_indices,
                                                  invalid_to_bottom)
            s = topi.generic.schedule_nms(out)

        out_nd = tvm.nd.array(np.zeros(dshp, dtype=data_tvm.dtype), ctx)
        f = tvm.build(s, [data_tvm, valid_count_tvm, out], device)
        f(data_nd, valid_count_nd, out_nd)
        return [out_nd.asnumpy()]

    op_units.eval_data("non_max_suppression", non_max_suppression, True)
Пример #5
0
def verify_max_pool2d():
    batch = IntIter(list_constraint([1, 4]))
    channel = IntIter(list_constraint([1, 4]))
    h = IntIter(range_constraint(1, 9, 3))
    w = IntIter(range_constraint(1, 9, 3))
    dshp = opg.ExtendIter(batch, channel, h, w)
    datas = []
    for i in range(len(dshp)):
        size = np.product(dshp[i])
        arr1 = ConstantIter(rand_constraint(-127, 127, size), shape=dshp[i])
        arr2 = ConstantIter(rand_constraint(0, 127, size), shape=dshp[i])
        datas.extend([arr1, arr2])
    data = ConcatIter(*datas)
    print(len(data))

    iattr = IntIter(range_constraint(1, 4))
    pool_size = ConcatIter(RepeatIter([1, 2, 3], 2), [(2, 3), (3, 1)],
                           name="pool_size")
    strides = ConcatIter(RepeatIter([1, 2], 2), [(1, 2), (2, 1)],
                         name="strides")
    iattr = IntIter(iter_constraint(2))
    padding = ConcatIter(VectorIter(iattr, 1),
                         VectorIter(iattr, 2),
                         name="padding")
    ceil_mode = BoolIter(name="ceil_mode")

    def max_pool2d(data, pool_size, strides, padding, ceil_mode):
        data_nd = nd.array(data)
        pad = padding
        if len(padding) == 1:
            pad = (padding[0], padding[0])
        out = nd.Pooling(data_nd,
                         pool_size,
                         pool_type="max",
                         global_pool=False,
                         stride=strides,
                         pad=pad)

        data_npy = np.array(data)
        ashape = data_npy.shape
        n, ic, ih, iw = data_npy.shape
        sh, sw = strides
        kh, kw = pool_size
        bshape = [n, ic, ih, iw]
        if len(padding) == 1:
            pt = pl = pb = pr = padding[0]
        else:
            pt = pb = padding[0]
            pl = pr = padding[1]
        if ceil_mode:
            pb += sh - 1
            pr += sw - 1
        pad_np = np.full((n, ic, ih + pt + pb, iw + pl + pr),
                         -127).astype(INT32)
        # pad_np = np.zeros(shape=(n, ic, ih+pt+pb, iw+pl+pr)).astype(INT32)
        no_zero = (range(n), range(ic), (range(pt,
                                               ih + pt)), (range(pl, iw + pl)))
        pad_np[np.ix_(*no_zero)] = data_npy
        bshape[2] = int(math.floor(float(ashape[2] - kh + pt + pb) / sh) + 1)
        bshape[3] = int(math.floor(float(ashape[3] - kw + pl + pr) / sw) + 1)
        if pt >= kh or (bshape[2] - 1) * sh - pt >= ashape[2]:
            raise ValueError("ceil_mode exceed out of input")
        if pl >= kw or (bshape[3] - 1) * sw - pl >= ashape[3]:
            raise ValueError("ceil mode exceed out of input")
        _, oc, oh, ow = bshape
        b_np = np.zeros(shape=(n, oc, oh, ow)).astype(INT32)
        for i in range(oh):
            for j in range(ow):
                b_np[:, :, i, j] = np.max(pad_np[:, :, i * sh:i * sh + kh,
                                                 j * sw:j * sw + kw],
                                          axis=(2, 3))
        return [b_np]

    op_units = opg.OpUnitIter([data, pool_size, strides, padding, ceil_mode],
                              1)
    op_units.eval_data("max_pool2d", max_pool2d, is_dump=True)
Пример #6
0
def verify_conv2d():
    batch = IntIter(list_constraint([1, 4, 8]))
    channel = IntIter(list_constraint([1, 3, 4, 8]))
    h = IntIter(range_constraint(1, 9, 3))
    w = IntIter(range_constraint(1, 9, 3))
    dshp = opg.ExtendIter(batch, channel, h, w)
    datas = []
    for i in range(len(dshp)):
        size = np.product(dshp[i])
        arr1 = ConstantIter(rand_constraint(-127, 127, size), shape=dshp[i])
        arr2 = ConstantIter(rand_constraint(0, 127, size), shape=dshp[i])
        datas.extend([arr1, arr2])
    data = ConcatIter(*datas)
    print(len(data))

    num_filter = IntIter(list_constraint([1, 16, 32]), name="num_filter")
    kernel = VectorIter(IntIter(list_constraint([1, 2, 3])),
                        size=2,
                        name="kernel_size")
    strides = RandomVectorIter(1, 4, 2, 1, name="strides")
    padding = RandomVectorIter(0, 3, 2, 1, name="padding")
    groups = IntIter(list_constraint([1, 2, 4]), name="groups")
    use_bias = RandomBoolIter(name="use_bias")
    op_units = opg.OpUnitIter(
        [data, num_filter, kernel, strides, padding, groups, use_bias], 1)
    for i in range(len(op_units)):
        data, num_filter, kernel, strides, padding, \
            groups, use_bias = op_units[i]
        a_np = np.array(data)
        batch, ic, h, w = a_np.shape
        if groups == 1:
            pass
        elif random.randint(0, 10) < 5 and ic % groups == 0:
            pass
        else:
            groups = ic
        wshp = (num_filter, ic // groups, *kernel)
        wsize = np.product(wshp)
        weight = ConstantIter(rand_constraint(-127, 127, wsize), shape=wshp)
        w_np = np.array(weight[0])
        bshp = (num_filter, )
        bias = ConstantIter(rand_constraint(-127, 127, num_filter), shape=bshp)
        b_np = np.array(bias[0])

        rand = random.randint(0, 100)
        if rand < 60:
            dilation = (1, 1)
        elif rand < 70:
            dilation = (1, 2)
        elif rand < 80:
            dilation = (2, 1)
        else:
            dilation = (2, 2)
        attr = {
            'channels': num_filter,
            'kernel_size': kernel,
            'strides': strides,
            'padding': padding,
            'dilation': dilation,
            'groups': groups,
            'layout': "NCHW",
            'kernel_layout': "OIHW",
            'use_bias': use_bias,
        }
        ins = [a_np, w_np, b_np] if use_bias else [a_np, w_np]
        outs, err = None, None
        try:
            b_nd = nd.array(b_np) if use_bias else None
            _ = nd.Convolution(nd.array(a_np),
                               nd.array(w_np),
                               b_nd,
                               kernel,
                               strides,
                               dilation,
                               padding,
                               num_filter,
                               groups,
                               no_bias=(not use_bias))

            dw_np = topi.testing.dilate_python(w_np, (1, 1, *dilation))
            c_np = topi.testing.conv2d_nchw_python(a_np, dw_np, strides,
                                                   padding)
            if use_bias:
                c_np += b_np.reshape(num_filter, 1, 1)
            outs = [c_np]
        except Exception as e:
            err = "Error:\n" + str(e)
        print(a_np.shape, wshp, bshp, attr, outs[0].shape if outs else None,
              err.replace("\n", "") if err else None)
        opg.dump("conv2d", attr, ins, outs, err)