How masworks/ml-agent works
1. What this repository is
This repo is an ML-agent training and evaluation project whose released code is primarily the exploration-enriched fine-tuning stage. The codebase is built on top of LLaMA-Factory, so the architecture is centered on LLM fine-tuning, data preprocessing, inference/chat serving, and evaluation. The README explicitly says the step-wise RL stage lives in VeRL, not here.
2. Top-level execution paths
The main user entry points are:
- src/train.py for training runs
- src/webui.py for the interactive UI
- src/api.py for API serving
- scripts/train_step1.sh and scripts/eval.sh as repo-level orchestration scripts
The examples/ directory contains ready-to-run configs for SFT, LoRA, QLoRA, inference, merge, and accelerator-specific setups.
3. Configuration and experiment management
The repo is config-driven. Hyperparameters are organized under src/llamafactory/hparams/, with separate argument groups for model, data, training, generation, and evaluation. The examples/ tree shows the intended experiment matrix: full finetuning, LoRA, QLoRA, distributed training variants, and inference backends. This is the main boundary for defining an experiment before any model/data code runs.
4. Data subsystem
The data path starts in src/llamafactory/data/:
- dataset metadata and task definitions are in data/dataset_info.json
- loaders/parsers/formatters/converters normalize raw datasets into training examples
- processors split the behavior by objective: supervised, unsupervised, pretraining, pairwise, and feedback
- template.py and tool_utils.py shape prompts, chat templates, and tool-style examples
- mm_plugin.py adds multimodal handling for image/vision-language tasks
In architectural terms, this layer converts heterogeneous benchmark/task data into a unified tokenized format consumed by the trainers.
5. Model loading and adaptation
The src/llamafactory/model/ layer is responsible for instantiating base models and applying training-time modifications. It includes:
- loader.py for model construction
- adapter.py for parameter-efficient tuning paths such as LoRA-style adapters
- patcher.py and model_utils/ for runtime model edits
- support modules for quantization, RoPE, attention, checkpointing, embedding, MoE, visual components, and long-context behavior
This is the main boundary between the generic Hugging Face model stack and the project-specific training strategies.
6. Training workflows
Training is split by objective under src/llamafactory/train/:
- sft/ for supervised fine-tuning
- pt/ for pretraining
- rm/ for reward modeling
- dpo/ for preference optimization
- kto/ and ppo/ for additional alignment/RL-style methods
Each mode has a workflow.py that wires arguments, data, model, and trainer together, plus a trainer.py that encapsulates the actual optimization loop. Shared logic lives in trainer_utils.py, callbacks.py, and tuner.py.
7. Evaluation subsystem
Evaluation is handled separately from training in src/llamafactory/eval/, with an evaluator and templates for task-specific scoring. The standalone evaluation/ directory contains benchmark-specific scripts and mappings for:
- MMLU
- CMMLU
- CEval
This indicates a clean separation between model training and benchmark evaluation, with evaluation scripts adapting model outputs to benchmark scoring rules.
8. Inference, chat, and serving
The runtime inference boundary is in src/llamafactory/chat/ and src/llamafactory/api/:
- chat_model.py and engine implementations wrap either Hugging Face or vLLM
- base_engine.py, hf_engine.py, and vllm_engine.py select execution backends
- api/app.py, api/chat.py, and api/protocol.py expose serving endpoints and request/response schemas
This layer is for post-training use of the model, not for optimization. The repo also includes scripts/vllm_infer.py for direct vLLM-based inference.
9. Web UI as an orchestration layer
The src/llamafactory/webui/ package is a Gradio-style interactive front end that wraps training, inference, evaluation, data management, and export flows. Components are split by function:
- chat
- data
- eval
- infer
- train
- export
Supporting modules like runner.py, engine.py, and manager.py coordinate background execution and state management. Architecturally, the UI does not own model logic; it delegates to the same underlying training/inference subsystems.
10. Scripts and operational tooling
The repo includes utility scripts for:
- checkpoint conversion (scripts/convert_ckpt/)
- initializing specific adapter methods such as PiSSA and LoFTQ
- statistics and diagnostics (scripts/stat_utils/)
- API examples and multimodal/tool-call demos (scripts/api_example/)
These scripts are operational glue around the core training stack and help move models between formats or inspect training behavior.
11. External infrastructure and execution backends
The architecture assumes integration with the broader Hugging Face ecosystem and distributed training backends. The examples show support for:
- Accelerate
- DeepSpeed
- FSDP
- vLLM
There are also specialized QLoRA/quantization paths, which affect memory layout and backend selection. These are important architectural boundaries because they change how the same trainer executes on different hardware budgets.
12. Repository boundary relative to the ML-Agent paper
For the ML-Agent project described in the README, this repo covers the fine-tuning foundation and evaluation scaffolding, but not the full agentic training stack. The held-in/held-out ML task framing is reflected in the data/evaluation organization, while the online RL stage is intentionally externalized. If you are drawing the system diagram, treat this repository as the model adaptation and benchmark-facing half of the overall ML-Agent pipeline.
Main entry points: data data pipeline · webui web UI · chat chat engine · hparams hparams
Components 22
Entry points
Primary ways to launch training, UI, and serving.
- train.py · Main CLI for training runs.
LLaMA-Factory/src/train.py - webui.py · Launches the interactive Gradio-style UI.
LLaMA-Factory/src/webui.py - api.py · Starts the API serving process.
LLaMA-Factory/src/api.py - cli · Shared command-line dispatch and execution entry logic.
LLaMA-Factory/src/llamafactory/cli.py
Core stack
Configuration, data prep, model adaptation, and training workflows.
- hparams · Argument groups and parser for model, data, training, generation, and evaluation.
LLaMA-Factory/src/llamafactory/hparams - dataset info · Dataset metadata and task mapping registry.
LLaMA-Factory/data/dataset_info.json - data pipeline · Loads, parses, formats, converts, and collates heterogeneous datasets.
LLaMA-Factory/src/llamafactory/data - templates · Chat templates, tool-use formatting, and multimodal prompt shaping.
LLaMA-Factory/src/llamafactory/data/template.py - mm plugin · Adds image and vision-language handling to the data path.
LLaMA-Factory/src/llamafactory/data/mm_plugin.py - model loader · Constructs base models and applies runtime/model-specific setup.
LLaMA-Factory/src/llamafactory/model/loader.py - adapters · Applies LoRA-style adaptation and model patching.
LLaMA-Factory/src/llamafactory/model/adapter.py - model utils · Quantization, RoPE, attention, checkpointing, visual, and other model edits.
LLaMA-Factory/src/llamafactory/model/model_utils - SFT · Supervised fine-tuning workflow and trainer.
LLaMA-Factory/src/llamafactory/train/sft/workflow.py - alignment · Preference and alignment workflows for PT, RM, DPO, KTO, and PPO.
LLaMA-Factory/src/llamafactory/train - trainer utils · Common trainer utilities, callbacks, and tuning helpers.
LLaMA-Factory/src/llamafactory/train/trainer_utils.py - evaluator · General evaluation orchestration and scoring templates.
LLaMA-Factory/src/llamafactory/eval/evaluator.py
Runtime
Inference, chat, and API serving backends.
- chat engine · Chat model wrapper with Hugging Face and vLLM backend selection.
LLaMA-Factory/src/llamafactory/chat - API layer · Request protocol and HTTP handlers for model serving.
LLaMA-Factory/src/llamafactory/api - web UI · Interactive front end that delegates to training, inference, evaluation, data, and export subsystems.
LLaMA-Factory/src/llamafactory/webui
Ops and benchmarks
Repository scripts, examples, and external benchmark evaluation.
- examples · Ready-to-run configs for SFT, LoRA, QLoRA, inference, merge, and distributed setups.
LLaMA-Factory/examples - benchmarks · Benchmark-specific scripts and mappings for MMLU, CMMLU, and CEval.
LLaMA-Factory/evaluation - scripts · Utility scripts for checkpoint conversion, initialization, statistics, and direct inference.
LLaMA-Factory/scripts
Connections 27
- train.py → cli: dispatches · Training entrypoint uses shared CLI dispatch.
- webui.py → web UI: launches · UI entrypoint starts the web interface.
- api.py → API layer: launches · API entrypoint starts the serving layer.
- cli → hparams: parses · CLI builds typed argument groups from config schema.
- hparams → examples: instantiated by · Example configs define concrete experiment variants.
- hparams → data pipeline: configures · Data-related arguments drive dataset loading and formatting.
- dataset info → data pipeline: drives · Dataset registry informs loader and task selection.
- data pipeline → templates: formats with · Unified examples are shaped by templates and tool prompts.
- data pipeline → mm plugin: extends · Multimodal tasks pass through the multimodal plugin.
- data pipeline → SFT: feeds · Prepared examples are consumed by training workflows.
- model loader → adapters: applies · Loaded base models receive adaptation and patching.
- adapters → model utils: uses · Adapters rely on runtime model utilities and patches.
- hparams → model loader: configures · Model and tuning arguments control model construction.
- SFT → trainer utils: shares · SFT uses common trainer utilities and callbacks.
- alignment → trainer utils: shares · Other training objectives reuse the same trainer support.
- alignment → model loader: runs on · All optimization modes build models through the loader boundary.
- alignment → data pipeline: consumes · Alignment workflows consume the normalized data pipeline.
- evaluator → benchmarks: adapts to · Benchmark scripts provide task-specific evaluation rules.
- chat engine → model loader: loads · Inference reuses the model loading boundary.
- chat engine → model utils: selects · Backend execution depends on model/runtime utilities.
- API layer → chat engine: wraps · HTTP serving is built on the chat runtime.
- web UI → alignment: orchestrates · UI delegates training actions to the training workflows.
- web UI → chat engine: orchestrates · UI delegates inference and chat actions to runtime backends.
- web UI → evaluator: orchestrates · UI triggers evaluation through the evaluation core.
- web UI → data pipeline: orchestrates · UI includes dataset management and preprocessing flows.
- scripts → adapters: supports · Utility scripts initialize or convert adapter-related checkpoints.
- scripts → chat engine: supports · Inference and API helper scripts target runtime backends.