def test_correct_double_backward_unary(): chainerx.check_double_backward( lambda xs: (xs[0] * xs[0],), (chainerx.array([1, 2, 3], chainerx.float32).require_grad(),), (chainerx.ones((3,), chainerx.float32).require_grad(),), (chainerx.ones((3,), chainerx.float32),), (chainerx.full((3,), 1e-3, chainerx.float32), chainerx.full((3,), 1e-3, chainerx.float32)), 1e-4, 1e-3, )
def test_array_grad_invalid_grad(invalid_shape, invalid_dtype, invalid_device): shape = (3, 1) dtype = chainerx.float64 device = 'native:0' array = chainerx.ones(shape, dtype, device=device) array.require_grad() grad_shape = shape if invalid_shape is None else invalid_shape grad_dtype = dtype if invalid_dtype is None else invalid_dtype grad_device = device if invalid_device is None else invalid_device invalid_grad = chainerx.ones(grad_shape, grad_dtype, device=grad_device) with pytest.raises(chainerx.GradientError): array.set_grad(invalid_grad) with pytest.raises(chainerx.GradientError): array.grad = invalid_grad
def test_cast_scalar_invalid(device, shape): dtype = chainerx.float32 a = chainerx.ones(shape, dtype) with pytest.raises(chainerx.DimensionError): float(a) a = chainerx.ones(shape, dtype) with pytest.raises(chainerx.DimensionError): int(a) a = chainerx.ones(shape, dtype) with pytest.raises(chainerx.DimensionError): bool(a) a = chainerx.ones(shape, dtype) with pytest.raises(chainerx.DimensionError): a.item()
def test_array_grad_invalid_grad(invalid_shape, invalid_dtype, invalid_device): shape = (3, 1) dtype = chainerx.float64 device = 'native:0' array = chainerx.ones(shape, dtype, device=device) array.require_grad() grad_shape = shape if invalid_shape is None else invalid_shape grad_dtype = dtype if invalid_dtype is None else invalid_dtype grad_device = device if invalid_device is None else invalid_device invalid_grad = chainerx.ones( grad_shape, grad_dtype, device=grad_device) with pytest.raises(chainerx.GradientError): array.set_grad(invalid_grad) with pytest.raises(chainerx.GradientError): array.grad = invalid_grad
def test_to_device(): a = chainerx.ones((2,), chainerx.float32, device='native:0') dst_device = chainerx.get_device('native:1') b0 = a.to_device(dst_device) # by device instance assert b0.device is dst_device chainerx.testing.assert_array_equal_ex(a, b0) b1 = a.to_device('native:1') # by device name assert b1.device is dst_device chainerx.testing.assert_array_equal_ex(a, b1) b2 = a.to_device('native', 1) # by backend name and index assert b2.device is dst_device chainerx.testing.assert_array_equal_ex(a, b2)
def chx_ones(*args, **kwargs): return chx.ones(*args, **kwargs, device='cuda:0')
def test_ones_with_device(device): a = chainerx.ones((2,), 'float32', device) b = chainerx.ones((2,), 'float32') array_utils.check_device(a, device) chainerx.testing.assert_array_equal_ex(a, b)
def orig_chainerx(self, device_name): return chainerx.ones((2, 3), numpy.float32, device=device_name)
def __init__(self, size, dtype=chx.float32): shape = size, self.avg_mean = chx.zeros(shape, dtype) self.avg_var = chx.zeros(shape, dtype) self.gamma = chx.ones(shape, dtype) self.beta = chx.zeros(shape, dtype)