def test_frombuffer_with_device(device): obj = array_utils.create_dummy_ndarray( numpy, (2, 3), 'int32', padding=False) a = chainerx.frombuffer(obj, obj.dtype, device=device) b = chainerx.frombuffer(obj, obj.dtype) chainerx.testing.assert_array_equal_ex(a, b) array_utils.check_device(a, device)
def test_frombuffer_from_device_buffer(device): dtype = 'int32' device_buffer = chainerx.testing._DeviceBuffer( [1, 2, 3, 4, 5, 6], (2, 3), dtype) a = chainerx.frombuffer(device_buffer, dtype) e = chainerx.array([1, 2, 3, 4, 5, 6], dtype) chainerx.testing.assert_array_equal_ex(e, a) assert a.device is chainerx.get_device(device)
def test_frombuffer_from_numpy_array(device): obj = array_utils.create_dummy_ndarray( numpy, (2, 3), 'int32', padding=False) a_chx = chainerx.frombuffer(obj, obj.dtype) a_np = numpy.frombuffer(obj, obj.dtype) chainerx.testing.assert_array_equal_ex(a_np, a_chx) assert a_chx.device is chainerx.get_device(device) # test buffer is shared obj += obj chainerx.testing.assert_array_equal_ex(obj.ravel(), a_chx) # test possibly freed memory obj_copy = obj.copy() del obj chainerx.testing.assert_array_equal_ex(obj_copy.ravel(), a_chx)
def test_frombuffer_from_numpy_array_with_cuda(device): obj = array_utils.create_dummy_ndarray(numpy, (2, 3), 'int32') with pytest.raises(chainerx.ChainerxError): chainerx.frombuffer(obj, obj.dtype)