|
| 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