Skip to content

【训练营】Checkpoint 读取工具#129

Open
ArcaLunar wants to merge 3 commits intoInfiniTensor:masterfrom
ArcaLunar:master
Open

【训练营】Checkpoint 读取工具#129
ArcaLunar wants to merge 3 commits intoInfiniTensor:masterfrom
ArcaLunar:master

Conversation

@ArcaLunar
Copy link

Checkpoint 读取工具主要参数:

  • --checkpoint_dir 训练过程中的保存目录
  • --save_steps 每 N 次保存一次,设置为 0 则不保存
  • --max_checkpoint_keep 最多保留 K 个 checkpoint
  • --save_optimizer_state 是否保存优化器的状态
  • --resume_from 从指定 checkpoint 目录恢复训练

Checkpoint 文件可以通过从 /data/shared/....../llmc/gpt2 (or llama3) 的原始模型参数训练而来,例子可见仓库中的 REPORT.md(Experiment 实际上也测试了llama3,但是命令只记录了 GPT2 训练),model.bin, optimizer.bin, trainer_state.json 都可以从训练中获取.因此不在附件中提供

Experiment

CUDA_VISIBLE_DEVICES=5,6,7 ./gpt2 --input_bin ../../data/llmc/gpt2/tinyshakespeare/tiny_shakespeare_train.bin --llmc_filepath ../../data/llmc/gpt2/gpt2_124M.bin --checkpoint_dir ../ckpt2/gpt2-noresume/ --num_iteration 100 --save_steps 20 --save_optimizer_state true --max_checkpoint_keep 10
CUDA_VISIBLE_DEVICES=5,6,7 ./gpt2 --input_bin ../../data/llmc/gpt2/tinyshakespeare/tiny_shakespeare_train.bin --llmc_filepath ../../data/llmc/gpt2/gpt2_124M.bin --checkpoint_dir ../ckpt2/gpt2-resumefrom40/ --num_iteration 100 --save_steps 20 --save_optimizer_state true --max_checkpoint_keep 10 --resume_from ../ckpt2/gpt2-noresume/checkpoint_step_000040/ > ../ckpt2/gpt2-resumefrom40/gpt2-resume.log 2>&1

(以上两条训练命令同样用 llama3 也运行了)

运行 compare_loss.py,对于 llama3 模型,由于从 step 40 恢复训练,所以 step 1~40 数据缺失,而其余 60 步的 loss 在 FP32, BF16 下均吻合

  Summary: 60/100 steps matched

==================================================
Overall Summary:
  fp32:    0/1 test cases passed (threshold: 1e-05)
  bfloat16: 0/0 test cases passed (threshold: 1e-02)
  Total:   0/1 test cases passed
==================================================

==================================================
Overall Summary:
  fp32:    0/0 test cases passed (threshold: 1e-05)
  bfloat16: 0/1 test cases passed (threshold: 1e-02)
  Total:   0/1 test cases passed
==================================================

对于 GPT2,模型保存的逻辑有误:训练中 lm_head 与 wte 并非真共享,而 LLMC 存取又按“共享”假设处理,resume 后 lm_head 很容易和 noresume 不一致。解决方法是把训练用 checkpoint 从 LLMC 回调路径切到原生 StateDict 二进制路径,并在加载后显式重建权重绑定语义 (example/gpt2/main.cc).经过修复后,也可以通过.

@JYMiracle305
Copy link
Contributor

  1. 请把所有commit信息rebase成一个
  2. 另外,解决一下当前的代码冲突

@ArcaLunar
Copy link
Author

  1. 请把所有commit信息rebase成一个

  2. 另外,解决一下当前的代码冲突

已整理 commit 历史(保留上游 merge)+解决冲突

@JYMiracle305
Copy link
Contributor

  1. 请把所有commit信息rebase成一个
  2. 另外,解决一下当前的代码冲突

已整理 commit 历史(保留上游 merge)+解决冲突

需要解决一下format check报错
image

@ArcaLunar
Copy link
Author

  1. 请把所有commit信息rebase成一个
  2. 另外,解决一下当前的代码冲突

已整理 commit 历史(保留上游 merge)+解决冲突

需要解决一下format check报错 image

已对 example/infini_train/ 进行 format

@ArcaLunar
Copy link
Author

image

本地是过的,不知道为什么 check 没过,难道是 clang-format 版本的问题吗

image

format: use clang-format-16 instead
@ArcaLunar
Copy link
Author

image

本地是过的,不知道为什么 check 没过,难道是 clang-format 版本的问题吗
image

找到问题了,还真是 clang-format 的问题,通过 pip 安装 clang-format-16 还真有没有 format 的文件。重新 commit 了一下应该好了


int start_step = 0;
float best_loss = std::numeric_limits<float>::infinity();
if (!FLAGS_resume_from.empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

建议把主流程中恢复、保存、清理旧的Checkpoint提成公共函数,尽量让主流程简洁,另外各个训练入口可以复用。

Copy link
Contributor

Choose a reason for hiding this comment

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

参数如果太多可以用struct整合在一起

DEFINE_string(checkpoint_dir, "./checkpoints", "root directory used to store checkpoints");
DEFINE_uint32(max_checkpoint_keep, 3, "max number of checkpoint steps to keep");
DEFINE_bool(save_optimizer_state, true, "whether optimizer state is persisted in checkpoints");
DEFINE_string(checkpoint_format, "bin", "checkpoint format: bin|pth");
Copy link
Contributor

Choose a reason for hiding this comment

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

在llama3的训练中没有设置use_llmc_checkpoint_io,这个是出于什么考虑还是漏了

Copy link
Author

Choose a reason for hiding this comment

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

其实应该是没有这个 use_llmc_checkpoint_io 的,但是因为 GPT2 FromLLMC() 里的 TODO 说明了wtelm_head 不是真共享权重,而 FromLLMC() 不能动因为要读取原始的 gpt2_124M.bin 数据.SaveAsLLMC() 没有写入 lm_head 也是为了和原始 LLMC 文件兼容.然而这样的话,恢复训练就会导致权重不对,从而导致 loss 不一致.所以在 GPT2 训练时加了这个参数用 fallback save & load.

Copy link
Contributor

Choose a reason for hiding this comment

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

那现在这个设计,LLaMA3进行save和load主要依赖格式FLAGS_checkpoint_format,而GPT2同时依赖FLAGS_checkpoint_format和FLAGS_use_llmc_checkpoint_io,这么看FLAGS_use_llmc_checkpoint_io是否有必要,统一使用文件格式的标志作为控制即可。在GPT2::SaveAsLLMC和FromLLMC()先进行同样的说明。

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