Skip to content

Commit d600659

Browse files
committed
remove from_pretrained method
1 parent 31dc269 commit d600659

File tree

7 files changed

+17
-119
lines changed

7 files changed

+17
-119
lines changed

optimum/executorch/modeling.py

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -114,85 +114,7 @@ def forward(
114114
torch.Tensor: Logits output from the model.
115115
"""
116116
return self.et_model.forward((input_ids, cache_position))[0]
117-
118-
@classmethod
119-
def from_pretrained(
120-
cls,
121-
model_name_or_path: Union[str, Path],
122-
export: bool = True,
123-
recipe: str = "",
124-
config: "PretrainedConfig" = None,
125-
subfolder: str = "",
126-
revision: Optional[str] = None,
127-
cache_dir: str = HUGGINGFACE_HUB_CACHE,
128-
force_download: bool = False,
129-
local_files_only: bool = False,
130-
use_auth_token: Optional[Union[bool, str]] = None,
131-
token: Optional[Union[bool, str]] = None,
132-
**kwargs,
133-
) -> "ExecuTorchModelForCausalLM":
134-
"""
135-
Load a pre-trained ExecuTorch model.
136-
137-
Args:
138-
model_name_or_path (`Union[str, Path]`):
139-
Model ID on huggingface.co or path on disk to the model repository to export. Example: `model_name_or_path="meta-llama/Llama-3.2-1B"` or `mode_name_or_path="/path/to/model_folder`.
140-
export (`bool`, *optional*, defaults to `True`):
141-
If `True`, the model will be exported from eager to ExecuTorch after fetched from huggingface.co. `model_name_or_path` must be a valid model ID on huggingface.co.
142-
If `False`, the previously exported ExecuTorch model will be loaded from a local path. `model_name_or_path` must be a valid local directory where a `model.pte` is stored.
143-
recipe (`str`, defaults to `""`):
144-
The recipe to use to do the export, e.g. "xnnpack". It is required to specify a task when `export` is `True`.
145-
config (`PretrainedConfig`, *optional*):
146-
Configuration of the pre-trained model.
147-
subfolder (`str`, defaults to `""`):
148-
In case the relevant files are located inside a subfolder of the model repo either locally or on huggingface.co, you can
149-
specify the folder name here.
150-
revision (`str`, defaults to `"main"`):
151-
Revision is the specific model version to use. It can be a branch name, a tag name, or a commit id.
152-
cache_dir (`Optional[str]`, defaults to `None`):
153-
Path indicating where to store cache. The default Hugging Face cache path will be used by default.
154-
force_download (`bool`, defaults to `False`):
155-
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
156-
cached versions if they exist.
157-
local_files_only (`Optional[bool]`, defaults to `False`):
158-
Whether or not to only look at local files (i.e., do not try to download the model).
159-
use_auth_token (`Optional[Union[bool,str]]`, defaults to `None`):
160-
Deprecated. Please use the `token` argument instead.
161-
token (`Optional[Union[bool,str]]`, defaults to `None`):
162-
The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
163-
when running `huggingface-cli login` (stored in `huggingface_hub.constants.HF_TOKEN_PATH`).
164-
**kwargs:
165-
Additional configuration options to tasks and recipes.
166-
167-
Returns:
168-
`ExecuTorchModelForCausalLM`: An instance of the ExecuTorch model for text generation task.
169-
"""
170-
if use_auth_token is not None:
171-
warnings.warn(
172-
"The `use_auth_token` argument is deprecated and will be removed soon. Please use the `token` argument instead.",
173-
FutureWarning,
174-
)
175-
if token is not None:
176-
raise ValueError("You cannot use both `use_auth_token` and `token` arguments at the same time.")
177-
token = use_auth_token
178-
179-
if export:
180-
# Fetch the model from huggingface.co and export it to ExecuTorch
181-
if recipe == "":
182-
raise ValueError("Please specify a recipe to export the model for.")
183-
return cls._export(
184-
model_id=model_name_or_path,
185-
recipe=recipe,
186-
config=config,
187-
**kwargs,
188-
)
189-
else:
190-
# Load the ExecuTorch model from a local path
191-
return cls._from_pretrained(
192-
model_dir_path=model_name_or_path,
193-
config=config,
194-
)
195-
117+
196118
@classmethod
197119
def _from_pretrained(
198120
cls,
@@ -269,7 +191,7 @@ def _export(
269191
270192
Args:
271193
model_id (`str`):
272-
Model ID on huggingface.co, for example: `model_name_or_path="meta-llama/Llama-3.2-1B"`.
194+
Model ID on huggingface.co, for example: `model_id="meta-llama/Llama-3.2-1B"`.
273195
recipe (`str`):
274196
The recipe to use to do the export, e.g. "xnnpack".
275197
config (`PretrainedConfig`, *optional*):
@@ -440,3 +362,9 @@ def text_generation(
440362
max_seq_len=max_seq_len,
441363
)
442364
return self.tokenizer.decode(generated_tokens, skip_special_tokens=True)
365+
366+
@classmethod
367+
def _from_transformers(cls, *args, **kwargs):
368+
# TODO : add warning when from_pretrained_method is set to cls._export instead of cls._from_transformers when export=True
369+
# logger.warning("The method `_from_transformers` is deprecated, please use `_export` instead")
370+
return cls._export(*args, **kwargs)

tests/models/test_modeling.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ def __init__(self, *args, **kwargs):
3131
@slow
3232
@pytest.mark.run_slow
3333
def test_load_model_from_hub(self):
34-
model = ExecuTorchModelForCausalLM.from_pretrained(
35-
model_name_or_path="NousResearch/Llama-3.2-1B",
36-
export=True,
37-
recipe="xnnpack",
38-
)
34+
model = ExecuTorchModelForCausalLM.from_pretrained("NousResearch/Llama-3.2-1B", export=True, recipe="xnnpack")
3935
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4036
self.assertIsInstance(model.model, ExecuTorchModule)
4137

@@ -59,9 +55,6 @@ def test_load_model_from_local_path(self):
5955
self.assertTrue(os.path.exists(f"{tempdir}/model.pte"))
6056

6157
# Load the exported model from a local dir
62-
model = ExecuTorchModelForCausalLM.from_pretrained(
63-
model_name_or_path=tempdir,
64-
export=False,
65-
)
58+
model = ExecuTorchModelForCausalLM.from_pretrained(tempdir, export=False)
6659
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
6760
self.assertIsInstance(model.model, ExecuTorchModule)

tests/models/test_modeling_gemma.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ def test_gemma_text_generation_with_xnnpack(self):
3333
# TODO: Switch to use google/gemma-2b once https://github.com/huggingface/optimum/issues/2127 is fixed
3434
# model_id = "google/gemma-2b"
3535
model_id = "weqweasdas/RM-Gemma-2B"
36-
model = ExecuTorchModelForCausalLM.from_pretrained(
37-
model_name_or_path=model_id,
38-
export=True,
39-
recipe="xnnpack",
40-
)
36+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
4137
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4238
self.assertIsInstance(model.model, ExecuTorchModule)
4339

tests/models/test_modeling_gemma2.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ def test_gemma2_text_generation_with_xnnpack(self):
3333
# TODO: Switch to use google/gemma-2-2b once https://github.com/huggingface/optimum/issues/2127 is fixed
3434
# model_id = "google/gemma-2-2b"
3535
model_id = "unsloth/gemma-2-2b-it"
36-
model = ExecuTorchModelForCausalLM.from_pretrained(
37-
model_name_or_path=model_id,
38-
export=True,
39-
recipe="xnnpack",
40-
)
36+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
4137
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4238
self.assertIsInstance(model.model, ExecuTorchModule)
4339

tests/models/test_modeling_llama.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ def test_llama3_2_1b_text_generation_with_xnnpack(self):
3333
# TODO: Switch to use meta-llama/Llama-3.2-1B once https://github.com/huggingface/optimum/issues/2127 is fixed
3434
# model_id = "lama/Llama-3.2-1B"
3535
model_id = "NousResearch/Llama-3.2-1B"
36-
model = ExecuTorchModelForCausalLM.from_pretrained(
37-
model_name_or_path=model_id,
38-
export=True,
39-
recipe="xnnpack",
40-
)
36+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
4137
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4238
self.assertIsInstance(model.model, ExecuTorchModule)
4339

@@ -57,11 +53,8 @@ def test_llama3_2_3b_text_generation_with_xnnpack(self):
5753
# TODO: Switch to use meta-llama/Llama-3.2-3B once https://github.com/huggingface/optimum/issues/2127 is fixed
5854
# model_id = "lama/Llama-3.2-3B"
5955
model_id = "NousResearch/Hermes-3-Llama-3.2-3B"
60-
model = ExecuTorchModelForCausalLM.from_pretrained(
61-
model_name_or_path=model_id,
62-
export=True,
63-
recipe="xnnpack",
64-
)
56+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
57+
6558
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
6659
self.assertIsInstance(model.model, ExecuTorchModule)
6760

tests/models/test_modeling_olmo.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ def __init__(self, *args, **kwargs):
3131
@pytest.mark.run_slow
3232
def test_olmo_text_generation_with_xnnpack(self):
3333
model_id = "allenai/OLMo-1B-hf"
34-
model = ExecuTorchModelForCausalLM.from_pretrained(
35-
model_name_or_path=model_id,
36-
export=True,
37-
recipe="xnnpack",
38-
)
34+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
3935
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4036
self.assertIsInstance(model.model, ExecuTorchModule)
4137

tests/models/test_modeling_qwen2.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ def __init__(self, *args, **kwargs):
3131
@pytest.mark.run_slow
3232
def test_qwen2_5_text_generation_with_xnnpack(self):
3333
model_id = "Qwen/Qwen2.5-0.5B"
34-
model = ExecuTorchModelForCausalLM.from_pretrained(
35-
model_name_or_path=model_id,
36-
export=True,
37-
recipe="xnnpack",
38-
)
34+
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, export=True, recipe="xnnpack")
3935
self.assertIsInstance(model, ExecuTorchModelForCausalLM)
4036
self.assertIsInstance(model.model, ExecuTorchModule)
4137

0 commit comments

Comments
 (0)