Repository navigation
Speed up the u64 mining kernel ~1.8x on Apple Metal - #87
Merged
Merged
Conversation
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
commented
Aug 25, 2026
n13
left a comment
Contributor
Author
There was a problem hiding this comment.
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...b84101cpassed.cargo run --locked -p engine-gpupassed 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 -- 25passed 25/25 CPU-verified jobs, including the2^256nonce-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.
illuzen
approved these changes
Aug 31, 2026
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.
Overview
Apple M5 Pro (20-core GPU),
quantus-miner benchmark --gpu-devices 1 --cpu-workers 0, same machine back to back: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:
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 pergf64_add.gf64_mul_add: the row sum (as anAcc) is folded into the 128-bit product before a single plaingf64_reduce. Valid for the diagonal constants (b <= 2^64 - 2^32), documented on the function.mul_wide/gf64_sqrassemblelo/hifrom 32-bit-valued partial sums with no carryselects.Kernel structure — on Apple's compiler, code size mattered more than op count; every unrolled variant measured slower:
RC_EXTtable;RC_INTERNALpadded with a trailing zero).sbox_lanes: 12 for external rounds, 1 for internal).mining_mainruns pad / first squeeze / second squeeze through a single inlinedpermute64call 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_parity25/25 jobs, every GPU hash verified againstpow_core::hash_from_nonce(includes the 2^256 boundary job)cargo run -p engine-gpucomponent suite: both shader variants passcargo test --workspace --release,cargo clippy --workspace --all-targets -- -D warnings,cargo fmt --all -- --check,taplo fmt --check: cleanRisks and mitigations
Acccarries ≤ 27,gf64_mul_addprecondition) are stated in comments and exercised by the component suite and parity example.mining.wgsl(32-bit fallback) is untouched.Follow-ups