Skip to content

asahi: fold horizontal sums of packed bytes - #1542

Closed
RyanTheTide wants to merge 1 commit into
perf/closeout-804-packed-w8from
perf/closeout-804-byte-sum
Closed

RyanTheTide wants to merge 1 commit into
perf/closeout-804-packed-w8from
perf/closeout-804-byte-sum

Conversation

@RyanTheTide

Copy link
Copy Markdown

What

Carry the horizontal W8 byte sum from #804 with Dj’s sign-off. Retain the load as a packed word for a full-vector sum and accumulate the byte totals in two 16-bit lanes before folding them together. The pass leaves partial-vector consumers intact.

Tested

Exact head 54cd94a79deda on byte-store parent e135858eb62b4: AArch64 Vulkan build, 1,960 host compiler passes with one skip and 66 focused tests. Neo J700 G17P used saved 7.3.0-aurora-fleet94. Each process verified 524,288 CPU reference outputs and 524,288 padding words.

Isolated via per-arm VK_DRIVER_FILES and MESA_SHADER_CACHE_DISABLE=1. Commands ./store-aarch64 1 2, ./store-aarch64 1 3 and ./store-aarch64 1 4. Four alternating ABBA/BAAB blocks per shape, 20 GPU timestamps per process after four warmups.

Case Base pooled median (ns) Head pooled median (ns) Median paired change
One full W8 vector 39,042 39,000 −0.08%
Two full W8 vectors 39,521 39,125 −1.16%
Partial-vector control 39,250 39,208 −0.03%

The two-vector case improved in all four blocks. NIR switched the complete reductions to packed loads and kept the partial-vector load as before. The emitted shader shrank from 586 to 488 bytes. Neo stayed on the same saved boot with Enforcing, zero failed units and no kernel entries.

Risk

Two-way · AGX3B and AGX3C shaders that sum packed W8 vectors.

Shader and individual GPU samples
#version 450
#extension GL_EXT_shader_8bit_storage : require
layout(local_size_x = 64) in;
layout(push_constant) uniform Params { int unused; int start; int end; };
layout(set = 0, binding = 1, std430) readonly buffer Input { u8vec4 src[]; };
layout(set = 0, binding = 2, std430) writeonly buffer Output { uint dst[]; };

void main()
{
    uint i = gl_GlobalInvocationID.x;
    int mode = end - start;
    if (mode == 2) {
        uvec4 a = uvec4(src[i]);
        dst[i] = a.x + a.y + a.z + a.w;
    } else if (mode == 3) {
        uvec4 a = uvec4(src[i]);
        uvec4 b = uvec4(src[i ^ 1u]);
        dst[i] = a.x + a.y + a.z + a.w + b.x + b.y + b.z + b.w;
    } else {
        uvec4 a = uvec4(src[i]);
        dst[i] = a.x + a.y + a.z;
    }
}

Exact-head run

mode	process	arm	samples (ns)
1x2	1	base	39167,38833,38875,39083,38625,38791,38625,39250,39083,38458,38708,39208,38791,38667,38833,38584,38917,38917,38791,38917
1x2	2	head	39166,39250,38917,39334,38958,39375,39334,39375,39500,38917,39000,38958,39417,38917,39166,39250,39834,38917,39250,39292
1x2	3	head	39292,38750,38916,38959,39250,38875,39583,39167,38875,38667,38625,38958,38666,39292,38792,38959,39209,39000,38542,38625
1x2	4	base	39125,39292,39458,39708,38708,38750,38916,39292,39500,39000,39125,38791,38709,39125,38917,39000,39000,39042,39042,38875
1x2	5	head	39208,38792,39125,39209,38958,38959,39083,39084,39375,39042,39167,39125,39208,39000,39000,39125,38917,38667,38792,39042
1x2	6	base	39125,38834,38750,38583,39083,38917,39042,38583,38833,38667,38875,39166,39209,38666,38750,39250,39250,38875,38708,39000
1x2	7	base	39167,38333,39208,39500,38917,39041,39125,38750,38750,39125,38958,38959,38583,38916,39167,39000,38875,38916,38500,38625
1x2	8	head	39417,39125,39083,39667,38500,38833,39125,38916,39417,38750,39250,39000,39292,39292,39250,39083,39250,38958,38958,38959
1x2	9	base	39250,38834,39208,39667,38708,39833,39500,39250,39250,39042,39750,39208,38708,38875,38875,39000,40167,39000,38666,39125
1x2	10	head	39333,39333,38708,39125,39125,39042,39542,39167,39208,38750,39250,38666,38875,38917,38833,38875,39333,38875,38916,38750
1x2	11	head	38917,38417,38959,38708,38709,38542,38916,39250,38959,38875,38750,38792,39125,39042,39042,39167,39000,39083,38708,39000
1x2	12	base	39375,38667,38917,39041,39334,39042,38625,39167,39000,39458,39291,39292,38500,39125,39166,38750,39250,38833,38750,38958
1x2	13	head	39041,39541,38709,38833,38916,38750,39000,39541,39333,38958,38750,39291,39375,38500,38958,38792,39291,38625,39292,38708
1x2	14	base	39208,39042,39209,39459,39333,39250,39333,39708,38958,39000,39000,38875,38916,39375,39709,39333,39333,39458,39917,39459
1x2	15	base	39166,38458,39500,39375,39167,39167,39000,39542,39792,38791,39250,39084,39250,39042,39292,39500,39833,39000,39584,39542
1x2	16	head	39084,38709,38958,38791,38625,39292,39250,38958,38833,39208,39333,39208,39208,39125,38959,38708,39250,39083,38625,38583
1x3	1	base	39584,39041,38708,39584,39333,39708,39291,39833,39875,39708,39250,39542,39125,39375,39458,39375,39375,39000,39333,40083
1x3	2	head	40042,39500,38583,39333,39292,39625,39542,39708,39417,39167,39000,39084,39250,38917,39250,39125,39708,39291,39041,39500
1x3	3	head	39959,39292,38916,39125,39208,39583,39167,39167,39375,38959,39084,39500,39250,39208,39583,39666,39625,39083,39000,39125
1x3	4	base	40292,39416,40334,40000,40167,39875,40042,39792,40041,39875,39750,40333,40250,39708,40125,40209,39750,39209,39417,39709
1x3	5	head	38833,38792,38583,38917,38708,38792,39250,39167,39375,38667,38750,38625,38833,38583,38667,39000,38625,38792,38875,38708
1x3	6	base	39458,39167,39000,39584,39625,39791,39917,39917,39375,39375,39667,39208,39334,39375,39375,39708,40375,39500,40292,38625
1x3	7	base	39833,39458,39417,39917,39750,39458,40000,39584,39292,39500,39292,39000,39500,39916,39708,39500,39917,39959,39000,38958
1x3	8	head	38958,39083,39291,40125,38958,39000,39041,39208,39917,38917,39125,39166,39500,39500,39000,39292,38916,38791,39083,39875
1x3	9	base	39792,39750,39791,39750,38792,39125,39292,39459,39833,39750,39750,39583,39959,39666,39583,39500,39583,39458,39417,39208
1x3	10	head	39625,38625,39250,39083,39208,39417,39500,39083,39209,38750,39084,38625,39042,38417,39166,39084,39167,39209,38917,39041
1x3	11	head	38875,38958,39333,39417,39000,38958,39209,39167,38833,38792,38917,39083,38709,39291,39167,39208,39042,38625,38791,39208
1x3	12	base	40208,39291,39167,39791,39750,39209,39375,39166,39625,39208,40000,39875,39708,39375,40125,39500,39458,39000,39125,39041
1x3	13	head	39625,38916,39250,38833,39208,39750,39542,39292,39292,39000,39500,38834,38875,38834,39250,39166,39500,38875,39375,39042
1x3	14	base	40000,40125,39167,38917,39417,39833,39708,39500,39834,39333,39458,39584,40250,39042,39542,39333,39583,39708,39291,39167
1x3	15	base	39250,39250,39750,39375,39875,39083,39542,39792,39458,39334,39125,39875,39667,38958,39250,39375,39167,39208,39583,38833
1x3	16	head	39834,38958,38959,38875,38750,39083,39083,39917,39417,39291,39292,39167,39084,39125,39292,39125,39458,38958,39375,39041
1x4	1	base	39125,39333,38875,38792,38917,39041,39041,40250,39583,38959,38834,39000,39041,38959,39084,39208,39625,38833,39916,38792
1x4	2	head	39292,39125,39208,39792,39709,38958,39375,39208,40042,39583,39084,39125,39083,39584,39958,39583,39458,38916,39292,39541
1x4	3	head	39375,38958,39334,39334,39042,39167,39000,39541,39292,39041,38958,39042,39084,39167,39417,39208,39334,39041,39167,39042
1x4	4	base	39209,39209,39375,38625,39625,39292,39083,39250,39458,38875,39542,39083,38875,39167,39416,39375,39083,39125,38917,39250
1x4	5	head	39917,38625,38292,38958,39208,39125,39292,39375,39583,38959,38708,39084,38750,38750,39250,39333,39209,38709,39416,39250
1x4	6	base	39250,38958,38709,38916,38917,39500,39750,39250,39416,39250,38667,38625,39334,39125,39542,39417,39375,39291,39167,39292
1x4	7	base	39625,38792,39542,40084,39625,39709,39458,39166,39459,39292,39333,39417,38917,38917,39542,39708,39208,38875,38625,39125
1x4	8	head	38958,39292,39500,39250,39375,39000,39208,39292,39000,39209,38833,39167,39334,39125,38959,39541,39458,39375,39167,38959
1x4	9	base	39167,39250,39125,39333,39375,39084,39125,39500,39417,38917,39084,39084,39292,39458,39375,38833,39250,39375,38500,39291
1x4	10	head	39250,39500,38916,39167,38917,39208,39083,39333,40125,38959,38917,39125,39166,39125,39042,39541,39458,38958,39209,39042
1x4	11	head	39208,39250,39125,39209,39250,39500,39334,38958,39125,39084,39084,38875,39166,39500,39208,39416,39333,38959,39000,39000
1x4	12	base	39708,39375,39791,40083,39083,39209,39625,39917,40375,39458,39250,39375,39708,39750,39792,39292,39541,39375,39500,39791
1x4	13	head	39458,38667,39667,38625,39166,39000,39375,39208,39708,39000,39333,38958,39000,39750,39708,38750,38958,38625,39417,39042
1x4	14	base	39209,39125,38833,39292,39541,39000,39167,39625,39291,39250,39250,38667,39292,38542,38875,39583,38292,39167,39000,38750
1x4	15	base	39084,39375,39542,39250,39000,39375,38917,39125,38833,38583,38875,39125,39083,39375,39083,39709,39375,39166,38667,39208
1x4	16	head	39375,38917,38583,39125,39417,39209,39292,39291,39292,39250,39292,39125,39458,38958,38917,39416,39208,38834,39333,39292

Add agx_nir_fold_w8_byte_sum() for AGX3B and AGX3C (!emit_agx3d): when an
iadd reduction tree in a block sums all 3 or 4 u2u32 lanes of one or more
packed R8_UINT device loads (with >=2 loads or at least one vec4 load),
keep each load as a single packed 32-bit word (via
AGX_NIR_MEMORY_PACKED_COUNT_SHIFT) and accumulate up to 32 words in two
16-bit SWAR lanes (max 32 * 510 = 16320 <= 65535, zero cross-lane carry)
before one final (s & 0xffff) + (s >> 16) reduction. Preserves exact
mod-2^32 semantics while cutting per-byte unpack ALU and improving load
batching.

Signed-off-by: Diljit Singh <hey@djdev.me>
(cherry picked from commit 179980b)
(cherry picked from commit 8bf65d33f3f9be031e4bb442423e31ff5da29bc6)
@satchlj

satchlj commented Dec 2, 2026

Copy link
Copy Markdown
Member

Tested exact head 54cd94a79ded (stacked on #1541 e135858eb62b) on Apple M5 Max (J775c, G17C B1, kernel 7.3.0-1-54.20261202-ARCH, default command line):

  • Native AArch64 assert-enabled build & compiler tests (buildtype=debugoptimized, b_ndebug=false, gallium-drivers=asahi, vulkan-drivers=asahi):
    • Full Meson test suite: 343/343 OK, 0 Fail.
    • Targeted agx_tests (*Wave8*:*W8*:*Byte*:*Vision*): 184/184 PASS, including AGX3VisionOpts.Wave8SwarW8ByteSum.
  • Live G17C B1 Borealis Vulkan CTS & RT (isolated release ICD at 54cd94a79ded, MESA_SHADER_CACHE_DISABLE=true):
    • dEQP-VK.api.smoke.*: 8/8 PASS (0 fail).
    • dEQP-VK.subgroups.basic.compute.*: 12/12 PASS (0 fail).
    • gpubench-rt-vk: raster median 2,265.69 fps (crc32=cca69c73), medium median 138.7 fps (336.2 Mray/s, crc32=e91ea686).
  • Same-boot isolated release ABBA llama-bench (llama-2-7b.Q4_0, -fa 1, -r 3) vs #1541 base e135858eb62b at parity:
    • Default NV_coopmat2: pp128 792.10 → 791.84 t/s (-0.03%), pp512 878.20 → 878.34 t/s (+0.02%), tg32 122.26 → 122.46 t/s (+0.16%).
    • GGML_VK_DISABLE_COOPMAT=1: pp128 373.89 → 373.86 t/s (-0.01%), pp512 776.46 → 775.68 t/s (-0.10%), tg32 122.35 → 122.47 t/s (+0.10%).
  • Faults / stability: 0 GPU faults and 0 kernel oops/WARN in dmesg.

Scope note: this row covers the native AArch64 assert-enabled compiler/unit test suite (including Wave8SwarW8ByteSum) and G17C B1 Borealis Vulkan compute/graphics/RT/LLM no-regression parity; it did not re-run the standalone byte-sum microbenchmark from the PR description.

@RyanTheTide

Copy link
Copy Markdown
Author

J713 (M4 Air, G16G B0) saved fleet94 on exact 54cd94a79ded against e135858eb62b. All readbacks and padding matched. Longer runs of the two-vector workload used 256 dispatches per sample and 24 warmup batches. Across eight balanced blocks the median paired change was −0.85%, and the pooled change was −0.88%; two blocks varied slightly above zero. No faults or kernel entries.

The byte-store parent has a measured J713 reverse-store slowdown. I’m testing this byte-sum change directly on aurora-wip and will link the replacement here.

@RyanTheTide

Copy link
Copy Markdown
Author

Superseded by #1548, which carries Dj’s horizontal byte-sum commit directly on aurora-wip. The exact standalone head passed Neo and J713. The two-vector median improved 1.26% and 2.84% respectively. The byte-store slowdown remains tracked on #1541.

@RyanTheTide RyanTheTide closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants