Refactor space-filling curves from examples into common utilities#252
Draft
Refactor space-filling curves from examples into common utilities#252
Conversation
Co-authored-by: neoblizz <[email protected]>
Co-authored-by: neoblizz <[email protected]>
Copilot
AI
changed the title
[WIP] Refactor space-filling curves into common module
Refactor space-filling curves from examples into common utilities
Oct 17, 2025
neoblizz
requested changes
Oct 17, 2025
examples/common/utils.py
Outdated
Comment on lines
177
to
179
| This function reorders program IDs to improve locality when multiple compute | ||
| dies (XCDs) are present. It ensures that consecutive PIDs are distributed | ||
| across different XCDs before moving to the next set of programs within an XCD. |
Member
There was a problem hiding this comment.
@copilot Slightly incorrect, it ensures the program IDs are reordered such that you "fill" an XCD with work before going to the next.
Contributor
Author
There was a problem hiding this comment.
Fixed the docstring in commit 2d6f8c4. The description now correctly states that the function "fills an XCD with work before going to the next XCD".
examples/common/utils.py
Outdated
|
|
||
|
|
||
| @triton.jit | ||
| def apply_xcd_reordering(pid, NUM_XCDS: tl.constexpr, NUM_SMS: tl.constexpr): |
Contributor
Author
There was a problem hiding this comment.
Renamed to chiplet_reorder in commit 2d6f8c4.
examples/common/utils.py
Outdated
|
|
||
|
|
||
| @triton.jit | ||
| def compute_tile_coordinates(tile_id, num_pid_m, num_pid_n, GROUP_SIZE_M: tl.constexpr): |
Contributor
Author
There was a problem hiding this comment.
Renamed to program_id_reorder in commit 2d6f8c4.
…reorder Co-authored-by: neoblizz <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR addresses issue #251 by refactoring repeated space-filling curve patterns from multiple example files into reusable utility functions in
examples/common/utils.py.Problem
The same space-filling curve patterns were duplicated across 9+ example files:
This code duplication made maintenance difficult and increased the risk of inconsistencies.
Solution
Created two new Triton JIT utility functions in
examples/common/utils.py:1.
chiplet_reorder(pid, NUM_XCDS, NUM_SMS)Applies XCD (compute die) space-filling curve reordering to program IDs. This reorders program IDs such that you fill an XCD (chiplet) with work before going to the next XCD, improving locality when multiple compute dies are present.
2.
program_id_reorder(tile_id, num_pid_m, num_pid_n, GROUP_SIZE_M)Computes 2D tile coordinates
(pid_m, pid_n)from lineartile_idusing swizzling. This implements a space-filling curve that groups tiles along the M dimension to improve memory coalescing and cache locality.Both functions include comprehensive docstrings explaining their purpose, parameters, and return values.
Changes
Updated 9 example files to use the new utility functions:
examples/07_gemm_all_scatter/gemm_all_scatter.pyexamples/08_gemm_atomics_all_reduce/gemm_atomics_all_reduce.pyexamples/09_gemm_one_shot_all_reduce/gemm_one_shot_all_reduce.pyexamples/10_gemm_all_scatter_wg_specialization/gemm_all_scatter_wg_specialization.pyexamples/11_gemm_all_scatter_producer_consumer/gemm_all_scatter_producer_consumer.pyexamples/12_gemm_all_scatter_bulk_synchronous/gemm_all_scatter_bulk_synchronous.pyexamples/14_all_gather_gemm/all_gather_gemm_push.pyexamples/14_all_gather_gemm/all_gather_gemm_pull.pyexamples/20_gemm_all_scatter_independent/gemm_all_scatter_bulk_synchronous.pyImpact
Testing
Fixes #251
Original prompt
Fixes #251
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.