Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
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)