Skip to content

Commit dd5d08f

Browse files
authored
Look into models from huggingface (#22)
* hub * bb * black * data * mypy * doc * add tiny * fix dummy inputs * dd * doc * fix llm * fix issues
1 parent 81890a1 commit dd5d08f

File tree

21 files changed

+785
-11
lines changed

21 files changed

+785
-11
lines changed

.github/workflows/spell-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ jobs:
2121

2222
# Run codespell
2323
- name: Run codespell
24-
run: codespell --skip="*.png,*.jpg,*.jpeg,*.gif,*.svg,*.ico,*.pdf,*.js" --ignore-words-list="nd,te,OT" --check-filenames
24+
run: codespell --skip="*.png,*.jpg,*.jpeg,*.gif,*.svg,*.ico,*.pdf,*.js,*.css,*.map" --ignore-words-list="nd,te,OT" --check-filenames
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
onnx_diagnostic.torch_models.hghub.hub_api
3+
==========================================
4+
5+
.. automodule:: onnx_diagnostic.torch_models.hghub.hub_api
6+
:members:
7+
:no-undoc-members:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
onnx_diagnostic.torch_models.hghub.hub_data
3+
===========================================
4+
5+
.. automodule:: onnx_diagnostic.torch_models.hghub.hub_data
6+
:members:
7+
:no-undoc-members:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
onnx_diagnostic.torch_models.hghub
2+
==================================
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
:caption: submodules
7+
8+
hub_api
9+
hub_data
10+
model_inputs
11+
12+
.. autofunction:: onnx_diagnostic.torch_models.hghub.get_untrained_model_with_inputs
13+
14+
.. automodule:: onnx_diagnostic.torch_models.hghub
15+
:members:
16+
:no-undoc-members:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
onnx_diagnostic.torch_models.hghub.model_inputs
3+
===============================================
4+
5+
.. automodule:: onnx_diagnostic.torch_models.hghub.model_inputs
6+
:members:
7+
:no-undoc-members:
8+
:exclude-members: get_untrained_model_with_inputs

_doc/api/torch_models/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ onnx_diagnostic.torch_models
55
:maxdepth: 1
66
:caption: submodules
77

8+
hghub/index
89
llms
910

1011
.. automodule:: onnx_diagnostic.torch_models

_doc/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
("py:class", "transformers.cache_utils.MambaCache"),
125125
("py:func", "torch.export._draft_export.draft_export"),
126126
("py:func", "torch._export.tools.report_exportability"),
127+
("py:meth", "huggingface_hub.HfApi.list_models"),
127128
("py:meth", "transformers.GenerationMixin.generate"),
128129
]
129130

@@ -182,6 +183,7 @@
182183
"graph break": "https://pytorch.org/docs/stable/torch.compiler_faq.html#graph-breaks",
183184
"GraphModule": "https://pytorch.org/docs/stable/fx.html#torch.fx.GraphModule",
184185
"HuggingFace": "https://huggingface.co/docs/hub/en/index",
186+
"huggingface_hub": "https://github.com/huggingface/huggingface_hub",
185187
"Linux": "https://www.linux.org/",
186188
"ml_dtypes": "https://github.com/jax-ml/ml_dtypes",
187189
"monai": "https://monai.io/",
@@ -224,3 +226,13 @@
224226
"vocos": "https://github.com/gemelo-ai/vocos",
225227
"Windows": "https://www.microsoft.com/windows",
226228
}
229+
230+
# models
231+
epkg_dictionary.update(
232+
{
233+
"arnir0/Tiny-LLM": "https://huggingface.co/arnir0/Tiny-LLM",
234+
"microsoft/phi-2": "https://huggingface.co/microsoft/phi-2",
235+
"microsoft/Phi-3.5-mini-instruct": "https://huggingface.co/microsoft/Phi-3.5-mini-instruct",
236+
"microsoft/Phi-3.5-vision-instruct": "https://huggingface.co/microsoft/Phi-3.5-vision-instruct",
237+
}
238+
)

_doc/examples/plot_export_tiny_llm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
That example shows to guess the inputs of this method even though the model
1414
is executed through meth ``generate``.
1515
16-
We focus on the model
17-
`Tiny-LLM <https://huggingface.co/arnir0/Tiny-LLM>`_.
16+
We focus on the model :epkg:`arnir0/Tiny-LLM`.
1817
To avoid downloading any weights, we write a function creating a
1918
random model based on the same architecture.
2019

_doc/examples/plot_export_tiny_llm_patched.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
77
Many models from :epkg:`transformers` cannot be converted because
88
the implementation uses cache classes. Let's see how to get around that.
9-
We focus on the model
10-
`Tiny-LLM <https://huggingface.co/arnir0/Tiny-LLM>`_.
9+
We focus on the model :epkg:`arnir0/Tiny-LLM`.
1110
To avoid downloading any weights, we write a function creating a
1211
random model based on the same architecture.
1312
This continues example :ref:`l-plot-tiny-llm-export`.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import unittest
2+
import pandas
3+
from onnx_diagnostic.ext_test_case import (
4+
ExtTestCase,
5+
hide_stdout,
6+
never_test,
7+
requires_torch,
8+
requires_transformers,
9+
)
10+
from onnx_diagnostic.torch_models.hghub.hub_api import (
11+
enumerate_model_list,
12+
get_model_info,
13+
get_pretrained_config,
14+
task_from_id,
15+
task_from_arch,
16+
task_from_tags,
17+
)
18+
from onnx_diagnostic.torch_models.hghub.hub_data import load_architecture_task
19+
20+
21+
class TestHuggingFaceHubApi(ExtTestCase):
22+
23+
@requires_transformers("4.50") # we limit to some versions of the CI
24+
@requires_torch("2.7")
25+
def test_enumerate_model_list(self):
26+
models = list(
27+
enumerate_model_list(
28+
2,
29+
verbose=1,
30+
dump="test_enumerate_model_list.csv",
31+
filter="text-generation",
32+
library="transformers",
33+
)
34+
)
35+
self.assertEqual(len(models), 2)
36+
df = pandas.read_csv("test_enumerate_model_list.csv")
37+
self.assertEqual(df.shape, (2, 12))
38+
tasks = [task_from_id(c) for c in df.id]
39+
self.assertEqual(["text-generation", "text-generation"], tasks)
40+
41+
@requires_transformers("4.50")
42+
@requires_torch("2.7")
43+
def test_task_from_id(self):
44+
for name, etask in [
45+
("arnir0/Tiny-LLM", "text-generation"),
46+
("microsoft/phi-2", "text-generation"),
47+
]:
48+
with self.subTest(name=name, task=etask):
49+
task = task_from_id(name, True)
50+
self.assertEqual(etask, task)
51+
52+
@requires_transformers("4.50")
53+
@requires_torch("2.7")
54+
@never_test()
55+
def test_task_from_id_long(self):
56+
for name, etask in [
57+
("microsoft/Phi-3.5-mini-instruct", "text-generation"),
58+
("microsoft/Phi-3.5-vision-instruct", "text-generation"),
59+
]:
60+
with self.subTest(name=name, task=etask):
61+
task = task_from_id(name, True)
62+
self.assertEqual(etask, task)
63+
64+
@requires_transformers("4.50")
65+
@requires_torch("2.7")
66+
@hide_stdout()
67+
def test_get_pretrained_config(self):
68+
conf = get_pretrained_config("microsoft/phi-2")
69+
self.assertNotEmpty(conf)
70+
print(conf)
71+
72+
@requires_transformers("4.50")
73+
@requires_torch("2.7")
74+
@hide_stdout()
75+
def test_get_model_info(self):
76+
info = get_model_info("microsoft/phi-2")
77+
self.assertEqual(info.pipeline_tag, "text-generation")
78+
79+
info = get_model_info("microsoft/Phi-3.5-vision-instruct")
80+
self.assertEqual(info.pipeline_tag, "image-text-to-text")
81+
82+
info = get_model_info("microsoft/Phi-4-multimodal-instruct")
83+
self.assertEqual(info.pipeline_tag, "automatic-speech-recognition")
84+
85+
def test_task_from_arch(self):
86+
task = task_from_arch("LlamaForCausalLM")
87+
self.assertEqual("text-generation", task)
88+
89+
@never_test()
90+
def test_hf_all_models(self):
91+
list(enumerate_model_list(-1, verbose=1, dump="test_hf_all_models.csv"))
92+
93+
def test_load_architecture_task(self):
94+
data = load_architecture_task()
95+
print(set(data.values()))
96+
97+
def test_task_from_tags(self):
98+
_tags = [
99+
("text-generation|nlp|code|en|text-generation-inference", "text-generation"),
100+
(
101+
"text-generation|nlp|code|vision|image-text-to-text|conversational",
102+
"image-text-to-text",
103+
),
104+
(
105+
"text-generation|nlp|code|audio|automatic-speech-recognition|speech-summarization|speech-translation|visual-question-answering",
106+
"automatic-speech-recognition",
107+
),
108+
]
109+
for tags, etask in _tags:
110+
with self.subTest(tags=tags, task=etask):
111+
task = task_from_tags(tags)
112+
self.assertEqual(etask, task)
113+
114+
115+
if __name__ == "__main__":
116+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)