Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use dit.safetensors instead of mp.pt? #118

Open
foreverpiano opened this issue Dec 11, 2024 · 1 comment
Open

How to use dit.safetensors instead of mp.pt? #118

foreverpiano opened this issue Dec 11, 2024 · 1 comment

Comments

@foreverpiano
Copy link

It is risky to use .pt ckpt considering previous experience from Bytedance.
Is there any way to convert safetensors ckpt?

I try to use this command but it fails. It seems that the loading is different from VAE. Can you

import torch
from safetensors.torch import save_file

# 输入文件路径
pt_file_path = "transformers/mp_rank_00_model_states.pt"
safetensors_file_path = "transformers/diffusion_pytorch_model.safetensors"

# 加载 .pt 文件
state_dict = torch.load(pt_file_path, map_location="cuda")  # 加载为 CPU 以避免 GPU 内存问题

import ipdb; ipdb.set_trace()
# 保存为 .safetensors 格式
save_file(state_dict, safetensors_file_path)

print(f"转换完成,文件已保存为 {safetensors_file_path}")

error message

(fast) (base) root@76faf61dd144:/workspace/HunyuanVideo/ckpts/hunyuan-video-t2v-720p# python process.py
/workspace/HunyuanVideo/ckpts/hunyuan-video-t2v-720p/process.py:9: 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.
state_dict = torch.load(pt_file_path, map_location="cuda") # 加载为 CPU 以避免 GPU 内存问题
Traceback (most recent call last):
File "/workspace/HunyuanVideo/ckpts/hunyuan-video-t2v-720p/process.py", line 12, in <module>
save_file(state_dict, safetensors_file_path)
File "/workspace/miniconda3/envs/fast/lib/python3.10/site-packages/safetensors/torch.py", line 286, in save_file
serialize_file(_flatten(tensors), filename, metadata=metadata)
File "/workspace/miniconda3/envs/fast/lib/python3.10/site-packages/safetensors/torch.py", line 470, in _flatten
raise ValueError(f"Key {k} is invalid, expected torch.Tensor but received {type(v)}")
ValueError: Key module is invalid, expected torch.Tensor but received <class 'dict'>

I find the config is below. Does it mean we have two models now in the .pt?

#################################################################################
#                             HunyuanVideo Configs                              #
#################################################################################

HUNYUAN_VIDEO_CONFIG = {
    "HYVideo-T/2": {
        "mm_double_blocks_depth": 20,
        "mm_single_blocks_depth": 40,
        "rope_dim_list": [16, 56, 56],
        "hidden_size": 3072,
        "heads_num": 24,
        "mlp_width_ratio": 4,
    },
    "HYVideo-T/2-cfgdistill": {
        "mm_double_blocks_depth": 20,
        "mm_single_blocks_depth": 40,
        "rope_dim_list": [16, 56, 56],
        "hidden_size": 3072,
        "heads_num": 24,
        "mlp_width_ratio": 4,
        "guidance_embed": True,
    },
}
@foreverpiano
Copy link
Author

cc @JacobKong @TianQi-777 @xibosun

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

No branches or pull requests

1 participant