Skip to content

Commit e92c72b

Browse files
authored
Add command line to print out the configuration for a model id (#28)
* command line * fix issues * fix image
1 parent 64999f9 commit e92c72b

File tree

13 files changed

+706
-3
lines changed

13 files changed

+706
-3
lines changed

CHANGELOGS.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Change Logs
44
0.3.0
55
+++++
66

7+
* :pr:`28`: adds command line to print out the configuration for a model id,
8+
support image-text-to-text
79
* :pr:`26`: creates a folder ``helpers`` to gather all the functions
810
used in many places
911
* :pr:`25`: improve patches for DynamicCache
@@ -12,7 +14,7 @@ Change Logs
1214
``convert_dynamic_axes_into_dynamic_shapes`` to convert dynamic axes
1315
into dynamic shapes, add support for ``T5ForConditionalGeneration``
1416
* :pr:`23`: dummy inputs for ``image-classification``
15-
* :pr:`22`: api to create untrained model copying the architecture
17+
* :pr:`22`, :pr:`27`: api to create untrained model copying the architecture
1618
of the trained models and dummy inputs for them,
1719
support for ``text-generation``
1820

_doc/cmds/config.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
-m onnx_diagnostic config ... prints the config for a model id
2+
==============================================================
3+
4+
Description
5+
+++++++++++
6+
7+
The command lines prints out the configuration file a model id
8+
available on :epkg:`HuggingFace`.
9+
10+
.. runpython::
11+
12+
from onnx_diagnostic._command_lines_parser import get_parser_config
13+
14+
get_parser_config().print_help()
15+
16+
Example
17+
+++++++
18+
19+
.. code-block:: bash
20+
21+
python -m onnx_diagnostic config HuggingFaceM4/tiny-random-idefics
22+
23+
.. code-block:: text
24+
25+
IdeficsConfig {
26+
"additional_vocab_size": 2,
27+
"alpha_initializer": "ones",
28+
"alpha_type": "vector",
29+
"alphas_initializer_range": 0.0,
30+
"architectures": [
31+
"IdeficsForVisionText2Text"
32+
],
33+
"bos_token_id": 1,
34+
"cross_layer_activation_function": "swiglu",
35+
"cross_layer_interval": 1,
36+
"dropout": 0.0,
37+
"eos_token_id": 2,
38+
"ffn_dim": 64,
39+
"freeze_lm_head": false,
40+
"freeze_text_layers": false,
41+
"freeze_text_module_exceptions": [],
42+
"freeze_vision_layers": false,
43+
"freeze_vision_module_exceptions": [],
44+
"hidden_act": "silu",
45+
"hidden_size": 16,
46+
"initializer_range": 0.02,
47+
"intermediate_size": 11008,
48+
"max_new_tokens": 128,
49+
"max_position_embeddings": 128,
50+
"model_type": "idefics",
51+
"num_attention_heads": 4,
52+
"num_hidden_layers": 2,
53+
"pad_token_id": 0,
54+
"perceiver_config": {
55+
"model_type": "idefics_perciever",
56+
"qk_layer_norms_perceiver": false,
57+
"resampler_depth": 2,
58+
"resampler_head_dim": 8,
59+
"resampler_n_heads": 2,
60+
"resampler_n_latents": 16,
61+
"use_resampler": false
62+
},
63+
"qk_layer_norms": false,
64+
"rms_norm_eps": 1e-06,
65+
"tie_word_embeddings": false,
66+
"torch_dtype": "float16",
67+
"transformers_version": "4.51.0.dev0",
68+
"use_cache": true,
69+
"use_resampler": true,
70+
"vision_config": {
71+
"attention_dropout": 0.0,
72+
"embed_dim": 32,
73+
"hidden_act": "gelu",
74+
"image_size": 30,
75+
"initializer_factor": 1.0,
76+
"initializer_range": 0.02,
77+
"intermediate_size": 37,
78+
"layer_norm_eps": 1e-05,
79+
"model_type": "idefics_vision",
80+
"num_attention_heads": 4,
81+
"num_channels": 3,
82+
"num_hidden_layers": 5,
83+
"patch_size": 2,
84+
"vision_model_name": "hf-internal-testing/tiny-random-clip"
85+
},
86+
"vocab_size": 32000,
87+
"word_embed_proj_dim": 16
88+
}

_doc/cmds/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Command Lines
2+
=============
3+
4+
.. code-block:: bash
5+
6+
python -m onnx_diagnostic
7+
8+
.. toctree::
9+
:maxdepth: 1
10+
11+
config

_doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Source are `sdpython/onnx-diagnostic
2929
:caption: Contents
3030

3131
api/index
32+
cmds/index
3233
auto_examples/index
3334

3435
.. toctree::

_unittests/ut_torch_models/test_hghub_model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ def test_get_untrained_model_with_inputs_text2text_generation(self):
100100
raise unittest.SkipTest(f"not working for {mid!r}")
101101
model(**inputs)
102102

103+
@hide_stdout()
104+
def test_get_untrained_model_with_inputs_imagetext2text_generation(self):
105+
mid = "HuggingFaceM4/tiny-random-idefics"
106+
# mid = "Salesforce/codet5-small"
107+
data = get_untrained_model_with_inputs(mid, verbose=1)
108+
self.assertIn((data["size"], data["n_weights"]), [(12742888, 3185722)])
109+
model, inputs = data["model"], data["inputs"]
110+
model(**inputs)
111+
103112
@hide_stdout()
104113
@requires_torch("2.7", "reduce test time")
105114
@requires_transformers("4.50", "reduce test time")

_unittests/ut_torch_models/try_tasks.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TestHuggingFaceHubModel(ExtTestCase):
88
@never_test()
9-
def test_image_classiciation(self):
9+
def test_image_classification(self):
1010
# clear&&NEVERTEST=1 python _unittests/ut_torch_models/try_tasks.py -k image_c
1111

1212
from transformers import ViTImageProcessor, ViTModel
@@ -51,6 +51,37 @@ def test_text2text_generation(self):
5151
)
5252
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
5353

54+
@never_test()
55+
def test_imagetext2text_generation(self):
56+
# clear&&NEVERTEST=1 python _unittests/ut_torch_models/try_tasks.py -k etext2t
57+
# https://huggingface.co/docs/transformers/main/en/tasks/idefics
58+
59+
import torch
60+
from transformers import IdeficsForVisionText2Text, AutoProcessor
61+
62+
mid = "HuggingFaceM4/tiny-random-idefics"
63+
processor = AutoProcessor.from_pretrained(mid)
64+
model = IdeficsForVisionText2Text.from_pretrained(
65+
mid, torch_dtype=torch.bfloat16, device_map="auto"
66+
)
67+
68+
prompt = [
69+
"https://images.unsplash.com/photo-1583160247711-2191776b4b91?ixlib=rb-4.0.3"
70+
"&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3542&q=80",
71+
]
72+
inputs = processor(text=prompt, return_tensors="pt").to("cuda")
73+
bad_words_ids = processor.tokenizer(
74+
["<image>", "<fake_token_around_image>"], add_special_tokens=False
75+
).input_ids
76+
print()
77+
with steel_forward(model):
78+
generated_ids = model.generate(
79+
**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids
80+
)
81+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
82+
83+
print(generated_text[0])
84+
5485

5586
if __name__ == "__main__":
5687
unittest.main(verbosity=2)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import unittest
2+
from contextlib import redirect_stdout
3+
from io import StringIO
4+
from onnx_diagnostic.ext_test_case import ExtTestCase
5+
from onnx_diagnostic._command_lines_parser import (
6+
get_main_parser,
7+
get_parser_find,
8+
get_parser_lighten,
9+
get_parser_print,
10+
get_parser_unlighten,
11+
get_parser_config,
12+
)
13+
14+
15+
class TestCommandLines(ExtTestCase):
16+
def test_main_parser(self):
17+
st = StringIO()
18+
with redirect_stdout(st):
19+
get_main_parser().print_help()
20+
text = st.getvalue()
21+
self.assertIn("lighten", text)
22+
23+
def test_parser_lighten(self):
24+
st = StringIO()
25+
with redirect_stdout(st):
26+
get_parser_lighten().print_help()
27+
text = st.getvalue()
28+
self.assertIn("model", text)
29+
30+
def test_parser_unlighten(self):
31+
st = StringIO()
32+
with redirect_stdout(st):
33+
get_parser_unlighten().print_help()
34+
text = st.getvalue()
35+
self.assertIn("model", text)
36+
37+
def test_parser_print(self):
38+
st = StringIO()
39+
with redirect_stdout(st):
40+
get_parser_print().print_help()
41+
text = st.getvalue()
42+
self.assertIn("pretty", text)
43+
44+
def test_parser_find(self):
45+
st = StringIO()
46+
with redirect_stdout(st):
47+
get_parser_find().print_help()
48+
text = st.getvalue()
49+
self.assertIn("names", text)
50+
51+
def test_parser_config(self):
52+
st = StringIO()
53+
with redirect_stdout(st):
54+
get_parser_config().print_help()
55+
text = st.getvalue()
56+
self.assertIn("mid", text)
57+
58+
59+
if __name__ == "__main__":
60+
unittest.main(verbosity=2)

onnx_diagnostic/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ._command_lines_parser import main
2+
3+
if __name__ == "__main__":
4+
main()

0 commit comments

Comments
 (0)