warp icon indicating copy to clipboard operation
warp copied to clipboard

[REQ] Use 8B/16B vectorized loads in CUDA kernels for performance

Open adenzler-nvidia opened this issue 1 year ago • 4 comments

Description

We should add a fast path to load wider types using float2/float4 loads as this can give a nice performance boost for memory-heavy kernels.

Context

We observed good speedups in Mujoco-Warp doing this, and would like to have this available for any type that is properly aligned such that users can build padded data types on top of it.

adenzler-nvidia avatar May 08 '25 15:05 adenzler-nvidia

@adenzler-nvidia could you point me to where this was applied for MJWarp?

It looks like this might have some overlap with #827.

c0d1f1ed avatar Oct 20 '25 13:10 c0d1f1ed

I had a prototype that did some runtime checks a while ago but never followed up on it due to other fires and this being a runtime check, we saw some good boost out of this.

The thing we did now in MjWarp is add some specific padding such that we hit the vectorized path in tile_load: https://github.com/google-deepmind/mujoco_warp/commit/f38c536d0420c1f4f12bb2b51286c13609383811

In general looking at the profiles and the instruction sequences generated for many cases we could benefit from an array type that has c-style array semantics, where we could go from dozens of instructions down to a few. We anyway won't change strides/shapes at runtime with CUDA graphs everywhere. I always wanted to prototype that to show how much we could gain, but never got to it.

adenzler-nvidia avatar Oct 20 '25 14:10 adenzler-nvidia

Having Claude summarize what was discussed in the Newton channel:

@c0d1f1ed to add alignment metadata to array descriptors that enables code generation of _ldg128() operations for 128-bit vectorized loads for Warp v1.12.

Kernel parameters would use a type hint to declare alignment requirements:

@wp.kernel
def my_kernel(data: wp.array(dtype=wp.quat, aligned=True)):
    ...
  • User responsibility: It's up to the user to ensure arrays passed to annotated kernel parameters satisfy the 16-byte alignment requirement.
  • Default allocations are safe: Any Warp allocation from wp.zeros(), wp.empty(), etc. will automatically be 16-byte aligned.
  • Slicing caveat: Users should be aware that slicing or offsetting an array can create pointers that are no longer aligned.

@daedalus5 will explore similar alignment optimizations for tiles.

Related: #827 (struct alignment)

shi-eric avatar Feb 03 '26 17:02 shi-eric

This is addressed by c1e2085dd230415573d762a244bdac82a1cf2bed.

Aligned 16-byte elements such as wp.vec4, wp.quat, and a struct containing wp.vec3 plus a float can now use 16-byte loads. Stores of eligible elements of 16 bytes or larger use 8- or 16-byte words. Alignment checks account for the accessed strides, and misaligned arrays retain scalar accesses.

The optimization deliberately targets selected element sizes: eight-byte elements such as wp.vec2 and larger elements such as wp.spatial_vector and wp.mat44 still use scalar loads in the checked kernels. The reporter approved this implementation on GitLab, so closing as completed.

Public API behavior

Existing kernel array indexing gains this optimization automatically; no aligned=True annotation is required. Python configuration wp.config.enable_wide_array_access defaults to True; set it to False before compiling/loading affected modules to disable the optimization.

Test coverage

  • warp/tests/test_array.py::test_array_wide_access: copies across 15 element types, including padded structs, with aligned, offset, irregular, padded, and differently aligned source/destination layouts, using direct and looped indexing.
  • warp/tests/test_array.py::test_array_wide_access_2d: packed, padded-row, and transposed layouts.
  • warp/tests/test_array.py::test_array_wide_access_indexed: aligned and offset indexed arrays.
  • warp/tests/test_array.py::test_array_wide_access_per_thread_arrays: threads accessing different arrays with mixed alignment through struct fields.
  • warp/tests/test_array.py::test_array_wide_access_disabled: aligned and offset copies with the optimization disabled.
  • warp/tests/cuda/test_clang_cuda.py::test_vec4_copy_kernel: aligned and offset looped copies using Clang-CUDA.

Behavioral verification

All 17 device-specific cases of these new tests passed on CPU and two Blackwell CUDA devices, as applicable. Separate reproductions confirmed vectorized PTX loads for vec4 and a padded struct, 8-byte stores for spatial vectors, 16-byte stores for matrices, correct misaligned/strided copies, and removal of vectorized accesses when disabled. These checks verify instruction generation and correctness; application speedups were not benchmarked.

Drafted with the warp-closing-issue skill.

shi-eric avatar Nov 11 '26 21:11 shi-eric