From 3d28697cd051dfafd75e6daab246c2da388627f7 Mon Sep 17 00:00:00 2001 From: Sonica-B Date: Sun, 8 Nov 2026 17:25:29 -0800 Subject: [PATCH] Forward revision, cache_dir and token to the task loaders in main_export main_export stored revision, cache_dir, subfolder, trust_remote_code and force_download in kwargs and dropped token and local_files_only, but the fill-mask, text2text-generation, automatic-speech-recognition and text-generation loaders called from_pretrained(model_name_or_path) without them. An export with revision="v2" therefore loaded and exported the weights from main, from the default cache, without the given token. Collect these options with a get_hub_kwargs helper and pass them to from_pretrained (and to AutoConfig in the causal LM loader). Default cache_dir to HF_HUB_CACHE instead of the legacy HUGGINGFACE_HUB_CACHE constant, so forwarding it does not override a user's HF_HUB_CACHE. --- optimum/executorch/modeling.py | 8 ++-- optimum/exporters/executorch/__main__.py | 6 ++- optimum/exporters/executorch/tasks/asr.py | 5 +- .../exporters/executorch/tasks/causal_lm.py | 6 ++- .../exporters/executorch/tasks/masked_lm.py | 3 +- .../exporters/executorch/tasks/seq2seq_lm.py | 3 +- optimum/exporters/executorch/utils.py | 16 +++++++ tests/models/test_modeling_common.py | 47 +++++++++++++++++++ 8 files changed, 83 insertions(+), 11 deletions(-) diff --git a/optimum/executorch/modeling.py b/optimum/executorch/modeling.py index 819cfa68..235bfcd8 100644 --- a/optimum/executorch/modeling.py +++ b/optimum/executorch/modeling.py @@ -24,7 +24,7 @@ import torch from huggingface_hub import hf_hub_download, is_offline_mode -from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE +from huggingface_hub.constants import HF_HUB_CACHE from torch.ao.quantization.fx._decomposed import quantized_decomposed_lib # noqa from transformers import ( AutoModel, @@ -166,7 +166,7 @@ def _from_pretrained( subfolder: str = "", force_download: bool = False, local_files_only: bool = False, - cache_dir: str = HUGGINGFACE_HUB_CACHE, + cache_dir: str = HF_HUB_CACHE, file_name: Optional[str] = None, **kwargs, ) -> Dict[str, "ExecuTorchModule"]: @@ -268,7 +268,7 @@ def _export( config: Optional[PretrainedConfig] = None, token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, - cache_dir: str = HUGGINGFACE_HUB_CACHE, + cache_dir: str = HF_HUB_CACHE, trust_remote_code: bool = False, subfolder: str = "", force_download: bool = False, @@ -322,7 +322,7 @@ def from_pretrained( trust_remote_code: bool = False, force_download: bool = False, local_files_only: bool = False, - cache_dir: str = HUGGINGFACE_HUB_CACHE, + cache_dir: str = HF_HUB_CACHE, config: Optional[PretrainedConfig] = None, **kwargs, ) -> "ExecuTorchModelBase": diff --git a/optimum/exporters/executorch/__main__.py b/optimum/exporters/executorch/__main__.py index 854bf260..329c6d19 100644 --- a/optimum/exporters/executorch/__main__.py +++ b/optimum/exporters/executorch/__main__.py @@ -20,7 +20,7 @@ import warnings from pathlib import Path -from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE +from huggingface_hub.constants import HF_HUB_CACHE from transformers import PretrainedConfig from transformers.utils import is_torch_available @@ -43,7 +43,7 @@ def main_export( recipe: str, output_dir: Union[str, Path], config: Optional[PretrainedConfig] = None, - cache_dir: str = HUGGINGFACE_HUB_CACHE, + cache_dir: str = HF_HUB_CACHE, trust_remote_code: bool = False, pad_token_id: Optional[int] = None, subfolder: str = "", @@ -129,6 +129,8 @@ def main_export( kwargs["subfolder"] = subfolder kwargs["revision"] = revision kwargs["force_download"] = force_download + kwargs["local_files_only"] = local_files_only + kwargs["token"] = token kwargs["config"] = config # 1. Load model, apply source transformations, and torch.export() into a graph (ExportedProgram). diff --git a/optimum/exporters/executorch/tasks/asr.py b/optimum/exporters/executorch/tasks/asr.py index 7650743e..7ad89176 100644 --- a/optimum/exporters/executorch/tasks/asr.py +++ b/optimum/exporters/executorch/tasks/asr.py @@ -17,6 +17,7 @@ from ..integrations import Seq2SeqLMExportableModule from ..quantization import quantize_model_ from ..task_registry import register_task +from ..utils import get_hub_kwargs # NOTE: It’s important to map the registered task name to the pipeline name in https://github.com/huggingface/transformers/blob/main/utils/update_metadata.py. @@ -51,7 +52,9 @@ def load_seq2seq_speech_model(model_name_or_path: str, **kwargs) -> Seq2SeqLMExp max_seq_len = kwargs.get("max_seq_len", 1024) dtype = kwargs.get("dtype", "float32") - full_model = AutoModelForSpeechSeq2Seq.from_pretrained(model_name_or_path, dtype=dtype, device_map=device).eval() + full_model = AutoModelForSpeechSeq2Seq.from_pretrained( + model_name_or_path, dtype=dtype, device_map=device, **get_hub_kwargs(kwargs) + ).eval() for param in full_model.parameters(): if isinstance(param, torchao.utils.TorchAOBaseTensor): diff --git a/optimum/exporters/executorch/tasks/causal_lm.py b/optimum/exporters/executorch/tasks/causal_lm.py index bb04bbb7..dff151e3 100644 --- a/optimum/exporters/executorch/tasks/causal_lm.py +++ b/optimum/exporters/executorch/tasks/causal_lm.py @@ -20,7 +20,7 @@ from ..integrations import CausalLMExportableModule from ..quantization import quantize_model_ from ..task_registry import register_task -from ..utils import disable_dynamic_rope_for_export +from ..utils import disable_dynamic_rope_for_export, get_hub_kwargs # NOTE: It’s important to map the registered task name to the pipeline name in https://github.com/huggingface/transformers/blob/main/utils/update_metadata.py. @@ -63,12 +63,13 @@ def load_causal_lm_model(model_name_or_path: str, **kwargs) -> CausalLMExportabl max_seq_len = kwargs.get("max_seq_len", None) max_length = max_seq_len if max_seq_len is not None else kwargs.get("max_length", 2048) gguf_file = kwargs.get("gguf_file", None) + hub_kwargs = get_hub_kwargs(kwargs) if gguf_file: logging.warning( "GGUF weights are dequantized to float32 before export, which increases " "peak memory. Use --dtype float16 to halve memory usage." ) - config = kwargs.get("config") or AutoConfig.from_pretrained(model_name_or_path, gguf_file=gguf_file) + config = kwargs.get("config") or AutoConfig.from_pretrained(model_name_or_path, gguf_file=gguf_file, **hub_kwargs) # Downgrade longrope/dynamic RoPE to the static "default" RoPE, otherwise export fails on the # data-dependent control flow in the RoPE frequency update. See helper for version details. @@ -95,6 +96,7 @@ def _load_eager_pretrained( config=config, attn_implementation=attn_implementation, gguf_file=gguf_file, + **hub_kwargs, generation_config=GenerationConfig( use_cache=True, cache_implementation=cache_implementation, diff --git a/optimum/exporters/executorch/tasks/masked_lm.py b/optimum/exporters/executorch/tasks/masked_lm.py index 9a6ad250..bd33d7f8 100644 --- a/optimum/exporters/executorch/tasks/masked_lm.py +++ b/optimum/exporters/executorch/tasks/masked_lm.py @@ -17,6 +17,7 @@ from ..integrations import MaskedLMExportableModule from ..quantization import quantize_model_ from ..task_registry import register_task +from ..utils import get_hub_kwargs # NOTE: It’s important to map the registered task name to the pipeline name in https://github.com/huggingface/transformers/blob/main/utils/update_metadata.py. @@ -39,7 +40,7 @@ def load_masked_lm_model(model_name_or_path: str, **kwargs) -> MaskedLMExportabl An instance of `MaskedLMExportableModule` for exporting and lowering to ExecuTorch. """ - eager_model = AutoModelForMaskedLM.from_pretrained(model_name_or_path).to("cpu").eval() + eager_model = AutoModelForMaskedLM.from_pretrained(model_name_or_path, **get_hub_kwargs(kwargs)).to("cpu").eval() qlinear_config = kwargs.get("qlinear", None) qlinear_packing_format = kwargs.get("qlinear_packing_format", None) diff --git a/optimum/exporters/executorch/tasks/seq2seq_lm.py b/optimum/exporters/executorch/tasks/seq2seq_lm.py index b4cb026d..bf7230bc 100644 --- a/optimum/exporters/executorch/tasks/seq2seq_lm.py +++ b/optimum/exporters/executorch/tasks/seq2seq_lm.py @@ -16,6 +16,7 @@ from ..integrations import Seq2SeqLMExportableModule from ..task_registry import register_task +from ..utils import get_hub_kwargs # NOTE: It’s important to map the registered task name to the pipeline name in https://github.com/huggingface/transformers/blob/main/utils/update_metadata.py. @@ -49,7 +50,7 @@ def load_seq2seq_lm_model(model_name_or_path: str, **kwargs) -> Seq2SeqLMExporta max_hidden_seq_len = kwargs.get("max_hidden_seq_len", 4096) max_seq_len = kwargs.get("max_seq_len", 1024) - full_model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path).to(device).eval() + full_model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path, **get_hub_kwargs(kwargs)).to(device).eval() return Seq2SeqLMExportableModule( full_model, batch_size=batch_size, diff --git a/optimum/exporters/executorch/utils.py b/optimum/exporters/executorch/utils.py index 38a25b24..91104643 100644 --- a/optimum/exporters/executorch/utils.py +++ b/optimum/exporters/executorch/utils.py @@ -24,6 +24,22 @@ from transformers.tokenization_utils_tokenizers import TokenizersBackend +_HUB_KWARGS = ( + "cache_dir", + "force_download", + "local_files_only", + "revision", + "subfolder", + "token", + "trust_remote_code", +) + + +def get_hub_kwargs(kwargs: Dict[str, Any]) -> Dict[str, Any]: + """Returns the Hub loading options (revision, cache_dir, token, ...) found in `kwargs`, for `from_pretrained`.""" + return {key: kwargs[key] for key in _HUB_KWARGS if key in kwargs} + + def save_config_to_constant_methods( config: PretrainedConfig, generation_config: Optional[GenerationConfig] = None, diff --git a/tests/models/test_modeling_common.py b/tests/models/test_modeling_common.py index 63017df1..a1b64a3b 100644 --- a/tests/models/test_modeling_common.py +++ b/tests/models/test_modeling_common.py @@ -20,14 +20,22 @@ import unittest from pathlib import Path from tempfile import TemporaryDirectory +from unittest import mock import torch from executorch.extension.pybindings.portable_lib import ExecuTorchModule from huggingface_hub import HfApi +from parameterized import parameterized from transformers import ( AutoModelForCausalLM, AutoTokenizer, + BertConfig, + BertForMaskedLM, GenerationConfig, + LlamaConfig, + LlamaForCausalLM, + T5Config, + T5ForConditionalGeneration, ) from optimum.executorch import ExecuTorchModelForCausalLM @@ -183,3 +191,42 @@ def forward(self, x): if node.op == "call_function" and node.target == exir_ops.edge.aten.embedding.default ) ) + + +class MainExportHubKwargsTest(unittest.TestCase): + @parameterized.expand( + [ + ("fill-mask", BertForMaskedLM, BertConfig, {"hidden_size": 32, "num_attention_heads": 2}), + ("text-generation", LlamaForCausalLM, LlamaConfig, {"hidden_size": 32, "num_attention_heads": 2}), + ( + "text2text-generation", + T5ForConditionalGeneration, + T5Config, + {"d_model": 32, "d_kv": 16, "num_heads": 2}, + ), + ] + ) + def test_main_export_loads_requested_revision(self, task, model_cls, config_cls, config_kwargs): + # A Hub cache (huggingface_hub layout: refs/ holds a commit hash, snapshots/ holds the files) + # with two revisions of the same repo that differ in depth: main has 1 layer, v2 has 2. + with TemporaryDirectory() as cache_dir: + repo_dir = Path(cache_dir) / "models--demo--tiny" + for ref, sha, num_layers in [("main", "a" * 40, 1), ("v2", "b" * 40, 2)]: + config = config_cls(vocab_size=99, num_hidden_layers=num_layers, **config_kwargs) + model_cls(config).save_pretrained(repo_dir / "snapshots" / sha) + (repo_dir / "refs").mkdir(parents=True, exist_ok=True) + (repo_dir / "refs" / ref).write_text(sha) + + with mock.patch("optimum.exporters.executorch.__main__.export_to_executorch") as export_mock: + main_export( + "demo/tiny", + task=task, + recipe="xnnpack", + output_dir=Path(cache_dir) / "out", + cache_dir=cache_dir, + revision="v2", + local_files_only=True, + ) + + exported = export_mock.call_args.kwargs["model"] + self.assertEqual(exported.config.num_hidden_layers, 2)