|
1 |
| -import pytest |
2 |
| - |
3 | 1 | import outlines
|
4 | 2 |
|
5 | 3 | outlines.disable_cache()
|
6 | 4 |
|
7 | 5 | from outlines.fsm.guide import RegexGuide # noqa: E402
|
8 | 6 | from outlines.fsm.json_schema import build_regex_from_schema # noqa: E402
|
9 | 7 |
|
| 8 | +from .common import ( # noqa: E402 |
| 9 | + clear_outlines_cache, |
| 10 | + ensure_numba_compiled, |
| 11 | + setup_tokenizer, |
| 12 | +) |
| 13 | + |
10 | 14 | simple_schema = """{
|
11 | 15 | "$defs": {
|
12 | 16 | "Armor": {
|
|
63 | 67 | "required": ["id", "work", "recording_artists"]
|
64 | 68 | }"""
|
65 | 69 |
|
66 |
| - |
67 | 70 | schemas = dict(simple_schema=simple_schema, complex_schema=complex_schema)
|
68 | 71 |
|
69 | 72 |
|
70 |
| -@pytest.mark.parametrize("schema_name", schemas.keys()) |
71 |
| -def test_benchmark_json_schema_to_regex(benchmark, ensure_numba_compiled, schema_name): |
72 |
| - """Benchmark convert json schema to regex""" |
73 |
| - schema = schemas[schema_name] |
74 |
| - benchmark.pedantic( |
75 |
| - build_regex_from_schema, |
76 |
| - args=(schema,), |
77 |
| - rounds=8, |
78 |
| - ) |
| 73 | +class JsonSchemaBenchmark: |
| 74 | + params = schemas.keys() |
| 75 | + |
| 76 | + def setup(self, schema_name): |
| 77 | + clear_outlines_cache() |
| 78 | + self.tokenizer = setup_tokenizer() |
| 79 | + self.schema = schemas[schema_name] |
| 80 | + ensure_numba_compiled(self.tokenizer) |
79 | 81 |
|
| 82 | + def time_json_schema_to_regex(self, schema_name): |
| 83 | + build_regex_from_schema(self.schema) |
80 | 84 |
|
81 |
| -@pytest.mark.parametrize("schema_name", schemas.keys()) |
82 |
| -def test_benchmark_json_schema_to_fsm( |
83 |
| - benchmark, tokenizer, ensure_numba_compiled, schema_name |
84 |
| -): |
85 |
| - """Benchmark compile json schema as FSM""" |
86 |
| - schema = schemas[schema_name] |
87 |
| - regex = build_regex_from_schema(schema) |
88 |
| - benchmark.pedantic( |
89 |
| - RegexGuide, |
90 |
| - args=(regex, tokenizer), |
91 |
| - rounds=8, |
92 |
| - ) |
| 85 | + def time_json_schema_to_fsm(self, schema_name): |
| 86 | + regex = build_regex_from_schema(self.schema) |
| 87 | + RegexGuide(regex, self.tokenizer) |
0 commit comments