@@ -573,7 +573,7 @@ def test_v2_profile_is_noop_on_non_cuda(monkeypatch):
573573 assert runner .cudagraph_memory_graph_pool_estimate == 0
574574
575575
576- def test_v2_cuda_graph_pool_sample_uses_peak (monkeypatch ):
576+ def test_v2_cuda_graph_pool_sample_separates_residual_and_peak (monkeypatch ):
577577 from vllm .v1 .worker .gpu import model_runner as gpu_model_runner_v2
578578 from vllm .v1 .worker .gpu .model_runner import GPUModelRunner
579579
@@ -623,18 +623,77 @@ def test_v2_cuda_graph_pool_sample_uses_peak(monkeypatch):
623623 lambda device : 530 ,
624624 )
625625
626- assert (
627- runner . _measure_cuda_graph_pool_sample ( lambda : events . append ( "capture" )) == 120
628- )
626+ sample = runner . _measure_cuda_graph_pool_sample ( lambda : events . append ( "capture" ))
627+ assert sample . residual == 50
628+ assert sample . peak == 120
629629 assert events == [
630630 "sync" ,
631631 "empty_cache" ,
632632 "reset_peak" ,
633633 "capture" ,
634634 "sync" ,
635+ "empty_cache" ,
635636 ]
636637
637638
639+ def test_v2_graph_pool_profile_does_not_multiply_transient_peak (monkeypatch ):
640+ from vllm .v1 .worker .gpu import model_runner as gpu_model_runner_v2
641+ from vllm .v1 .worker .gpu .cudagraph_utils import BatchExecutionDescriptor
642+ from vllm .v1 .worker .gpu .model_runner import (
643+ CudaGraphMemorySample ,
644+ CUDAGraphMode ,
645+ GPUModelRunner ,
646+ )
647+
648+ descs = [
649+ BatchExecutionDescriptor (CUDAGraphMode .PIECEWISE , 128 , None ),
650+ BatchExecutionDescriptor (CUDAGraphMode .PIECEWISE , 64 , None ),
651+ BatchExecutionDescriptor (CUDAGraphMode .PIECEWISE , 32 , None ),
652+ ]
653+
654+ class FakeCudaGraphManager :
655+ pool = "manager-original"
656+ graphs = {}
657+ _graphs_captured = False
658+ hidden_states = None
659+ aux_hidden_states = []
660+ intermediate_tensors = None
661+ use_aux_hidden_state_outputs = False
662+ use_breakable_cg = False
663+ breakable_cg_runner = None
664+
665+ def get_capture_descs (self ):
666+ return [(CUDAGraphMode .PIECEWISE , descs )]
667+
668+ runner = GPUModelRunner .__new__ (GPUModelRunner )
669+ runner .cudagraph_manager = FakeCudaGraphManager ()
670+ samples = iter (
671+ [
672+ CudaGraphMemorySample (residual = 10 << 20 , peak = 200 << 20 ),
673+ CudaGraphMemorySample (residual = 2 << 20 , peak = 180 << 20 ),
674+ ]
675+ )
676+
677+ monkeypatch .setattr (
678+ gpu_model_runner_v2 .current_platform ,
679+ "graph_pool_handle" ,
680+ lambda : "profile-pool" ,
681+ )
682+ monkeypatch .setattr (
683+ gpu_model_runner_v2 .torch .accelerator ,
684+ "empty_cache" ,
685+ lambda : None ,
686+ )
687+ monkeypatch .setattr (
688+ runner ,
689+ "_measure_cuda_graph_pool_sample" ,
690+ lambda capture_fn : capture_fn () or next (samples ),
691+ )
692+ monkeypatch .setattr (runner , "_capture_model_cudagraphs" , lambda ** kwargs : None )
693+
694+ assert runner ._profile_cudagraph_memory_graph_pool () == 204 << 20
695+
696+
638697def test_v2_graph_pool_profile_restores_capture_state (monkeypatch ):
639698 from vllm .v1 .worker .gpu import model_runner as gpu_model_runner_v2
640699 from vllm .v1 .worker .gpu .cudagraph_utils import BatchExecutionDescriptor
@@ -680,7 +739,16 @@ def init_breakable_cg_runner(self, model):
680739 runner = GPUModelRunner .__new__ (GPUModelRunner )
681740 runner .model = "model"
682741 runner .cudagraph_manager = FakeCudaGraphManager ()
683- sample_values = iter ([1_000 , 2_000_000 , 800 , 3_000_000 ])
742+ from vllm .v1 .worker .gpu .model_runner import CudaGraphMemorySample
743+
744+ sample_values = iter (
745+ [
746+ CudaGraphMemorySample (residual = 1_000 , peak = 10_000 ),
747+ CudaGraphMemorySample (residual = 2_000_000 , peak = 200_000_000 ),
748+ CudaGraphMemorySample (residual = 800 , peak = 8_000 ),
749+ CudaGraphMemorySample (residual = 3_000_000 , peak = 300_000_000 ),
750+ ]
751+ )
684752 capture_calls = []
685753
686754 monkeypatch .setattr (
@@ -727,7 +795,7 @@ def capture_model_cudagraphs(**kwargs):
727795
728796 estimate = runner ._profile_cudagraph_memory_graph_pool ()
729797
730- assert estimate == 7_001_000
798+ assert estimate == 7_010_000
731799 assert [call ["capture_descs" ] for call in capture_calls ] == [
732800 {CUDAGraphMode .PIECEWISE : [piecewise_descs [0 ]]},
733801 {CUDAGraphMode .PIECEWISE : [piecewise_descs [1 ]]},
0 commit comments