Skip to content

[train, data] refactor: separate training, engine, model, and data layers - #232

Open
dyedd wants to merge 19 commits into
baidu-baige:masterfrom
dyedd:refactor/new-structure
Open

dyedd wants to merge 19 commits into
baidu-baige:masterfrom
dyedd:refactor/new-structure

Conversation

@dyedd

@dyedd dyedd commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR aligns LoongForge with the repository's ownership and naming rules. It separates training lifecycle code from engine execution, organizes model and data code by responsibility, and makes loongforge/train.py select the training runner through loongforge/models/catalog.py. The in-repo launch scripts, configs, tests, and documentation are updated to the new layout.

Change Summary

  • What changed?

    • loongforge/training/{megatron,torch}/ owns runners, training lifecycle, recipe/task/data arguments, method registries, learning-rate schedules, logging, profiling, and per-model training methods. The two training paths do not import each other.
    • loongforge/engines/{megatron,torch}/ owns parser and engine arguments, initialization, parallelism, model wrapping, train steps, optimizer integration, and checkpoint formats. Megatron global state now lives under loongforge/engines/megatron/.
    • loongforge/models/ organizes models by capability; loongforge/data/ organizes pipelines by data family. Embodied evaluation moves to loongforge/evaluation/embodied/.
    • loongforge/extensions/<engine>/<model>/ owns training extensions that must know both a concrete model and an engine.
    • loongforge/train.py is the single training entrypoint. It uses --model-name to select the catalog's megatron or torch runner. The LoongForge console script resolves to the same entrypoint.
    • tools/ remains a repository-level home for shared offline utilities. Layering tests reject cross-layer imports and stale dotted paths.
  • Which issue or requirement does this address?

    There is no separate public issue linked to this PR. The requirement comes from the ownership, dependency, and naming rules in AGENTS.md; the PR title follows CONTRIBUTING.md.

  • Which modules are affected?

    Training dispatch and runners, engine execution, model and data organization, embodied evaluation, training extensions, configuration paths, examples, documentation, packaging, CI, and tests.

Validation

  • python3 -m unittest tests.test_data_layers tests.test_import_targets -q: 25 tests passed, 1 skipped.
  • The new title passes .github/scripts/pr_title.js.
  • Layering tests enforce the engine, training, model, data, extension, and evaluation boundaries.
  • I checked compatibility with existing APIs and configurations for downstream users. Direct imports of moved modules and the old loongforge/embodied/train.py path require migration.

CI, GPU, and Image Impact

  • This change updates development/test dependencies and CI: pyflakes is added to the test extra and runs in workflow-lint.
  • GPU validation is required. Suggested suites: llm_vlm / embodied.
  • A local candidate image should be built during GPU validation.
  • No GPU or image validation is required.

Reviewer Notes

Direct imports of moved LoongForge modules and the old loongforge/embodied/train.py launch path are not kept through a compatibility layer. In-repo consumers are updated to loongforge/train.py, the LoongForge console script, and the current module paths. External scripts that use old deep imports or launch paths must migrate.

The static boundary and import-path checks pass. GPU and XPU end-to-end training were not run as part of this description update. Remaining models -> engines dependencies are recorded explicitly as technical debt in tests/test_data_layers.py.

@github-actions github-actions Bot added checkpoint ci docker documentation Improvements or additions to documentation model training labels Sep 16, 2026
@dyedd
dyedd marked this pull request as draft September 16, 2026 06:31
@dyedd
dyedd force-pushed the refactor/new-structure branch from cc474c6 to b614ce7 Compare September 21, 2026 08:41
@nullnonenilNULL

Copy link
Copy Markdown
Collaborator

engine 和 trainer 概念有点混用了,未来不是很容易扩展新的 train方式支持;你的调整方案,需要明确下每个目录的职责是否边界清楚,以及后续扩展新的功能是否容易;

Squash of the layout refactor (engines split, data and models by family,
shared code lifted to the top level, embodied preprocessing grouped under
tools/embodied_data_preprocess) rebased onto origin/master as one linear
commit.
@dyedd
dyedd force-pushed the refactor/new-structure branch from 276589f to 4e21139 Compare October 10, 2026 09:23
Place MCore and Torch runners next to the engine that owns their lifecycle, and retry embodied data imports after a failed optional dependency load.
@dyedd
dyedd force-pushed the refactor/new-structure branch from 4e21139 to 8b96edd Compare October 13, 2026 06:31
dyedd added 8 commits October 16, 2026 17:02
- Rename the MCore engine to Megatron and move training orchestration into
  dedicated runners with direct training-method dispatch.
- Pass model configuration and preprocessing dependencies explicitly,
  consolidate heterogeneous DP state, and reuse upstream Megatron helpers.
- Separate Torch FP8 quantization from distributed setup and reorganize
  chat-template plugins and model-specific utilities.
- Move single-model offline tools into examples and share Kimi K2.x NVFP4
  conversion utilities across model variants.
- Update launch scripts, dependency checks, tests, and bilingual guides
  for the revised package layout and interfaces.
- Move Megatron task and data arguments into training, keeping backend
  options in the engine and registering both on the same parser.
- Move Torch training arguments, CLI parsing, learning-rate schedules,
  and profiling into training, and update their callers.
- Remove unused CLI options, config fields, helpers, and write-only state
  across models, data pipelines, engines, and evaluation.
- Fix the DreamZero evaluation action queue initialization and remove
  unused GR00T timing work while retaining replay profiling logs.
- Update model configs, launch scripts, tests, documentation, and argument
  ownership rules for the revised interfaces.
- Split Megatron and Torch training packages and registries.\n- Move Torch optimizations under accelerations and extract shared helpers.\n- Update imports, docs, tests, and distributed utilities for the new layout.
- loongforge/accelerations -> loongforge/extensions
- loongforge/models/common -> loongforge/models/base
- update imports, registry strings, LoRA targets, ruff per-file-ignores
… explicitly

- move CP tensor helpers into models/base/context_parallel.py
- resolve dtype and RNG reset in training instead of models
- drop the data -> engines tokenizer assertion
- make chat_templates a leaf with a local tokenizer Protocol
- rewrite layering checks for models/data/engines/extensions boundaries
- register models -> engines debt explicitly
- add pyflakes and test_undefined_names to workflow lint
- drop one-off scripts and rename the checkpoint converter driver
- single-ownership wording instead of "shared common layers"
- data may import declarative model modules, never implementations
- engines use models/base; models -> engines is registered debt
- rename extensions and document the ChunkPipe queue contract
@dyedd
dyedd marked this pull request as ready for review October 20, 2026 13:05
@dyedd dyedd changed the title refactor: split the package into engine, model, and data layers [train, data] refactor: separate training, engine, model, and data layers Oct 20, 2026
@dyedd
dyedd requested a review from nullnonenilNULL October 21, 2026 02:39
dyedd added 6 commits October 21, 2026 10:57
- restore `--pretrained-checkpoint` and `--resume` in `_CheckpointIOArgs`
- keep `training/torch/runner.py` argument access valid
- allow Torch runs and cosmos3 argument parsing to use these options
- remove the unused `nullcontext` import
- rename the ambiguous loop variable
- keep whole-file Ruff checks clean for files in the PR diff
…nced code

- Move Megatron run, freeze/init, EMA, recipe, and log-tensor arguments from
  engines/megatron/arguments.py to training/megatron/args.py, leaving only
  backend-execution arguments in the engine. Both providers still register on
  the same parser, so the CLI and the parsed namespace are unchanged.
- Move Torch --pretrained-checkpoint and --resume into TrainingArgs; apply
  determinism, cuDNN autotune, and TF32 switches in engines/torch/initialize.py;
  move the switch-conflict and DMuon bucket-size checks into validate_engine_args
  so they fail at parse time.
- Move the llm-inspector add_hooks helper to the training runner, which owns the
  log-tensor arguments that enable it.
- Drop the local --padded-vocab-size definition; upstream Megatron provides it.
- Fix GR00T-N1.7 reading an undeclared pretrained_checkpoint field, wall-oss
  fallback imports that could never succeed, the llava_onevision_1.5 config
  path in tests and example scripts, and stale DSA kernel comments.
- Delete unreferenced modules and helpers: local_attention, canonical_lora,
  wan_ulysses, three orchestrator CLIs, LoRAMerge, forall, the ernie download
  helpers, an unused vla_mixin conversion, the cosmos3 demo block, and four
  unused YAML configs.
- Register kimi-k2.6-bf16 in the model catalog.
- Document the parameter ownership rule in AGENTS.md and refresh the README
  trees, tutorials, submit-pr module mapping, and pyproject metadata to the
  current layout.
…r model

`examples/` had drifted away from its convention: a wildcard grouping
directory sat next to real model directories, model directories mixed flat
launch scripts with offline helpers, the same script lived at a different
depth depending on the model, and DreamZero's data-preparation
implementation had been collapsed out of `tools/` into `examples/`.

Restore the split with `tools/` and give every model the same layout.

DreamZero preprocessing back to tools/
- move prepare_dataset.py, precompute_features.py,
  validate_precomputed_feature_artifact.py and cache_precompute/ to
  tools/dreamzero_data_preprocess/, keeping the current (post-rename)
  import paths, so tools/ holds the CLIs and examples/ keeps thin wrappers
- fix the config path built by precompute_dreamzero_cache.sh: the earlier
  refactor replaced configs/models/embodied/ with configs/models/ but the
  DreamZero YAMLs live in configs/models/dreamzero/
- split the README accordingly and refresh the tools table

One directory per model
- split kimi_k2.x/ into kimi_k2.5/ and kimi_k2.6/; the version-agnostic
  NVFP4 PTQ driver stays single-copy under the family root
  kimi_k2/checkpoint_convert/nvfp4_ptq/
- repoint COMMON_NVFP4_DIR in both wrappers and the two printed
  post-conversion commands

One phase layout per model
- every model now uses pretrain/, finetuning/, checkpoint_convert/, eval/
  and preprocess/, with dataset fixtures under assets/ and only README.md
  at the model root
- move the flat finetune/sft scripts of cosmos3, dreamzero, fastwam,
  groot_n1_6/7, lingbot_va, pi05, wall_oss_0_5, xvla and internvl2.5 into
  finetuning/; move offline helpers into preprocess/ keeping each .py next
  to its caller; give qwen_image and wan the pretrain/ + preprocess/ +
  checkpoint_convert/ split they were missing
- align examples_xpu/deepseek_v3.1 with the same rule
- scripts that derive the repo root from SCRIPT_DIR sat one level
  shallower before, so bump them to ../../.. and repoint intra-script
  references; the WAN converters no longer depend on the caller's cwd
- make pretrain_wan2.2 use a relative DATASET_PATH like its wan2.1
  sibling instead of a stale absolute path into the repo
- fix comments that pointed at a non-existent pi05 sft script and at the
  pre-rename FastWAM script name

Keep it from drifting again
- add tests in tests/test_layout_paths.py: every examples*/ directory names
  a model under configs/models/ (or a version of one), a model directory
  holds only phase directories/assets/README, and phase scripts resolve the
  repo root three levels up
- document the convention in support_new_model (en/zh)

Update tests/embodied/config/scripts.yaml, the README model list, the
support_model tables and every doc/config comment that referenced a moved
script.
- models/ no longer imports engines/: the training method injects
  MultimodalRuntime and HeteroStepContext
- ChunkPipe metadata travels with the batch instead of a shared queue
- padding rules live in training/megatron/data_padding.py; the data layer
  only applies them
- diffusion CP broadcast uses the generic broadcast primitives from the
  method side
- the MTP logging scale is computed in sft_llm; the model no longer reads
  DP size or microbatch counts
- DynamicRotaryEmbedding moves to models/base, with its three defects fixed
- LoRA shape has a single source (the YAML); --patch-size, --lora-alpha and
  --lora-dim are removed
- model YAML keys that shadow LoongForge CLI options are scanned by a test
- InternVL2.5 YAMLs carry the HF rope scaling factor
dyedd added 3 commits October 22, 2026 11:37
…xtensions

- Move the Wall-OSS-0.5 FSDP2 + DMuon wrap out of the model into
  extensions/torch/wall_oss_0_5; wrap_model now takes a dmuon_shard
  callback instead of probing convert_to_fsdp, and grad_sync_ctx
  suppresses DMuon gradient sync inside the engine
- Resolve execution facts into DataRuntime in the training method; collators,
  samplers, transforms and dataset builders read it instead of backend
  arguments or DistributedContext, which data and models no longer import
- Dispatch --dataset-format through DATASET_FORMATS so the shared dataloader
  no longer imports the X-VLA HDF5 builder
- Remove the cuDNN writes from sft_vlda; the cookbook script opts into
  --deterministic-mode so the engine owns determinism
- Extend test_data_layers with an AST check for engine-argument reads,
  TYPE_CHECKING import bans, and a DATASET_FORMATS registry check
- Sync AGENTS.md and the Chinese support/eval docs

--optimizer dmuon for Wall-OSS-0.5 now needs --trainer-type WallOss05Trainer.
…ckage table

- Rename `CustomModelFamilies` to `DiffusionModelFamilies`; the entries are diffusion models, not custom vision models
- Reference family constants in `FAMILY_GROUP` instead of string literals
- Replace the duplicated embodied `model_type -> package` tables with `MODEL_PACKAGES` in `models/embodied/registry.py`; `data/embodied/registry.py` reads it and keeps only the data-only `PLAIN_MODULES`
- Move `torch.nn` to `TYPE_CHECKING` so the data registry stays importable without torch
- Drop the two-table consistency test and update the registry tests and AGENTS.md
- Align the EN/ZH "Support New Model" guides: Wan is a diffusion model, the YAML examples carry the required `model_type`, and a new section lists the registrations a new architecture needs

Verified: 39 unittest cases pass in the three registry test modules, ruff clean, no `MODEL_MODULES` or `CustomModelFamilies` left, doc link check passes. Not verified: pytest and runtime `get_collator` resolution, this machine has no torch.
- Move each family's DataLoader entry into data/<family>/dataloader.py and pass frozen data configs plus DP facts instead of Megatron args or global distributed state.
- Move embodied transform and collator pipelines into <model>/transforms/, unify sampler modules as samplers.py, and rename the GR00T-N1.6 data processor.
- Move Eagle3 HF processors into the groot_n1_6 model package and keep its package __init__ lazy.
- Move full-hetero-DP encoder iterator assembly into engines/megatron/distributed/hetero_dp.py; methods now only prepare datasets and loaders.
- Read dataloader state in engines/megatron/checkpointing.py and restore streaming SFT position and diffusion consumed samples.
- Update layering tests, AGENTS.md, and docs for the new ownership rules.

This branch has not been deployed

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

Labels

checkpoint ci docker documentation Improvements or additions to documentation model training

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants