-
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
Changes from 22 commits
83d31dc
c67e3a8
5ef996f
c34d5e7
0315580
6292e2f
c448ec1
65bafa7
bd472bb
eb0cad6
13e0910
4ad047f
459488b
93d36e4
7216b9a
c894c2b
9fa55b8
4bfb4a6
771c2b3
1d92040
9433b88
1ba22b7
23d4c4b
4bd845c
045ce40
42eed70
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 |
|---|---|---|
|
|
@@ -170,3 +170,5 @@ cython_debug/ | |
|
|
||
| *.mp4 | ||
| *.mkv | ||
|
|
||
| /cccv/cache_models/ | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,7 +61,7 @@ def cuda_kernel(strFunction: str, strKernel: str, objVariables: typing.Dict): | |||||||||||||||||||||||||
| strKey += str(objValue.stride()) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| elif True: | ||||||||||||||||||||||||||
| print(strVariable, type(objValue)) | ||||||||||||||||||||||||||
| print(f"[CCCV] {strVariable}, {type(objValue)}") | ||||||||||||||||||||||||||
|
Comment on lines
63
to
+64
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. This
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # end | ||||||||||||||||||||||||||
| # end | ||||||||||||||||||||||||||
|
|
@@ -106,10 +106,10 @@ def cuda_kernel(strFunction: str, strKernel: str, objVariables: typing.Dict): | |||||||||||||||||||||||||
| strKernel = strKernel.replace("{{type}}", "long") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| elif isinstance(objValue, torch.Tensor): | ||||||||||||||||||||||||||
| print(strVariable, objValue.dtype) | ||||||||||||||||||||||||||
| print(f"[CCCV] {strVariable}, {objValue.dtype}") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| elif True: | ||||||||||||||||||||||||||
| print(strVariable, type(objValue)) | ||||||||||||||||||||||||||
| print(f"[CCCV] {strVariable}, {type(objValue)}") | ||||||||||||||||||||||||||
|
Comment on lines
108
to
+112
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. Similar to the previous comment, these
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # end | ||||||||||||||||||||||||||
| # end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,84 @@ | ||
| import importlib.util | ||
| import json | ||
| import warnings | ||
| 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
|
||
| """ | ||
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: | ||
| warnings.warn( | ||
| "[CCCV] 'pretrained_model_name' is deprecated, please use 'pretrained_model_name_or_path' instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.