Skip to content

Commit

Permalink
Merge branch 'lwawrzyniak/fix-typestr-for-bytes' into 'main'
Browse files Browse the repository at this point in the history
Fixed array interface type strings

See merge request omniverse/warp!756
  • Loading branch information
nvlukasz committed Sep 27, 2024
2 parents fbbcf58 + 93ea7bb commit e18931f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- Fix a crash when kernel functions are not found in CPU modules.
- Fix conditions not being evaluated as expected in `while` statements.
- Fix printing Boolean and 8-bit integer values.
- Fix array interface type strings used for Boolean and 8-bit integer values.

## [1.3.3] - 2024-09-04

Expand Down
20 changes: 20 additions & 0 deletions warp/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,25 @@ def test_array_from_int64_domain(test, device):
wp.zeros(np.array([1504, 1080, 520], dtype=np.int64), dtype=wp.float32, device=device)


def test_numpy_array_interface(test, device):
# We should be able to convert between NumPy and Warp arrays using __array_interface__ on CPU.
# This tests all scalar types supported by both.

n = 10

scalar_types = wp.types.scalar_types

for dtype in scalar_types:
# test round trip
a1 = wp.zeros(n, dtype=dtype, device="cpu")
na = np.array(a1)
a2 = wp.array(na, device="cpu")

assert a1.dtype == a2.dtype
assert a1.shape == a2.shape
assert a1.strides == a2.strides


devices = get_test_devices()


Expand Down Expand Up @@ -2648,6 +2667,7 @@ def test_array_new_del(self):
add_function_test(TestArray, "test_array_of_structs_roundtrip", test_array_of_structs_roundtrip, devices=devices)
add_function_test(TestArray, "test_array_from_numpy", test_array_from_numpy, devices=devices)
add_function_test(TestArray, "test_array_aliasing_from_numpy", test_array_aliasing_from_numpy, devices=["cpu"])
add_function_test(TestArray, "test_numpy_array_interface", test_numpy_array_interface, devices=["cpu"])

add_function_test(TestArray, "test_array_inplace_ops", test_array_inplace_ops, devices=devices)
add_function_test(TestArray, "test_direct_from_numpy", test_direct_from_numpy, devices=["cpu"])
Expand Down
24 changes: 24 additions & 0 deletions warp/tests/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@ def wrap_vec_tensor_with_warp_grad(vec_dtype):
wrap_vec_tensor_with_warp_grad(wp.transform)


def test_cuda_array_interface(test, device):
# We should be able to construct Torch tensors from Warp arrays via __cuda_array_interface__ on GPU.
# Note that Torch does not support __array_interface__ on CPU.

torch_device = wp.device_to_torch(device)
n = 10

# test the types supported by both Warp and Torch
scalar_types = [wp.float16, wp.float32, wp.float64, wp.int8, wp.int16, wp.int32, wp.int64, wp.uint8]

for dtype in scalar_types:
# test round trip
a1 = wp.zeros(n, dtype=dtype, device=device)
t = torch.tensor(a1, device=torch_device)
a2 = wp.array(t, device=device)

assert a1.dtype == a2.dtype
assert a1.shape == a2.shape
assert a1.strides == a2.strides


def test_to_torch(test, device):
import torch

Expand Down Expand Up @@ -918,6 +939,9 @@ class TestTorch(unittest.TestCase):
test_warp_graph_torch_stream,
devices=torch_compatible_cuda_devices,
)
add_function_test(
TestTorch, "test_cuda_array_interface", test_cuda_array_interface, devices=torch_compatible_cuda_devices
)

# multi-GPU tests
if len(torch_compatible_cuda_devices) > 1:
Expand Down
6 changes: 3 additions & 3 deletions warp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,17 +1302,17 @@ def type_to_warp(dtype):

def type_typestr(dtype):
if dtype == bool:
return "?"
return "|b1"
elif dtype == float16:
return "<f2"
elif dtype == float32:
return "<f4"
elif dtype == float64:
return "<f8"
elif dtype == int8:
return "b"
return "|i1"
elif dtype == uint8:
return "B"
return "|u1"
elif dtype == int16:
return "<i2"
elif dtype == uint16:
Expand Down

0 comments on commit e18931f

Please sign in to comment.