@@ -14,30 +14,44 @@ use gkr_iop::gpu::{
1414 gpu_prover:: { BB31Ext , CudaHalBB31 , MemTracker } ,
1515} ;
1616use mpcs:: PolynomialCommitmentScheme ;
17+ use std:: sync:: OnceLock ;
1718
1819use 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" ) ]
2433pub 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" ) ]
3953pub 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 ( ) ;
0 commit comments