Skip to content

test: add Llama3 Megatron baseline - #14

Open
JYMiracle305 wants to merge 7 commits into
masterfrom
feat/add-llama3-megatron-baseline
Open

JYMiracle305 wants to merge 7 commits into
masterfrom
feat/add-llama3-megatron-baseline

Conversation

@JYMiracle305

@JYMiracle305 JYMiracle305 commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

概述

本 PR 在 InfiniTrain-Test 中建立按模型组织的 Megatron shadow baseline,并先接入 Llama3。InfiniTrain、PyTorch 和 Megatron-LM 使用相同 LLMC 初始权重、相同 token 顺序和相同训练参数,以比较逐步 loss 与训练吞吐。

现有 baseline/pytorch 用例保持不变;Megatron baseline 作为独立目录并行验证,不替换原有 PyTorch baseline,也不修改 third_party/Megatron-LM。

目录结构

baseline/megatron/
├── .gitignore
├── common/
│   ├── data/
│   │   └── convert_llmc_dataset.py   # LLMC token 转 Megatron indexed dataset
│   └── tools/
│       ├── compare_loss.py           # 单组 InfiniTrain/Megatron loss 严格比较
│       └── compare_tensors.py        # 中间 tensor/NumPy 输出比较
├── models/
│   └── llama3/
│       ├── README.md                 # 配置、运行与验证说明
│       ├── prepare/
│       │   └── prepare_dataset.sh    # 数据准备入口
│       ├── adapter/
│       │   └── llmc_loader.py        # LLMC 权重校验与 Megatron 参数映射
│       ├── train/
│       │   ├── pretrain.py           # Megatron 训练入口、autocast 与性能统计
│       │   ├── run_training.sh       # 单个配置训练入口
│       │   └── run_basic_cases.sh    # 六个基础用例批量入口
│       └── compare/
│           └── compare_loss.sh       # 单用例 loss/JSON 诊断入口
└── scripts/
    ├── compare_utils.py              # 批量日志发现、过滤与配对
    ├── compare_loss.py               # 批量 loss PASS/FAIL 汇总
    └── compare_tps.py                # 批量对比 infiniTrain 和 megatron-LM 性能

运行生成的数据集、cache、日志和比较结果放在 baseline/megatron/artifacts/,由 .gitignore 排除,不进入仓库。根目录 README.md 已补充仓库结构和 Llama3 Megatron 快速入口。

数据与模型对齐

同一份 LLMC checkpoint
├── InfiniTrain:原生 loader 读取 FP32 权重
└── Megatron:llmc_loader.py 读取并映射到 Megatron 参数布局

同一份 LLMC token stream
├── InfiniTrain:直接读取原始 token bin
└── Megatron:convert_llmc_dataset.py 转为 indexed dataset,并禁用随机 shuffle

训练结果
├── 单例诊断:models/llama3/compare/compare_loss.sh
├── 批量精度:scripts/compare_loss.py
└── 批量性能:scripts/compare_tps.py

Llama3 adapter 校验 LLMC header 和完整文件大小,将 block Q/K/V 重排为 Megatron GQA 布局,并按 Megatron SwiGLU 顺序打包 gate/up 权重。

基础用例

Case 精度 GPU / DP Micro batch Tokens/step Sequence Steps
llama3_1 FP32 1 4 256 64 10
llama3_1_bfloat16 BF16 autocast 1 4 256 64 10
llama3_2 FP32 1 80 5120 64 10
llama3_2_bfloat16 BF16 autocast 1 80 5120 64 10
llama3_3 FP32 8 10 5120 64 10
llama3_3_bfloat16 BF16 autocast 8 10 5120 64 10

BF16 基础用例保留 FP32 参数,在 forward 中使用 PyTorch BF16 autocast,与 InfiniTrain/PyTorch 的精度策略对齐。Megatron 原生 DTYPE=bfloat16 仍可用于额外诊断,但不属于默认基础矩阵。

精度与性能统计

  • FP32 loss 最大绝对误差阈值:1e-5
  • BF16 loss 最大绝对误差阈值:1e-2
  • Megatron iteration 2 的 running-average 日志在比较时恢复为单步 loss
  • 性能计时位于 Megatron 标准 train_step 内
  • 统计包含数据读取、forward、loss、backward、梯度通信和 optimizer update
  • 多卡取最慢 rank 的耗时
  • 共训练 10 步,step 1 作为 warmup,平均 step 2-10
  • TPS 默认允许相对 InfiniTrain 最多回退 20%

与配套 InfiniTrain 日志比较时,六个基础用例 loss 均通过各自阈值。BF16 autocast 相比 Megatron 原生 BF16 明显改善数值一致性;256-token BF16 小 batch 对不同运行版本更敏感,其他 InfiniTrain 运行记录中可能出现略高于 1e-2 的单步差异。

验证方式

准备数据并运行全部六个 Megatron 用例:

bash baseline/megatron/models/llama3/prepare/prepare_dataset.sh
bash baseline/megatron/models/llama3/train/run_basic_cases.sh

只运行选定用例:

CASES=llama3_1,llama3_3_bfloat16 bash baseline/megatron/models/llama3/train/run_basic_cases.sh

比较单个用例并输出逐步 JSON:

INFINITRAIN_LOG=/path/to/infinitrain.log MEGATRON_LOG=/path/to/megatron.log OUTPUT_JSON=/path/to/result.json ATOL=1e-2 bash baseline/megatron/models/llama3/compare/compare_loss.sh

直接比较 InfiniTrain 标准运行目录和 Megatron 日志目录:

python3 baseline/megatron/scripts/compare_loss.py /path/to/infinitrain/logs/basic baseline/megatron/artifacts/llama3/logs --include-prefix llama3_
python3 baseline/megatron/scripts/compare_tps.py /path/to/infinitrain/logs/basic baseline/megatron/artifacts/llama3/logs --include-prefix llama3_

loss 脚本明确列出 PASS、FAIL、缺失日志和总计;TPS 脚本仅输出平均吞吐、比例、解析错误和缺失日志。任一解析错误或缺失日志都会返回非零退出码。

后续扩展

common/ 只放跨模型复用的数据转换和单例诊断工具,scripts/ 放跨模型批量比较工具;新增模型放在 models/<model>/ 下,并分别维护 prepare、adapter、train 和 compare。



if __name__ == "__main__":
gpt_dataset._build_shuffle_index = ordered_shuffle_index

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这里将 Megatron 默认的 shuffle 索引替换为顺序索引

@JYMiracle305
JYMiracle305 force-pushed the feat/add-llama3-megatron-baseline branch from 050b4e5 to 47a38b9 Compare September 4, 2026 09:08
@JYMiracle305

Copy link
Copy Markdown
Collaborator Author

统计精度

image

统计性能

image

@JYMiracle305
JYMiracle305 force-pushed the feat/add-llama3-megatron-baseline branch from 47a38b9 to 3a5990b Compare September 4, 2026 09:15
@JYMiracle305
JYMiracle305 force-pushed the feat/add-llama3-megatron-baseline branch 3 times, most recently from f4e0804 to e8aaa52 Compare September 24, 2026 02:03
Comment thread README.md Outdated

Generated files under `baseline/megatron/artifacts/` are not committed.

## Llama3 Megatron baseline

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

建议将 Llama3 Megatron baseline 的具体使用说明放在对应目录的 README 中,仓库根目录的 README 主要保留项目整体介绍及各类 baseline 的使用入口。

目前根目录 README 只详细介绍了 Llama3 Megatron baseline,未涉及其他模型及 PyTorch baseline,文档组织上不太统一,也不利于后续扩展。

如果希望统一在根目录维护使用说明,也建议一并补充其他模型及 PyTorch baseline 的相关内容。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这里先删掉,各个模型目录下现在已有README

@JYMiracle305
JYMiracle305 force-pushed the feat/add-llama3-megatron-baseline branch from e8aaa52 to 4ee7794 Compare October 10, 2026 03:25
@JYMiracle305
JYMiracle305 force-pushed the feat/add-llama3-megatron-baseline branch from 4ee7794 to 3725c37 Compare October 10, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants