mesh: add mesh_ring_axis to give a mesh axis physical rings on a torus without wraparound - #4911
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the mesh_ring_axis configuration to lay out a logical mesh axis as a physical TPU ring for single-slice setups, implementing the create_ring_axis_device_mesh utility and corresponding unit tests. The review feedback highlights a potential issue in create_ring_axis_device_mesh where the hardcoded transposition check can cause a ValueError on certain valid TPU topologies (such as a 2x16 grid with ring=8), and suggests a more robust, dynamic tiling check.
| # Preserve locality for non-ring axes. | ||
| if physical.shape[0] > physical.shape[1]: | ||
| physical = physical.T | ||
| ring_height, ring_width = ring // 2, 2 |
There was a problem hiding this comment.
The current implementation of create_ring_axis_device_mesh only transposes the physical grid if physical.shape[0] > physical.shape[1]. However, this hardcoded check is too restrictive and can cause a ValueError on valid TPU topologies (such as a 2x16 grid with ring=8, where ring_height=4 and ring_width=2). In this case, the height of the block (4) is greater than the height of the grid (2), so tiling fails in the default orientation, but would succeed if the grid were transposed to 16x2.
To make the mesh creation more robust, we should dynamically check if transposing the physical grid is required to make it divisible by ring_height and ring_width.
ring_height, ring_width = ring // 2, 2
# Determine if transposing is needed/preferred to allow valid tiling.
can_tile_normal = (physical.shape[0] % ring_height == 0) and (physical.shape[1] % ring_width == 0)
can_tile_transposed = (physical.shape[1] % ring_height == 0) and (physical.shape[0] % ring_width == 0)
if not can_tile_normal and can_tile_transposed:
physical = physical.T
elif can_tile_normal and can_tile_transposed and physical.shape[0] > physical.shape[1]:
physical = physical.T…s without wraparound Tiles the physical mesh with k-by-2 torus blocks so every ring hop is a nearest-neighbor link. Placement only: HLO is byte-identical with the flag off. Measured 4.632 to 4.138 s/step (1.12x). Co-authored-by: Sudarsanan <[email protected]> Co-authored-by: Armin <[email protected]> Co-authored-by: utlz <[email protected]>
55e7770 to
cc08438
Compare
Description
Adds
mesh_ring_axisto arrange an even-sized logical mesh axis into physical rings built from k-by-2 torus blocks. This provides a physical closing link when the axis is mapped to a slice dimension without wraparound.The change affects device placement only. AOT HLO was byte-identical by md5 with the option disabled and enabled.
Performance
Setup:
ici_expert_parallelism=16,ici_tensor_parallelism=8,ici_fsdp_parallelism=1,max_target_length=16384,per_device_batch_size=0.125(global batch 16),LIBTPU_INIT_ARGS=--xla_tpu_scoped_vmem_limit_kib=98304, dynamic-splash attention path enabled.mesh_ring_axis=tensorplaces the 8-way tensor axis on physical rings.Tests
python3 -m pytest tests/unit/max_utils_test.py -k RingAxisDeviceMeshTestpassed 3 tests.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.