feat(wgpu): add global stable sorting - #10
Conversation
# Conflicts: # src/runtime/wgpu/shaders/sort_f32.wgsl # src/runtime/wgpu/shaders/sort_i32.wgsl # src/runtime/wgpu/shaders/sort_u32.wgsl
|
Reviewed this properly — a sorting network isn't something you can eyeball, so I re-implemented the shader logic standalone (pack → tiled 512 pass → global stages → scatter, including the key transform, padding-key selection and the index tiebreak) and diffed it against a stable reference. It's correct. u32/i32/f32 at 513, 700, 1024, 1025, 1536 and 2049, both orders, duplicate-heavy inputs, NaN/±0.0/±inf, and multi-segment Two things I looked at hard and was happy with: the tiled pass XNORing the stage direction with tile parity isn't the textbook per-stage direction, but it's endpoint-equivalent, since flipping every comparator gives you the reverse-sorted tile that the global k=512 stage expects. And the padding keys do collide with real extremes ( Two things I'd like fixed before merge — project conventions rather than bugs:
Performance notes, not merge gates:
One minor thing: the Good work — this is the scope we settled on in #8 and it landed clean. |
|
Addressed in 1a84747: moved the global launcher and GlobalSortParams into sort_global.rs; replaced the temporary-output Option/expect proof with structurally owned fallback buffers; and returned empty outputs before buffer lookup for zero-element tensors. Coverage includes sort, argsort, and sort_with_indices on [0, 1024]. Validation: 23 active sorting tests passed; the ignored 1,000,003-element validator passed; all 913 library tests passed; strict Clippy and formatting passed. |
Summary
Closes #8.
sort,sort_with_indices, andargsortU32,I32, andF32, both orders, and arbitrary axes-0.0/+0.0The branch is rebased onto the NaN-ordering fix from #9 and leaves the <=512 comparator owned by the existing shared
sort_cmp.rsmachinery.topkremains out of scope.Validation
cargo test --offline --release --features wgpu --test backend_parity_all backend_parity::sort -- --nocapture: 22 passed, 1 ignoredcargo test --offline --features wgpu --lib: 913 passedcargo fmt --all --check: passedCoverage includes CPU/WGPU parity for all three supported dtypes, ascending and descending order, arbitrary axes, duplicate stability, NaNs, signed zero, and all three sorting APIs.
WGPU timing diagnostic
RTX 4070 Ti SUPER, driver 591.86. Five independent processes. Each process validates output before timing. Boundary is the public sort call plus queue completion; input upload and output readback are excluded.
The one-million case is visibly bimodal, so these results establish scaling/capability rather than a stable throughput or speedup claim. Before this patch, WGPU sorting axes above 512 returned
BackendLimitation; there is no runnable large-axis WGPU baseline.