Simplify layer_norm_wg_size_select to reduce tree-reduce barrier cost#3738
Open
CuiYifeng wants to merge 1 commit into
Open
Simplify layer_norm_wg_size_select to reduce tree-reduce barrier cost#3738CuiYifeng wants to merge 1 commit into
CuiYifeng wants to merge 1 commit into
Conversation
Contributor
Author
|
LayerNorm Speedup Ratio:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Skill file(s) read: .github/skills/xpu-ops-pr-review/SKILL.md.
This PR adjusts work-group size selection for the vectorized LayerNorm SYCL kernel to prefer a smaller fixed work-group (SIMD * 4) in order to reduce inter-subgroup tree-reduce barrier overhead.
Changes:
- Replaced the previous “shrink max_wg_size by powers of two” logic with a fixed preferred work-group size (
SIMD * 4) whenN / vec_size <= max_wg_size. - Kept the existing fallback of using
max_wg_sizewhenN / vec_size > max_wg_size.
Comment on lines
+594
to
599
| // with 4 subgroups per workgroup | ||
| constexpr int64_t preferred_wg_size = SIMD * 4; | ||
| if (n <= max_wg_size && preferred_wg_size <= max_wg_size) { | ||
| return preferred_wg_size; | ||
| } | ||
| return max_wg_size; |
jianyizh
requested changes
May 22, 2026
| // with 4 subgroups per workgroup | ||
| constexpr int64_t preferred_wg_size = SIMD * 4; | ||
| if (n <= max_wg_size && preferred_wg_size <= max_wg_size) { | ||
| return preferred_wg_size; |
Contributor
There was a problem hiding this comment.
preferred_wg_size <= max_wg_sizeWhy we need this? Max_wg_size for every device should > 128- Copilot comments makes sense when n < 128?
- You also need consider occupancy. Consider batchsize=64 on B70, n=1024. if you have 32 xe core. wg_size = 1024 gives occupancy 100%, but may suffer sync stall. wg_size 512 only 50% occupancy, but less cost on barrier. Does wg_size 128 gives the best performance? You need have a test.
Contributor
Author
There was a problem hiding this comment.
- Because not sure if max_wg_size >= SIMD * 4 always holds true. However, agree with your opinion about
max_wg_size.preferred_wg_size <= max_wg_sizehas been removed. - Do you mean
when max_wg_size < SIMD*4, the function now returns max_wg_size without shrinking it relative to n? Make sense. That's another reason to removepreferred_wg_size <= max_wg_size. - Good point. Based on the ratio ratio for batch_size=40 and dim_size=4096 (n=1024), no obvious has been observed. Below is the data collected from B60:
| dtype | Speedup |
|---|---|
| fp32 | 0.98x |
| fp16 | 1.00x |
| bf16 | 1.01x |
c9aa77c to
f5f0634
Compare
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.
This PR is to set
wg_sizetoSIMD * 4for the vectorized LayerNorm kernel whenN / vec_size <= max_wg_size.Motivation:
The vectorized LayerNorm kernel uses inter-subgroup tree-reduce in
compute_stats(). Forwg_size = 1024andSIMD32, the inter-subgroup tree-reduce requires 5 rounds of reduction, where average subgroup utilization during reduce is only ~19.4%. Stall sampling shows barrier synchronization as the biggest stall source (~31.5% of all stalls). Reducingwg_size / SIMDto 4 cuts tree-reduce to 2 rounds, eliminates ~92% of sync stalls.Why gate on
N / vec_size <= max_wg_size:Smaller
wg_size / SIMDmeans more concurrent workgroups per Xe-core, increasing L1 pressure. The Pass2 of vectorized LayerNorm kernel has a working set that scales as:W_L1 = concurrent_WGs×N×element_sizewhen
W_L1exceeds the LSC cache capacity, Pass2 degrades from L1 hit to L3 hit, offsetting the barrier reduction gains. Since LSC cache sizes vary across platforms and cannot be queried at runtime, we need a platform-independent guard.