ms-swift icon indicating copy to clipboard operation
ms-swift copied to clipboard

resume_from_checkpoint报错

Open HuangFeiY opened this issue 1 year ago • 6 comments

断点续训时,load checkpoint时发生如下报错: [rank0]: Traceback (most recent call last): [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/cli/rlhf.py", line 5, in [rank0]: rlhf_main() [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/llm/train/rlhf.py", line 183, in rlhf_main [rank0]: return SwiftRLHF(args).main() [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/llm/base.py", line 49, in main [rank0]: result = self.run() [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/llm/train/sft.py", line 122, in run [rank0]: return self.train(trainer) [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/llm/train/sft.py", line 183, in train [rank0]: trainer.train(trainer.args.resume_from_checkpoint) [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/trainers/mixin.py", line 459, in train [rank0]: res = super().train(*args, **kwargs) [rank0]: File "/home/chengjialiang.cjl/miniconda3/envs/hfy-swift/lib/python3.10/site-packages/transformers/trainer.py", line 2206, in train [rank0]: return inner_training_loop( [rank0]: File "/home/chengjialiang.cjl/miniconda3/envs/hfy-swift/lib/python3.10/site-packages/transformers/trainer.py", line 2522, in _inner_training_loop [rank0]: self._load_rng_state(resume_from_checkpoint) [rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/trainers/mixin.py", line 209, in _load_rng_state [rank0]: return super()._load_rng_state(*args, **kwargs) [rank0]: File "/home/chengjialiang.cjl/miniconda3/envs/hfy-swift/lib/python3.10/site-packages/transformers/trainer.py", line 3130, in _load_rng_state [rank0]: checkpoint_rng_state = torch.load(rng_file) [rank0]: File "/home/chengjialiang.cjl/miniconda3/envs/hfy-swift/lib/python3.10/site-packages/torch/serialization.py", line 1470, in load [rank0]: raise pickle.UnpicklingError(_get_wo_message(str(e))) from None [rank0]: _pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint. [rank0]: (1) In PyTorch 2.6, we changed the default value of the weights_only argument in torch.load from False to True. Re-running torch.load with weights_only set to False will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source. [rank0]: (2) Alternatively, to load with weights_only=True please check the recommended steps in the following error message. [rank0]: WeightsUnpickler error: Unsupported global: GLOBAL numpy._core.multiarray._reconstruct was not an allowed global by default. Please use torch.serialization.add_safe_globals([_reconstruct]) or the torch.serialization.safe_globals([_reconstruct]) context manager to allowlist this global if you trust this class/function.

[rank0]: Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.

HuangFeiY avatar Aug 04 '25 07:08 HuangFeiY

最新main分支代码安装swift试一下

slin000111 avatar Aug 06 '25 02:08 slin000111

any solution?

liuxingbin avatar Aug 13 '25 03:08 liuxingbin

any solution?

后面解决了,在运行脚本前加入如下内容即可解决: cat > sitecustomize.py <<'PY' """ Patch transformers.Trainer._load_rng_state so that:

  1. 先按官方安全方式(weights_only=True)加载;
  2. 如果失败,回退到 weights_only=False;
  3. 同时把 numpy reconstruct 放入白名单,减少失败概率。 """ import os, torch, numpy as np from torch.serialization import add_safe_globals add_safe_globals({'_reconstruct': np.core.multiarray._reconstruct})

from transformers import Trainer

def _patched_load_rng_state(self, checkpoint_folder: str = None): if checkpoint_folder is None: return rng_file = os.path.join(checkpoint_folder, "rng_state.pth") if not os.path.isfile(rng_file): return try: torch.load(rng_file, weights_only=True) except Exception as err: # 仅当你信任 checkpoint 时才允许回退 print( "[sitecustomize] torch.load(weights_only=True) 失败," "转而以 weights_only=False 重新加载。错误信息:", err ) torch.load(rng_file, weights_only=False)

Trainer._load_rng_state = _patched_load_rng_state PY

export PYTHONPATH="$(pwd):${PYTHONPATH}"

HuangFeiY avatar Aug 13 '25 03:08 HuangFeiY

这个猴子补丁牛逼

AshOneN avatar Aug 13 '25 13:08 AshOneN

https://github.com/huggingface/transformers/issues/40500

Jintao-Huang avatar Sep 19 '25 06:09 Jintao-Huang

https://docs.pytorch.org/docs/stable/miscellaneous_environment_variables.html

try TORCH_FORCE_WEIGHTS_ONLY_LOAD

Jintao-Huang avatar Oct 17 '25 05:10 Jintao-Huang