Ejemplo n.º 1
0
def test_ixor_invalid_dtypes(device, dtypes):
    (in_dtype1, in_dtype2), _ = dtypes
    shape = (2, 3)
    a = chainerx.array(array_utils.uniform(shape, in_dtype1))
    b = chainerx.array(array_utils.uniform(shape, in_dtype2))
    with pytest.raises(chainerx.DtypeError):
        a ^= b
Ejemplo n.º 2
0
    def generate_inputs(self):
        h_shape = (self.n_layers, self.batches[0], self.hidden_size)
        dtype = self.in_dtypes[0]

        h = array_utils.uniform(h_shape, dtype)
        c = array_utils.uniform(h_shape, dtype)
        in_size = self.input_size
        out_size = self.hidden_size
        xs = [
            array_utils.uniform((self.batches[b], in_size), dtype)
            for b in range(len(self.batches))
        ]

        def w_in(i, j):
            return in_size if i == 0 and j < 4 else out_size

        inputs = []
        inputs.append(h)
        inputs.append(c)
        for i in range(len(self.batches)):
            inputs.append(xs[i])

        for n in range(self.n_layers):
            for i in range(8):
                inputs.append(
                    array_utils.uniform((out_size, w_in(n, i)), dtype))
            for i in range(8):
                inputs.append(array_utils.uniform((out_size, ), dtype))
        return tuple(inputs)
Ejemplo n.º 3
0
 def generate_inputs(self):
     (x_dtype, y_dtype) = self.in_dtypes
     x = array_utils.uniform(self.x_shape, x_dtype)
     y = array_utils.uniform(self.y_shape, y_dtype)
     condition = numpy.random.uniform(0, 1, size=self.cond_shape)
     self.condition = (condition > 0.5).astype(self.condition_dtype)
     return (x, y)
Ejemplo n.º 4
0
def test_minimum_invalid_dtypes(device, dtype):
    shape = (3, 2)
    bool_array = chainerx.array(array_utils.uniform(shape, 'bool_'))
    numeric_array = chainerx.array(array_utils.uniform(shape, dtype))
    with pytest.raises(chainerx.DtypeError):
        chainerx.minimum(bool_array, numeric_array)
    with pytest.raises(chainerx.DtypeError):
        chainerx.minimum(numeric_array, bool_array)
Ejemplo n.º 5
0
    def generate_inputs(self):
        c_shape = self.c_shape
        x_shape = self.x_shape
        c_dtype, x_dtype = self.in_dtypes

        c = array_utils.uniform(c_shape, c_dtype)
        x = array_utils.uniform(x_shape, x_dtype)
        return c, x
Ejemplo n.º 6
0
def test_floordiv_invalid_dtypes(device, dtypes, is_module):
    (in_dtype1, in_dtype2), _ = dtypes
    shape = (2, 3)
    a = chainerx.array(array_utils.uniform(shape, in_dtype1))
    b = chainerx.array(array_utils.uniform(shape, in_dtype2))
    with pytest.raises(chainerx.DtypeError):
        if is_module:
            a // b
        else:
            chainerx.floor_divide(a, b)
Ejemplo n.º 7
0
def test_sub_invalid_dtypes(device, dtypes, is_module):
    (in_dtype1, in_dtype2), _ = dtypes
    shape = (2, 3)
    a = chainerx.array(array_utils.uniform(shape, in_dtype1))
    b = chainerx.array(array_utils.uniform(shape, in_dtype2))
    with pytest.raises(chainerx.DtypeError):
        if is_module:
            a - b
        else:
            chainerx.subtract(a, b)
Ejemplo n.º 8
0
    def generate_inputs(self):
        dst_dtype, src_dtype = self.in_dtypes

        dst = array_utils.uniform(self.dst_shape, dst_dtype)
        src = array_utils.uniform(self.src_shape, src_dtype)
        where = array_utils.uniform(
            self.where_shape if self.where_shape is not None else (1,),
            'float32', 0, 1) > 0.5

        return dst, src, where
Ejemplo n.º 9
0
 def generate_inputs(self):
     x_shape = self.x_shape
     w_shape = self.w_shape
     b_shape = self.b_shape
     dtype = self.dtype
     x = array_utils.uniform(x_shape, dtype)
     w = array_utils.uniform(w_shape, dtype)
     if b_shape in (None, Unspecified):
         return x, w
     else:
         b = array_utils.uniform(b_shape, dtype)
         return x, w, b
Ejemplo n.º 10
0
 def generate_inputs(self):
     x_shape = self.x_shape
     w_shape = self.w_shape
     b_shape = self.b_shape
     if len(self.in_dtypes) == 3:
         x_dtype, w_dtype, b_dtype = self.in_dtypes
     else:
         (x_dtype, w_dtype), b_dtype = self.in_dtypes, None
     x = array_utils.uniform(x_shape, x_dtype)
     w = array_utils.uniform(w_shape, w_dtype)
     if b_shape is None:
         return x, w
     else:
         b = array_utils.uniform(b_shape, b_dtype)
         return x, w, b
Ejemplo n.º 11
0
 def generate_inputs(self):
     dtype = self.dtype
     if self.input_lhs == 'random':
         a = array_utils.uniform(self.shape, dtype)
     elif isinstance(self.input_lhs, (bool, int, float)):
         a = numpy.full(self.shape, self.input_lhs, dtype=dtype)
     else:
         assert False
     if self.input_rhs == 'random':
         b = array_utils.uniform(self.shape, dtype)
     elif isinstance(self.input_rhs, (bool, int, float)):
         b = numpy.full(self.shape, self.input_rhs, dtype=dtype)
     else:
         assert False
     return a, b
Ejemplo n.º 12
0
 def generate_inputs(self):
     in_dtype1, in_dtype2 = self.in_dtypes
     in_shape1, in_shape2 = self.in_shapes
     if self.input_lhs == 'random':
         a = array_utils.uniform(in_shape1, in_dtype1)
     elif isinstance(self.input_lhs, (bool, int, float)):
         a = numpy.full(in_shape1, self.input_lhs, dtype=in_dtype1)
     else:
         assert False
     if self.input_rhs == 'random':
         b = array_utils.uniform(in_shape2, in_dtype2)
     elif isinstance(self.input_rhs, (bool, int, float)):
         b = numpy.full(in_shape2, self.input_rhs, dtype=in_dtype2)
     else:
         assert False
     return a, b
Ejemplo n.º 13
0
    def generate_inputs(self):
        h_shape = (self.n_layers * 2, self.batches[0], self.hidden_size)
        dtype = self.in_dtypes[0]
        low = -1.0
        high = 1.0
        if dtype == 'float16':
            low = -0.5
            high = 0.5

        h = array_utils.uniform(h_shape, dtype)
        in_size = self.input_size
        out_size = self.hidden_size
        xs = [
            array_utils.uniform((self.batches[b], in_size),
                                dtype,
                                low=low,
                                high=high) for b in range(len(self.batches))
        ]

        def w_in(i, j):
            if i == 0 and j < 1:
                return in_size
            elif i > 0 and j < 1:
                return out_size * 2
            else:
                return out_size

        inputs = []
        inputs.append(h)
        for i in range(len(self.batches)):
            inputs.append(xs[i])

        for n in range(self.n_layers):
            for direction in (0, 1):
                for i in range(2):
                    inputs.append(
                        array_utils.uniform((out_size, w_in(n, i)),
                                            dtype,
                                            low=low,
                                            high=high))
                for i in range(2):
                    inputs.append(
                        array_utils.uniform((out_size, ),
                                            dtype,
                                            low=low,
                                            high=high))
        return tuple(inputs)
Ejemplo n.º 14
0
 def generate_inputs(self):
     in_dtype, = self.in_dtypes
     if isinstance(self.input, numpy.ndarray):
         return self.input.astype(in_dtype),
     if self.input == 'random':
         return array_utils.uniform(self.shape, in_dtype),
     if isinstance(self.input, (bool, int, float)):
         return numpy.full(self.shape, self.input, dtype=in_dtype),
     assert False
Ejemplo n.º 15
0
 def generate_inputs(self):
     in_dtype, = self.in_dtypes
     if isinstance(self.input, numpy.ndarray):
         return self.input.astype(in_dtype),
     if self.input == 'random':
         return array_utils.uniform(self.shape, in_dtype),
     if isinstance(self.input, (bool, int, float)):
         return numpy.full(self.shape, self.input, dtype=in_dtype),
     assert False
Ejemplo n.º 16
0
def _random_condition(shape, dtype, *, random_state=None):
    if random_state is None:
        random_state = numpy.random.RandomState()
    neg_mask = random_state.randint(0, 2, size=shape).astype('bool')
    cond = array_utils.uniform(shape, dtype, random_state=random_state)
    # Replace zeros with nonzero, making the average number of zero elements
    # in cond independent of the dtype.
    cond[cond == 0] = 1
    cond[neg_mask] = 0
    return cond
Ejemplo n.º 17
0
 def generate_inputs(self):
     # Do not divide by small number to avoid ridiculously large outputs.
     if not self.is_scalar_rhs and self.input == 'random':
         in_dtype, = self.in_dtypes
         low = -5 if numpy.dtype(in_dtype).kind != 'u' else 2
         high = 5
         x = array_utils.uniform(self.shape, in_dtype, low=low, high=high)
         x[(-1 < x) & (x < 0)] = -2
         x[(0 <= x) & (x < 1)] = 2
         return x,
     return super().generate_inputs()
Ejemplo n.º 18
0
def test_power_invalid_bool_dtype(device, dtype, is_bool_rhs,
                                  is_bool_primitive, is_module):
    shape = (3, 2)

    a = chainerx.array(array_utils.uniform(shape, dtype))

    if is_bool_primitive:
        b = True
    else:
        b = chainerx.array(array_utils.uniform(shape, 'bool'))

    with pytest.raises(chainerx.DtypeError):
        if is_module:
            if is_bool_rhs:
                chainerx.power(a, b)
            else:
                chainerx.power(b, a)
        else:
            if is_bool_rhs:
                a**b
            else:
                b**a
Ejemplo n.º 19
0
 def generate_inputs(self):
     in_dtype, = self.in_dtypes
     a = array_utils.uniform(self.shape, in_dtype)
     return a,
Ejemplo n.º 20
0
 def generate_inputs(self):
     in_dtype, = self.in_dtypes
     if hasattr(self, 'array'):
         return self.array.astype(in_dtype),
     return array_utils.uniform(self.shape, in_dtype),
Ejemplo n.º 21
0
def test_moveaxis_invalid(xp, shape, source, dst):
    a = array_utils.uniform(shape, 'float')
    a = xp.array(a)
    return xp.moveaxis(a, source, dst)
Ejemplo n.º 22
0
 def generate_inputs(self):
     a = array_utils.uniform(self.shape, self.dtype)
     return a,
Ejemplo n.º 23
0
def test_square_invalid_dtypes(device):
    shape = (3, 2)
    bool_array = chainerx.array(array_utils.uniform(shape, 'bool_'))
    with pytest.raises(chainerx.DtypeError):
        chainerx.square(bool_array)
Ejemplo n.º 24
0
def _random_condition(shape, dtype):
    size = int(numpy.prod(shape))
    mask = numpy.random.randint(0, 1, size).astype('bool_').reshape(shape)
    pos = array_utils.uniform(shape, dtype)
    pos[numpy.logical_not(pos)] = True  # All elements are True
    return pos * mask
Ejemplo n.º 25
0
 def generate_inputs(self):
     shape = self.shape
     x1_dtype, x2_dtype = self.in_dtypes
     x1 = array_utils.uniform(shape, x1_dtype)
     x2 = array_utils.uniform(shape, x2_dtype)
     return x1, x2
Ejemplo n.º 26
0
    def generate_inputs(self):
        dst = array_utils.uniform((2, 3), 'float32')
        src = array_utils.uniform((2, 3), 'float32')

        return dst, src