Skip to content

Commit d534c2f

Browse files
authored
Add benchmark suite for compilation (#542)
1 parent 46dc706 commit d534c2f

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed

docs/community/contribute.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ And run the code style checks:
5656
pre-commit run --all-files
5757
```
5858

59+
#### Performance testing
60+
61+
Run benchmark tests:
62+
63+
```python
64+
pytest --benchmark-only
65+
```
66+
67+
([other pytest-benchmark command line options](https://pytest-benchmark.readthedocs.io/en/latest/usage.html#commandline-options))
68+
5969
### Open a Pull Request
6070

6171
Create a new branch on your fork, commit and push the changes:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dynamic = ["version"]
4646
test = [
4747
"pre-commit",
4848
"pytest",
49+
"pytest-benchmark",
4950
"pytest-cov",
5051
"pytest-mock",
5152
"transformers",

tests/benchmark/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
from outlines.fsm.fsm import RegexFSM
4+
from outlines.models.transformers import TransformerTokenizer
5+
6+
7+
@pytest.fixture
8+
def tokenizer():
9+
return TransformerTokenizer("gpt2")
10+
11+
12+
@pytest.fixture
13+
def ensure_numba_compiled(tokenizer):
14+
RegexFSM("a", tokenizer)
15+
return True
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import importlib
2+
3+
import interegular
4+
import numba
5+
6+
import outlines
7+
8+
outlines.disable_cache()
9+
10+
11+
def test_benchmark_compile_numba(benchmark, tokenizer, mocker):
12+
"""Compile a basic regex to benchmark the numba compilation time"""
13+
14+
def setup():
15+
from outlines.fsm import regex
16+
17+
original_njit = numba.njit
18+
19+
def mock_njit(*args, **kwargs):
20+
kwargs["cache"] = False
21+
return original_njit(*args, **kwargs)
22+
23+
mocker.patch("numba.njit", new=mock_njit)
24+
importlib.reload(regex)
25+
26+
regex_pattern, _ = regex.make_deterministic_fsm(
27+
interegular.parse_pattern("a").to_fsm().reduce()
28+
)
29+
return (regex, regex_pattern, tokenizer), {}
30+
31+
benchmark.pedantic(
32+
lambda r, *args: r.create_fsm_index_tokenizer(*args), rounds=2, setup=setup
33+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
import outlines
4+
5+
outlines.disable_cache()
6+
7+
from outlines.fsm.fsm import RegexFSM # noqa: E402
8+
9+
regex_samples = {
10+
"email": r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
11+
"complex_phone": "\\+?\\d{1,4}?[-.\\s]?\\(?\\d{1,3}?\\)?[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,9}",
12+
"simple_phone": "\\+?[1-9][0-9]{7,14}",
13+
"date": r"([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])(\.|-|/)([1-9]|0[1-9]|1[0-2])(\.|-|/)([0-9][0-9]|19[0-9][0-9]|20[0-9][0-9])|([0-9][0-9]|19[0-9][0-9]|20[0-9][0-9])(\.|-|/)([1-9]|0[1-9]|1[0-2])(\.|-|/)([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])",
14+
"time": r"(0?[1-9]|1[0-2]):[0-5]\d\s?(am|pm)?",
15+
"ip": r"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
16+
"url": r"(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?",
17+
"ssn": r"\d{3}-\d{2}-\d{4}",
18+
"complex_span_constrained_relation_extraction": "(['\"\\ ,]?((?:of|resulting|case|which|cultures|a|core|extreme|selflessness|spiritual|various|However|both|vary|in|other|secular|the|religious|among|moral|and|It|object|worldviews|altruism|traditional|material|aspect|or|life|beings|virtue|is|however|opposite|concern|an|practice|it|for|s|quality|religions|In|Altruism|animals|happiness|many|become|principle|human|selfishness|may|synonym)['\"\\ ,]?)+['\"\\ ,]?\\s\\|\\s([^|\\(\\)\n]{1,})\\s\\|\\s['\"\\ ,]?((?:of|resulting|case|which|cultures|a|core|extreme|selflessness|spiritual|various|However|both|vary|in|other|secular|the|religious|among|moral|and|It|object|worldviews|altruism|traditional|material|aspect|or|life|beings|virtue|is|however|opposite|concern|an|practice|it|for|s|quality|religions|In|Altruism|animals|happiness|many|become|principle|human|selfishness|may|synonym)['\"\\ ,]?)+['\"\\ ,]?(\\s\\|\\s\\(([^|\\(\\)\n]{1,})\\s\\|\\s([^|\\(\\)\n]{1,})\\))*\\n)*",
19+
}
20+
21+
22+
@pytest.mark.parametrize("regex_name", regex_samples.keys())
23+
def test_benchmark_regex_to_fsm(
24+
benchmark, tokenizer, ensure_numba_compiled, regex_name
25+
):
26+
"""Benchmark converting regex to FSM"""
27+
regex_str = regex_samples[regex_name]
28+
benchmark.pedantic(
29+
RegexFSM,
30+
args=(regex_str, tokenizer),
31+
rounds=8,
32+
)

0 commit comments

Comments
 (0)