wp.array() from a list of float16/bfloat16 vectors or matrices stores the raw 16-bit encodings as values
Bug Description
Creating an array from a list of float16 or bfloat16 vectors, matrices, quaternions or transforms stores the wrong values. Each component comes out as the integer value of its 16-bit encoding, so wp.vec3h(3.0, -4.0, 12.0) becomes (16896.0, 50176.0, 18944.0) (0x4200, 0xC400 and 0x4A00 are the float16 encodings of 3, -4 and 12). There is no error or warning. I found this while checking wp.length() on wp.vec3h arrays: every result was inf, because the input array already held these values.
import warp as wp
wp.init()
a = wp.array([wp.vec3h(3.0, -4.0, 12.0)], dtype=wp.vec3h)
print(a.numpy()) # expected [[ 3. -4. 12.]]
b = wp.array([wp.mat22h(1.0, 2.0, 3.0, 4.0)], dtype=wp.mat22h)
print(b.numpy()) # expected [[[1. 2.] [3. 4.]]]
vec3bf = wp.types.vector(length=3, dtype=wp.bfloat16)
c = wp.array([vec3bf(3.0, -4.0, 12.0)], dtype=vec3bf)
print(c.list()) # expected [vec3bf(3.0, -4.0, 12.0)]
Output with Warp 1.18.0 (the same on main):
[[1.690e+04 5.018e+04 1.894e+04]]
[[[1.536e+04 1.638e+04]
[1.690e+04 1.741e+04]]]
[vec3bf([bfloat16(16384.0), bfloat16(49152.0), bfloat16(16640.0)])]
The same happens for vec2h, vec4h, quath and transformh, on CPU and CUDA devices, with array.assign() given a list of these values, and with wp.array() without a dtype, which also infers uint16. Lists of tuples, lists of wp.float16 scalars, wp.full() and array.fill_() give the right values, as do float32 and float64 vectors.
In array._init_from_data() (warp/_src/types.py), a list for these types is converted with np.asarray(data, dtype=np.float16), or through np.float32 for bfloat16. Warp's float16 and bfloat16 vector and matrix types store their components as raw uint16 values, and NumPy reads that buffer as integers instead of going through the element accessors, which return the float values. Converting such elements to lists of floats first gives the right result. I can open a PR with this change and tests.
System Information
- Warp 1.18.0 (pip); the same on main (bfd59c41d)
- Ubuntu 26.04 (WSL2), x86_64, Python 3.12, NumPy 2.5.4
- NVIDIA GeForce RTX 5060 Ti, driver 610.88, CUDA 13
This is addressed by c9e3473b7c264833ee221356f28d7792dab1b409.
wp.array() let NumPy convert the list, and NumPy read each float16 or bfloat16 vector, matrix, quaternion, or transform through the buffer it exports, which holds its components as uint16 encodings. On Python 3.12 and later, these types now export a buffer that NumPy reads as float16 values, or no buffer for bfloat16, which NumPy then reads as float32 values. On Python 3.11, wp.array() decodes them itself before NumPy reads its input. The fix will ship in Warp 1.20.
Public API behavior
Python scope
wp.array()andarray.assign()store the values of float16 and bfloat16 vectors, matrices, quaternions, and transforms in Python lists and tuples, flat or nested, on every device.- Without a
dtype,wp.array()infersfloat16from float16 values andfloat32from bfloat16 values, instead ofuint16. - On Python 3.12 and later, NumPy reads these values directly too, as in
np.asarray(wp.vec3h(1.0, 2.0, 3.0)). - On Python 3.12 and later,
bytes()andmemoryview()of a bfloat16 vector, matrix, quaternion, or transform raiseBufferError, since no buffer format describes bfloat16.
Performance
The same change makes wp.array() faster on the inputs it copies from the host. Median time of one call on the CPU, before → after:
| Input | Python 3.12 | Python 3.11 |
|---|---|---|
| a list of 3 floats | 10.3 → 6.23 µs (1.66×) | 13.5 → 7.48 µs (1.81×) |
a list of 4 vec3f |
12.6 → 7.78 µs (1.62×) | 16.1 → 8.54 µs (1.88×) |
a list of 4 vec3h |
12.7 → 9.12 µs (1.39×) | 16.2 → 8.84 µs (1.84×) |
a list of 100,000 vec3f |
62.5 → 2.76 ms (22.6×) | 60.8 → 2.66 ms (22.8×) |
a list of 100,000 vec3h |
64.1 → 11.2 ms (5.71×) | 62.1 → 2.64 ms (23.5×) |
| a list of 100,000 tuples of 3 floats | 15.3 → 15.3 ms (1.00×) | 15.7 → 16.5 ms (0.95×) |
Timings on a CUDA device are similar. Long lists of tuples, named tuples, or NumPy arrays are within noise on Python 3.12, and 2–6% slower on Python 3.11, which scans them for half-precision values. Measured on Linux with an RTX 3090 Ti, with NumPy 2.5.4 on Python 3.12 and NumPy 2.4.6 on Python 3.11.
Test coverage
warp/tests/test_array.py: float16 and bfloat16 vectors, matrices, quaternions, and transforms in flat, nested, mixed, and custom sequences, compared with their exact values; lists long enough to be copied in batches; inputs that must convert as before, such as array-like objects, sequence subclasses, and self-referencing lists; and copies from contiguous, non-contiguous, empty, and pinned host data. Runs on CPU and CUDA devices.warp/tests/test_fp16.py: NumPy reads of float16 vectors and matrices, alone and in mixed sequences, a NumPy view that shares their memory, and theirbytes().warp/tests/test_bf16.py: NumPy reads of bfloat16 vectors and matrices, alone, in mixed sequences, and without a copy;BufferErrorfrom theirbytes()andmemoryview(); and a kernel that uses a bfloat16 vector constant, on CPU and CUDA devices.warp/tests/test_builtins_resolution.py: the identity quaternion and transform test reads their stored bytes directly, sincebytes()of a bfloat16 value now raises.
Behavioral verification
The examples in the report give the expected values on CPU and CUDA devices with Python 3.11 through 3.14, as do vec2h, vec4h, quath, transformh, and their bfloat16 counterparts, with and without a dtype, and through array.assign(). wp.length() of the wp.vec3h(3.0, -4.0, 12.0) element now returns 13.
Closing this issue as completed.
Drafted with the warp-closing-issue skill.