2727 get_default_load_sharded_strategy ,
2828)
2929from megatron .core .dist_checkpointing .validation import StrictHandling
30- from megatron .core .inference .contexts .static_context import StaticInferenceContext
31- from megatron .core .inference .engines .mcore_engine import MCoreEngine
32- from megatron .core .inference .model_inference_wrappers .gpt .gpt_inference_wrapper import (
33- GPTInferenceWrapper ,
34- )
35- from megatron .core .inference .text_generation_controllers .text_generation_controller import (
36- TextGenerationController ,
37- )
30+ from megatron .core .inference .apis import MegatronLLM
31+ from megatron .core .inference .config import InferenceConfig
3832from megatron .core .transformer .enums import AttnBackend
3933from megatron .core .transformer .module import MegatronModule
4034from megatron .core .transformer .transformer_config import MLATransformerConfig
@@ -390,27 +384,24 @@ def setup_model_and_tokenizer_for_inference(
390384
391385
392386class MCoreEngineWithCleanup :
393- """Wrapper around MCoreEngine that ensures proper cleanup of distributed resources.
387+ """Wrapper around MegatronLLM that ensures proper cleanup of distributed resources.
394388
395- This class delegates all operations to the underlying MCoreEngine while ensuring that
396- distributed resources are properly cleaned up when the engine is destroyed.
389+ This class delegates all operations to the underlying MegatronLLM engine while ensuring
390+ that distributed resources are properly cleaned up when the engine is destroyed.
397391 """
398392
399393 def __init__ (
400394 self ,
401- mcore_engine : MCoreEngine ,
402- model_inference_wrapper : GPTInferenceWrapper ,
395+ llm : MegatronLLM ,
403396 tokenizer : Union [MCoreTokenizerWrappper , MegatronTokenizer ],
404397 ):
405398 """Initialize the MCoreEngineWithCleanup.
406399
407400 Args:
408- mcore_engine (MCoreEngine): The underlying MCoreEngine instance
409- model_inference_wrapper (GPTInferenceWrapper): The model inference wrapper
401+ llm (MegatronLLM): The underlying MegatronLLM instance
410402 tokenizer (Union[MCoreTokenizerWrappper, MegatronTokenizer]): The tokenizer instance
411403 """
412- self .mcore_engine = mcore_engine
413- self .model_inference_wrapper = model_inference_wrapper
404+ self .mcore_engine = llm
414405 self .tokenizer = tokenizer
415406
416407 def __del__ (self ):
@@ -446,16 +437,16 @@ def create_mcore_engine(
446437 buffer_size_gb : float = 10.0 ,
447438 legacy_model_format : bool = False ,
448439 ** model_config_kwargs ,
449- ) -> Tuple [MCoreEngineWithCleanup , GPTInferenceWrapper , Union [MCoreTokenizerWrappper , MegatronTokenizer ]]:
450- """Set up the model, tokenizer and MCoreEngine for inference.
440+ ) -> Tuple [MCoreEngineWithCleanup , Union [MCoreTokenizerWrappper , MegatronTokenizer ]]:
441+ """Set up the model, tokenizer and MegatronLLM engine for inference.
451442
452443 Args:
453444 path (Path): Path to the checkpoint file
454445 params_dtype (torch.dtype): Data type for model parameters (default: torch.bfloat16)
455446 inference_batch_times_seqlen_threshold (int): Threshold for batch size times sequence length
456447 inference_max_seq_length (int): Maximum sequence length for inference
457448 max_batch_size (int): Maximum batch size for inference
458- random_seed (Optional[int]): Random seed for reproducibility
449+ random_seed (Optional[int]): Random seed for reproducibility (set globally during init)
459450 tensor_model_parallel_size (Optional[int]): Size of tensor model parallelism
460451 pipeline_model_parallel_size (Optional[int]): Size of pipeline model parallelism
461452 context_parallel_size (Optional[int]): Size of context parallelism
@@ -466,11 +457,10 @@ def create_mcore_engine(
466457 model_type (str): Type of model to load (default: "gpt")
467458 model_format (str): Format of model to load (default: "nemo")
468459 micro_batch_size (Optional[int]): Micro batch size for model execution
469- legacy_model_format (bool): Whether to use the legacy StaticInferenceEngine path in MCoreEngine (default: False )
460+ legacy_model_format (bool): Deprecated; no longer used (DynamicInferenceEngine is always used )
470461 Returns:
471- Tuple[MCoreEngineWithCleanup, GPTInferenceWrapper, Union[MCoreTokenizerWrappper, MegatronTokenizer]]: Tuple containing:
462+ Tuple[MCoreEngineWithCleanup, Union[MCoreTokenizerWrappper, MegatronTokenizer]]: Tuple containing:
472463 - MCoreEngineWithCleanup: Engine for text generation with proper cleanup
473- - GPTInferenceWrapper: Inference-wrapped model
474464 - Union[MCoreTokenizerWrappper, MegatronTokenizer]: Tokenizer instance
475465 """
476466 # Default to 1 for any parallelism dimension that's None
@@ -512,34 +502,32 @@ def create_mcore_engine(
512502 else :
513503 raise ValueError (f"Model format { model_format } not supported." )
514504
515- # MLA models require block_size_tokens=64 for the dynamic engine, which is not
516- # configurable in the current Megatron-LM version. Fall back to the legacy static
517- # engine so MLA inference works correctly without touching Megatron-LM.
505+ model .eval ()
506+
507+ # MLA models require block_size_tokens=64 for correct KV cache operation with the
508+ # dynamic inference engine. Set the attention backend to flash if not already set.
509+ block_size_tokens = 256
518510 model_config = getattr (model , "config" , None )
519511 if isinstance (model_config , MLATransformerConfig ):
520- legacy_model_format = True
521- # The legacy static engine requires an explicit attention backend.
522- # MLA models use flash attention (attention_mask is handled internally).
512+ block_size_tokens = 64
523513 if not model_config .attention_backend :
524514 model_config .attention_backend = AttnBackend .flash
525515
526- inference_context = StaticInferenceContext (
527- max_batch_size = max_batch_size ,
516+ inference_config = InferenceConfig (
528517 max_sequence_length = inference_max_seq_length ,
518+ buffer_size_gb = int (buffer_size_gb ),
519+ max_requests = max_batch_size ,
520+ block_size_tokens = block_size_tokens ,
521+ materialize_only_last_token_logits = True ,
529522 )
530- model_inference_wrapper = GPTInferenceWrapper (model , inference_context )
531- text_generation_controller = TextGenerationController (
532- inference_wrapped_model = model_inference_wrapper , tokenizer = tokenizer
533- )
534- mcore_engine = MCoreEngine (
535- text_generation_controller = text_generation_controller ,
536- max_batch_size = max_batch_size ,
537- random_seed = random_seed ,
538- buffer_size_gb = buffer_size_gb ,
539- legacy = legacy_model_format ,
523+
524+ llm = MegatronLLM (
525+ model = model ,
526+ tokenizer = tokenizer ,
527+ inference_config = inference_config ,
540528 )
541529
542530 # Wrap the engine to ensure cleanup
543- wrapped_engine = MCoreEngineWithCleanup (mcore_engine , model_inference_wrapper , tokenizer )
531+ wrapped_engine = MCoreEngineWithCleanup (llm , tokenizer )
544532
545- return wrapped_engine , model_inference_wrapper , tokenizer
533+ return wrapped_engine , tokenizer
0 commit comments