hpcaitech / Open-Sora

Open-Sora: Democratizing Efficient Video Production for All
https://hpcaitech.github.io/Open-Sora/
Apache License 2.0
20.34k stars 1.92k forks source link

Unable to utilize the locally downloaded pre-trained model weights #441

Closed LoserLus closed 3 weeks ago

LoserLus commented 3 weeks ago

Due to certain reasons, my machine cannot access the internet, so I cannot download the pre-trained model weights online. I have to utilize the pre-trained model weight locally. I referenced issue218, modified the location of the pre-trained model in configs/opensora-v1-1/inference/sample.py to point to a local folder, and commented out the assert statements in the relevant files, but it still unable to resolve my issue. My configs/opensora-v1-1/inference/sample.py now looks like:

text_encoder = dict(
    type="t5",
    # from_pretrained="DeepFloyd/t5-v1_1-xxl",
    from_pretrained="/data/models/t5-v1_1xxl",
    # from_pretrained="/data/models/",
    # cache_dir=None,  # "/mnt/hdd/cached_models",
    model_max_length=200,
)

When I try to inference with command python scripts/inference.py configs/opensora-v1-1/inference/sample.py --ckpt-path CKPT_PATH --prompt "A beautiful sunset over the city" --num-frames 16 --image-size 480 854, I got the output:

/opt/conda/envs/pytorch/lib/python3.9/site-packages/diffusers/models/transformers/transformer_2d.py:34: FutureWarning: `Transformer2DModelOutput` is deprecated and will be removed in version 1.0.0. Importing `Transformer2DModelOutput` from `diffusers.models.transformer_2d` is deprecated and this will be removed in a future version. Please use `from diffusers.models.modeling_outputs import Transformer2DModelOutput`, instead.
  deprecate("Transformer2DModelOutput", "1.0.0", deprecation_message)
Config (path: configs/opensora-v1-1/inference/sample.py): {'num_frames': 16, 'frame_interval': 3, 'fps': 24, 'image_size': [480, 854], 'multi_resolution': 'STDiT2', 'model': {'type': 'STDiT2-XL/2', 'from_pretrained': 'CKPT_PATH', 'input_sq_size': 512, 'qk_norm': True, 'enable_flash_attn': True, 'enable_layernorm_kernel': True}, 'vae': {'type': 'VideoAutoencoderKL', 'from_pretrained': '/data/models/sd-vae-ft-ema', 'micro_batch_size': 4, 'local_files_only': True}, 'text_encoder': {'type': 't5', 'from_pretrained': '/data/models/t5-v1_1xxl', 'model_max_length': 200, 'local_files_only': True}, 'scheduler': {'type': 'iddpm', 'num_sampling_steps': 100, 'cfg_scale': 7.0, 'cfg_channel': 3}, 'dtype': 'bf16', 'prompt_path': './assets/texts/t2v_samples.txt', 'prompt': ['A beautiful sunset over the city'], 'batch_size': 1, 'seed': 42, 'save_dir': './samples/samples/', 'config': 'configs/opensora-v1-1/inference/sample.py', 'prompt_as_path': False, 'reference_path': None, 'loop': 1, 'sample_name': None, 'num_sample': 1}
Traceback (most recent call last):
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/transformers/utils/hub.py", line 389, in cached_file
    resolved_file = hf_hub_download(
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 106, in _inner_fn
    validate_repo_id(arg_value)
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 154, in validate_repo_id
    raise HFValidationError(
huggingface_hub.errors.HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/data/models/t5-v1_1xxl'. Use `repo_type` argument if needed.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/workspace/Open-Sora/scripts/inference.py", line 174, in <module>
    main()
  File "/workspace/Open-Sora/scripts/inference.py", line 57, in main
    text_encoder = build_module(cfg.text_encoder, MODELS, device=device)  # T5 must be fp32
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/opensora/registry.py", line 22, in build_module
    return builder.build(cfg)
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/mmengine/registry/registry.py", line 570, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/mmengine/registry/build_functions.py", line 121, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/opensora/models/text_encoder/t5.py", line 151, in __init__
    self.t5 = T5Embedder(
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/opensora/models/text_encoder/t5.py", line 103, in __init__
    self.tokenizer = AutoTokenizer.from_pretrained(
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 737, in from_pretrained
    tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 569, in get_tokenizer_config
    resolved_config_file = cached_file(
  File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/transformers/utils/hub.py", line 454, in cached_file
    raise EnvironmentError(
OSError: Incorrect path_or_model_id: '/data/models/t5-v1_1xxl'. Please provide either the path to a local folder or the repo_id of a model on the Hub.
CN-COTER commented 2 weeks ago

Could u tell us how to deal with the issiue that use offline model to do inference? Thx !

LoserLus commented 2 weeks ago

Could u tell us how to deal with the issiue that use offline model to do inference? Thx !

你可以修改你所使用的config里面有关模型路径的配置,例如你可以将Open-Sora/configs/opensora-v1-1/inference/samply.py里面的配置,修改为下面的配置:

# Define model
model = dict(
    type="STDiT2-XL/2",
    from_pretrained="<path_to_your_model>/OpenSora-STDiT-v2-stage3",
    input_sq_size=512,
    qk_norm=True,
    qk_norm_legacy=True,
    enable_flash_attn=True,
    enable_layernorm_kernel=True,
)
vae = dict(
    type="VideoAutoencoderKL",
    from_pretrained="<path_to_your_model>/sd-vae-ft-ema",
    cache_dir=None,  # "/mnt/hdd/cached_models",
    micro_batch_size=4,
)
text_encoder = dict(
    type="t5",
    from_pretrained="<path_to_your_model>/t5-v1_1-xxl",
    cache_dir=None,  # "/mnt/hdd/cached_models",
    model_max_length=200,
)

这一部分你可以参考issue218。 但是经过我的测试,我发现,对于OpenSora-STDiT-v2-stage3而言,你需要在from_pretrained="<path_to_your_model>/OpenSora-STDiT-v2-stage3"所指向的目录中新建一个子目录model,然后将你下载到的相关文件都移入model文件夹中,存放模型的目录结构如下所示:

tree models/
models/
|-- OpenSora-STDiT-v2-stage3
|   `-- model
|       |-- README.md
|       |-- config.json
|       |-- configuration_stdit2.py
|       |-- gitattributes
|       |-- layers.py
|       |-- model.safetensors
|       |-- modeling_stdit2.py
|       `-- utils.py
|-- sd-vae-ft-ema
|   |-- README.md
|   |-- config.json
|   |-- diffusion_pytorch_model.bin
|   |-- diffusion_pytorch_model.safetensors
|   `-- gitattributes
`-- t5-v1_1-xxl
    |-- config.json
    |-- gitattributes
    |-- pytorch_model-00001-of-00002.bin
    |-- pytorch_model-00002-of-00002.bin
    |-- pytorch_model.bin.index.json
    |-- special_tokens_map.json
    |-- spiece.model
    `-- tokenizer_config.json

你也可以参考[潞晨云部署视频教程],通过ln -s软连接命令将已下载好的模型权重连接到.cache目录。

CN-COTER commented 1 week ago

Could u tell us how to deal with the issiue that use offline model to do inference? Thx !

你可以修改你所使用的config里面有关模型路径的配置,例如你可以将Open-Sora/configs/opensora-v1-1/inference/samply.py里面的配置,修改为下面的配置:

# Define model
model = dict(
    type="STDiT2-XL/2",
    from_pretrained="<path_to_your_model>/OpenSora-STDiT-v2-stage3",
    input_sq_size=512,
    qk_norm=True,
    qk_norm_legacy=True,
    enable_flash_attn=True,
    enable_layernorm_kernel=True,
)
vae = dict(
    type="VideoAutoencoderKL",
    from_pretrained="<path_to_your_model>/sd-vae-ft-ema",
    cache_dir=None,  # "/mnt/hdd/cached_models",
    micro_batch_size=4,
)
text_encoder = dict(
    type="t5",
    from_pretrained="<path_to_your_model>/t5-v1_1-xxl",
    cache_dir=None,  # "/mnt/hdd/cached_models",
    model_max_length=200,
)

这一部分你可以参考issue218。 但是经过我的测试,我发现,对于OpenSora-STDiT-v2-stage3而言,你需要在from_pretrained="<path_to_your_model>/OpenSora-STDiT-v2-stage3"所指向的目录中新建一个子目录model,然后将你下载到的相关文件都移入model文件夹中,存放模型的目录结构如下所示:

tree models/
models/
|-- OpenSora-STDiT-v2-stage3
|   `-- model
|       |-- README.md
|       |-- config.json
|       |-- configuration_stdit2.py
|       |-- gitattributes
|       |-- layers.py
|       |-- model.safetensors
|       |-- modeling_stdit2.py
|       `-- utils.py
|-- sd-vae-ft-ema
|   |-- README.md
|   |-- config.json
|   |-- diffusion_pytorch_model.bin
|   |-- diffusion_pytorch_model.safetensors
|   `-- gitattributes
`-- t5-v1_1-xxl
    |-- config.json
    |-- gitattributes
    |-- pytorch_model-00001-of-00002.bin
    |-- pytorch_model-00002-of-00002.bin
    |-- pytorch_model.bin.index.json
    |-- special_tokens_map.json
    |-- spiece.model
    `-- tokenizer_config.json

你也可以参考[潞晨云部署视频教程],通过ln -s软连接命令将已下载好的模型权重连接到.cache目录。

Got, thank u very much!