def test_batch_norm(device, x_shape, reduced_shape, eps, decay, axis, float_dtype): def create_args(xp): return _create_batch_norm_ndarray_args(xp, device, x_shape, reduced_shape, reduced_shape, reduced_shape, reduced_shape, float_dtype) x_chx, gamma_chx, beta_chx, running_mean_chx, running_var_chx = ( create_args(chainerx)) x_np, gamma_np, beta_np, running_mean_np, running_var_np = ( create_args(numpy)) # Save copies of running values before updating to later check that they # are updated. initial_running_mean = running_mean_chx.copy() initial_running_var = running_var_chx.copy() optional_args = {} if eps is not None: optional_args['eps'] = eps if decay is not None: optional_args['decay'] = decay if axis is not None: optional_args['axis'] = axis y_chx = chainerx.batch_norm(x_chx, gamma_chx, beta_chx, running_mean=running_mean_chx, running_var=running_var_chx, **optional_args) y_np = chainer.functions.batch_normalization(x_np, gamma_np, beta_np, running_mean=running_mean_np, running_var=running_var_np, **optional_args).data # Check that the running values are updated. assert not numpy.allclose(chainerx.to_numpy(initial_running_mean), chainerx.to_numpy(running_mean_chx)) assert not numpy.allclose(chainerx.to_numpy(initial_running_var), chainerx.to_numpy(running_var_chx)) chainerx.testing.assert_allclose_ex(y_chx, y_np, rtol=1e-6, atol=1e-5) chainerx.testing.assert_allclose_ex(running_mean_chx, running_mean_np, rtol=1e-6, atol=1e-6) chainerx.testing.assert_allclose_ex(running_var_chx, running_var_np, rtol=1e-6, atol=1e-6)
def new_evaluate(self): local_mean_dict = self._mn_original_evaluate() # ChainerX support: # We need convert chainerx ndarray to Native array because # (1) allreduce_obj is used to compute global mean values, since # a simple allreduce operation cannot be applied in evaluation. # (2) allreduce_obj calls mpi4py.allreduce, which pickles the object # (3) chainerx.ndarray preserves CUDA device internally when pickled # (4) An error will occur when an ndarray is unpickled in another # process arrays = list(local_mean_dict.values()) if len(arrays) > 0: array0 = list(local_mean_dict.values())[0] xp = backend.get_array_module(array0) if xp == chx and array0.device.backend.name == 'cuda': # Results of evaluation is fairly small, so # the ndarray is transferred to CPU and allreduce()-ed. # NOTE: Matrices for evaluation are transferred to the # host memory and sent via MPI instead of NCCL. # Although evaluation matrices are small in most cases, # this is a potential performance issue. local_mean_dict = { name: chx.to_numpy(value) for name, value in local_mean_dict.items() } global_mean_dict = { name: self._mn_communicator.allreduce_obj(value) / self._mn_communicator.size for name, value in sorted(local_mean_dict.items()) } return global_mean_dict
def __call__(self, key, value): if self.group is None: if not self.strict: return value else: raise ValueError('Inexistent group is specified') if not self.strict and key not in self.group: return value dataset = self.group[key] if dataset.shape is None: # Empty return None if value is None: return numpy.asarray(dataset) if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) dataset.read_direct(value_view) elif isinstance(value, numpy.ndarray): dataset.read_direct(value) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value = type(value)(numpy.asarray(dataset)) return value
def __call__(self, key, value): if self.group is None: if not self.strict: return value else: raise ValueError('Inexistent group is specified') if not self.strict and key not in self.group: return value dataset = self.group[key] if dataset.shape is None: # Empty return None if value is None: return numpy.asarray(dataset) if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) dataset.read_direct(value_view) elif isinstance(value, numpy.ndarray): dataset.read_direct(value) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value = type(value)(numpy.asarray(dataset)) return value
def test_batch_norm( device, x_shape, reduced_shape, eps, decay, axis, float_dtype): def create_args(xp): return _create_batch_norm_ndarray_args( xp, device, x_shape, reduced_shape, reduced_shape, reduced_shape, reduced_shape, float_dtype) x_chx, gamma_chx, beta_chx, running_mean_chx, running_var_chx = ( create_args(chainerx)) x_np, gamma_np, beta_np, running_mean_np, running_var_np = ( create_args(numpy)) # Save copies of running values before updating to later check that they # are updated. initial_running_mean = running_mean_chx.copy() initial_running_var = running_var_chx.copy() optional_args = {} if eps is not None: optional_args['eps'] = eps if decay is not None: optional_args['decay'] = decay if axis is not None: optional_args['axis'] = axis y_chx = chainerx.batch_norm( x_chx, gamma_chx, beta_chx, running_mean=running_mean_chx, running_var=running_var_chx, **optional_args) y_np = chainer.functions.batch_normalization( x_np, gamma_np, beta_np, running_mean=running_mean_np, running_var=running_var_np, **optional_args).data # Check that the running values are updated. assert not numpy.allclose(chainerx.to_numpy( initial_running_mean), chainerx.to_numpy(running_mean_chx)) assert not numpy.allclose(chainerx.to_numpy( initial_running_var), chainerx.to_numpy(running_var_chx)) chainerx.testing.assert_allclose_ex( y_chx, y_np, rtol=1e-6, atol=1e-5, float16_rtol=1e-2, float16_atol=1e-2) chainerx.testing.assert_allclose_ex( running_mean_chx, running_mean_np, rtol=1e-6, atol=1e-6, float16_rtol=1e-2, float16_atol=1e-2) chainerx.testing.assert_allclose_ex( running_var_chx, running_var_np, rtol=1e-6, atol=1e-6, float16_rtol=1e-2, float16_atol=1e-2)
def _check_array_from_numpy_array(a_chx, a_np, device=None): assert a_chx.offset == 0 array_utils.check_device(a_chx, device) # recovered data should be equal a_np_recovered = chainerx.to_numpy(a_chx) chainerx.testing.assert_array_equal_ex( a_chx, a_np_recovered, strides_check=False)
def _check_to_numpy(a_np, a_chx, device, copy): chainerx.testing.assert_array_equal_ex(a_chx, a_np, strides_check=False) if a_np.size > 0: # test buffer is shared or not a_np.fill(1) expected = not copy and device.backend.name == 'native' actual = numpy.array_equal(a_np, chainerx.to_numpy(a_chx)) assert expected == actual
def _check_array_from_numpy_array(a_chx, a_np, device=None): assert a_chx.offset == 0 array_utils.check_device(a_chx, device) # recovered data should be equal a_np_recovered = chainerx.to_numpy(a_chx) chainerx.testing.assert_array_equal_ex( a_chx, a_np_recovered, strides_check=False)
def _check_to_numpy(a_np, a_chx, device, copy): chainerx.testing.assert_array_equal_ex(a_chx, a_np, strides_check=False) if a_np.size > 0: # test buffer is shared or not a_np.fill(1) expected = not copy and device.backend.name == 'native' actual = numpy.array_equal(a_np, chainerx.to_numpy(a_chx)) assert expected == actual
def test_asarray_to_numpy_identity(device, slice1, slice2): start1, end1, step1 = slice1 start2, end2, step2 = slice2 x = numpy.arange(1500).reshape((30, 50))[ start1:end1:step1, start2:end2:step2] y = chainerx.asarray(x) z = chainerx.to_numpy(y) chainerx.testing.assert_array_equal_ex(x, y) chainerx.testing.assert_array_equal_ex(x, z, strides_check=False)
def test_asarray_to_numpy_identity(device, slice1, slice2): start1, end1, step1 = slice1 start2, end2, step2 = slice2 x = numpy.arange(1500).reshape((30, 50))[ start1:end1:step1, start2:end2:step2] y = chainerx.asarray(x) z = chainerx.to_numpy(y) chainerx.testing.assert_array_equal_ex(x, y) chainerx.testing.assert_array_equal_ex(x, z, strides_check=False)
def __call__(self, key, value): key = self.path + key.lstrip('/') if not self.strict and key not in self.npz: return value if isinstance(self.ignore_names, (tuple, list)): ignore_names = self.ignore_names else: ignore_names = (self.ignore_names, ) for ignore_name in ignore_names: if isinstance(ignore_name, str): if key == ignore_name: return value elif callable(ignore_name): if ignore_name(key): return value else: raise ValueError( 'ignore_names needs to be a callable, string or ' 'list of them.') dataset = self.npz[key] if dataset[()] is None: return None if value is None: return dataset if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) numpy.copyto(value_view, dataset) elif isinstance(value, numpy.ndarray): numpy.copyto(value, dataset) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value_type = type(value) dataset_arr = numpy.asarray(dataset) if (issubclass(dataset_arr.dtype.type, numpy.number) and not (issubclass(dataset_arr.dtype.type, numpy.integer) and value_type in six.integer_types) # Casting a `numpy.integer` scalar by `int()` case above is # safe as `int()` gives unlimited precision integer (it's # also true for `long()`/`int()` on Python 2). For such a # case, the check below may be too strict. For example, # `numpy.can_cast(numpy.int64, int)`, which checks cast- # ability to `dtype(int)`, gives `False` on a platform # whose `dtype(int)` is `numpy.int32` like Windows/x64. and not numpy.can_cast(dataset_arr.dtype, value_type, casting='safe')): raise TypeError( 'Cannot safely deserialize from numpy array with dtype={} ' 'into a variable of type {}.'.format( dataset.dtype, type(value))) value = value_type(dataset_arr) return value
def __call__(self, key, value): key = self.path + key.lstrip('/') if not self.strict and key not in self.npz: return value if isinstance(self.ignore_names, (tuple, list)): ignore_names = self.ignore_names else: ignore_names = (self.ignore_names,) for ignore_name in ignore_names: if isinstance(ignore_name, str): if key == ignore_name: return value elif callable(ignore_name): if ignore_name(key): return value else: raise ValueError( 'ignore_names needs to be a callable, string or ' 'list of them.') dataset = self.npz[key] if dataset[()] is None: return None if value is None: return dataset if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) numpy.copyto(value_view, dataset) elif isinstance(value, numpy.ndarray): numpy.copyto(value, dataset) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value_type = type(value) dataset_arr = numpy.asarray(dataset) if (issubclass(dataset_arr.dtype.type, numpy.number) and not (issubclass(dataset_arr.dtype.type, numpy.integer) and value_type in six.integer_types) # Casting a `numpy.integer` scalar by `int()` case above is # safe as `int()` gives unlimited precision integer (it's # also true for `long()`/`int()` on Python 2). For such a # case, the check below may be too strict. For example, # `numpy.can_cast(numpy.int64, int)`, which checks cast- # ability to `dtype(int)`, gives `False` on a platform # whose `dtype(int)` is `numpy.int32` like Windows/x64. and not numpy.can_cast( dataset_arr.dtype, value_type, casting='safe')): raise TypeError( 'Cannot safely deserialize from numpy array with dtype={} ' 'into a variable of type {}.'.format( dataset.dtype, type(value))) value = value_type(dataset_arr) return value
def test_asarray_from_numpy_array_with_copy(): obj = array_utils.create_dummy_ndarray(numpy, (2, 3), 'int32') a = chainerx.asarray(obj, dtype='float32') e = chainerx.array(obj, dtype='float32', copy=False) chainerx.testing.assert_array_equal_ex(e, a) assert e.device is a.device # test buffer is not shared a += a assert not numpy.array_equal(obj, chainerx.to_numpy(a))
def chainerx_max_pool(): y = chainerx.max_pool(**create_args(chainerx)) # In the case of CUDA, we get huge negative numbers instead of -inf # around boundaries. # Align them to chainer (native) results. if device.backend.name == 'cuda': y = chainerx.to_numpy(y) y[y < -3.e+34] = -float('inf') y = chainerx.array(y) return y
def test_asarray_from_numpy_array_with_copy(): obj = array_utils.create_dummy_ndarray(numpy, (2, 3), 'int32') a = chainerx.asarray(obj, dtype='float32') e = chainerx.array(obj, dtype='float32', copy=False) chainerx.testing.assert_array_equal_ex(e, a) assert e.device is a.device # test buffer is not shared a += a assert not numpy.array_equal(obj, chainerx.to_numpy(a))
def chainerx_max_pool(): y = chainerx.max_pool(**create_args(chainerx)) # In the case of CUDA, we get huge negative numbers instead of -inf # around boundaries. # Align them to chainer (native) results. if device.backend.name == 'cuda': y = chainerx.to_numpy(y) y[y < -3.e+34] = -float('inf') y = chainerx.array(y) return y
def _array_to_gpu(array, device, stream): if array is None: return None if isinstance(array, chainerx.ndarray): # TODO(niboshi): Update this logic once both CuPy and ChainerX support # the array interface. if array.device.backend.name == 'cuda': # Convert to cupy.ndarray on the same device as source array array = cupy.ndarray( array.shape, array.dtype, cupy.cuda.MemoryPointer( cupy.cuda.UnownedMemory( array.data_ptr + array.offset, array.data_size, array, array.device.index), 0), strides=array.strides) else: array = chainerx.to_numpy(array) elif isinstance(array, (numpy.number, numpy.bool_)): array = numpy.asarray(array) elif isinstance(array, intel64.mdarray): array = numpy.asarray(array) if isinstance(array, ndarray): if array.device == device: return array is_numpy = False elif isinstance(array, numpy.ndarray): is_numpy = True else: raise TypeError( 'The array sent to gpu must be an array or a NumPy scalar.' '\nActual type: {0}.'.format(type(array))) if stream is not None: with device: with stream: if is_numpy: return cupy.asarray(array) # Need to make a copy when an array is copied to another device return cupy.array(array, copy=True) with device: if is_numpy: return cupy.asarray(array) # Need to make a copy when an array is copied to another device return cupy.array(array, copy=True)
def _array_to_gpu(array, device, stream): if array is None: return None if isinstance(array, chainerx.ndarray): # TODO(niboshi): Update this logic once both CuPy and ChainerX support # the array interface. if array.device.backend.name == 'cuda': # Convert to cupy.ndarray on the same device as source array array = cupy.ndarray( array.shape, array.dtype, cupy.cuda.MemoryPointer( cupy.cuda.UnownedMemory( array.data_ptr + array.offset, array.data_size, array, array.device.index), 0), strides=array.strides) else: array = chainerx.to_numpy(array) elif isinstance(array, (numpy.number, numpy.bool_)): array = numpy.asarray(array) elif isinstance(array, intel64.mdarray): array = numpy.asarray(array) if isinstance(array, ndarray): if array.device == device: return array is_numpy = False elif isinstance(array, numpy.ndarray): is_numpy = True else: raise TypeError( 'The array sent to gpu must be an array or a NumPy scalar.' '\nActual type: {0}.'.format(type(array))) if stream is not None: with device: with stream: if is_numpy: return cupy.asarray(array) # Need to make a copy when an array is copied to another device return cupy.array(array, copy=True) with device: if is_numpy: return cupy.asarray(array) # Need to make a copy when an array is copied to another device return cupy.array(array, copy=True)
def _array_to_cpu(array): if array is None: return None if isinstance(array, numpy.ndarray): return array if isinstance(array, chainer.backends.intel64.mdarray): return numpy.asarray(array) if isinstance(array, chainerx.ndarray): return chainerx.to_numpy(array, copy=False) if isinstance(array, chainer.backends.cuda.ndarray): with chainer.backends.cuda.get_device_from_array(array): return array.get() if numpy.isscalar(array): return numpy.asarray(array) raise TypeError('Array cannot be converted into an numpy.ndarray' '\nActual type: {0}.'.format(type(array)))
def __call__(self, key, value): key = self.path + key.lstrip('/') if not self.strict and key not in self.npz: return value if isinstance(self.ignore_names, (tuple, list)): ignore_names = self.ignore_names else: ignore_names = (self.ignore_names, ) for ignore_name in ignore_names: if isinstance(ignore_name, str): if key == ignore_name: return value elif callable(ignore_name): if ignore_name(key): return value else: raise ValueError( 'ignore_names needs to be a callable, string or ' 'list of them.') dataset = self.npz[key] if dataset[()] is None: return None if value is None: return dataset if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) numpy.copyto(value_view, dataset) elif isinstance(value, numpy.ndarray): numpy.copyto(value, dataset) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value_type = type(value) dataset_arr = numpy.asarray(dataset) if (issubclass(dataset_arr.dtype.type, numpy.number) and not numpy.can_cast( dataset_arr.dtype, value_type, casting='safe')): raise TypeError( 'Cannot safely deserialize from numpy array with dtype={} ' 'into a variable of type {}.'.format( dataset.dtype, type(value))) value = value_type(dataset_arr) return value
def __call__(self, key, value): key = self.path + key.lstrip('/') if not self.strict and key not in self.npz: return value if isinstance(self.ignore_names, (tuple, list)): ignore_names = self.ignore_names else: ignore_names = (self.ignore_names,) for ignore_name in ignore_names: if isinstance(ignore_name, str): if key == ignore_name: return value elif callable(ignore_name): if ignore_name(key): return value else: raise ValueError( 'ignore_names needs to be a callable, string or ' 'list of them.') dataset = self.npz[key] if dataset[()] is None: return None if value is None: return dataset if isinstance(value, chainerx.ndarray): value_view = chainerx.to_numpy(value, copy=False) numpy.copyto(value_view, dataset) elif isinstance(value, numpy.ndarray): numpy.copyto(value, dataset) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset, dtype=value.dtype)) elif isinstance(value, intel64.mdarray): intel64.ideep.basic_copyto(value, numpy.asarray(dataset)) else: value_type = type(value) dataset_arr = numpy.asarray(dataset) if (issubclass(dataset_arr.dtype.type, numpy.number) and not numpy.can_cast( dataset_arr.dtype, value_type, casting='safe')): raise TypeError( 'Cannot safely deserialize from numpy array with dtype={} ' 'into a variable of type {}.'.format( dataset.dtype, type(value))) value = value_type(dataset_arr) return value
def _array_to_cpu(array): if array is None: return None if isinstance(array, numpy.ndarray): return array if isinstance(array, chainer.backends.intel64.mdarray): return numpy.asarray(array) if isinstance(array, chainerx.ndarray): return chainerx.to_numpy(array, copy=False) if isinstance(array, chainer.backends.cuda.ndarray): with chainer.backends.cuda.get_device_from_array(array): return array.get() if numpy.isscalar(array): return numpy.asarray(array) raise TypeError( 'Array cannot be converted into an numpy.ndarray' '\nActual type: {0}.'.format(type(array)))
def test_dummy_ndarray(xp, device, shape, dtype, pattern, padding): a = array_utils.create_dummy_ndarray(xp, shape, dtype, device=device, pattern=pattern, padding=padding) assert isinstance(a, xp.ndarray) assert a.dtype == xp.dtype(dtype) assert a.shape == shape # Check values if xp is chainerx: a_np = chainerx.to_numpy(a) else: a_np = a if pattern == 1: if a.dtype.name == 'bool': expected_data = [i % 2 == 1 for i in range(a.size)] elif a.dtype.name in chainerx.testing.unsigned_dtypes: expected_data = list(range(a.size)) else: expected_data = list(range(-1, a.size - 1)) else: if a.dtype.name == 'bool': expected_data = [i % 3 == 0 for i in range(a.size)] elif a.dtype.name in chainerx.testing.unsigned_dtypes: expected_data = list(range(1, a.size + 1)) else: expected_data = list(range(-2, a.size - 2)) numpy.testing.assert_equal(a_np.ravel(), expected_data) # Check strides if xp is chainerx: assert a.device is device if not padding: if xp is chainerx: assert a.is_contiguous else: assert a.flags.c_contiguous
def test_dummy_ndarray(xp, device, shape, dtype, pattern, padding): a = array_utils.create_dummy_ndarray( xp, shape, dtype, device=device, pattern=pattern, padding=padding) assert isinstance(a, xp.ndarray) assert a.dtype == xp.dtype(dtype) assert a.shape == shape # Check values if xp is chainerx: a_np = chainerx.to_numpy(a) else: a_np = a if pattern == 1: if a.dtype.name == 'bool': expected_data = [i % 2 == 1 for i in range(a.size)] elif a.dtype.name in chainerx.testing.unsigned_dtypes: expected_data = list(range(a.size)) else: expected_data = list(range(-1, a.size - 1)) else: if a.dtype.name == 'bool': expected_data = [i % 3 == 0 for i in range(a.size)] elif a.dtype.name in chainerx.testing.unsigned_dtypes: expected_data = list(range(1, a.size + 1)) else: expected_data = list(range(-2, a.size - 2)) numpy.testing.assert_equal(a_np.ravel(), expected_data) # Check strides if xp is chainerx: assert a.device is device if not padding: if xp is chainerx: assert a.is_contiguous else: assert a.flags.c_contiguous
def _to_numpy(array): assert isinstance(array, chainerx.ndarray) return chainerx.to_numpy(array, copy=False)
def random_choice(device, a, size, p): if device.xp is chainerx: return device.send( numpy.random.choice(a, size=size, p=chainerx.to_numpy(p))) return device.xp.random.choice(a, size=size, p=p)
def _to_numpy(array): assert isinstance(array, chainerx.ndarray) return chainerx.to_numpy(array, copy=False)
def _as_numpy(x): if isinstance(x, chainerx.ndarray): return chainerx.to_numpy(x) assert isinstance(x, numpy.ndarray) or numpy.isscalar(x) return x
def _array_to_gpu(array, device, stream): if array is None: return None if isinstance(array, chainerx.ndarray): # TODO(niboshi): Update this logic once both CuPy and ChainerX support # the array interface. if array.device.backend.name == 'cuda': # Convert to cupy.ndarray on the same device as source array array = cupy.ndarray( array.shape, array.dtype, cupy.cuda.MemoryPointer( cupy.cuda.UnownedMemory( array.data_ptr + array.offset, array.data_size, array, array.device.index), 0), strides=array.strides) else: array = chainerx.to_numpy(array) elif isinstance(array, (numpy.number, numpy.bool_)): array = numpy.asarray(array) elif isinstance(array, intel64.mdarray): array = numpy.asarray(array) if isinstance(array, ndarray): if array.device == device: return array is_numpy = False elif isinstance(array, numpy.ndarray): is_numpy = True else: raise TypeError( 'The array sent to gpu must be an array or a NumPy scalar.' '\nActual type: {0}.'.format(type(array))) if stream is not None and stream.ptr != 0: ret = cupy.empty_like(array) if is_numpy: # cpu to gpu mem = cupy.cuda.alloc_pinned_memory(array.nbytes) src = numpy.frombuffer( mem, array.dtype, array.size).reshape(array.shape) src[...] = array ret.set(src, stream) cupy.cuda.pinned_memory._add_to_watch_list( stream.record(), mem) else: # gpu to gpu with array.device: src = array.copy() event = Stream.null.record() stream.wait_event(event) ret.data.copy_from_device_async( src.data, src.nbytes, stream) # to hold a reference until the end of the asynchronous # memcpy stream.add_callback(lambda *x: None, (src, ret)) return ret with device: if is_numpy: return cupy.asarray(array) # Need to make a copy when an array is copied to another device return cupy.array(array, copy=True)
def test_to_numpy_non_contiguous(shape, dtype, device, copy): a_chx = array_utils.create_dummy_ndarray(chainerx, shape, dtype).T a_np = chainerx.to_numpy(a_chx, copy) _check_to_numpy(a_np, a_chx, device, copy)
def test_to_numpy_positive_offset(device, copy): a_chx = chainerx.arange(6).reshape(2, 3)[:, 1:] a_np = chainerx.to_numpy(a_chx, copy) _check_to_numpy(a_np, a_chx, device, copy)
def test_to_numpy_non_contiguous(shape, dtype, device, copy): a_chx = array_utils.create_dummy_ndarray(chainerx, shape, dtype).T a_np = chainerx.to_numpy(a_chx, copy) _check_to_numpy(a_np, a_chx, device, copy)
def random_choice(device, a, size, p): if device.xp is chainerx: return device.send( numpy.random.choice(a, size=size, p=chainerx.to_numpy(p))) return device.xp.random.choice(a, size=size, p=p)
def test_to_numpy_positive_offset(device, copy): a_chx = chainerx.arange(6).reshape(2, 3)[:, 1:] a_np = chainerx.to_numpy(a_chx, copy) _check_to_numpy(a_np, a_chx, device, copy)
def _as_numpy(x): if isinstance(x, chainerx.ndarray): return chainerx.to_numpy(x) assert isinstance(x, numpy.ndarray) or numpy.isscalar(x) return x