xuan-luo's picture
download
raw
7.07 kB
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
"""Sample Generate"""
import os
import sys
import warnings
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
import os
import sys
from argparse import Namespace
from contextlib import nullcontext
import torch
from megatron.core.inference.contexts import StaticInferenceContext
from megatron.core.inference.engines import AbstractEngine, StaticInferenceEngine
from megatron.core.inference.engines.abstract_engine import AbstractEngine
from megatron.core.inference.model_inference_wrappers.gpt.gpt_inference_wrapper import (
GPTInferenceWrapper,
)
from megatron.core.inference.sampling_params import SamplingParams
from megatron.core.inference.text_generation_controllers.text_generation_controller import (
TextGenerationController,
)
from megatron.core.inference.text_generation_server import MegatronServer
from megatron.core.inference.text_generation_server.run_mcore_engine import run_mcore_engine
from megatron.core.process_groups_config import ProcessGroupCollection
from megatron.core.transformer.module import MegatronModule
from megatron.inference.utils import get_model_builder
from megatron.post_training.arguments import add_modelopt_args
from megatron.training import get_model, print_rank_0
try:
from megatron.post_training.model_builder import modelopt_gpt_hybrid_builder
HAS_NVIDIA_MODELOPT = True
except ImportError:
HAS_NVIDIA_MODELOPT = False
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir))
)
from megatron.core import mpu
from megatron.training import get_args, get_model, get_tokenizer
from megatron.training.arguments import parse_and_validate_args
from megatron.training.checkpointing import load_checkpoint
from megatron.training.initialize import initialize_megatron
def get_inference_engine(args: Namespace, model: MegatronModule) -> AbstractEngine:
"""Get the relevant backend for running inference
This function will automatically choose the TRTLLMBackend when possible, and default to Mcore
backend if the user does not specify any backends. TRTLLMBackend is not implmented yet.
Args:
args (Namespace): The user arguments parsed from command line
model (MegatronModule): The megatron model.
Returns:
AbstractBackend: The chosen backend
"""
# TODO(ksanthanam): Convert this to use dynamic inference counterparts
tokenizer = get_tokenizer()
inference_context = StaticInferenceContext(args.inference_max_requests, args.inference_max_sequence_length)
inference_wrapped_model = GPTInferenceWrapper(
model, inference_context
)
text_generation_controller = TextGenerationController(
inference_wrapped_model=inference_wrapped_model, tokenizer=tokenizer
)
return StaticInferenceEngine(
text_generation_controller=text_generation_controller,
)
def add_text_generate_args(parser):
"""Adds text generation arguments to parser."""
group = parser.add_argument_group(title='text generation')
group.add_argument(
"--port", type=int, default=5000, help='port for text generation server to run on'
)
group.add_argument("--temperature", type=float, default=1.0, help='Sampling temperature.')
group.add_argument("--top_k", type=int, default=1, help='Top k sampling.')
group.add_argument("--top_p", type=float, default=0.0, help='Top p sampling.')
group.add_argument(
"--return-log-probs",
action='store_true',
default=True,
help='Return the log probabilities of the final output tokens',
)
group.add_argument(
"--num-tokens-to-generate",
type=int,
default=30,
help='Number of tokens to generate for each prompt',
)
group.add_argument(
"--prompts",
metavar='N',
type=str,
nargs='+',
help='Input prompts with each prompt within quotes and separated by space',
)
group.add_argument(
"--max-batch-size",
type=int,
default=None,
help='Deprecated in favor of `--inference-max-batch-size`',
)
add_modelopt_args(parser)
return parser
@torch.inference_mode()
def main(model_type: str = "gpt"):
"""Runs the text generation server with the specified model type."""
parse_and_validate_args(
extra_args_provider=add_text_generate_args,
args_defaults={
'no_load_rng': True,
'no_load_optim': True,
'exit_on_missing_checkpoint': True,
},
)
initialize_megatron()
args = get_args()
if args.num_layers_per_virtual_pipeline_stage is not None:
print("Interleaved pipeline schedule is not yet supported for text generation.")
exit()
print_rank_0("WARNING: Forcing exit_on_missing_checkpoint to True for text " "generation.")
args.exit_on_missing_checkpoint = True
# Set up model and load checkpoint
load_context = nullcontext()
if args.fp8:
from transformer_engine.pytorch.fp8 import fp8_model_init
load_context = fp8_model_init()
with load_context:
if HAS_NVIDIA_MODELOPT and getattr(args, "modelopt_enabled", False):
# ModelOpt path keeps the legacy callable-based builder because the
# modelopt hooks have not been ported to the new ``ModelBuilder``
# API yet. ``get_model`` also handles the modelopt-checkpoint
# auto-detection side effect.
model = get_model(modelopt_gpt_hybrid_builder, wrap_with_ddp=False)
else:
builder = get_model_builder(args, provider=model_type)
pg_collection = ProcessGroupCollection.use_mpu_process_groups()
model = builder.build_distributed_models(
pg_collection=pg_collection, wrap_with_ddp=False
)
if args.load is not None:
_ = load_checkpoint(model, None, None, strict=False)
assert len(model) == 1, "Above condition should have caught this"
model = model[0]
model.eval()
inference_engine = get_inference_engine(args, model)
if args.cuda_graph_impl == "local":
print(f"Running warmup for CUDA graphs...")
inference_engine.generate(
prompts=["Test prompt"], sampling_params=SamplingParams(num_tokens_to_generate=10)
)
if (
mpu.is_pipeline_first_stage()
and mpu.get_tensor_model_parallel_rank() == 0
and mpu.get_expert_model_parallel_rank() == 0
):
server = MegatronServer(inference_engine, args)
server.run("0.0.0.0", port=args.port)
while True:
choice = torch.tensor(1, dtype=torch.long, device='cuda')
torch.distributed.broadcast(choice, 0)
if choice.item() == 0:
try:
run_mcore_engine(inference_engine)
except ValueError as ve:
pass
elif choice.item() == 1:
break
if __name__ == "__main__":
main(model_type="gpt")

Xet Storage Details

Size:
7.07 kB
·
Xet hash:
fffaf0be030247701e8648ff270ffbd026e0179fed0283302a0453398026bc82

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.