-
Notifications
You must be signed in to change notification settings - Fork 3
feat: enhance Auto Class to support remote and local path #5
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
83d31dc
refactor: rename parameter pretrained_model_name_or_path
Tohrusky c67e3a8
refactor: rename parameter pretrained_model_name_or_path
Tohrusky 5ef996f
refactor: rename parameter pretrained_model_name_or_path
Tohrusky c34d5e7
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 0315580
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 6292e2f
feat: enhance AutoConfig to support path-based model configuration
Tohrusky c448ec1
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 65bafa7
feat: enhance AutoConfig to support path-based model configuration
Tohrusky bd472bb
feat: enhance AutoConfig to support path-based model configuration
Tohrusky eb0cad6
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 13e0910
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 4ad047f
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 459488b
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 93d36e4
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 7216b9a
feat: enhance AutoConfig to support path-based model configuration
Tohrusky c894c2b
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 9fa55b8
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 4bfb4a6
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 771c2b3
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 1d92040
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 9433b88
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 1ba22b7
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 23d4c4b
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 4bd845c
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 045ce40
feat: enhance AutoConfig to support path-based model configuration
Tohrusky 42eed70
feat: enhance AutoConfig to support path-based model configuration
Tohrusky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,3 +170,5 @@ cython_debug/ | |
|
|
||
| *.mp4 | ||
| *.mkv | ||
|
|
||
| /cccv/cache_models/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,81 @@ | ||
| import importlib.util | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any, Optional, Union | ||
|
|
||
| from cccv.config import CONFIG_REGISTRY, BaseConfig | ||
| from cccv.config import CONFIG_REGISTRY, AutoBaseConfig | ||
| from cccv.type import ConfigType | ||
| from cccv.util.remote import git_clone | ||
|
|
||
|
|
||
| class AutoConfig: | ||
| @staticmethod | ||
| def from_pretrained( | ||
| pretrained_model_name: Union[ConfigType, str], | ||
| pretrained_model_name_or_path: Union[ConfigType, str, Path], | ||
| *, | ||
| model_dir: Optional[Union[Path, str]] = None, | ||
| **kwargs: Any, | ||
| ) -> Any: | ||
| """ | ||
| Get a config instance of a pretrained model configuration. | ||
| Get a config instance of a pretrained model configuration, can be a registered config name or a local path or a git url. | ||
|
|
||
| :param pretrained_model_name: The name of the pretrained model configuration | ||
| :param pretrained_model_name_or_path: The name or path of the pretrained model configuration | ||
| :param model_dir: The path to cache the downloaded model configuration. Should be a full path. If None, use default cache path. | ||
| :return: | ||
| """ | ||
Tohrusky marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
20
to
26
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. The new functionality to load from a path or URL is great! To help users adopt it, it would be very beneficial to expand this docstring to describe the expected directory structure. For example:
|
||
| return CONFIG_REGISTRY.get(pretrained_model_name) | ||
| if "pretrained_model_name" in kwargs: | ||
| print( | ||
| "[CCCV] warning: 'pretrained_model_name' is deprecated, please use 'pretrained_model_name_or_path' instead." | ||
Tohrusky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| pretrained_model_name_or_path = kwargs.pop("pretrained_model_name") | ||
|
|
||
| @staticmethod | ||
| def register(config: Union[BaseConfig, Any], name: Optional[str] = None) -> None: | ||
| """ | ||
| Register the given config class instance under the name BaseConfig.name or the given name. | ||
| Can be used as a function call. See docstring of this class for usage. | ||
| # 1. check if it's a registered config name, early return if found | ||
| if isinstance(pretrained_model_name_or_path, ConfigType): | ||
| pretrained_model_name_or_path = pretrained_model_name_or_path.value | ||
| if str(pretrained_model_name_or_path) in CONFIG_REGISTRY: | ||
| return CONFIG_REGISTRY.get(str(pretrained_model_name_or_path)) | ||
|
|
||
| :param config: The config class instance to register | ||
| :param name: The name to register the config class instance under. If None, use BaseConfig.name | ||
| :return: | ||
| """ | ||
| # used as a function call | ||
| CONFIG_REGISTRY.register(obj=config, name=name) | ||
| # 2. check is a url or not, if it's a url, git clone it to model_dir then replace pretrained_model_name_or_path with the local path (Path) | ||
| if str(pretrained_model_name_or_path).startswith("http"): | ||
| pretrained_model_name_or_path = git_clone( | ||
| git_url=str(pretrained_model_name_or_path), | ||
| model_dir=model_dir, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| # 3. check if it's a real path | ||
| dir_path = Path(str(pretrained_model_name_or_path)) | ||
|
|
||
| if not dir_path.exists() or not dir_path.is_dir(): | ||
| raise ValueError(f"[CCCV] model configuration '{dir_path}' is not a valid config name or path") | ||
|
|
||
| # load config,json from the directory | ||
| config_path = dir_path / "config.json" | ||
| # check if config.json exists | ||
| if not config_path.exists(): | ||
| raise FileNotFoundError(f"[CCCV] no valid config.json not found in {dir_path}") | ||
|
|
||
| with open(config_path, "r", encoding="utf-8") as f: | ||
| config_dict = json.load(f) | ||
|
|
||
| for k in ["arch", "model", "name"]: | ||
| if k not in config_dict: | ||
| raise KeyError( | ||
| f"[CCCV] no key '{k}' in config.json in {dir_path}, you should provide a valid config.json contain a key '{k}'" | ||
| ) | ||
|
|
||
| # auto import all .py files in the directory to register the arch, model and config | ||
| for py_file in dir_path.glob("*.py"): | ||
| spec = importlib.util.spec_from_file_location(py_file.stem, py_file) | ||
| if spec is None or spec.loader is None: | ||
| continue | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
|
|
||
| if "path" not in config_dict or config_dict["path"] is None or config_dict["path"] == "": | ||
| # add the path to the config_dict | ||
| config_dict["path"] = str(dir_path / config_dict["name"]) | ||
|
|
||
| # convert config_dict to pydantic model | ||
| cfg = AutoBaseConfig.model_validate(config_dict) | ||
| return cfg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.