def parametrize_dtype_specifier(argname, dtypes=None, additional_args=None): """Parametrizes a test with various arguments that can be used as dtypes. Args: argname(str): Argument name to pass the value that can be used as a dtype. dtypes(list of strs): List of dtype names. additional_args(tuple of list): Additional values to be included. """ if dtypes is None: dtypes = all_dtypes assert isinstance(argname, str) assert isinstance(dtypes, (tuple, list)) assert all(isinstance(dt, str) for dt in dtypes) lst = [] # dtype names lst += list(dtypes) # numpy dtypes lst += [numpy.dtype(dt) for dt in dtypes] # char codes lst += [chainerx.dtype(dt).char for dt in dtypes] # User-specified args if additional_args is not None: assert isinstance(additional_args, (tuple, list)) lst += list(additional_args) return pytest.mark.parametrize(argname, lst)
def test_array_from_tuple_or_list(xp, obj, dtype_spec, device): if xp is numpy and isinstance(dtype_spec, chainerx.dtype): dtype_spec = dtype_spec.name # Skip nan/inf -> integer conversion that would cause a cast error. if (not _is_all_finite(obj) and dtype_spec not in (None, Unspecified) and chainerx.dtype(dtype_spec).kind not in ('f', 'c')): return chainerx.testing.ignore() if dtype_spec is Unspecified: return xp.array(obj) else: return xp.array(obj, dtype_spec)
def _check_array_from_chainerx_array_with_dtype( shape, src_dtype, dst_dtype_spec, copy, device=None): t = array_utils.create_dummy_ndarray( chainerx, shape, src_dtype, device=device) a = chainerx.array(t, dtype=dst_dtype_spec, copy=copy) src_dtype = chainerx.dtype(src_dtype) dst_dtype = src_dtype if dst_dtype_spec is None else chainerx.dtype( dst_dtype_spec) device = chainerx.get_device(device) if (not copy and src_dtype == dst_dtype and device is chainerx.get_default_device()): assert t is a else: assert t is not a chainerx.testing.assert_array_equal_ex(a, t.astype(dst_dtype)) assert a.dtype == dst_dtype assert a.device is chainerx.get_default_device()
def test_array_from_chainerx_array_with_device( src_dtype, dst_dtype, copy, device, dst_device_spec): t = array_utils.create_dummy_ndarray( chainerx, (2,), src_dtype, device=device) a = chainerx.array(t, dtype=dst_dtype, copy=copy, device=dst_device_spec) dst_device = chainerx.get_device(dst_device_spec) if not copy and src_dtype == dst_dtype and device is dst_device: assert t is a else: assert t is not a chainerx.testing.assert_array_equal_ex( a, t.to_device(dst_device).astype(dst_dtype)) assert a.dtype == chainerx.dtype(dst_dtype) assert a.device is dst_device
def _check_array( array, expected_dtype, expected_shape, expected_data_list=None, device=None): expected_dtype = chainerx.dtype(expected_dtype) assert isinstance(array.dtype, chainerx.dtype) assert isinstance(array.shape, tuple) assert array.dtype == expected_dtype assert array.shape == expected_shape assert array.itemsize == expected_dtype.itemsize assert array.size == array_utils.total_size(expected_shape) assert array.nbytes == expected_dtype.itemsize * \ array_utils.total_size(expected_shape) if expected_data_list is not None: assert array._debug_flat_data == expected_data_list assert array.is_contiguous array_utils.check_device(array, device)
def test_asarray_from_chainerx_array_with_device(src_dtype, dst_dtype, device, dst_device_spec): t = array_utils.create_dummy_ndarray(chainerx, (2, ), src_dtype, device=device) a = chainerx.asarray(t, dtype=dst_dtype, device=dst_device_spec) dst_device = chainerx.get_device(dst_device_spec) if ((dst_dtype is None or src_dtype == dst_dtype) and (dst_device_spec is None or device is dst_device)): assert t is a else: assert t is not a if dst_dtype is None: dst_dtype = t.dtype chainerx.testing.assert_array_equal_ex( a, t.to_device(dst_device).astype(dst_dtype)) assert a.dtype == chainerx.dtype(dst_dtype) assert a.device is dst_device
def test_parametrize_dtype_specifier_with_dtypes(spec): assert chainerx.dtype(spec).name in ('int32', 'float64')
def create_dummy_ndarray(xp, shape, dtype, device=None, pattern=1, padding=True, start=None): dtype = chainerx.dtype(dtype).name size = total_size(shape) if dtype in ('bool', 'bool_'): if pattern == 1: data = [i % 2 == 1 for i in range(size)] else: data = [i % 3 == 0 for i in range(size)] else: if start is None: if dtype in chainerx.testing.unsigned_dtypes: start = 0 if pattern == 1 else 1 else: start = -1 if pattern == 1 else -2 data = list(range(start, size + start)) if padding is True: padding = 1 elif padding is False: padding = 0 # Unpadded array a_unpad = numpy.array(data, dtype=dtype).reshape(shape) if padding == 0: a_np = a_unpad else: # Create possibly padded (non-contiguous) array. # Elements in each axis will be spaced with corresponding padding. # The padding for axis `i` is computed as `itemsize * padding[i]`. if numpy.isscalar(padding): padding = (padding, ) * len(shape) assert len(padding) == len(shape) # Allocate 1-dim raw buffer buf_nitems = 1 for dim, pad in zip((1, ) + shape[::-1], padding[::-1] + (0, )): buf_nitems = buf_nitems * dim + pad # intentionally using uninitialized padding values buf_a = numpy.empty((buf_nitems, ), dtype=dtype) # Compute strides strides = [] st = 1 itemsize = buf_a.itemsize for dim, pad in zip(shape[::-1], padding[::-1]): st += pad strides.append(st * itemsize) st *= dim strides = tuple(strides[::-1]) # Create strided array and copy data a_np = numpy.asarray( numpy.lib.stride_tricks.as_strided(buf_a, shape, strides)) a_np[...] = a_unpad numpy.testing.assert_array_equal(a_np, a_unpad) # Convert to NumPy or chainerx array if xp is chainerx: a = chainerx.testing._fromnumpy(a_np, keepstrides=True, device=device) assert a.strides == a_np.strides else: a = a_np # Checks if padding == 0 or all(pad == 0 for pad in padding): if xp is chainerx: assert a.is_contiguous else: assert a.flags.c_contiguous assert a.shape == shape assert a.dtype.name == dtype return a
def _is_bool_spec(dtype_spec): # Used in arange tests if dtype_spec is None: return False return chainerx.dtype(dtype_spec) == chainerx.bool_
def test_init_with_dtype(value, dtype_spec): expected_dtype = chainerx.dtype(dtype_spec) scalar = chainerx.Scalar(value, dtype_spec) assert scalar.dtype == expected_dtype assert scalar == chainerx.Scalar(value, expected_dtype)
def create_dummy_ndarray( xp, shape, dtype, device=None, pattern=1, padding=True, start=None): dtype = chainerx.dtype(dtype).name size = total_size(shape) if dtype in ('bool', 'bool_'): if pattern == 1: data = [i % 2 == 1 for i in range(size)] else: data = [i % 3 == 0 for i in range(size)] else: if start is None: if dtype in chainerx.testing.unsigned_dtypes: start = 0 if pattern == 1 else 1 else: start = -1 if pattern == 1 else -2 data = list(range(start, size + start)) if padding is True: padding = 1 elif padding is False: padding = 0 # Unpadded array a_unpad = numpy.array(data, dtype=dtype).reshape(shape) if padding == 0: a_np = a_unpad else: # Create possibly padded (non-contiguous) array. # Elements in each axis will be spaced with corresponding padding. # The padding for axis `i` is computed as `itemsize * padding[i]`. if numpy.isscalar(padding): padding = (padding,) * len(shape) assert len(padding) == len(shape) # Allocate 1-dim raw buffer buf_nitems = 1 for dim, pad in zip((1,) + shape[::-1], padding[::-1] + (0,)): buf_nitems = buf_nitems * dim + pad # intentionally using uninitialized padding values buf_a = numpy.empty((buf_nitems,), dtype=dtype) # Compute strides strides = [] st = 1 itemsize = buf_a.itemsize for dim, pad in zip(shape[::-1], padding[::-1]): st += pad strides.append(st * itemsize) st *= dim strides = tuple(strides[::-1]) # Create strided array and copy data a_np = numpy.asarray( numpy.lib.stride_tricks.as_strided(buf_a, shape, strides)) a_np[...] = a_unpad numpy.testing.assert_array_equal(a_np, a_unpad) # Convert to NumPy or chainerx array if xp is chainerx: a = chainerx.testing._fromnumpy(a_np, keepstrides=True, device=device) assert a.strides == a_np.strides else: a = a_np # Checks if padding == 0 or all(pad == 0 for pad in padding): if xp is chainerx: assert a.is_contiguous else: assert a.flags.c_contiguous assert a.shape == shape assert a.dtype.name == dtype return a