huggingface / diffusers

🤗 Diffusers: State-of-the-art diffusion models for image and audio generation in PyTorch and FLAX.
https://huggingface.co/docs/diffusers
Apache License 2.0
25.66k stars 5.3k forks source link

Pytorch 2.4.0 - module 'torch.library' has no attribute 'register_fake' #8958

Closed jonluca closed 2 months ago

jonluca commented 2 months ago

Describe the bug

When using diffusers with pytorch 2.4.0, there is an error on import

Reproduction

Install diffusers and then instantiate StableVideoDiffusionXL

from diffusers import StableVideoDiffusionPipeline

import torch
from diffusers import StableVideoDiffusionPipeline
import PIL.Image
import random
from typing import List
from diffusers.utils import load_image, export_to_video

from hosted.utils.generic_runner import config

model: StableVideoDiffusionPipeline | None = None
MODEL = "weights/stable-video-diffusion-img2vid-xt-1-1"
HEIGHT = 768
WIDTH = 768
INPUT_IMAGE = "https://assets.weights.gg/rocket.png"

def main():
    # start model
    model = StableVideoDiffusionPipeline.from_pretrained(MODEL, torch_dtype=torch.float16, variant="fp16").to(
        config.device
    )

    backend = "torch_tensorrt"

    model.unet = torch.compile(
        model.unet,
        backend=backend,
        options={
            "truncate_long_and_double": True,
            "enabled_precisions": {torch.float32, torch.float16},
        },
        dynamic=False,
    )

    input_image_url = INPUT_IMAGE
    image = load_image(input_image_url)
    image = image.resize((WIDTH, HEIGHT))
    # Ensure using the same inference steps as the loaded model and CFG set to 0.
    # get random seed
    seed = random.randint(0, (2**32) - 1)
    generator = torch.manual_seed(seed)

    frames: List[PIL.Image.Image] = model(
        image,
        height=HEIGHT,
        width=WIDTH,
        num_frames=25,
        num_inference_steps=25,
        generator=generator,
        motion_bucket_id=127,
    ).frames[0]

    if frames is None or len(frames) == 0:
        print("Failed to generate image")
        return None

    # For each image, resize back
    mirrored_frames = frames[::-1]
    frames = frames + mirrored_frames
    output_video = export_to_video(frames, "generated_video.mp4", fps=14)

    print(output_video)

if __name__ == "__main__":
    main()

Logs

2024-07-24T23:42:57.499313371Z stderr F   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module

2024-07-24T23:42:57.499408839Z stderr F     return _bootstrap._gcd_import(name[level:], package, level)

2024-07-24T23:42:57.499453974Z stderr F            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.499465906Z stderr F   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import

2024-07-24T23:42:57.499482808Z stderr F   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load

2024-07-24T23:42:57.49951041Z stderr F   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked

2024-07-24T23:42:57.499538853Z stderr F   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked

2024-07-24T23:42:57.499553601Z stderr F   File "<frozen importlib._bootstrap_external>", line 940, in exec_module

2024-07-24T23:42:57.499560053Z stderr F   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed

2024-07-24T23:42:57.499566385Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/models/clip/image_processing_clip.py", line 21, in <module>

2024-07-24T23:42:57.499582625Z stderr F     from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict

2024-07-24T23:42:57.499594177Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/image_processing_utils.py", line 21, in <module>

2024-07-24T23:42:57.499647747Z stderr F     from .image_transforms import center_crop, normalize, rescale

2024-07-24T23:42:57.499668376Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/image_transforms.py", line 22, in <module>

2024-07-24T23:42:57.499728719Z stderr F     from .image_utils import (

2024-07-24T23:42:57.499742746Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/image_utils.py", line 58, in <module>

2024-07-24T23:42:57.499790325Z stderr F     from torchvision.transforms import InterpolationMode

2024-07-24T23:42:57.499802387Z stderr F   File "/usr/local/lib/python3.11/site-packages/torchvision/__init__.py", line 10, in <module>

2024-07-24T23:42:57.499857801Z stderr F     from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils  # usort:skip

2024-07-24T23:42:57.499977546Z stderr F     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.499994408Z stderr F   File "/usr/local/lib/python3.11/site-packages/torchvision/_meta_registrations.py", line 163, in <module>

2024-07-24T23:42:57.500078526Z stderr F     @torch.library.register_fake("torchvision::nms")

2024-07-24T23:42:57.50011302Z stderr F      ^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.50014487Z stderr F AttributeError: module 'torch.library' has no attribute 'register_fake'

2024-07-24T23:42:57.500154999Z stderr F 

2024-07-24T23:42:57.500160569Z stderr F The above exception was the direct cause of the following exception:

2024-07-24T23:42:57.50016599Z stderr F 

2024-07-24T23:42:57.50017134Z stderr F Traceback (most recent call last):

2024-07-24T23:42:57.500189193Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/utils/import_utils.py", line 808, in _get_module

2024-07-24T23:42:57.500358596Z stderr F     return importlib.import_module("." + module_name, self.__name__)

2024-07-24T23:42:57.500421955Z stderr F            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.500431673Z stderr F   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module

2024-07-24T23:42:57.500524567Z stderr F     return _bootstrap._gcd_import(name[level:], package, level)

2024-07-24T23:42:57.500605699Z stderr F            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.500643731Z stderr F   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import

2024-07-24T23:42:57.500653389Z stderr F   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load

2024-07-24T23:42:57.50065909Z stderr F   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked

2024-07-24T23:42:57.500664409Z stderr F   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked

2024-07-24T23:42:57.500668517Z stderr F   File "<frozen importlib._bootstrap_external>", line 940, in exec_module

2024-07-24T23:42:57.500672545Z stderr F   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed

2024-07-24T23:42:57.500677103Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py", line 22, in <module>

2024-07-24T23:42:57.500725404Z stderr F     from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection

2024-07-24T23:42:57.500736024Z stderr F   File "<frozen importlib._bootstrap>", line 1229, in _handle_fromlist

2024-07-24T23:42:57.500740683Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1577, in __getattr__

2024-07-24T23:42:57.500998571Z stderr F     value = getattr(module, name)

2024-07-24T23:42:57.501037664Z stderr F             ^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.501049557Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1576, in __getattr__

2024-07-24T23:42:57.501307986Z stderr F     module = self._get_module(self._class_to_module[name])

2024-07-24T23:42:57.501382962Z stderr F              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.501397609Z stderr F   File "/usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1588, in _get_module

2024-07-24T23:42:57.501653359Z stderr F     raise RuntimeError(

2024-07-24T23:42:57.501663648Z stderr F RuntimeError: Failed to import transformers.models.clip.image_processing_clip because of the following error (look up to see its traceback):

2024-07-24T23:42:57.501668337Z stderr F module 'torch.library' has no attribute 'register_fake'

2024-07-24T23:42:57.501672374Z stderr F 

2024-07-24T23:42:57.501676592Z stderr F The above exception was the direct cause of the following exception:

2024-07-24T23:42:57.501681211Z stderr F 

2024-07-24T23:42:57.50168573Z stderr F Traceback (most recent call last):

2024-07-24T23:42:57.501697832Z stderr F   File "<frozen runpy>", line 198, in _run_module_as_main

2024-07-24T23:42:57.501707761Z stderr F   File "<frozen runpy>", line 88, in _run_code

2024-07-24T23:42:57.501711939Z stderr F   File "/app/hosted/video/video_job_runner.py", line 22, in <module>

2024-07-24T23:42:57.501748622Z stderr F     from diffusers import StableVideoDiffusionPipeline

2024-07-24T23:42:57.501766326Z stderr F   File "<frozen importlib._bootstrap>", line 1229, in _handle_fromlist

2024-07-24T23:42:57.501784349Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/utils/import_utils.py", line 799, in __getattr__

2024-07-24T23:42:57.501909549Z stderr F     value = getattr(module, name)

2024-07-24T23:42:57.50194177Z stderr F             ^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.501966105Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/utils/import_utils.py", line 799, in __getattr__

2024-07-24T23:42:57.502117549Z stderr F     value = getattr(module, name)

2024-07-24T23:42:57.502151608Z stderr F             ^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.502164081Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/utils/import_utils.py", line 798, in __getattr__

2024-07-24T23:42:57.502358266Z stderr F     module = self._get_module(self._class_to_module[name])

2024-07-24T23:42:57.502422437Z stderr F              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2024-07-24T23:42:57.502466269Z stderr F   File "/usr/local/lib/python3.11/site-packages/diffusers/utils/import_utils.py", line 810, in _get_module

2024-07-24T23:42:57.502615108Z stderr F     raise RuntimeError(

2024-07-24T23:42:57.502624215Z stderr F RuntimeError: Failed to import diffusers.pipelines.stable_video_diffusion.pipeline_stable_video_diffusion because of the following error (look up to see its traceback):

2024-07-24T23:42:57.502629545Z stderr F Failed to import transformers.models.clip.image_processing_clip because of the following error (look up to see its traceback):

2024-07-24T23:42:57.502633873Z stderr F module 'torch.library' has no attribute 'register_fake'

System Info

Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.

Who can help?

@yiyixuxu

HelloWorld-1017 commented 2 months ago

Maybe you can try to comment this line @torch.library.register_fake("torchvision::nms") in your file /usr/local/lib/python3.11/site-packages/torchvision/_meta_registrations.py.

yiyixuxu commented 2 months ago

hi! can you try to upgrade torch and torchvision to the latest version?

jonluca commented 2 months ago

I'm using 0.19.0 already - are you unable to repro?

This is our requirements.txt

PyYAML==6.0.1
accelerate==0.33.0
aioboto3==13.1.1
aiobotocore==2.13.1
aiohttp==3.9.5
audioread==3.0.1
bitarray==2.9.2
boto3==1.34.106
diffq==0.2.4
diffusers==0.29.2
einops-exts==0.0.4
einops==0.8.0
fastapi==0.111.1
ffmpeg-python==0.2.0
faiss-cpu
huggingface_hub==0.24.2
julius==0.2.7
librosa==0.10.2.post1
llvmlite==0.43.0
munch==4.0.0
ninja==1.11.1.1
mutagen==1.47.0
nltk==3.8.1
numba==0.60.0
numpy==1.26.4
omegaconf==2.3.0
onnx2torch==1.5.14
onnxruntime-gpu==1.18.0; sys_platform == 'win32'
onnxruntime==1.18.1; sys_platform != 'win32'
phonemizer==3.2.1
prisma @ git+https://github.com/jonluca/prisma-client-py@6d0a9746b84708cd0a21bdea6ac590ff38f49b70
psutil==6.0.0
pydantic==2.8.2
pydub==0.25.1
pyinstaller==6.9.0
python-dotenv==1.0.1
pytorch-lightning==2.3.3
pyworld==0.3.4
redis[hiredis]==5.0.7
regex==2024.5.15
sacrebleu==2.4.2
safetensors==0.4.3
samplerate==0.2.1
scipy==1.14.0
soundfile==0.12.1
spacy==3.7.5
torch==2.4.0
torchaudio==2.4.0
torchcrepe==0.0.23
torchvision==0.19.0
imageio[ffmpeg]==2.34.2
transformers==4.43.2
triton==3.0.0; sys_platform == 'linux' and platform_machine == 'x86_64'
txtsplit==1.0.0
uvicorn==0.30.3
yt-dlp==2024.7.16
tensorboard==2.17.0
black
ytmusicapi==1.7.5
cutlet==0.4.0
coqpit==0.0.17
pysbd==0.3.4
trainer
rotary-embedding-torch==0.6.4
num2words==0.5.13
asomoza commented 2 months ago

Even so, this seems something related to your environment/installation and not from diffusers.

Torch 2.4 has the "register_fake" so if you have installed torch 2.4 you should have that function.

Also I can run your demo code (simplified) without any problem, I have torch 2.4 and torchvision 0.19.

Try a simple demo without diffusers, in the same environment can you run this code which is the example in the same function?

import torch
from torch import Tensor

# Example 1: an operator without data-dependent output shape
@torch.library.custom_op("mylib::custom_linear", mutates_args=())
def custom_linear(x: Tensor, weight: Tensor, bias: Tensor) -> Tensor:
    raise NotImplementedError("Implementation goes here")

@torch.library.register_fake("mylib::custom_linear")
def _(x, weight, bias):
    assert x.dim() == 2
    assert weight.dim() == 2
    assert bias.dim() == 1
    assert x.shape[1] == weight.shape[1]
    assert weight.shape[0] == bias.shape[0]
    assert x.device == weight.device

    return (x @ weight.t()) + bias

with torch._subclasses.fake_tensor.FakeTensorMode():
    x = torch.randn(2, 3)
    w = torch.randn(3, 3)
    b = torch.randn(3)
    y = torch.ops.mylib.custom_linear(x, w, b)

assert y.shape == (2, 3)
jonluca commented 2 months ago

Got it, so it's something with our env. Will close this out, thanks!

Parthiv911 commented 2 months ago

Maybe you can try to comment this line @torch.library.register_fake("torchvision::nms") in your file /usr/local/lib/python3.11/site-packages/torchvision/_meta_registrations.py.

This worked for me but my torch version was 2.2.2. I was trying to import sentence-transformers package and in the error trace it showed that [Pytorch 2.4.0 - module 'torch.library' has no attribute 'register_fake']. I did your fix and now it is working.

654h commented 2 months ago

原因很简单啊,你的torch版本和torchvision版本不适配,可以去看一下官方文档两者适配的版本号即可,一般来说如果你的torch是2.3.1版本的话,那么你的torchvision一般为0.18.1即可

Cyebukayire commented 2 weeks ago

Maybe you can try to comment this line @torch.library.register_fake("torchvision::nms") in your file /usr/local/lib/python3.11/site-packages/torchvision/_meta_registrations.py.

Thank you so much, that worked for me!