Skip to content

[Feature] AutoModel can load components using model_index.json #11401

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

ishan-modi
Copy link
Contributor

What does this PR do?

Allows AutoModel to load components using model_index.json. If model_index.json is not found config.json(existing) is used as a fallback

Fixes #11388

Who can review?

@yiyixuxu

@yiyixuxu
Copy link
Collaborator

thanks @ishan-modi !
can you run on the testing script here #11388 (comment)

@ishan-modi
Copy link
Contributor Author

On it

library = importlib.import_module("diffusers")
try:
config = cls.load_config(os.path.join(pretrained_model_or_path, "model_index.json"), **load_config_kwargs)
library, orig_class_name = config["subfolder"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably this?

Suggested change
library, orig_class_name = config["subfolder"]
library, orig_class_name = config[subfolder]

@ishan-modi
Copy link
Contributor Author

ishan-modi commented Apr 23, 2025

@yiyixuxu, should be working now !

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently test1 and 3 still fail

@@ -22,7 +22,7 @@


class AutoModel(ConfigMixin):
config_name = "config.json"
config_name = "model_index.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config_name = "model_index.json"

this should not changed, the model config should still be saved in a config.json
maybe, you can try to use this to download model_index https://github.com/huggingface/diffusers/blob/main/src/diffusers/loaders/single_file.py#L242

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, completely missed it. Let me know if the testcases work now. I think they were failing earlier because I didn't have latest changes in the branch

config_path = hf_hub_download(pretrained_model_or_path, **mindex_kwargs)
config = cls.load_config(config_path, **load_config_kwargs)
library, orig_class_name = config[subfolder]
library = importlib.import_module(library)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work for custom modules? like this

# test3: load a custom diffusers model 
# https://huggingface.co/Kwai-Kolors/Kolors-diffusers/blob/main/model_index.json#L9
try:
    model = AutoModel.from_pretrained("Kwai-Kolors/Kolors-diffusers", subfolder="text_encoder", torch_dtype=torch.bfloat16)
    print(f"test3 passed!")
except Exception as e:
    print(f"test3 failed: {e}")

you can probably use this

def get_class_obj_and_candidates(

@ishan-modi
Copy link
Contributor Author

Thanks for the patience, all test cases work now

Script
from diffusers import AutoModel 
import torch


# test1: load a diffusers model
try:
    model = AutoModel.from_pretrained(
        "Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers", subfolder="transformer", torch_dtype=torch.bfloat16, 
    )
    print(f"test1 passed!")
except Exception as e:
    print(f"test1 failed: {e}")


# test2: load a non-diffusers model
try:
    model = AutoModel.from_pretrained(
        "HiDream-ai/HiDream-I1-Full", subfolder="text_encoder", torch_dtype=torch.bfloat16, 
    )
    print(f"test2 passed!")
except Exception as e:
    print(f"test2 failed: {e}")


# test3: load a custom diffusers model 
# https://huggingface.co/Kwai-Kolors/Kolors-diffusers/blob/main/model_index.json#L9
try:
    model = AutoModel.from_pretrained("Kwai-Kolors/Kolors-diffusers", subfolder="text_encoder", variant="fp16")
    print(f"test3 passed!")
except Exception as e:
    print(f"test3 failed: {e}")


# test4: load a model directly (not subfolder)
controlnet_repo = "InstantX/SD3-Controlnet-Canny"
try:
    controlnet_model = AutoModel.from_pretrained(
        controlnet_repo, revision="refs/pr/12"
    )
    print(f"test4 passed!")
except Exception as e:
    print(f"test4 failed: {e}")

Please let me know if anything seems incorrect !

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

Successfully merging this pull request may close these issues.

extend AutoModel to be able to load transformer models & custom diffusers models
2 participants