Exemplo n.º 1
0
def verify_concatenate():
    data = ConcatIter(ConstantIter(iter_constraint(6), shape=(1, 2, 3)),
                      ConstantIter(iter_constraint(3)))
    axis = IntIter(range_constraint(-4, 4), name="axis")

    def concatenate(*inputs):
        data_npys = [np.array(d) for d in inputs[:-1]]
        out_npy = np.concatenate(data_npys, axis=inputs[-1])
        return [out_npy]

    def cstr_func(*inputs):
        axis = inputs[-1]
        shp = np.array(inputs[0]).shape
        ndim = len(shp)
        if (-ndim - 1 <= axis) and (axis <= ndim):
            return True
        return False

    is_dump = True
    op_units = opg.OpUnitIter([data, axis], 1, [cstr_func])
    op_units.eval_data("concatenate", concatenate, is_dump)

    op_units = opg.OpUnitIter([data, data, axis], 2, [cstr_func])
    op_units.eval_data("concatenate", concatenate, is_dump)

    op_units = opg.OpUnitIter([data, data, data, axis], 3, [cstr_func])
    op_units.eval_data("concatenate", concatenate, is_dump)
Exemplo n.º 2
0
def verify_slice_like():
    dshp = (1, 2, 3)
    data = opg.ConstantIter(opg.iter_constraint(6), shape=dshp)
    sshp = (1, 2, 2, 2)
    sdata = ConstantIter(iter_constraint(8), shape=sshp)
    iattr = IntIter(range_constraint(-4, 4))
    axis = ConcatIter(AllOverIter(iattr, shape_constraint(4)),
                      [[-5], [0, 1, 4]],
                      name="axis")

    def slice_like(data, shape, axis):
        data_nd = nd.array(data)
        shape_nd = nd.array(shape)
        out = nd.slice_like(data_nd, shape_nd, axis)
        return [out]

    op_units = opg.OpUnitIter([data, sdata, axis], 2)
    op_units.eval_data("slice_like", slice_like, is_dump=True)

    sdata = ConcatIter(
        ConstantIter(iter_constraint(1), shape=(1, 1, 1)),
        ConstantIter(iter_constraint(2), shape=(1, 1, 2)),
        ConstantIter(iter_constraint(2), shape=(1, 2, 1)),
        ConstantIter(iter_constraint(2), shape=(2, 1, 1)),
        ConstantIter(iter_constraint(2), shape=(1, 1, 1, 2)),
        ConstantIter(iter_constraint(2), shape=(1, 2)),
        [[]],
    )
    axis = VectorIter(iattr, 0, name="axis")
    op_units = opg.OpUnitIter([data, sdata, axis], 2)
    op_units.eval_data("slice_like", slice_like, is_dump=True)
Exemplo n.º 3
0
def verify_repeat():
    dshp = (1, 2, 3, 4)
    data = opg.ConstantIter(opg.iter_constraint(24), shape=dshp)
    repeats = opg.IntIter(iter_constraint(3), name="repeats")
    axis = opg.IntIter(range_constraint(-5, 5), name="axis")

    def repeat(data, repeats, axis):
        if repeats < 1:
            raise ValueError("repeats invalid: %s" % repeats)
        data_npy = np.array(data)
        out_npy = np.repeat(data_npy, repeats, axis)
        return [out_npy]

    op_units = opg.OpUnitIter([data, repeats, axis], 1)
    op_units.eval_data("repeat", repeat, is_dump=True)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def verify_transpose():
    def transpose(data, axes):
        data_npy = np.array(data, dtype=INT32)
        return [np.transpose(data_npy, axes)]

    # dshp = (1, 2, 3)
    # data = opg.ConstantIter(iter_constraint(6), shape=dshp)
    # axes = opg.PermutationIter(list_constraint([0, 1, 2]),
    #                            list_constraint([-1, 0, 1]),
    #                            list_constraint([-3, -2, -1]),
    #                            list_constraint([-3, 1, 2]))
    # axes = opg.ConcatIter(axes,
    #             opg.NoneIter(),
    #             opg.RepeatIter(IntIter(std_int_constraint(2)), 3),
    #             opg.ConstantIter(list_constraint([-4, 0, 1])),
    #             opg.ConstantIter(list_constraint([-1, 0, 4])),
    #             opg.ConstantIter(list_constraint([0, 1])),
    #             opg.ConstantIter(list_constraint([1])),
    #             name="axes")
    # op_units = opg.OpUnitIter([data, axes], attr_index=1)
    # op_units.eval_data("transpose", transpose, is_dump=True)

    dshp = opg.PermutationIter(list_constraint([5, 4, 3]))
    datas = []
    for i in range(len(dshp)):
        shp = dshp[i]
        size = np.product(shp)
        data = ConstantIter(iter_constraint(size), shape=shp)
        datas.append(data)
    data = ConcatIter(*datas)
    axes = ConcatIter([[1, 2, 0]], name="axes")
    op_units = opg.OpUnitIter([data, axes], attr_index=1)
    op_units.eval_data("transpose", transpose, is_dump=True)
Exemplo n.º 6
0
def verify_reduce(op_name):
    dshp = (1, 2, 3)
    data = opg.ConstantIter(opg.iter_constraint(6), shape=dshp)

    iattr = IntIter(range_constraint(-3, 3))
    axis = ConcatIter(AllOverIter(iattr, shape_constraint(3)),
                      [[0, 1, 2, 2], [1, 2, 3], [-1, -4, 1], []],
                      name="axis")
    keepdims = opg.BoolIter(name="keepdims")
    exclude = opg.BoolIter(name="exclude")

    def _reduce(data, axis, keepdims, exclude):
        data_nd = nd.array(data)
        out_nd = getattr(nd, op_name)(data_nd, axis, keepdims, exclude)
        return [out_nd]

    op_units = opg.OpUnitIter([data, axis, keepdims, exclude], 1)
    op_units.eval_data(op_name, _reduce, is_dump=True)
Exemplo n.º 7
0
def verify_tile():
    dshp = (1, 2, 3)
    data = opg.ConstantIter(opg.iter_constraint(6), shape=dshp)
    iattr = IntIter(range_constraint(1, 3))
    reps = ConcatIter(opg.AllOverIter(iattr, shape_constraint(4)),
                      opg.VectorIter(iattr, 0), [[2, 1, 0], [-1]],
                      name="reps")

    def tile(data, reps):
        invalid = (len(reps) == 0)
        for rep in reps:
            if rep <= 0:
                invalid = True
                break
        if invalid:
            raise ValueError("reps invalid %s" % reps)
        data_npy = np.array(data)
        out_npy = np.tile(data_npy, reps)
        return [out_npy]

    op_units = opg.OpUnitIter([data, reps], 1)
    op_units.eval_data("tile", tile, is_dump=True)
Exemplo n.º 8
0
def verify_take():
    def take(data, indices, axis):
        data_npy = np.array(data, dtype=INT32)
        if axis is None:
            return [np.take(data_npy, indices, mode="clip")]
        else:
            return [np.take(data_npy, indices, axis=axis, mode="clip")]

    # dshp = (1, 2, 3)
    # data = opg.ConstantIter(opg.iter_constraint(6), shape=dshp)
    # iattr = opg.RandomVectorIter(-1, 7, 5)
    # iattr2 = opg.VectorIter(iattr, 2)
    # indices = opg.ConcatIter(iattr, iattr2)
    # axis = opg.IntIter(opg.range_constraint(-4, 4), opg.gen_non_constraint(),
    #         name="axis")
    # print (len(data), len(indices), len(axis))
    # def take_func(data, indices, axis):
    #     if axis == None:
    #         return True
    #     dim = dshp[axis % 3]
    #     np_idx = np.array(indices).flatten()
    #     if (np_idx > dim).any():
    #         return True
    #     return False
    # op_units = opg.OpUnitIter([data, indices, axis], 2, [take_func])
    # op_units.eval_data("take", take, is_dump=True)

    dshp = (2, 3)
    data = opg.ConstantIter(opg.iter_constraint(6), shape=dshp)
    iattr = opg.RandomVectorIter(-1, 7, 5)
    iattr2 = opg.VectorIter(iattr, 2)
    indices = opg.ConcatIter(iattr, iattr2)
    axis = opg.IntIter(opg.range_constraint(-2, 2),
                       opg.gen_non_constraint(),
                       name="axis")
    op_units = opg.OpUnitIter([data, indices, axis], 2)
    op_units.eval_data("take", take, is_dump=True)
Exemplo n.º 9
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)
Exemplo n.º 10
0
def verify_strided_slice():
    dshp = (2, 2)
    data = opg.ConstantIter(opg.iter_constraint(4), shape=dshp)
    attr = IntIter(range_constraint(-3, 3))
    begin = ConcatIter(VectorIter(attr, 2),
                       VectorIter(attr, 1),
                       VectorIter(attr, 0),
                       name="begin")
    end = ConcatIter(VectorIter(attr, 2),
                     VectorIter(attr, 1),
                     VectorIter(attr, 0),
                     name="end")
    sattr = IntIter(list_constraint([-2, -1, 1, 2]))
    strides = ConcatIter(VectorIter(sattr, 2),
                         VectorIter(sattr, 1), [[0], [1, 0], [0, 1]],
                         NoneIter(),
                         name="stride")

    def cstr_func(*inputs):
        data, begin, end, strides = inputs
        if strides is not None and strides[0] != 1:
            return False
        satisfied = True
        for i in range(len(data)):
            dim = len(data[i])
            b = begin[i] % dim if (begin
                                   is not None) and (i < len(begin)) else 0
            e = end[i] % dim if (end is not None) and (i < len(end)) else dim
            s = strides[i] if (strides
                               is not None) and (i < len(strides)) else 1
            if (s < 0) and (b <= e):
                satisfied = False
                break
            elif (s > 0) and (e <= b):
                satisfied = False
                break
        return satisfied

    def strided_slice(data, begin, end, strides):
        dshp = len(data)
        for i in range(len(begin), dshp):
            begin.append(0)
        for i in range(len(end), dshp):
            end.append(len(data[i]))
        if strides is None:
            strides = []
        for i in range(len(strides), dshp):
            strides.append(1)
        for i in range(dshp):
            dim = len(data[i])
            b, e, s = begin[i], end[i], strides[i]
            begin_range = -1 if s < 0 else 0
            end_range = dim - 1 if s < 0 else dim
            b = b + dim if b < 0 else b
            e = e + dim if e < 0 else e
            b = min(max(b, begin_range), end_range)
            e = min(max(e, begin_range), end_range)
            if ((s < 0 and (b <= e)) or \
                (s > 0 and (e <= b))):
                raise ValueError("begin=%d;%d, end=%d;%d, stride=%d" \
                        % (begin[i], b, end[i], e, s))

        data_npy = np.array(data)
        out_npy = topi.testing.strided_slice_python(data_npy, begin, end,
                                                    strides)
        return [out_npy]

    op_units = opg.OpUnitIter([data, begin, end, strides], 1, [cstr_func])
    op_units.eval_data("strided_slice", strided_slice, is_dump=True)

    dshp = (10, 10)
    data = ConstantIter(iter_constraint(100), shape=dshp)
    begin = ConcatIter([[0, 9]], name="begin")
    end = ConcatIter([[9, -20]], name="end")
    strides = ConcatIter([[1, -1], [2, -2], [3, -3], [4, -4], [5, -5], [6, -6],
                          [9, -9], [10, -10], [20, -20]],
                         name="stride")
    op_units = opg.OpUnitIter([data, begin, end, strides], 1)
    op_units.eval_data("strided_slice", strided_slice, is_dump=True)