#工作記錄
一、問題描述
在運行項目時,出現以下警告:
FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
二、受影響的文件和行號
-
文件:
F:\PythonProjects\SkyReels-V2\skyreels_v2_infer\modules\vae.py
-
行號:第 566 行
-
原代碼
model.load_state_dict(torch.load(pretrained_path, map_location=device), assign=True)
-
-
文件:
F:\PythonProjects\SkyReels-V2\skyreels_v2_infer\modules\t5.py
-
行號:第 437 行
-
原代碼
model.load_state_dict(torch.load(checkpoint_path, map_location="cpu"))
-
三、修復過程
-
查找相關代碼:在項目中查找使用
torch.load
的代碼片段。 -
修改代碼:在
torch.load
中添加weights_only=True
參數。 -
測試修改后的代碼:重新運行腳本,確認警告是否消失。
四、修改后的代碼
-
vae.py
文件第 566 行:model.load_state_dict(torch.load(pretrained_path, map_location=device, weights_only=True), assign=True)
-
t5.py
文件第 437 行model.load_state_dict(torch.load(checkpoint_path, map_location="cpu", weights_only=True))
五、驗證修改
完成上述修改后,重新運行項目以確保所有問題都已解決:
python generate_video.py --resolution 540P
六、總結
通過上述步驟,成功修復了項目中的 torch.load
警告。這些修改確保了代碼在未來版本的庫中仍然兼容,并提高了代碼的安全性。
希望這份修復筆記能幫助你解決相同的問題。