Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Fix for gpu tensor reshape and add unit tests. Fixes #267.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stewart Hall authored and Jennifer Myers committed Jul 7, 2016
1 parent a01a2d9 commit 508c517
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion neon/backends/nervanagpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2963,7 +2963,7 @@ def _reshape_strides(orig_strides, orig_shape, new_shape):
orig_strides = tuple(list(orig_strides) + [1] * (len(new_shape) - len(orig_strides)))

reshape_size = np.prod(new_shape[matched_dims:])
orig_size = np.prod(orig_strides[matched_dims:]) * orig_shape[matched_dims]
orig_size = np.prod(orig_strides[matched_dims]) * orig_shape[matched_dims]

if orig_size != reshape_size:
raise ValueError("Reshaping of non-contiguous dimensions unsupported.")
Expand Down
41 changes: 41 additions & 0 deletions tests/test_backend_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,44 @@ def test_slicing(fargs_tests, backend_pair_dtype):
array_nc[0] = 0

assert tensors_allclose(array_ng, array_nc, rtol=0, atol=1e-3)


@pytest.mark.hasgpu
def test_reshape_separate(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]

gpu, cpu = backend_pair_dtype
dtype = gpu.default_dtype

array_np = np.random.uniform(-1, 1, dims).astype(dtype)
array_ng = gpu.array(array_np, dtype=dtype)
array_nc = cpu.array(array_np, dtype=dtype)

assert array_ng.is_contiguous

if (dims[0] % 2) == 0:
reshaped_ng = array_ng.reshape((2, dims[0] // 2, dims[1]))
reshaped_nc = array_nc.reshape((2, dims[0] // 2, dims[1]))

assert tensors_allclose(reshaped_ng, reshaped_nc, rtol=0, atol=1e-6)


@pytest.mark.hasgpu
def test_reshape_combine(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]

gpu, cpu = backend_pair_dtype
dtype = gpu.default_dtype

if (dims[0] % 2) == 0:
orig_shape = (2, dims[0] // 2, dims[1])
array_np = np.random.uniform(-1, 1, orig_shape).astype(dtype)
array_ng = gpu.array(array_np, dtype=dtype)
array_nc = cpu.array(array_np, dtype=dtype)

assert array_ng.is_contiguous

reshaped_ng = array_ng.reshape(dims)
reshaped_nc = array_nc.reshape(dims)

assert tensors_allclose(reshaped_ng, reshaped_nc, rtol=0, atol=1e-6)

0 comments on commit 508c517

Please sign in to comment.