|
| 1 | +import pytest |
| 2 | + |
| 3 | +import outlines |
| 4 | + |
| 5 | +outlines.disable_cache() |
| 6 | + |
| 7 | +from outlines.fsm.fsm import RegexFSM # noqa: E402 |
| 8 | +from outlines.fsm.json_schema import build_regex_from_object # noqa: E402 |
| 9 | + |
| 10 | +simple_schema = """{ |
| 11 | + "$defs": { |
| 12 | + "Armor": { |
| 13 | + "enum": ["leather", "chainmail", "plate"], |
| 14 | + "title": "Armor", |
| 15 | + "type": "string" |
| 16 | + } |
| 17 | + }, |
| 18 | + "properties": { |
| 19 | + "name": {"maxLength": 10, "title": "Name", "type": "string"}, |
| 20 | + "age": {"title": "Age", "type": "integer"}, |
| 21 | + "armor": {"$ref": "#/$defs/Armor"}, |
| 22 | + "strength": {"title": "Strength", "type": "integer"}\ |
| 23 | + }, |
| 24 | + "required": ["name", "age", "armor", "strength"], |
| 25 | + "title": "Character", |
| 26 | + "type": "object" |
| 27 | + }""" |
| 28 | + |
| 29 | + |
| 30 | +complex_schema = """{ |
| 31 | + "$schema": "http://json-schema.org/draft-04/schema#", |
| 32 | + "title": "Schema for a recording", |
| 33 | + "type": "object", |
| 34 | + "definitions": { |
| 35 | + "artist": { |
| 36 | + "type": "object", |
| 37 | + "properties": { |
| 38 | + "id": {"type": "number"}, |
| 39 | + "name": {"type": "string"}, |
| 40 | + "functions": { |
| 41 | + "type": "array", |
| 42 | + "items": {"type": "string"} |
| 43 | + } |
| 44 | + }, |
| 45 | + "required": ["id", "name", "functions"] |
| 46 | + } |
| 47 | + }, |
| 48 | + "properties": { |
| 49 | + "id": {"type": "number"}, |
| 50 | + "work": { |
| 51 | + "type": "object", |
| 52 | + "properties": { |
| 53 | + "id": {"type": "number"}, |
| 54 | + "name": {"type": "string"}, |
| 55 | + "composer": {"$ref": "#/definitions/artist"} |
| 56 | + } |
| 57 | + }, |
| 58 | + "recording_artists": { |
| 59 | + "type": "array", |
| 60 | + "items": {"$ref": "#/definitions/artist"} |
| 61 | + } |
| 62 | + }, |
| 63 | + "required": ["id", "work", "recording_artists"] |
| 64 | +}""" |
| 65 | + |
| 66 | + |
| 67 | +schemas = dict(simple_schema=simple_schema, complex_schema=complex_schema) |
| 68 | + |
| 69 | + |
| 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_object, |
| 76 | + args=(schema,), |
| 77 | + rounds=8, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 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_object(schema) |
| 88 | + benchmark.pedantic( |
| 89 | + RegexFSM, |
| 90 | + args=(regex, tokenizer), |
| 91 | + rounds=8, |
| 92 | + ) |
0 commit comments