-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[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
base: main
Are you sure you want to change the base?
Conversation
thanks @ishan-modi ! |
On it |
src/diffusers/models/auto_model.py
Outdated
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"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably this?
library, orig_class_name = config["subfolder"] | |
library, orig_class_name = config[subfolder] |
@yiyixuxu, should be working now ! |
There was a problem hiding this 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
src/diffusers/models/auto_model.py
Outdated
@@ -22,7 +22,7 @@ | |||
|
|||
|
|||
class AutoModel(ConfigMixin): | |||
config_name = "config.json" | |||
config_name = "model_index.json" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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
src/diffusers/models/auto_model.py
Outdated
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) |
There was a problem hiding this comment.
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( |
Thanks for the patience, all test cases work now Scriptfrom 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 ! |
What does this PR do?
Allows
AutoModel
to load components usingmodel_index.json
. Ifmodel_index.json
is not foundconfig.json
(existing) is used as a fallbackFixes #11388
Who can review?
@yiyixuxu