11 Commits

Author SHA1 Message Date
Nikolaus Heger
09323d33e4 Different kernels for Apple Metal vs Nvidia and everything else (#92)
* Select Apple Metal 4.0.1 kernel; keep 4.0.0 u64 elsewhere

NVIDIA was ~37% slower on the 4.0.1 Metal-tuned kernel. Split
Poseidon2 kernels and pick by backend:

- Metal + SHADER_INT64: v4.0.1 u64
- other SHADER_INT64: v4.0.0 u64
- no SHADER_INT64: 32-bit fallback (same as 4.0.0)

CI uploads the Linux release binary as an artifact so we can
bench NVIDIA without building on the GPU box.

* Drop version numbers from GPU kernel labels

Log native-u64 vs native-u64 Apple Metal without tying
the strings to release tags.
2026-09-01 23:21:43 +08:00
Nikolaus Heger
2b966e780a Revert "Fuzz GPU/CPU parity at scale and CPU-verify every seal before submission"
This reverts commit b0cfc3005e.
2026-08-12 15:41:38 +08:00
Nikolaus Heger
b0cfc3005e Fuzz GPU/CPU parity at scale and CPU-verify every seal before submission
Investigating reported seal submission failures. Root cause analysis:
the miner's job-id staleness chain (worker check, quic-loop filter,
node-side job_id compare) cannot mislabel a candidate — internal and
node job ids are updated atomically together. The remaining benign race
is node-side: a block-template rebuild (new pre_hash, same parent) does
not bump the node's job counter, so an in-flight seal mined on the old
template passes the job-id check and fails seal verification, logging
"Failed to submit seal". Fixing that belongs in the chain repo (bump the
job counter on template rebuild).

Miner-side hardening in this commit:

- gpu_cpu_parity is now a two-phase fuzzer. Phase 1 dumps FULL 512-bit
  hashes for randomized (header, nonce) pairs via the real
  midstate-resume datapath and verifies each against the canonical CPU
  implementation, multi-threaded (defaults to 1M hashes, ~5s; 10M runs
  in ~15s). Phase 2 fuzzes whole mining jobs with five profiles: random,
  carry-edge starts (saturated low limbs), 2^256-boundary crossings,
  impossible difficulty with exact hash-count assertion, and CPU-known
  solutions to catch false negatives. Seeded and reproducible; also
  asserts the submitted work bytes match the nonce.
  Verified clean: 10M bulk hashes + ~1,500 seals across 6 seeds.

- miner-service re-verifies every candidate on the CPU (one hash) before
  sending JobResult; an engine-produced invalid seal is never submitted
  and is logged loudly instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 14:24:05 +08:00
Nikolaus Heger
8aa81ec853 Disable naga runtime shader checks for the trusted mining shaders (+8-9%)
Exploring a fully native Metal port of the mining kernel showed it runs
~16% faster than the wgpu path at equal batch size. Decomposing that gap:
~9% is naga's injected runtime bounds checks and forced loop bounding,
~1.5% is mulhi()-based multiplication (inexpressible in WGSL), <1% is
the constant address space; the rest is binding/codegen residue.

The dominant term is recoverable inside wgpu on every backend:
the engine now builds its pipelines with create_shader_module_trusted +
ShaderRuntimeChecks::unchecked(). This is sound for our shaders: sources
are compiled into the binary, every buffer access is a constant-bounded
loop index into fixed-size bindings the engine allocates itself, all
loops have static bounds, and both variants remain covered by the
dual-shader component suite and the CPU parity example.

Apple M4: 11.39 -> 12.34 MH/s at the default 1M batch (+8.3%), 11.74 ->
12.73 MH/s at 8M (+8.4%). Cumulative vs main: 4.4x.

New examples/trusted_hashrate.rs measures checked vs trusted on the same
shader and CPU-verifies results.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 00:55:15 +08:00
Nikolaus Heger
d55b970ab6 Optimize GPU Poseidon2 mining 4.1x on Apple Silicon (u64 shader, midstate, lazy squeeze)
Three stacked optimizations, each verified bit-exact against the CPU
reference (qp-poseidon-core) by the dual-shader component suite and a
new GPU/CPU parity example:

- mining_u64.wgsl: native 64-bit Goldilocks arithmetic with plonky2-style
  lazy reduction, used automatically when the adapter supports
  SHADER_INT64 (all Apple Silicon, NVIDIA, modern AMD). mining.wgsl
  remains the 32-bit fallback. (+50%)
- Midstate precompute: the first two of five sponge permutations absorb
  the header and high nonce half, both constant per batch; they are now
  computed once on the CPU (pow_core::mining_midstate) and resumed on
  the GPU. Batches are clamped so nonces never carry past 2^256. (+77%)
- Lazy second squeeze: the first squeeze yields the most significant 256
  bits of the hash, which decide hash-vs-target unless exactly equal to
  the target's high half, so the common reject path skips the final
  permutation. (+56%)

Apple M4 throughput: 2.81 -> 11.4 MH/s at the default 1M batch
(criterion large_range_1m/gpu: 355.8ms -> 86.3ms, -75.7%).

Also:
- Fix criterion GPU bench crash: thread-local worker resources are now
  tagged with an engine id so multiple GpuEngines per process never mix
  devices.
- Fix stale end-to-end test harness (6-binding layout) and run the full
  component suite against both shader variants.
- New examples: hashrate, gpu_cpu_parity (25 jobs incl. 2^256-boundary
  crossing), gpu_features.
- Add Apple M5 family GPU tiers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-11 23:40:12 +08:00
illuzen
a47db3d1a2 Fix windows again (#72)
* handle lost device correctly

* Update lib.rs

* allow-integrated flag otherwise exclude

* DeviceLost is a status

* Integrated GPU skipped when discrete init fails

* nits
2026-06-30 16:30:11 +08:00
illuzen
5ce62b3e12 add gpu-throttle arg (#64)
* add gpu-throttle arg

* smarter sleep logic

* document cli args
2026-05-22 23:31:54 +08:00
illuzen
d7bf27854f GPU fixes (#58)
* improve logging

* don't let the queue spill, just drain it

* move cancellation out of the shader, since it doesn't work

* simplify batch size mechanism, improve logs

* fmt

* address review items
2026-04-22 13:42:01 +08:00
illuzen
443f0f9f89 QUIC miner (#49)
* implement quic

* simpler names

* simplify worker logic

* simplify gpu configuration logic

* miner initiates and picks random start nonce

* remove mining service

* clean up logs and remove unused engines

* clean up metrics

* metrics fix

* shared worker pool, cpu driven cancellation

just 10x the gpu thru-put, no more batch-size on gpu

* fmt

* non-local git

* taplo derp

* fix the benches

* handle stale results correctly in gpu

* more accurate gpu hashrate

* fmt

* remove unused

* handle duplicate solutions, cli controls gpu cancel frequency

* put plonky2 back

* demote noisy log
2026-02-02 16:38:21 +08:00
Nikolaus Heger
d14ebdf65c fix tests 2026-01-05 17:47:38 +08:00
illuzen
5a6de314ab add wgpu support (#28)
* ok wgpu runs hello world

* toy hasher

* separate wgsl file

* kinda poseidon2

* test vectors

* closer

* a bit of debug

* closer

* sort of almost

* fixed gf_mul

* merge gf_mul back into mining, clean up tests

* fix bytes_to_field_elements

* linear layer fixed

* maybe better?

* mds tests

* cleanup debug output

* found gf_mul bug

* fixed gf_mul bug

* efficient sbox again

* mds matches

* cleanup

* test internal layer

* cleaner

* external linear passes

* external layer tests

* fixed the constants

* moar tests

* more cleaning

* actually test just internal

* fairly clean tests now

* removed another pointless test

* more cleanup

* gf_from_const tests

* testing

* fixed permutations

* double hash tests, but they fail

* moar tests!

* hash-twice works, thanks Gemini 3 Pro!

* gpu runs but doesn't mine

* better logging

* lock workers to gpu

* send target to gpu not difficulty

* fix verify_nonce

* double hash failures

* double hash matches

* tests pass and it mines now

* fix benches

* a bit faster now

* improved logging

* fix benches

* simplified gpu code

* further simplification

* a bit simpler

* slight simplification

* a bit faster

* dynamic thread allocation and work coarsening

* more benching

* faster

* faster

* faster

* clean up documentation

* a bit faster

* simplify

* simplify

* optimized mds

* almost par with cpu now

* fmt

* clippy

* support multiple gpus

* clippy

* unused

* fixed some logs

---------

Co-authored-by: Nikolaus Heger <nheger@gmail.com>
2025-12-19 19:25:14 +08:00