Skip to content

Speed up the u64 mining kernel ~1.8x on Apple Metal - #87

Merged
n13 merged 1 commit into
mainfrom
n13/metal-kernel-optimizations
Aug 31, 2026
Merged

n13 merged 1 commit into
mainfrom
n13/metal-kernel-optimizations

Conversation

@n13

@n13 n13 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Overview

Apple M5 Pro (20-core GPU), quantus-miner benchmark --gpu-devices 1 --cpu-workers 0, same machine back to back:

10 s 30 s
main 24.0 MH/s (242M hashes) —
this PR 43.2 MH/s (435M hashes) 43.4 MH/s (1.305G hashes)

One file changed: crates/engine-gpu/src/mining_u64.wgsl. Hash output is unchanged and bit-exact against the CPU reference. NVIDIA and modern AMD run the same shader via Vulkan, so they get these changes too (fewer ops there as well, but not measured in this PR).

What changed (crates/engine-gpu/src/mining_u64.wgsl)

Field arithmetic:

  • Deferred-carry additions (Acc): the external layer and the internal row sum accumulate unreduced 64-bit sums with a carry counter and fold once per output, instead of two ε-corrections per gf64_add.
  • gf64_mul_add: the row sum (as an Acc) is folded into the 128-bit product before a single plain gf64_reduce. Valid for the diagonal constants (b <= 2^64 - 2^32), documented on the function.
  • Compare-free 128-bit product: mul_wide / gf64_sqr assemble lo/hi from 32-bit-valued partial sums with no carry selects.

Kernel structure — on Apple's compiler, code size mattered more than op count; every unrolled variant measured slower:

  • One loop drives all 30 rounds so the external layer, S-box and internal layer are each emitted once. Round constants are added by the linear layer preceding each S-box (RC_EXT table; RC_INTERNAL padded with a trailing zero).
  • One S-box site with a runtime lane count (sbox_lanes: 12 for external rounds, 1 for internal).
  • mining_main runs pad / first squeeze / second squeeze through a single inlined permute64 call in a phase loop; the lazy-second-squeeze reject path is preserved.

Measured and rejected (slower or neutral): uniform buffers and unhoisted loads, workgroup sizes 64/128/512, the 8-add/4-shift MDS, a one-compare reduce, fully written-out layers, two nonces per thread, larger default batch (≥2M is flat; 1M costs <1% through the CLI).

Validation

  • examples/gpu_cpu_parity 25/25 jobs, every GPU hash verified against pow_core::hash_from_nonce (includes the 2^256 boundary job)
  • cargo run -p engine-gpu component suite: both shader variants pass
  • cargo test --workspace --release, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all -- --check, taplo fmt --check: clean
  • Bench method: each variant built once, then interleaved runs with medians (GPU thermal drift on this machine is ±10% over minutes, larger than several of the individual steps)

Risks and mitigations

  • Arithmetic bounds (Acc carries ≤ 27, gf64_mul_add precondition) are stated in comments and exercised by the component suite and parity example.
  • Only the u64 shader changed; mining.wgsl (32-bit fallback) is untouched.
  • Not measured on NVIDIA/AMD in this PR.

Follow-ups

  • Run the gpu-bench sweep on an NVIDIA card to confirm the Vulkan path gains.

Apple M5 Pro, CLI benchmark: 24.0 -> 43.2 MH/s. Kernel output is
unchanged and bit-exact against the CPU reference.

Field arithmetic:
- Deferred-carry additions (Acc): the external layer and the internal
  row sum accumulate unreduced with a carry counter and fold once per
  output instead of two epsilon corrections per add.
- gf64_mul_add folds the row sum into the 128-bit product before a
  single plain reduction.
- mul_wide / gf64_sqr assemble the 128-bit product from 32-bit-valued
  partial sums with no carry compares.

Kernel structure (code size dominates on Apple's compiler; every
unrolled variant measured slower):
- One loop drives all 30 rounds so each layer is emitted once, with
  round constants added by the linear layer preceding each S-box
  (RC_EXT table, RC_INTERNAL padded with a trailing zero).
- One S-box site with a runtime lane count (12 external, 1 internal).
- mining_main runs pad / first squeeze / second squeeze through a
  single inlined permute64 call in a phase loop.

Measured and rejected: uniform buffers and unhoisted loads, workgroup
sizes 64/128/512, 8-add MDS, one-compare reduce, fully unrolled
layers, two nonces per thread.
@n13 n13 added the bot-review label Aug 25, 2026

@n13 n13 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewer model: GPT Sol

Verdict (advisory): Approve

No blocking findings. The deferred-carry/fused-reduction arithmetic preserves the Goldilocks residue, the shifted round constants still feed the same S-box rounds, and the consolidated squeeze loop preserves the strict target comparison and lazy second-squeeze path.

Non-blocking documentation nit: crates/engine-gpu/src/mining_u64.wgsl:148-150 says the external accumulator has at most 27 carries. Including the fused round constant, the conservative full-range maximum is 28. This does not affect correctness—the u32 counter and acc_fold handle 28—but the bound should be updated when convenient.

Validation:

  • git diff --check 8d0c9f8...b84101c passed.
  • cargo run --locked -p engine-gpu passed every component group against both shader variants, including 67 full permutations, 250 squeeze-twice hashes, and end-to-end CPU verification.
  • cargo run --locked -p engine-gpu --example gpu_cpu_parity --release -- 25 passed 25/25 CPU-verified jobs, including the 2^256 nonce-boundary case.
  • An independent 200,000-case randomized check of the wide multiply, reduction, accumulator fold, fused multiply-add, and external layer matched exact integer/modular arithmetic.
  • Matched 10-second Apple M4 runs improved from 12.41 MH/s at the base to 18.78 MH/s at this head (~51%). The exact M5 ratio remains hardware-specific; NVIDIA/AMD were not available locally.
  • All four GitHub CI checks are successful at b84101c.

@n13 n13 removed the bot-review label Aug 25, 2026
@n13
n13 merged commit ea5e5e7 into main Aug 31, 2026
4 checks passed
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.

2 participants