Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions megatron/core/transformer/cuda_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ class _CudagraphGlobalRecord:
"""A pool-like data structure to reuse input and output buffers across cudagraph."""
tensor_reuse_pool = TensorReusePool()

@classmethod
def reset(cls):
"""Reset all global tracking state. Only necessary for testing."""
cls.cudagraph_created = False
cls.cudagraph_record = []
cls.cudagraph_inference_record = []
cls.mtp_cudagraph_managers.clear()

@classmethod
def record_fwd_graph(cls, runner, args, kwargs, out):
"""Record a fwd graph to 'cudagraph_record"""
Expand Down Expand Up @@ -533,9 +541,7 @@ def delete_cuda_graphs():
mgr.inference_cudagraphs_lookup_table.clear()

# Reset global tracking state
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.cudagraph_inference_record = []
_CudagraphGlobalRecord.reset()

# TODO: Optional?: Force garbage collection to clean up memory
gc.collect()
Expand Down
7 changes: 2 additions & 5 deletions tests/unit_tests/inference/engines/test_dynamic_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ def _build_test_env(cls, test_config):
)

# Reset global cuda graph state.
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None

# Inference engine.
Expand Down Expand Up @@ -4500,9 +4499,7 @@ def _create_model(self, model_provider, num_cuda_graphs):

def _reset_cuda_graph_state(self, model):
"""Reset all CUDA graph global and per-module state."""
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.cudagraph_inference_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
for module in model.modules():
if isinstance(module, CudaGraphManager):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ def _build_engine(
vocab_size=VOCAB_SIZE, detokenize=lambda tokens: "tokenized_prompt"
),
)
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.cudagraph_inference_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
for module in model.modules():
if isinstance(module, CudaGraphManager):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ def _create_model(self, model_type, num_cuda_graphs=None):

def _reset_cuda_graph_state(self, model):
"""Reset all CUDA graph global and per-module state."""
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.cudagraph_inference_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
for module in model.modules():
if isinstance(module, CudaGraphManager):
Expand Down Expand Up @@ -360,9 +358,7 @@ def _create_hybrid_model(self, num_cuda_graphs=None):

def _reset_cuda_graph_state(self, model):
"""Reset all CUDA graph global and per-module state."""
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.cudagraph_inference_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
for module in model.modules():
if isinstance(module, CudaGraphManager):
Expand Down
3 changes: 1 addition & 2 deletions tests/unit_tests/rl/test_rl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,7 @@ def test_get_logprobs_cuda_graphs(self, initialize_model_parallel):
# Ensure all pending work is complete and graph destruction runs now
torch.cuda.synchronize()

_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
CudaGraphManager.fwd_mempools = None
CudaGraphManager.bwd_mempools = None
Expand Down
12 changes: 4 additions & 8 deletions tests/unit_tests/transformer/test_cuda_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def setup_method(self, method):

def teardown_method(self, method):
Utils.destroy_model_parallel()
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None

@pytest.mark.skipif(
Expand Down Expand Up @@ -270,8 +269,7 @@ def test_cuda_graph_determine_first_last_layer_logic(

# Teardown
Utils.destroy_model_parallel()
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()
CudaGraphManager.global_mempool = None
CudaGraphManager.fwd_mempools = None
CudaGraphManager.bwd_mempools = None
Expand Down Expand Up @@ -358,8 +356,7 @@ def setup_method(self, method):

def teardown_method(self, method):
Utils.destroy_model_parallel()
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()

@pytest.mark.skipif(
not (HAVE_TE and is_te_min_version("1.5.0")),
Expand Down Expand Up @@ -500,8 +497,7 @@ def get_mamba_block(hybrid_layer_pattern):

def teardown_method(self, method):
Utils.destroy_model_parallel()
_CudagraphGlobalRecord.cudagraph_created = False
_CudagraphGlobalRecord.cudagraph_record = []
_CudagraphGlobalRecord.reset()

@pytest.mark.skipif(
not (HAVE_TE and is_te_min_version("1.5.0")),
Expand Down