Skip to content

Commit 1e7fffb

Browse files
committed
CENO_GPU_MEM_TRACKING
1 parent 8505527 commit 1e7fffb

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

ceno_zkvm/src/scheme/gpu/memory.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,44 @@ use gkr_iop::gpu::{
1414
gpu_prover::{BB31Ext, CudaHalBB31, MemTracker},
1515
};
1616
use mpcs::PolynomialCommitmentScheme;
17+
use std::sync::OnceLock;
1718

1819
use crate::scheme::scheduler::{ChipProvingMode, get_chip_proving_mode};
1920

20-
const ESTIMATION_TOLERANCE_BYTES: usize = 1024 * 1024; // max estimation error: 1 MB
21-
const ESTIMATION_SAFETY_MARGIN_BYTES: usize = 5 * 1024 * 1024; // reserved headroom: 5 MB, 1MB for each sub-stage
21+
#[cfg(feature = "gpu")]
22+
static MEM_TRACKING_MODE: OnceLock<bool> = OnceLock::new();
23+
24+
#[cfg(feature = "gpu")]
25+
pub fn get_mem_tracking_mode() -> bool {
26+
*MEM_TRACKING_MODE.get_or_init(|| match std::env::var("CENO_GPU_MEM_TRACKING").as_deref() {
27+
Ok("1") => true,
28+
_ => false,
29+
})
30+
}
2231

2332
#[cfg(feature = "gpu")]
2433
pub fn start_gpu_mem_tracking<'a>(
2534
cuda_hal: &'a CudaHalBB31,
2635
label: &'static str,
2736
) -> Option<MemTracker<'a>> {
28-
if get_chip_proving_mode() == ChipProvingMode::Sequential {
37+
let is_sequential = get_chip_proving_mode() == ChipProvingMode::Sequential;
38+
let is_mem_tracking = get_mem_tracking_mode();
39+
if is_sequential && is_mem_tracking {
2940
Some(cuda_hal.inner.mem_tracker(label))
3041
} else {
3142
None
3243
}
3344
}
3445

46+
const ESTIMATION_TOLERANCE_BYTES: usize = 1024 * 1024; // max estimation error: 1 MB
47+
const ESTIMATION_SAFETY_MARGIN_BYTES: usize = 5 * 1024 * 1024; // reserved headroom: 5 MB, 1MB for each sub-stage
48+
3549
/// Validate that the estimated GPU memory matches actual usage within tolerance.
3650
/// - Under-estimate (actual > estimated): diff must be <= `ESTIMATION_TOLERANCE_BYTES`
3751
/// - Over-estimate (estimated > actual): diff must be <= `ESTIMATION_SAFETY_MARGIN_BYTES`
3852
#[cfg(feature = "gpu")]
3953
pub fn check_gpu_mem_estimation(mem_tracker: Option<MemTracker>, estimated_bytes: usize) {
40-
// `mem_tracker will` be Some only in sequential mode, so if it's None, do nothing
54+
// `mem_tracker will` be Some only in sequential mode with mem tracking enabled, so if it's None, do nothing
4155
if let Some(mem_tracker) = mem_tracker {
4256
const ONE_MB: usize = 1024 * 1024;
4357
let label = mem_tracker.name();

ceno_zkvm/src/scheme/gpu/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,6 @@ where
720720
E: ExtensionField,
721721
PCS: PolynomialCommitmentScheme<E>,
722722
{
723-
let (resident, temporary) = estimate_trace_extraction_bytes(expected_num, num_vars);
724-
let estimated_bytes = resident + temporary;
725-
726723
let pcs_data_basefold: &BasefoldCommitmentWithWitnessGpu<
727724
BB31Base,
728725
BufferImpl<BB31Base>,
@@ -740,7 +737,8 @@ where
740737
.get_trace(&cuda_hal, pcs_data_basefold, trace_idx, stream.as_ref())
741738
.unwrap_or_else(|err| panic!("Failed to extract trace {trace_idx}: {err}"));
742739

743-
check_gpu_mem_estimation(gpu_mem_tracker, estimated_bytes);
740+
let (resident, temporary) = estimate_trace_extraction_bytes(expected_num, num_vars);
741+
check_gpu_mem_estimation(gpu_mem_tracker, resident + temporary);
744742

745743
let mles: Vec<Arc<MultilinearExtensionGpu<'a, E>>> = poly_group
746744
.into_iter()

0 commit comments

Comments
 (0)