|
| 1 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | + |
| 3 | +import pytest |
| 4 | +from slangpy import DeviceType, NDBuffer |
| 5 | +from slangpy.types import Tensor |
| 6 | +from slangpy.testing import helpers |
| 7 | + |
| 8 | +MODULE = r""" |
| 9 | +struct Foo { |
| 10 | + int x; |
| 11 | +} |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 16 | +def test_buffer_to_string(device_type: DeviceType): |
| 17 | + device = helpers.get_device(device_type) |
| 18 | + buffer = NDBuffer.zeros(device, shape=(10, 20), dtype="float") |
| 19 | + |
| 20 | + # Test that repr() returns a meaningful string |
| 21 | + repr_str = repr(buffer) |
| 22 | + print(f"NDBuffer: {repr_str}") |
| 23 | + |
| 24 | + # Verify the repr contains expected information |
| 25 | + assert "NativeNDBuffer" in repr_str |
| 26 | + assert "shape" in repr_str |
| 27 | + assert "strides" in repr_str |
| 28 | + assert "dtype" in repr_str |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 32 | +def test_slangtype_struct_to_string(device_type: DeviceType): |
| 33 | + device = helpers.get_device(device_type) |
| 34 | + module = helpers.create_module(device, MODULE) |
| 35 | + foo_struct = module["Foo"].as_struct() |
| 36 | + |
| 37 | + # Access the underlying SlangType |
| 38 | + foo_type = foo_struct.struct |
| 39 | + |
| 40 | + # Test that repr() returns a meaningful string |
| 41 | + repr_str = repr(foo_type) |
| 42 | + print(f"SlangType: {repr_str}") |
| 43 | + |
| 44 | + # Verify the repr contains expected information |
| 45 | + assert "SlangType" in repr_str |
| 46 | + assert "name" in repr_str |
| 47 | + assert "Foo" in repr_str |
| 48 | + assert "shape" in repr_str |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 52 | +def test_slangtype_vector_to_string(device_type: DeviceType): |
| 53 | + device = helpers.get_device(device_type) |
| 54 | + module = helpers.create_module(device, MODULE) |
| 55 | + foo_struct = module["float3"].as_struct() |
| 56 | + |
| 57 | + # Access the underlying SlangType |
| 58 | + foo_type = foo_struct.struct |
| 59 | + |
| 60 | + # Test that repr() returns a meaningful string |
| 61 | + repr_str = repr(foo_type) |
| 62 | + print(f"SlangType: {repr_str}") |
| 63 | + |
| 64 | + # Verify the repr contains expected information |
| 65 | + assert "SlangType" in repr_str |
| 66 | + assert "name" in repr_str |
| 67 | + assert "vector<float,3>" in repr_str |
| 68 | + assert "shape" in repr_str |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 72 | +def test_tensor_to_string(device_type: DeviceType): |
| 73 | + device = helpers.get_device(device_type) |
| 74 | + |
| 75 | + # Create a tensor without gradients |
| 76 | + tensor = Tensor.empty(device, dtype="float", shape=(3, 4)) |
| 77 | + |
| 78 | + # Test that repr() returns a meaningful string - Tensor inherits from NativeTensor |
| 79 | + repr_str = repr(tensor) |
| 80 | + print(f"Tensor: {repr_str}") |
| 81 | + |
| 82 | + # Verify the repr contains expected information |
| 83 | + assert "NativeTensor" in repr_str |
| 84 | + assert "dtype" in repr_str |
| 85 | + assert "shape" in repr_str |
| 86 | + assert "has_grad_in=false" in repr_str |
| 87 | + assert "has_grad_out=false" in repr_str |
| 88 | + |
| 89 | + # Test with gradients |
| 90 | + grad_in = Tensor.zeros(device, dtype="float", shape=(3, 4)) |
| 91 | + grad_out = Tensor.zeros(device, dtype="float", shape=(3, 4)) |
| 92 | + tensor_with_grads = tensor.with_grads(grad_in, grad_out) |
| 93 | + |
| 94 | + repr_str_grads = repr(tensor_with_grads) |
| 95 | + print(f"Tensor with grads: {repr_str_grads}") |
| 96 | + assert "has_grad_in=true" in repr_str_grads |
| 97 | + assert "has_grad_out=true" in repr_str_grads |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 101 | +def test_module_to_string(device_type: DeviceType): |
| 102 | + device = helpers.get_device(device_type) |
| 103 | + module = helpers.create_module(device, MODULE) |
| 104 | + |
| 105 | + # Test that repr() returns a meaningful string for Module |
| 106 | + repr_str = repr(module) |
| 107 | + print(f"Module: {repr_str}") |
| 108 | + |
| 109 | + # Verify the repr contains expected information |
| 110 | + assert "Module" in repr_str |
| 111 | + assert "name=" in repr_str |
| 112 | + assert "linked_modules=" in repr_str |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES) |
| 116 | +def test_slangtype_reflection_to_string(device_type: DeviceType): |
| 117 | + device = helpers.get_device(device_type) |
| 118 | + module = helpers.create_module(device, MODULE) |
| 119 | + foo_struct = module["Foo"].as_struct() |
| 120 | + |
| 121 | + # Access the SlangType reflection |
| 122 | + foo_type_reflection = foo_struct.struct |
| 123 | + |
| 124 | + # Test that repr() returns a meaningful string for SlangType reflection |
| 125 | + repr_str = repr(foo_type_reflection) |
| 126 | + print(f"SlangType reflection: {repr_str}") |
| 127 | + |
| 128 | + # Verify the repr contains expected information - should get the Python SlangType repr |
| 129 | + assert "SlangType" in repr_str |
| 130 | + assert "name=" in repr_str |
| 131 | + assert "shape=" in repr_str |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + pytest.main([__file__, "-v", "-s"]) |
0 commit comments