resume_from_checkpoint报错
断点续训时,load checkpoint时发生如下报错:
[rank0]: Traceback (most recent call last):
[rank0]: File "/ProjectRoot/hfy/ms-swift-main/swift/cli/rlhf.py", line 5, in 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.
最新main分支代码安装swift试一下
any solution?
any solution?
后面解决了,在运行脚本前加入如下内容即可解决: cat > sitecustomize.py <<'PY' """ Patch transformers.Trainer._load_rng_state so that:
- 先按官方安全方式(weights_only=True)加载;
- 如果失败,回退到 weights_only=False;
- 同时把 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}"
这个猴子补丁牛逼
https://github.com/huggingface/transformers/issues/40500
https://docs.pytorch.org/docs/stable/miscellaneous_environment_variables.html
try TORCH_FORCE_WEIGHTS_ONLY_LOAD