Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions optimum/executorch/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
6 changes: 4 additions & 2 deletions optimum/exporters/executorch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = "",
Expand Down Expand Up @@ -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).
Expand Down
5 changes: 4 additions & 1 deletion optimum/exporters/executorch/tasks/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions optimum/exporters/executorch/tasks/causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion optimum/exporters/executorch/tasks/masked_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion optimum/exporters/executorch/tasks/seq2seq_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions optimum/exporters/executorch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions tests/models/test_modeling_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<ref> holds a commit hash, snapshots/<hash> 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)