-
Notifications
You must be signed in to change notification settings - Fork 11
Enable loading model from hub that has already been converted #13
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 17 commits
def6fd5
31dc269
d600659
e1d0eb4
567766b
8cb152a
216d71a
aa46b61
f33d6f5
6f4685b
0d62737
477c08d
9459572
30442f4
f351a2a
61d421f
aec1827
6c54460
5240b6a
adfe30c
5735f75
cc3796b
cd107b2
7f360f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,52 +16,68 @@ | |
import os | ||
import tempfile | ||
import unittest | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
|
||
import pytest | ||
from executorch.extension.pybindings.portable_lib import ExecuTorchModule | ||
from transformers.testing_utils import slow | ||
from huggingface_hub import HfApi | ||
|
||
from optimum.executorch import ExecuTorchModelForCausalLM | ||
from optimum.executorch.modeling import _FILE_PATTERN | ||
from optimum.exporters.executorch import main_export | ||
from optimum.utils.file_utils import find_files_matching_pattern | ||
|
||
|
||
class ExecuTorchModelIntegrationTest(unittest.TestCase): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
@slow | ||
@pytest.mark.run_slow | ||
def test_load_model_from_hub(self): | ||
model = ExecuTorchModelForCausalLM.from_pretrained( | ||
model_name_or_path="NousResearch/Llama-3.2-1B", | ||
export=True, | ||
recipe="xnnpack", | ||
) | ||
model_id = "optimum-internal-testing/tiny-random-llama" | ||
|
||
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, recipe="xnnpack") | ||
self.assertIsInstance(model, ExecuTorchModelForCausalLM) | ||
self.assertIsInstance(model.model, ExecuTorchModule) | ||
|
||
@slow | ||
@pytest.mark.run_slow | ||
def test_load_model_from_local_path(self): | ||
from optimum.exporters.executorch import main_export | ||
def test_load_et_model_from_hub(self): | ||
model_id = "optimum-internal-testing/tiny-random-llama" | ||
|
||
model = ExecuTorchModelForCausalLM.from_pretrained(model_id, revision="executorch", recipe="xnnpack") | ||
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.
Oh I see what it is. https://huggingface.co/optimum-internal-testing/tiny-random-llama/tree/executorch. Here are the follow up question: When 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. if no pte files detected then |
||
self.assertIsInstance(model, ExecuTorchModelForCausalLM) | ||
self.assertIsInstance(model.model, ExecuTorchModule) | ||
|
||
model_id = "NousResearch/Llama-3.2-1B" | ||
task = "text-generation" | ||
def test_load_model_from_local_path(self): | ||
echarlaix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model_id = "optimum-internal-testing/tiny-random-llama" | ||
recipe = "xnnpack" | ||
|
||
with tempfile.TemporaryDirectory() as tempdir: | ||
# Export to a local dir | ||
main_export( | ||
model_name_or_path=model_id, | ||
task=task, | ||
recipe=recipe, | ||
output_dir=tempdir, | ||
task="text-generation", | ||
) | ||
self.assertTrue(os.path.exists(f"{tempdir}/model.pte")) | ||
|
||
# Load the exported model from a local dir | ||
model = ExecuTorchModelForCausalLM.from_pretrained( | ||
model_name_or_path=tempdir, | ||
export=False, | ||
) | ||
model = ExecuTorchModelForCausalLM.from_pretrained(tempdir) | ||
self.assertIsInstance(model, ExecuTorchModelForCausalLM) | ||
self.assertIsInstance(model.model, ExecuTorchModule) | ||
|
||
def test_find_files_matching_pattern(self): | ||
model_id = "optimum-internal-testing/tiny-random-llama" | ||
|
||
# hub model | ||
for revision in ("main", "executorch"): | ||
pte_files = find_files_matching_pattern(model_id, pattern=_FILE_PATTERN, revision=revision) | ||
self.assertTrue(len(pte_files) == 0 if revision == "main" else len(pte_files) > 0) | ||
|
||
# local model | ||
api = HfApi() | ||
with TemporaryDirectory() as tmpdirname: | ||
for revision in ("main", "executorch"): | ||
local_dir = Path(tmpdirname) / revision | ||
api.snapshot_download(repo_id=model_id, local_dir=local_dir, revision=revision) | ||
pte_files = find_files_matching_pattern(local_dir, pattern=_FILE_PATTERN, revision=revision) | ||
self.assertTrue(len(pte_files) == 0 if revision == "main" else len(pte_files) > 0) |
Uh oh!
There was an error while loading. Please reload this page.