Skip to content

refactor(training): initialize runtime services after config validation - #8272

Merged
tdene merged 17 commits into
NVIDIA:mainfrom
cuichenx:chcui/omar/validated-runtime-startup
Oct 18, 2026
Merged

tdene merged 17 commits into
NVIDIA:mainfrom
cuichenx:chcui/omar/validated-runtime-startup

Conversation

@cuichenx

@cuichenx cuichenx commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

Move runtime-service initialization into initialize_megatron(), after run-config validation and before distributed/RNG setup.

  • Training entrypoints construct/register the config and call pretrain(); inference entrypoints call initialize_megatron() without separately constructing runtime services.
  • Resolve tokenizer-derived vocabulary centrally after tokenizer construction, before writers and other services consume it. Preserve explicit and checkpoint-restored vocabulary.
  • Forward build_tokenizer=False for heterogeneous MiMo, which supplies its own tokenization.
  • Keep the lightweight checkpoint-converter bootstrap, validating its config before creating services.

Most entrypoint changes remove the old explicit runtime/vocabulary calls and their imports. This does not change config ownership, training/inference selection, or distributed/RNG behavior.

This is the separate startup-order follow-up discussed in #7778; #7778 does not depend on it. Pending #7778 and #8149 changes will need to be merged here once they land.

Issue tracking

Related to #3554 and #7778.

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • If this PR adds or changes a GPU kernel (Triton, jit_fuser/torch.compile, CUDA extension, TE or external-library dispatch, or a scatter/index accumulation), I have added or updated its bit-exact determinism test and registered it in tests/unit_tests/determinism/kernels/manifest.py
  • I have added proper typing to my code
  • I have added relevant documentation
  • I have run the autoformatter on my PR

Validation at 77521cccbe1451984d949873ccc647ec34e9bc45: 405 targeted tests passed on each of two GPU ranks across argument adapters, initialization lifecycle, inference, RNG, profiling, kernel warmup, autotuning, run config, and container validation. Existing lifecycle tests cover validation before side effects, tokenizer-derived/explicit/checkpoint vocabulary, tokenizer suppression, and the RL entrypoint's single initialization boundary. Independent full-diff review completed.

Changed-test formatting and diff checks pass. The all-files pre-commit run reports existing lint/format issues in untouched files; those are left unchanged to keep this diff focused.

Code review

Draft for startup-order review. No GPU kernels or numerical operations changed.

Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 15, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@cuichenx cuichenx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the important two refactors are

)

# Runtime services consume the validated config, before distributed/RNG setup.
initialize_runtime_services(args, build_tokenizer=build_tokenizer, training=training)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this

Comment thread megatron/training/global_vars.py Outdated

if build_tokenizer:
_build_tokenizer(args)
resolve_tokenizer_vocab_size(get_run_config(), getattr(args, "padded_vocab_size", None))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before, this wasn't always running every time initialize_runtime_services() was called. is it fine to do that now? should it be on some condition, eg if build_tokenizer or if build_tokenizer and training ?

@cuichenx
cuichenx marked this pull request as ready for review October 15, 2026 22:00
@cuichenx
cuichenx requested review from a team as code owners October 15, 2026 22:00
skip_model_parallel_init=True,
p2p_communicator=communicator,
pg_collection=topology.schedule_pg_collection,
build_tokenizer=False,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for later, this should be handled by a cfg.tokenizer=None

@maanug-nv maanug-nv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just 1 minor question.

Comment thread megatron/training/global_vars.py Outdated

if build_tokenizer:
_build_tokenizer(args)
resolve_tokenizer_vocab_size(get_run_config(), getattr(args, "padded_vocab_size", None))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before, this wasn't always running every time initialize_runtime_services() was called. is it fine to do that now? should it be on some condition, eg if build_tokenizer or if build_tokenizer and training ?

@maanug-nv maanug-nv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed offline. good to merge now.

Signed-off-by: Chen Cui <chcui@nvidia.com>
assert cfg_container is not None

_ensure_var_is_not_initialized(_GLOBAL_ARGS, 'args')
cfg_container.validate()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple config in the inference container that are environment-dependent: --inference-kv-cache-management-mode and --inference-static-kv-memory-pointers. They both require torch_memory_saver to be installed under some of the argument values.

This validation check leads to an odd case, where a checkpoint that was RL-trained on a machine that has torch_memory_saver can fail validation if the checkpoint is loaded on a machine without torch_memory_saver.

I think that before this PR, most codepaths already did validation and would encounter this issue. But I see that tools/checkpoint/convert.py specifically did not validate before this, and this is new behavior.

I don't think this is a problem. But I'm noting it down here, because it's a strange interaction.

Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/39110341563

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 18, 2026
@tdene
tdene enabled auto-merge October 18, 2026 16:27
@tdene
tdene added this pull request to the merge queue Oct 18, 2026
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/39157400748

Merged via the queue into NVIDIA:main with commit ce852bf Oct 18, 2026
110 checks passed
cuichenx added a commit to cuichenx/Megatron-LM that referenced this pull request Oct 19, 2026
…tion config migration

Merge main including NVIDIA#8272. Resolve batch defaults during one-time finalization, keep repeated validation read-only, and consume config-owned batch policy throughout training.

Signed-off-by: Chen Cui <chcui@nvidia.com>
AAnoosheh added a commit to AAnoosheh/Megatron-LM that referenced this pull request Oct 19, 2026
Resolve conflicts with the runtime-services refactor (NVIDIA#8272), the config
validate/finalize split, and disaggregated inference process groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Asha Anoosheh <aanoosheh@nvidia.com>
nemo-autobot-origin Bot pushed a commit to svcnemo-autobot/Megatron-LM that referenced this pull request Oct 22, 2026
Repair main-to-dev sync compatibility for the 2026-10-20 slice
(main 13a9d46 merged into
dev ff4fbc9):

- moe/router: InferenceTopKRouter._compiled_topk_routing accepts and
  forwards precomputed_indices, matching the base router call site.
- tensor_parallel/random: MHCCheckpointManager subclasses
  CheckpointWithoutOutputManager so main's checkpoint consumers accept it.
- moe/token_dispatcher: _HybridEPManager.setup_metadata clears the stale
  num_permuted_tokens budget when no capacity factor is configured.
- tests: align dev-only tests with main API changes (test_training
  TrainingConfig and TrainState, test_mhc_schedule _model_chunk_state,
  test_hybrid_hash_routing tensor_parallel import, test_route_metadata_cpu
  token_dispatcher fake, MTP argmax acceptance-count test).
- tests: test_train_step_schedule_plumbing provides the cfg.train,
  cfg.validation, cfg.ddp and TrainState inputs main's train_step and
  setup_model_and_optimizer now read.
- tests: test_virtual_experts quantile cases select the micro_batch
  estimation scope that main's quantile estimator implements; the merged
  default keeps dev's global_batch scope.
- tests/test_utils/recipes/gb200/unit-tests.yaml: drop the duplicated
  tests/unit_tests/ssm/test_gated_delta_net.py bucket produced by the
  merge, restoring the one-bucket-per-file unit-test scope contract.
- tests: test_hybrid_distillation_entry expects the multimodal_dev entry
  point to leave runtime services and tokenizer vocabulary resolution to
  initialize_megatron, which main NVIDIA#8272 made their owner.
- examples/mimo_bagel/train.py: read the training iteration from
  get_train_state() for the PP>1 data seed and the loss-alignment audit
  log, because main NVIDIA#7932 no longer sets args.curr_iteration or
  args.iteration (review 5609274485).
- pyproject.toml: carry main NVIDIA#7494's [tool.setuptools.package-data]
  entry so wheel and sdist installs ship
  megatron/core/allocator/csrc/vmm_symm_allocator.cpp (review 5609274485).

Signed-off-by: svcnemo-autobot <svcnemo-autobot@nvidia.com>
nemo-autobot-origin Bot pushed a commit to svcnemo-autobot/Megatron-LM that referenced this pull request Oct 22, 2026
Repair main-to-dev sync compatibility for the 2026-10-20 slice
(main 0156c21 merged into
dev ae76c60):

- moe/router: InferenceTopKRouter._compiled_topk_routing accepts and
  forwards precomputed_indices, matching the base router call site.
- tensor_parallel/random: MHCCheckpointManager subclasses
  CheckpointWithoutOutputManager so main's checkpoint consumers accept it.
- moe/token_dispatcher: _HybridEPManager.setup_metadata clears the stale
  num_permuted_tokens budget when no capacity factor is configured.
- tests: align dev-only tests with main API changes (test_training
  TrainingConfig and TrainState, test_mhc_schedule _model_chunk_state,
  test_hybrid_hash_routing tensor_parallel import, test_route_metadata_cpu
  token_dispatcher fake, MTP argmax acceptance-count test).
- tests: test_train_step_schedule_plumbing provides the cfg.train,
  cfg.validation, cfg.ddp and TrainState inputs main's train_step and
  setup_model_and_optimizer now read.
- tests: test_virtual_experts quantile cases select the micro_batch
  estimation scope that main's quantile estimator implements; the merged
  default keeps dev's global_batch scope.
- tests/test_utils/recipes/gb200/unit-tests.yaml: drop the duplicated
  tests/unit_tests/ssm/test_gated_delta_net.py bucket produced by the
  merge, restoring the one-bucket-per-file unit-test scope contract.
- tests: test_hybrid_distillation_entry expects the multimodal_dev entry
  point to leave runtime services and tokenizer vocabulary resolution to
  initialize_megatron, which main NVIDIA#8272 made their owner.
- examples/mimo_bagel/train.py: read the training iteration from
  get_train_state() for the PP>1 data seed and the loss-alignment audit
  log, because main NVIDIA#7932 no longer sets args.curr_iteration or
  args.iteration (review 5609274485).
- pyproject.toml: carry main NVIDIA#7494's [tool.setuptools.package-data]
  entry so wheel and sdist installs ship
  megatron/core/allocator/csrc/vmm_symm_allocator.cpp (review 5609274485).

Signed-off-by: svcnemo-autobot <svcnemo-autobot@nvidia.com>

This branch was successfully deployed

2 active deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants