From f2d4e5beb12dfc12a0bb19cd5fdf8c6d8f72340c Mon Sep 17 00:00:00 2001 From: Kaihui-intel Date: Wed, 17 Jul 2024 13:45:25 +0800 Subject: [PATCH 1/3] fix load_empty_model Signed-off-by: Kaihui-intel --- neural_compressor/torch/utils/__init__.py | 1 - neural_compressor/torch/utils/utility.py | 59 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/neural_compressor/torch/utils/__init__.py b/neural_compressor/torch/utils/__init__.py index 25aadaa6d66..dab02a017c6 100644 --- a/neural_compressor/torch/utils/__init__.py +++ b/neural_compressor/torch/utils/__init__.py @@ -15,4 +15,3 @@ from .environ import * from .constants import * from .utility import * -from neural_compressor.torch.algorithms.layer_wise import load_empty_model diff --git a/neural_compressor/torch/utils/utility.py b/neural_compressor/torch/utils/utility.py index ecdbf2d6243..d4c9304353b 100644 --- a/neural_compressor/torch/utils/utility.py +++ b/neural_compressor/torch/utils/utility.py @@ -278,3 +278,62 @@ def get_processor_type_from_user_config(user_processor_type: Optional[Union[str, else: raise NotImplementedError(f"Unsupported processor type: {user_processor_type}") return processor_type + +def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): + """Download hugging face model from hf hub.""" + from huggingface_hub.constants import DEFAULT_REVISION, HUGGINGFACE_HUB_CACHE + from huggingface_hub.file_download import REGEX_COMMIT_HASH, repo_folder_name + from huggingface_hub.utils import EntryNotFoundError + import os + + if cache_dir is None: + cache_dir = HUGGINGFACE_HUB_CACHE + if revision is None: + revision = DEFAULT_REVISION + if repo_type is None: + repo_type = "model" + storage_folder = os.path.join(cache_dir, repo_folder_name(repo_id=repo_id, repo_type=repo_type)) + commit_hash = None + if REGEX_COMMIT_HASH.match(revision): + commit_hash = revision + else: + ref_path = os.path.join(storage_folder, "refs", revision) + if os.path.exists(ref_path): + with open(ref_path) as f: + commit_hash = f.read() + if storage_folder and commit_hash: + pointer_path = os.path.join(storage_folder, "snapshots", commit_hash) + if os.path.isdir(pointer_path): + return pointer_path + else: # pragma: no cover + from huggingface_hub import snapshot_download + + file_path = snapshot_download(repo_id) + return file_path + + +def load_empty_model(pretrained_model_name_or_path, cls=None, **kwargs): + """Load a empty model.""" + import os + from transformers import AutoConfig, AutoModelForCausalLM + from transformers.models.auto.auto_factory import _BaseAutoModelClass + from accelerate import init_empty_weights + + cls = AutoModelForCausalLM if cls is None else cls + is_local = os.path.isdir(pretrained_model_name_or_path) + if is_local: # pragma: no cover + path = pretrained_model_name_or_path + else: + path = dowload_hf_model(pretrained_model_name_or_path) + if cls.__base__ == _BaseAutoModelClass: + config = AutoConfig.from_pretrained(path, **kwargs) + with init_empty_weights(): + model = cls.from_config(config) + else: # pragma: no cover + config = cls.config_class.from_pretrained(path, **kwargs) + with init_empty_weights(): + model = cls(config) + model.tie_weights() + model.eval() + model.path = pretrained_model_name_or_path + return model \ No newline at end of file From 668e3d9be43c5bec4d7110343aeb7bd4dbfe7c7b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Jul 2024 05:50:42 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_compressor/torch/utils/utility.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/neural_compressor/torch/utils/utility.py b/neural_compressor/torch/utils/utility.py index d4c9304353b..b210288c1e7 100644 --- a/neural_compressor/torch/utils/utility.py +++ b/neural_compressor/torch/utils/utility.py @@ -279,12 +279,14 @@ def get_processor_type_from_user_config(user_processor_type: Optional[Union[str, raise NotImplementedError(f"Unsupported processor type: {user_processor_type}") return processor_type + def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): """Download hugging face model from hf hub.""" + import os + from huggingface_hub.constants import DEFAULT_REVISION, HUGGINGFACE_HUB_CACHE from huggingface_hub.file_download import REGEX_COMMIT_HASH, repo_folder_name from huggingface_hub.utils import EntryNotFoundError - import os if cache_dir is None: cache_dir = HUGGINGFACE_HUB_CACHE @@ -315,9 +317,10 @@ def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): def load_empty_model(pretrained_model_name_or_path, cls=None, **kwargs): """Load a empty model.""" import os + + from accelerate import init_empty_weights from transformers import AutoConfig, AutoModelForCausalLM from transformers.models.auto.auto_factory import _BaseAutoModelClass - from accelerate import init_empty_weights cls = AutoModelForCausalLM if cls is None else cls is_local = os.path.isdir(pretrained_model_name_or_path) @@ -336,4 +339,4 @@ def load_empty_model(pretrained_model_name_or_path, cls=None, **kwargs): model.tie_weights() model.eval() model.path = pretrained_model_name_or_path - return model \ No newline at end of file + return model From 03d5c3f43a2fef99de77280c242cc47a74ba943c Mon Sep 17 00:00:00 2001 From: Kaihui-intel Date: Wed, 17 Jul 2024 15:27:42 +0800 Subject: [PATCH 3/3] improve coverage Signed-off-by: Kaihui-intel --- neural_compressor/torch/algorithms/layer_wise/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_compressor/torch/algorithms/layer_wise/utils.py b/neural_compressor/torch/algorithms/layer_wise/utils.py index bbe59de3fe4..0cbe23d5cca 100644 --- a/neural_compressor/torch/algorithms/layer_wise/utils.py +++ b/neural_compressor/torch/algorithms/layer_wise/utils.py @@ -90,7 +90,7 @@ def get_named_children(model, pre=[]): return module_list -def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): +def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): # pragma: no cover """Download hugging face model from hf hub.""" from huggingface_hub.constants import DEFAULT_REVISION, HUGGINGFACE_HUB_CACHE from huggingface_hub.file_download import REGEX_COMMIT_HASH, repo_folder_name @@ -122,7 +122,7 @@ def dowload_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None): return file_path -def load_empty_model(pretrained_model_name_or_path, cls=AutoModelForCausalLM, **kwargs): +def load_empty_model(pretrained_model_name_or_path, cls=AutoModelForCausalLM, **kwargs): # pragma: no cover """Load a empty model.""" is_local = os.path.isdir(pretrained_model_name_or_path) if is_local: # pragma: no cover