-
Notifications
You must be signed in to change notification settings - Fork 6.4k
[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
Changes from 8 commits
e506314
85024b0
6a0d0be
d86b0f2
314b6cc
528e002
6e92f40
76ea98d
0e53ad0
f697631
5614a15
f6b6b42
4e5cac1
24f16f6
684384c
0fe68cd
2950372
67e3404
694b81c
3bf51cd
13420fb
af007ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,9 +16,12 @@ | |||||
| import os | ||||||
| from typing import Optional, Union | ||||||
|
|
||||||
| from huggingface_hub import constants, hf_hub_download | ||||||
| from huggingface_hub.utils import validate_hf_hub_args | ||||||
|
|
||||||
| from .. import pipelines | ||||||
| from ..configuration_utils import ConfigMixin | ||||||
| from ..pipelines.pipeline_loading_utils import ALL_IMPORTABLE_CLASSES, get_class_obj_and_candidates | ||||||
|
|
||||||
|
|
||||||
| class AutoModel(ConfigMixin): | ||||||
|
|
@@ -156,12 +159,28 @@ def from_pretrained(cls, pretrained_model_or_path: Optional[Union[str, os.PathLi | |||||
| "subfolder": subfolder, | ||||||
| } | ||||||
|
|
||||||
| config = cls.load_config(pretrained_model_or_path, **load_config_kwargs) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would avoid using exceptions for control flow and simplify this a bit load_config_kwargs = {
"cache_dir": cache_dir,
"force_download": force_download,
"proxies": proxies,
"token": token,
"local_files_only": local_files_only,
"revision": revision,
}
library = None
orig_class_name = None
from diffusers import pipelines
# Always attempt to fetch model_index.json first
try:
cls.config_name = "model_index.json"
config = cls.load_config(pretrained_model_or_path, **load_config_kwargs)
if subfolder is not None and subfolder in config:
library, orig_class_name = config[subfolder]
except EntryNotFoundError as e:
logger.debug(e)
# Unable to load from model_index.json so fallback to loading from config
if library is None and orig_class_name is None:
cls.config_name = "config.json"
load_config_kwargs.update({"subfolder": subfolder})
config = cls.load_config(pretrained_model_or_path, **load_config_kwargs)
orig_class_name = config["_class_name"]
library = "diffusers"
model_cls, _ = get_class_obj_and_candidates(
library_name=library,
class_name=orig_class_name,
importable_classes=ALL_IMPORTABLE_CLASSES,
pipelines=pipelines,
is_pipeline_module=hasattr(pipelines, library),
) |
||||||
| orig_class_name = config["_class_name"] | ||||||
| try: | ||||||
| mindex_kwargs = {k: v for k, v in load_config_kwargs.items() if k != "subfolder"} | ||||||
|
||||||
| mindex_kwargs["filename"] = "model_index.json" | ||||||
| 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] | ||||||
| model_cls, _ = get_class_obj_and_candidates( | ||||||
| library_name=library, | ||||||
| class_name=orig_class_name, | ||||||
| importable_classes=ALL_IMPORTABLE_CLASSES, | ||||||
| pipelines=pipelines, | ||||||
| is_pipeline_module=hasattr(pipelines, library), | ||||||
| component_name=subfolder, | ||||||
|
||||||
| component_name=subfolder, | |
| component_name=None, |
Outdated
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.
| cache_dir=constants.HF_HUB_CACHE, | |
| cache_dir=None, |
let's jjust pass None here since we ddon't actually have a cached_dir so not very meaningful
and update the other function so it works with cache_dir=None and component_name=NOne
Outdated
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.
We should catch the specific Exception here instead of making it generic. This will help eliminate other side-effects.
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -332,7 +332,7 @@ def maybe_raise_or_warn( | |||
|
|
||||
|
|
||||
| def get_class_obj_and_candidates( | ||||
| library_name, class_name, importable_classes, pipelines, is_pipeline_module, component_name=None, cache_dir=None | ||||
| library_name, class_name, importable_classes, pipelines, is_pipeline_module, component_name, cache_dir | ||||
|
||||
| library_name, class_name, importable_classes, pipelines, is_pipeline_module, component_name=None, cache_dir=None |
It didn't error out for me 👀
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.
Oh, they should run fine even without it, because of this line
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.
well, it is wrapped inside a try/exception.....
but what I meant we should update this function so that componenet_name and cache_dir aree optional argument (it is meant to be, they aree only needed for custom code)
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.
I think if we move this import inside the
tryblock, we should be able to get rid of the circular import problem: https://github.com/huggingface/diffusers/actions/runs/14787632114/job/41518909088?pr=11401#step:15:68