Skip to content

vulkan: stabilize InstanceNorm subgroup reduction - #7136

Merged
nihui merged 11 commits into
Tencent:masterfrom
nihui:fix-instancenorm-subgroup-reduce
Oct 18, 2026
Merged

nihui merged 11 commits into
Tencent:masterfrom
nihui:fix-instancenorm-subgroup-reduce

Conversation

@nihui

@nihui nihui commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Summary

Replacement for #6758. This branch preserves futz12's original InstanceNorm subgroup optimization commits and authorship, merges current master, and appends the correctness fixes below. The original PR is left unchanged.

  • follow current LayerNorm Vulkan centered, precise compensated mean accumulation for pack1 and pack4, retaining two-pass variance with precise compensated square-sum accumulation
  • merge subgroup partials through shared memory and broadcast the complete mean before variance accumulation
  • split channel workgroups across x/y with a bounds guard, avoiding the reduced z-dispatch limit
  • add focused black-box GPU regressions with double-precision references for long constant/nonconstant inputs, small deviations after large outliers, and 65,537 channels
  • skip unused fallback reduction pipelines when subgroup operations are selected
  • share a pure double-precision reference between numerical and dispatch regressions
  • use direct buffer transfers for the large-channel regression so Packing's independent dispatch limit does not interfere

Validation

  • unchanged existing InstanceNorm tests passed before editing
  • old shaders reproduced a constant-input error on SwiftShader: 0.133789 instead of zero for 65,536 copies of 10.1f, for both pack1 and pack4
  • fixed InstanceNorm regressions pass with subgroup operations enabled and disabled
  • independently reproduced and fixed ~0.7777% variance error for a long balanced-outlier input in both packs; the variance-specific regression uses 0.0001 tolerance
  • InstanceNorm, LayerNorm, and RMSNorm CTests pass against the final combined tree (3/3)
  • a separate validation-only harness exercised eight actual software Vulkan subgroups per workgroup; old pack1/pack4 statistics failed, fixed statistics matched double references
  • independent review of the final fix found no blocking issues

Validation used Chromium SwiftShader (native subgroup size 4), not a physical GPU. The multi-subgroup harness deliberately adjusted its local size/stride to exercise the fallback merge; it is not included in the production changes. No new hardware performance claims or fp16 storage/arithmetic coverage.

futz12 and others added 7 commits May 29, 2026 14:42
Add subgroup reduction fast-path for InstanceNorm Vulkan backend.
Replaces multi-pass reduce chain (sum -> mean -> sub -> square -> reduce -> var)
with a single dispatch per channel using subgroupAdd.

Changes:
- New shader: instancenorm_reduce_subgroup.comp (pack1)
- New shader: instancenorm_reduce_subgroup_pack4.comp (pack4)
- C++ dispatch uses w=1, h=c, c=1 to avoid dispatching over spatial dims
- Falls back to existing reduce chain when subgroup ops unavailable
- Added perf benchmark: tests/perf/perf_instancenorm.cpp

NVIDIA RTX 4060 Laptop (gpu-1) per-op speedup:

| shape          | precision | baseline (us) | optimized (us) | speedup |
|----------------|-----------|---------------|----------------|---------|
| [64,64,128]    | fp32      |         35.64 |          12.08 |    3.0x |
| [64,64,128]    | fp16ps    |         34.79 |           8.77 |    4.0x |
| [64,64,128]    | fp16psa   |         34.71 |           8.74 |    4.0x |
| [64,64,128]    | bf16ps    |         35.24 |           9.50 |    3.7x |
| [32,32,256]    | fp32      |         29.80 |          59.70 |    0.5x |
| [32,32,256]    | fp16ps    |         26.00 |           8.70 |    3.0x |
| [32,32,256]    | fp16psa   |         26.70 |           8.50 |    3.1x |
| [32,32,256]    | bf16ps    |         26.50 |           9.00 |    2.9x |
| [16,16,512]    | fp32      |         19.64 |          62.40 |    0.3x |
| [16,16,512]    | fp16ps    |         19.12 |           9.10 |    2.1x |
| [16,16,512]    | fp16psa   |         19.13 |           9.10 |    2.1x |
| [16,16,512]    | bf16ps    |         19.34 |           9.70 |    2.0x |
| [8,8,512]      | fp32      |         18.10 |           8.40 |    2.2x |
| [8,8,512]      | fp16ps    |         17.70 |           9.00 |    2.0x |
| [8,8,512]      | fp16psa   |         15.30 |           9.20 |    1.7x |
| [8,8,512]      | bf16ps    |         16.20 |           8.30 |    2.0x |
| [224,224,64]   | fp32      |        130.00 |          52.90 |    2.5x |
| [224,224,64]   | fp16ps    |         83.50 |          36.30 |    2.3x |
| [224,224,64]   | fp16psa   |         83.90 |          36.50 |    2.3x |
| [224,224,64]   | bf16ps    |         91.50 |          40.50 |    2.3x |
| [224,224,3]    | fp32      |         38.27 |          22.70 |    1.7x |
| [224,224,3]    | fp16ps    |         37.99 |          18.00 |    2.1x |
| [224,224,3]    | fp16psa   |         37.80 |          15.90 |    2.4x |
| [224,224,3]    | bf16ps    |         37.83 |          16.20 |    2.3x |
| [4096,1,1]     | fp32      |         24.87 |           6.76 |    3.7x |
| [4096,1,1]     | fp16ps    |         24.76 |           6.07 |    4.1x |
| [4096,1,1]     | fp16psa   |         24.76 |           6.03 |    4.1x |
| [4096,1,1]     | bf16ps    |         24.83 |           6.22 |    4.0x |
| [512,1,1]      | fp32      |         74.40 |           5.91 |   12.6x |
| [512,1,1]      | fp16ps    |         35.10 |           5.20 |    6.8x |
| [512,1,1]      | fp16psa   |         27.70 |           5.22 |    5.3x |
| [512,1,1]      | bf16ps    |         23.30 |           5.21 |    4.5x |

Note: fp32 pack1 path may regress on small spatial sizes (e.g. [32,32,256],
[16,16,512]) due to under-utilized 256-thread workgroups. fp16/fp16a/bf16
paths use pack4 and show consistent speedups across all tested shapes.
Follow LayerNorm centered compensated accumulation and cross-subgroup
reduction, split channel dispatches within device limits, and add GPU
regressions against double-precision references.
@tencent-adm

Copy link
Copy Markdown
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ futz12
❌ nihui
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov-commenter

codecov-commenter commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.09929% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.29%. Comparing base (21eb6ca) to head (95dedf2).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/layer/vulkan/instancenorm_vulkan.cpp 95.41% 10 Missing ⚠️
...kan/shader/instancenorm_reduce_subgroup_pack4.comp 96.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7136      +/-   ##
==========================================
+ Coverage   94.97%   95.29%   +0.31%     
==========================================
  Files        1464     1487      +23     
  Lines      497166   504725    +7559     
==========================================
+ Hits       472178   480964    +8786     
+ Misses      24988    23761    -1227     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nihui
nihui marked this pull request as ready for review October 18, 2026 09:38
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T13:38:24.194556Z 95dedf2 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a2eb31833

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/layer/vulkan/instancenorm_vulkan.cpp Outdated
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Binary size comparison

The binary size change of libncnn.so (bytes)

architecture base size pr size difference
x86_64 27763760 27772104 +8344 ⚠️
armhf 7723152 7727388 +4236 ⚠️
aarch64 11849720 11916024 +66304 ⚠️

Commit: 01e135b73b503ffc99ea1f540bbd5791defd3625 · Workflow run

nihui and others added 4 commits October 18, 2026 17:58
Preserve small squared deviations after large outliers in both packs.
Add a strict variance regression and use direct transfers for the large
channel test to avoid Packing's independent dispatch limit.
@nihui
nihui merged commit 44b02da into Tencent:master Oct 18, 2026
74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants