-
Notifications
You must be signed in to change notification settings - Fork 1
perf(gpu): grind the proof-of-work nonce on the GPU #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ColoCarletti
wants to merge
1
commit into
main
Choose a base branch
from
feat/gpu-grinding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! GPU proof-of-work grinding: a parallel Keccak nonce search that mirrors the | ||
| //! host `stark::grinding::generate_nonce`, offloading the ~2^grinding_factor | ||
| //! hashes it does per table per epoch from the CPU (where they dominate the | ||
| //! prove) to the otherwise-idle GPU. | ||
|
|
||
| use cudarc::driver::{LaunchConfig, PushKernelArg}; | ||
|
|
||
| use crate::device::backend; | ||
|
|
||
| const BLOCK_DIM: u32 = 256; | ||
| const GRID_DIM: u32 = 1024; | ||
|
|
||
| /// Below this grinding factor the CPU search finds a valid nonce in well under | ||
| /// a microsecond, so a device launch + shared-stream `synchronize` (which also | ||
| /// stalls whatever a rayon peer queued on that stream) is pure loss. Bounce | ||
| /// those to the CPU. The production factor is 20; only tests use tiny factors. | ||
| const GRIND_MIN_FACTOR: u8 = 12; | ||
|
|
||
| /// Smallest nonce whose grind head is `< limit`, or `None` when the CUDA path | ||
| /// is unavailable/errors (the caller then runs the CPU search). | ||
| /// | ||
| /// `inner_lanes` are the four little-endian-read u64 lanes of the 32-byte | ||
| /// `inner_hash` (`get_inner_hash` on the host). `grinding_factor` (1..=64) | ||
| /// fixes `limit = 1 << (64 - grinding_factor)` and sizes the search: the | ||
| /// expected first valid nonce is ~`2^grinding_factor`, so each launch scans a | ||
| /// contiguous block several times that, from 0 upward, and the first block that | ||
| /// hits yields the globally smallest valid nonce (the kernel `atomicMin`s it). | ||
| pub fn generate_nonce_gpu(inner_lanes: &[u64; 4], grinding_factor: u8) -> Option<u64> { | ||
| if !(GRIND_MIN_FACTOR..=64).contains(&grinding_factor) { | ||
| return None; | ||
| } | ||
| let limit: u64 = 1u64 << (64 - grinding_factor); | ||
|
|
||
| let be = backend().ok()?; | ||
| let stream = be.next_stream(); | ||
| let inner_dev = stream.clone_htod(inner_lanes.as_slice()).ok()?; | ||
|
|
||
| // Per-launch block size: ~8× the expected hit distance, clamped so tiny | ||
| // factors still launch a full grid and huge factors don't ask for an | ||
| // absurd single block. `2^grinding_factor` can overflow u64 (factor 64), so | ||
| // saturate. | ||
| let expected = 1u64.checked_shl(grinding_factor as u32).unwrap_or(u64::MAX); | ||
| let count = expected.saturating_mul(8).clamp(1 << 18, 1 << 28); | ||
|
|
||
| let cfg = LaunchConfig { | ||
| grid_dim: (GRID_DIM, 1, 1), | ||
| block_dim: (BLOCK_DIM, 1, 1), | ||
| shared_mem_bytes: 0, | ||
| }; | ||
|
|
||
| // One reusable device slot for the running minimum, reset to the sentinel | ||
| // (U64_MAX) before each block rather than reallocated every iteration. | ||
| // `sentinel` is a named binding so it outlives every async H2D below. | ||
| let sentinel = [u64::MAX]; | ||
| let mut result_dev = stream.clone_htod(&sentinel).ok()?; | ||
|
|
||
| let mut base: u64 = 0; | ||
| loop { | ||
| stream.memcpy_htod(&sentinel, &mut result_dev).ok()?; | ||
| unsafe { | ||
| stream | ||
| .launch_builder(&be.grind_search) | ||
| .arg(&inner_dev) | ||
| .arg(&limit) | ||
| .arg(&base) | ||
| .arg(&count) | ||
| .arg(&mut result_dev) | ||
| .launch(cfg) | ||
| .ok()?; | ||
| } | ||
| let host = stream.clone_dtoh(&result_dev).ok()?; | ||
| stream.synchronize().ok()?; | ||
| if host[0] != u64::MAX { | ||
| return Some(host[0]); | ||
| } | ||
| // Nothing in `[base, base+count)` — advance. Bail (→ CPU fallback) if | ||
| // the block would run past u64, matching the host search's finite range. | ||
| base = base.checked_add(count)?; | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //! Parity: the GPU proof-of-work nonce search must agree with the host | ||
| //! predicate. Runs on the merge-queue GPU box via `make test-math-cuda` | ||
| //! (`cargo test -p math-cuda --release`) — `device::backend()` inside | ||
| //! `generate_nonce_gpu` requires a real GPU, like the other tests here. | ||
| //! | ||
| //! Uses real grinding factors (>= the min-factor gate). The end-to-end prover | ||
| //! suite only exercises `grinding_factor: 1`, where `limit = 1 << 63` lets a | ||
| //! broken kernel return an accepted nonce ~half the time; these factors make a | ||
| //! wrong kernel fail deterministically. | ||
|
|
||
| use stark::grinding::{get_inner_hash, is_valid_nonce}; | ||
|
|
||
| fn lanes_for(seed: &[u8; 32], factor: u8) -> [u64; 4] { | ||
| let inner = get_inner_hash(seed, factor); | ||
| core::array::from_fn(|i| u64::from_le_bytes(inner[i * 8..i * 8 + 8].try_into().unwrap())) | ||
| } | ||
|
|
||
| /// At a moderate factor the kernel returns a valid nonce, and it is the | ||
| /// smallest one (the exhaustive CPU scan below it is cheap at factor 14). | ||
| #[test] | ||
| fn gpu_grind_returns_smallest_valid_nonce() { | ||
| let seed = [14u8; 32]; | ||
| let factor = 14u8; | ||
| let nonce = math_cuda::grinding::generate_nonce_gpu(&lanes_for(&seed, factor), factor) | ||
| .expect("GPU grind (needs a GPU)"); | ||
| assert!( | ||
| is_valid_nonce(&seed, nonce, factor), | ||
| "GPU nonce {nonce} fails is_valid_nonce (factor {factor})" | ||
| ); | ||
| assert!( | ||
| (0..nonce).all(|n| !is_valid_nonce(&seed, n, factor)), | ||
| "GPU nonce {nonce} is not the smallest valid nonce (factor {factor})" | ||
| ); | ||
| } | ||
|
|
||
| /// At the production factor the kernel returns a valid nonce (validity only — | ||
| /// scanning 0..nonce would be ~2^20 hashes). | ||
| #[test] | ||
| fn gpu_grind_valid_at_production_factor() { | ||
| let seed = [20u8; 32]; | ||
| let factor = 20u8; | ||
| let nonce = math_cuda::grinding::generate_nonce_gpu(&lanes_for(&seed, factor), factor) | ||
| .expect("GPU grind (needs a GPU)"); | ||
| assert!( | ||
| is_valid_nonce(&seed, nonce, factor), | ||
| "GPU nonce {nonce} fails is_valid_nonce (factor {factor})" | ||
| ); | ||
| } | ||
|
|
||
| /// Below the min-factor gate the GPU path declines (→ CPU search), so the tiny | ||
| /// factors every non-GPU-benchmark test uses never pay a launch. | ||
| #[test] | ||
| fn gpu_grind_declines_below_min_factor() { | ||
| let seed = [1u8; 32]; | ||
| assert!( | ||
| math_cuda::grinding::generate_nonce_gpu(&lanes_for(&seed, 1), 1).is_none(), | ||
| "GPU grind should decline factor 1" | ||
| ); | ||
| } |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.