dfax 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dfax-0.1.0/.gitignore +210 -0
- dfax-0.1.0/.python-version +1 -0
- dfax-0.1.0/LICENSE +21 -0
- dfax-0.1.0/PKG-INFO +136 -0
- dfax-0.1.0/README.md +127 -0
- dfax-0.1.0/dfax/__init__.py +2 -0
- dfax-0.1.0/dfax/dfax.py +356 -0
- dfax-0.1.0/dfax/samplers.py +179 -0
- dfax-0.1.0/dfax/utils.py +189 -0
- dfax-0.1.0/pyproject.toml +14 -0
- dfax-0.1.0/requirements.txt +45 -0
- dfax-0.1.0/test.py +36 -0
- dfax-0.1.0/uv.lock +7 -0
dfax-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
# macos
|
|
210
|
+
.DS_Store
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.10
|
dfax-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 RAD-Embeddings
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dfax-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dfax
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python library for modeling DFAs, Moore Machines, and Transition Systems in JAX.
|
|
5
|
+
Author-email: Beyazit Yalcinkaya <beyazit@berkeley.edu>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# DFAx
|
|
11
|
+
|
|
12
|
+
A JAX-compatible Python implementation of a Deterministic Finite Automaton (DFA).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
This package will soon be made pip-installable. In the meantime, pull the repo and and install locally.
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
git clone https://github.com/rad-dfa/dfax.git
|
|
20
|
+
pip install -e dfax
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Create DFAs by specifying a `start` state, `transitions` matrix, which is max number of states by number of alphabet symbols, and the associated `labels` for each state.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from dfax import DFAx
|
|
29
|
+
|
|
30
|
+
dfax = DFAx(
|
|
31
|
+
start=0, # State referred to as 0 is the initial state
|
|
32
|
+
transitions=jnp.array([
|
|
33
|
+
[1, 2, 0, 0, 0],
|
|
34
|
+
[1, 1, 1, 1, 1],
|
|
35
|
+
[2, 2, 2, 2, 2],
|
|
36
|
+
]), # Max number of states is 3 and number of tokens is 5
|
|
37
|
+
labels=jnp.array([False, True, False]) # State labels
|
|
38
|
+
) # Returns a DFA
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Take transitions on the DFA using a given symbol.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
dfax = dfax.advance(0) # Returns the resulting DFA after reading the symbol referred to as 0
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Minimize DFAs.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
dfax = dfax.minimize() # Returns a canonical minimal DFA
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
Canonicalize DFAs by relabeling states based on a BFS search.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
dfax = dfax.canonicalize() # Returns a canonical DFA
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Mutate DFAs by randomly toggling entries in the transition matrix.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import jax
|
|
64
|
+
|
|
65
|
+
key = jax.random.PRNGKey(0)
|
|
66
|
+
dfax = dfax.mutate(key) # Returns a mutated DFA
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Perform syntactic equality check between DFAs.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
dfax1 == dfax2
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Perform semantic equality check between DFAs.
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
dfax1.minimize() == dfax2.minimize()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Use DFAs as reward functions. With ternary semantics, reward is (i) `+1` if the `start` state has label `True`, (ii) `-1` if the `start` state has label `False` and is a sink state, and (iii) `0` otherwise. With binary semantics, `0` is returned instead of `-1`.
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
dfax.reward() # Returns a ternary reward
|
|
85
|
+
dfax.reward(binary=True) # Returns a binary reward
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
Sample from different DFA distributions: `Reach` samples DFAs ordering alphabet symbols, `ReachAvoid` samples `Reach` DFAs but also includes `Avoid` constraints, and `ReachAvoidDerived` samples randomly mutated `Reach` and `ReachAvoid` DFAs.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import jax
|
|
93
|
+
from dfax.samplers import ReachSampler, ReachAvoidSampler, RADSampler
|
|
94
|
+
|
|
95
|
+
key = jax.random.PRNGKey(0)
|
|
96
|
+
sampler = ReachAvoidSampler()
|
|
97
|
+
|
|
98
|
+
dfax = sampler.sample(key)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
Define your own DFA samplers by overloading `DFASampler `.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
@struct.dataclass
|
|
106
|
+
class MySampler(DFASampler):
|
|
107
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
108
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
109
|
+
# Write sampling code and return sampled DFA
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Visualize DFAs.
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from dfax.utils import visualize
|
|
116
|
+
visualize(dfax)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
This project is a JAX extension of [dfa](https://github.com/mvcisback/dfa). Therefore, we include helper methods for translating `DFAx` objects to and from `DFA` objects.
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from dfax import dfa2dfax, dfax2dfa
|
|
124
|
+
|
|
125
|
+
dfa = dfax2dfa(dfax) # Create DFA from DFAx
|
|
126
|
+
dfax = dfa2dfax(dfa) # Create DFAx from DFA
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## In progress
|
|
130
|
+
|
|
131
|
+
Currently, we are working on implementing Boolean operations on `DFAx` objects, e.g., conjunction, disjunction, etc. If there are other functionalities you would like to have in this package, create pull request or contact us to work together!
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
dfax-0.1.0/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# DFAx
|
|
2
|
+
|
|
3
|
+
A JAX-compatible Python implementation of a Deterministic Finite Automaton (DFA).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
This package will soon be made pip-installable. In the meantime, pull the repo and and install locally.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
git clone https://github.com/rad-dfa/dfax.git
|
|
11
|
+
pip install -e dfax
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Create DFAs by specifying a `start` state, `transitions` matrix, which is max number of states by number of alphabet symbols, and the associated `labels` for each state.
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from dfax import DFAx
|
|
20
|
+
|
|
21
|
+
dfax = DFAx(
|
|
22
|
+
start=0, # State referred to as 0 is the initial state
|
|
23
|
+
transitions=jnp.array([
|
|
24
|
+
[1, 2, 0, 0, 0],
|
|
25
|
+
[1, 1, 1, 1, 1],
|
|
26
|
+
[2, 2, 2, 2, 2],
|
|
27
|
+
]), # Max number of states is 3 and number of tokens is 5
|
|
28
|
+
labels=jnp.array([False, True, False]) # State labels
|
|
29
|
+
) # Returns a DFA
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Take transitions on the DFA using a given symbol.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
dfax = dfax.advance(0) # Returns the resulting DFA after reading the symbol referred to as 0
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Minimize DFAs.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
dfax = dfax.minimize() # Returns a canonical minimal DFA
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
Canonicalize DFAs by relabeling states based on a BFS search.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
dfax = dfax.canonicalize() # Returns a canonical DFA
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Mutate DFAs by randomly toggling entries in the transition matrix.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import jax
|
|
55
|
+
|
|
56
|
+
key = jax.random.PRNGKey(0)
|
|
57
|
+
dfax = dfax.mutate(key) # Returns a mutated DFA
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Perform syntactic equality check between DFAs.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
dfax1 == dfax2
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Perform semantic equality check between DFAs.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
dfax1.minimize() == dfax2.minimize()
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Use DFAs as reward functions. With ternary semantics, reward is (i) `+1` if the `start` state has label `True`, (ii) `-1` if the `start` state has label `False` and is a sink state, and (iii) `0` otherwise. With binary semantics, `0` is returned instead of `-1`.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
dfax.reward() # Returns a ternary reward
|
|
76
|
+
dfax.reward(binary=True) # Returns a binary reward
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
Sample from different DFA distributions: `Reach` samples DFAs ordering alphabet symbols, `ReachAvoid` samples `Reach` DFAs but also includes `Avoid` constraints, and `ReachAvoidDerived` samples randomly mutated `Reach` and `ReachAvoid` DFAs.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import jax
|
|
84
|
+
from dfax.samplers import ReachSampler, ReachAvoidSampler, RADSampler
|
|
85
|
+
|
|
86
|
+
key = jax.random.PRNGKey(0)
|
|
87
|
+
sampler = ReachAvoidSampler()
|
|
88
|
+
|
|
89
|
+
dfax = sampler.sample(key)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
Define your own DFA samplers by overloading `DFASampler `.
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
@struct.dataclass
|
|
97
|
+
class MySampler(DFASampler):
|
|
98
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
99
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
100
|
+
# Write sampling code and return sampled DFA
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Visualize DFAs.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from dfax.utils import visualize
|
|
107
|
+
visualize(dfax)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
This project is a JAX extension of [dfa](https://github.com/mvcisback/dfa). Therefore, we include helper methods for translating `DFAx` objects to and from `DFA` objects.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from dfax import dfa2dfax, dfax2dfa
|
|
115
|
+
|
|
116
|
+
dfa = dfax2dfa(dfax) # Create DFA from DFAx
|
|
117
|
+
dfax = dfa2dfax(dfa) # Create DFAx from DFA
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## In progress
|
|
121
|
+
|
|
122
|
+
Currently, we are working on implementing Boolean operations on `DFAx` objects, e.g., conjunction, disjunction, etc. If there are other functionalities you would like to have in this package, create pull request or contact us to work together!
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
dfax-0.1.0/dfax/dfax.py
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import chex
|
|
3
|
+
import jax.numpy as jnp
|
|
4
|
+
from flax import struct
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@jax.jit
|
|
8
|
+
def DFAx(start, transitions, labels):
|
|
9
|
+
n_states, n_tokens = transitions.shape
|
|
10
|
+
trans_flat = transitions.flatten()
|
|
11
|
+
is_reach_init = jnp.zeros((n_states,), dtype=bool).at[start].set(True)
|
|
12
|
+
|
|
13
|
+
def step(is_reach: jnp.ndarray) -> jnp.ndarray:
|
|
14
|
+
reach_repeat = jnp.repeat(is_reach, n_tokens)
|
|
15
|
+
dest_counts = jnp.zeros((n_states,), dtype=jnp.int32).at[trans_flat].add(reach_repeat)
|
|
16
|
+
return dest_counts > 0
|
|
17
|
+
|
|
18
|
+
def cond(pair):
|
|
19
|
+
prev_is_reach, curr_is_reach = pair
|
|
20
|
+
return jnp.any(prev_is_reach != curr_is_reach)
|
|
21
|
+
|
|
22
|
+
def body(pair):
|
|
23
|
+
prev_is_reach, curr_is_reach = pair
|
|
24
|
+
next_is_reach = step(curr_is_reach)
|
|
25
|
+
return (curr_is_reach, next_is_reach)
|
|
26
|
+
|
|
27
|
+
is_reach, _ = jax.lax.while_loop(cond, body, (is_reach_init, step(is_reach_init)))
|
|
28
|
+
|
|
29
|
+
return _DFAx(start=start,
|
|
30
|
+
transitions=transitions,
|
|
31
|
+
labels=labels,
|
|
32
|
+
is_reach=is_reach)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@struct.dataclass
|
|
36
|
+
class _DFAx:
|
|
37
|
+
start: int
|
|
38
|
+
transitions: jnp.ndarray
|
|
39
|
+
labels: jnp.ndarray
|
|
40
|
+
is_reach: jnp.ndarray
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def n_states(self):
|
|
44
|
+
return jnp.sum(self.is_reach)
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def max_n_states(self):
|
|
48
|
+
return self.transitions.shape[0]
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def n_tokens(self):
|
|
52
|
+
return self.transitions.shape[1]
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def is_reach_tile(self) -> jnp.ndarray:
|
|
56
|
+
return jnp.tile(self.is_reach.reshape(-1, 1), (1, self.n_tokens))
|
|
57
|
+
|
|
58
|
+
@jax.jit
|
|
59
|
+
def __eq__(self, other: "DFAx") -> jnp.ndarray:
|
|
60
|
+
n = min(self.max_n_states, other.max_n_states)
|
|
61
|
+
|
|
62
|
+
start_eq = (self.start == other.start)
|
|
63
|
+
transitions_eq = jnp.where(
|
|
64
|
+
self.n_tokens == other.n_tokens,
|
|
65
|
+
jnp.all(self.transitions[:n] == other.transitions[:n]),
|
|
66
|
+
False
|
|
67
|
+
)
|
|
68
|
+
labels_eq = jnp.all(self.labels[:n] == other.labels[:n])
|
|
69
|
+
|
|
70
|
+
return jnp.logical_and(start_eq, jnp.logical_and(transitions_eq, labels_eq))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@jax.jit
|
|
74
|
+
def advance(self, symbol: int) -> "DFAx":
|
|
75
|
+
return DFAx(
|
|
76
|
+
start=jnp.where(
|
|
77
|
+
jnp.logical_and(symbol >= 0, symbol < self.n_tokens),
|
|
78
|
+
self.transitions[self.start, symbol],
|
|
79
|
+
self.start
|
|
80
|
+
),
|
|
81
|
+
transitions=self.transitions,
|
|
82
|
+
labels=self.labels)
|
|
83
|
+
|
|
84
|
+
@jax.jit
|
|
85
|
+
def mutate(self, key: chex.PRNGKey) -> "DFAx":
|
|
86
|
+
key, k1, k2 = jax.random.split(key, 3)
|
|
87
|
+
|
|
88
|
+
flat_is_reach_tile = self.is_reach_tile.flatten()
|
|
89
|
+
|
|
90
|
+
scores, indices = jax.lax.top_k(jnp.where(flat_is_reach_tile, 1, 0), flat_is_reach_tile.size)
|
|
91
|
+
num_valid = jnp.sum(scores > 0)
|
|
92
|
+
valid_idx = jax.random.randint(k1, (), 0, num_valid)
|
|
93
|
+
flat_idx = indices[valid_idx]
|
|
94
|
+
s, a = jnp.divmod(flat_idx, self.n_tokens)
|
|
95
|
+
|
|
96
|
+
scores, indices = jax.lax.top_k(jnp.where(self.is_reach, 1, 0), self.max_n_states)
|
|
97
|
+
num_valid = jnp.sum(scores > 0)
|
|
98
|
+
valid_idx = jax.random.randint(k2, (), 0, num_valid)
|
|
99
|
+
t = indices[valid_idx]
|
|
100
|
+
|
|
101
|
+
transitions = self.transitions.at[s, a].set(t)
|
|
102
|
+
|
|
103
|
+
return DFAx(start=self.start,
|
|
104
|
+
transitions=transitions,
|
|
105
|
+
labels=self.labels)
|
|
106
|
+
|
|
107
|
+
@jax.jit
|
|
108
|
+
def mutate_reject_lang(self, key: chex.PRNGKey) -> "DFAx":
|
|
109
|
+
key, k1, k2 = jax.random.split(key, 3)
|
|
110
|
+
|
|
111
|
+
is_self_loop = self.transitions == jnp.arange(self.max_n_states)[:, None]
|
|
112
|
+
sa_mask = jnp.logical_and(
|
|
113
|
+
jnp.logical_and(self.is_reach_tile, is_self_loop),
|
|
114
|
+
jnp.logical_not(self.labels)[:, None]
|
|
115
|
+
).flatten()
|
|
116
|
+
|
|
117
|
+
scores, indices = jax.lax.top_k(jnp.where(sa_mask, 1, 0), sa_mask.size)
|
|
118
|
+
num_valid = jnp.sum(scores > 0)
|
|
119
|
+
valid_idx = jax.random.randint(k1, (), 0, num_valid)
|
|
120
|
+
flat_idx = indices[valid_idx]
|
|
121
|
+
s, a = jnp.divmod(flat_idx, self.n_tokens)
|
|
122
|
+
|
|
123
|
+
is_sink = jnp.all(is_self_loop, axis=-1)
|
|
124
|
+
is_reject = jnp.logical_and.reduce(jnp.array([self.is_reach, is_sink, jnp.logical_not(self.labels)]))
|
|
125
|
+
t_mask = jnp.where(jnp.any(is_reject), is_reject, jnp.logical_not(self.is_reach))
|
|
126
|
+
|
|
127
|
+
scores, indices = jax.lax.top_k(jnp.where(t_mask, 1, 0), t_mask.size)
|
|
128
|
+
num_valid = jnp.sum(scores > 0)
|
|
129
|
+
valid_idx = jax.random.randint(k2, (), 0, num_valid)
|
|
130
|
+
t = indices[valid_idx]
|
|
131
|
+
|
|
132
|
+
transitions = self.transitions.at[s, a].set(t)
|
|
133
|
+
|
|
134
|
+
return DFAx(start=self.start,
|
|
135
|
+
transitions=transitions,
|
|
136
|
+
labels=self.labels)
|
|
137
|
+
|
|
138
|
+
@jax.jit
|
|
139
|
+
def sink_accepts(self) -> "DFAx":
|
|
140
|
+
accept_mask = jnp.tile(self.labels.reshape(-1, 1), (1, self.n_tokens))
|
|
141
|
+
replace_mask = accept_mask & self.is_reach_tile
|
|
142
|
+
|
|
143
|
+
sink_indices = jnp.tile(jnp.arange(self.max_n_states).reshape(-1, 1), (1, self.n_tokens))
|
|
144
|
+
transitions = jnp.where(replace_mask, sink_indices, self.transitions)
|
|
145
|
+
|
|
146
|
+
return DFAx(
|
|
147
|
+
start=self.start,
|
|
148
|
+
transitions=transitions,
|
|
149
|
+
labels=self.labels
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
@jax.jit
|
|
153
|
+
def prune(self) -> "DFAx":
|
|
154
|
+
pruned_transitions = jnp.where(self.is_reach_tile, self.transitions, jnp.arange(self.max_n_states)[:, None])
|
|
155
|
+
pruned_labels = jnp.where(self.is_reach, self.labels, False)
|
|
156
|
+
|
|
157
|
+
return DFAx(start=self.start,
|
|
158
|
+
transitions=pruned_transitions,
|
|
159
|
+
labels=pruned_labels)
|
|
160
|
+
|
|
161
|
+
@jax.jit
|
|
162
|
+
def minimize(self) -> "DFAx":
|
|
163
|
+
return self.naivePR().prune().canonicalize()
|
|
164
|
+
|
|
165
|
+
@jax.jit
|
|
166
|
+
def naivePR(self) -> "DFAx":
|
|
167
|
+
# Algorithm 2 from https://arxiv.org/pdf/2410.22764
|
|
168
|
+
q_f = jnp.argmax(jnp.where(self.is_reach, self.labels, 0))
|
|
169
|
+
q_n = jnp.argmin(jnp.where(self.is_reach, self.labels, 1))
|
|
170
|
+
block = jnp.where(self.labels, q_f, q_n)
|
|
171
|
+
block = jnp.where(self.is_reach, block, jnp.arange(self.max_n_states))
|
|
172
|
+
|
|
173
|
+
qs = jnp.arange(self.max_n_states)
|
|
174
|
+
as_ = jnp.arange(self.n_tokens)
|
|
175
|
+
qas = jnp.stack(jnp.meshgrid(qs, as_, indexing="ij"), axis=-1).reshape(-1, 2)
|
|
176
|
+
|
|
177
|
+
def iteration(state):
|
|
178
|
+
block, _ = state
|
|
179
|
+
|
|
180
|
+
def elect(q_a):
|
|
181
|
+
q, a = q_a
|
|
182
|
+
return jax.lax.cond(
|
|
183
|
+
self.is_reach[q],
|
|
184
|
+
lambda _: (
|
|
185
|
+
block[q],
|
|
186
|
+
jnp.where(
|
|
187
|
+
block[self.transitions[q, a]] != block[self.transitions[block[q], a]],
|
|
188
|
+
q,
|
|
189
|
+
-1
|
|
190
|
+
)
|
|
191
|
+
),
|
|
192
|
+
lambda _: (block[q], -1),
|
|
193
|
+
operand=None
|
|
194
|
+
)
|
|
195
|
+
blk_idx, leaders = jax.vmap(elect)(qas)
|
|
196
|
+
new_leader = jax.ops.segment_max(leaders, blk_idx, num_segments=self.max_n_states)
|
|
197
|
+
|
|
198
|
+
def assign(q_a):
|
|
199
|
+
q, a = q_a
|
|
200
|
+
return jax.lax.cond(
|
|
201
|
+
self.is_reach[q],
|
|
202
|
+
lambda _: (
|
|
203
|
+
q,
|
|
204
|
+
jnp.where(
|
|
205
|
+
(block[self.transitions[q, a]] != block[self.transitions[block[q], a]]),
|
|
206
|
+
new_leader[block[q]],
|
|
207
|
+
-1
|
|
208
|
+
)
|
|
209
|
+
),
|
|
210
|
+
lambda _: (q, -1),
|
|
211
|
+
operand=None
|
|
212
|
+
)
|
|
213
|
+
qs_idx, new_vals = jax.vmap(assign)(qas)
|
|
214
|
+
new_block = jax.ops.segment_max(new_vals, qs_idx, num_segments=self.max_n_states)
|
|
215
|
+
new_block = jnp.where(new_block < 0, block, new_block)
|
|
216
|
+
|
|
217
|
+
return (new_block, jnp.any(new_block != block))
|
|
218
|
+
|
|
219
|
+
block, _ = jax.lax.while_loop(
|
|
220
|
+
lambda s: s[1],
|
|
221
|
+
lambda s: iteration((s[0], False)),
|
|
222
|
+
(block, True)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
minimized_start = block[self.start]
|
|
226
|
+
minimized_labels = self.labels[block]
|
|
227
|
+
minimized_transitions = block[self.transitions]
|
|
228
|
+
|
|
229
|
+
return DFAx(start=minimized_start,
|
|
230
|
+
transitions=minimized_transitions,
|
|
231
|
+
labels=minimized_labels)
|
|
232
|
+
|
|
233
|
+
@jax.jit
|
|
234
|
+
def canonicalize(self) -> "DFAx":
|
|
235
|
+
old_to_new = (-jnp.ones((self.max_n_states,), dtype=jnp.int32)).at[self.start].set(0)
|
|
236
|
+
visited = jnp.zeros((self.max_n_states,), dtype=bool).at[self.start].set(True)
|
|
237
|
+
queue = (-jnp.ones((self.max_n_states,), dtype=jnp.int32)).at[0].set(self.start)
|
|
238
|
+
head = 0
|
|
239
|
+
tail = (head + 1) % self.max_n_states
|
|
240
|
+
count = 0
|
|
241
|
+
|
|
242
|
+
def cond(carry):
|
|
243
|
+
_, _, _, head, tail, _ = carry
|
|
244
|
+
return head != tail
|
|
245
|
+
|
|
246
|
+
def body(carry):
|
|
247
|
+
visited, old_to_new, queue, head, tail, count = carry
|
|
248
|
+
|
|
249
|
+
current_state = queue[head]
|
|
250
|
+
head = (head + 1) % self.max_n_states
|
|
251
|
+
|
|
252
|
+
old_to_new = old_to_new.at[current_state].set(count)
|
|
253
|
+
count += 1
|
|
254
|
+
|
|
255
|
+
next_states = self.transitions[current_state]
|
|
256
|
+
|
|
257
|
+
def push(i, carry):
|
|
258
|
+
visited, queue, tail = carry
|
|
259
|
+
ns = next_states[i]
|
|
260
|
+
unseen = jnp.logical_not(visited[ns])
|
|
261
|
+
|
|
262
|
+
queue = queue.at[tail].set(jnp.where(unseen, ns, queue[tail]))
|
|
263
|
+
tail = (tail + unseen) % self.max_n_states
|
|
264
|
+
|
|
265
|
+
visited = visited.at[ns].set(True)
|
|
266
|
+
return visited, queue, tail
|
|
267
|
+
|
|
268
|
+
visited, queue, tail = jax.lax.fori_loop(
|
|
269
|
+
0, self.n_tokens, push, (visited, queue, tail)
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
return visited, old_to_new, queue, head, tail, count
|
|
273
|
+
|
|
274
|
+
visited, old_to_new, queue, head, tail, count = jax.lax.while_loop(
|
|
275
|
+
cond, body, (visited, old_to_new, queue, head, tail, count)
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
mask = (old_to_new < 0)
|
|
279
|
+
ranks = jnp.cumsum(mask) - 1
|
|
280
|
+
fill_vals = count + ranks
|
|
281
|
+
old_to_new = jnp.where(mask, fill_vals, old_to_new)
|
|
282
|
+
|
|
283
|
+
start = old_to_new[self.start]
|
|
284
|
+
transitions = self.transitions.at[old_to_new].set(old_to_new[self.transitions])
|
|
285
|
+
labels = self.labels.at[old_to_new].set(self.labels)
|
|
286
|
+
|
|
287
|
+
return DFAx(start=start,
|
|
288
|
+
transitions=transitions,
|
|
289
|
+
labels=labels)
|
|
290
|
+
|
|
291
|
+
@jax.jit
|
|
292
|
+
def to_graph(self):
|
|
293
|
+
srcs, tgts = jnp.meshgrid(jnp.arange(self.max_n_states), jnp.arange(self.max_n_states), indexing="ij")
|
|
294
|
+
srcs = srcs.flatten()
|
|
295
|
+
tgts = tgts.flatten()
|
|
296
|
+
|
|
297
|
+
edge_index = jnp.stack([srcs, tgts])
|
|
298
|
+
|
|
299
|
+
is_init = jnp.logical_and(
|
|
300
|
+
self.is_reach,
|
|
301
|
+
jnp.arange(self.max_n_states) == self.start
|
|
302
|
+
)
|
|
303
|
+
is_accept = jnp.logical_and(
|
|
304
|
+
self.is_reach,
|
|
305
|
+
self.labels
|
|
306
|
+
)
|
|
307
|
+
is_reject = jnp.logical_and(
|
|
308
|
+
self.is_reach,
|
|
309
|
+
jnp.all(
|
|
310
|
+
jnp.logical_and(
|
|
311
|
+
self.transitions == jnp.arange(self.max_n_states)[:, None], # has all loops
|
|
312
|
+
jnp.logical_not(self.labels[:, None]) # not accepting
|
|
313
|
+
),
|
|
314
|
+
axis=1
|
|
315
|
+
)
|
|
316
|
+
)
|
|
317
|
+
is_non_terminal = jnp.logical_and(
|
|
318
|
+
self.is_reach,
|
|
319
|
+
jnp.logical_and(
|
|
320
|
+
jnp.logical_not(is_accept),
|
|
321
|
+
jnp.logical_not(is_reject)
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
node_features = jnp.stack([is_init, is_accept, is_reject, is_non_terminal], axis=1).astype(jnp.float32)
|
|
326
|
+
|
|
327
|
+
edge_features = jnp.logical_and(
|
|
328
|
+
self.is_reach[:, None, None],
|
|
329
|
+
self.transitions[:, None, :] == jnp.arange(self.max_n_states)[None, :, None] # one hot tokens
|
|
330
|
+
).astype(jnp.float32).reshape(-1, self.n_tokens)
|
|
331
|
+
|
|
332
|
+
mask = jnp.any(edge_features != 0, axis=-1)[:, None]
|
|
333
|
+
edge_features = jnp.concatenate([node_features[srcs] * mask, edge_features, node_features[tgts] * mask], axis=-1)
|
|
334
|
+
|
|
335
|
+
graph = {
|
|
336
|
+
"node_features": node_features,
|
|
337
|
+
"edge_features": edge_features,
|
|
338
|
+
"edge_index": edge_index,
|
|
339
|
+
"current_state": jnp.array([self.start]),
|
|
340
|
+
"n_states": jnp.full(self.max_n_states, self.n_states)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return graph
|
|
344
|
+
|
|
345
|
+
@jax.jit
|
|
346
|
+
def reward(self, binary: bool = False) -> float:
|
|
347
|
+
is_accept = self.labels[self.start]
|
|
348
|
+
start_row = self.transitions[self.start]
|
|
349
|
+
start_vec = jnp.full((self.n_tokens,), self.start, dtype=start_row.dtype)
|
|
350
|
+
is_sink = jnp.all(start_row == start_vec)
|
|
351
|
+
|
|
352
|
+
return jnp.where(binary,
|
|
353
|
+
jnp.where(is_accept, 1.0, 0.0),
|
|
354
|
+
jnp.where(is_accept, 1.0, jnp.where(is_sink, -1.0, 0.0))
|
|
355
|
+
)
|
|
356
|
+
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import chex
|
|
3
|
+
import jax.numpy as jnp
|
|
4
|
+
from flax import struct
|
|
5
|
+
from dfax import DFAx
|
|
6
|
+
from functools import partial
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Base sampler: holds parameters
|
|
10
|
+
@struct.dataclass
|
|
11
|
+
class DFASampler:
|
|
12
|
+
n_tokens: int = 10
|
|
13
|
+
max_size: int = 10
|
|
14
|
+
p: float | None = None
|
|
15
|
+
|
|
16
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
17
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
18
|
+
raise NotImplementedError
|
|
19
|
+
|
|
20
|
+
@partial(jax.jit, static_argnums=(0, 2))
|
|
21
|
+
def sample_n(self, key: chex.PRNGKey, lower_bound: int = 2):
|
|
22
|
+
if self.p is not None:
|
|
23
|
+
values = jnp.arange(lower_bound, self.max_size + 1)
|
|
24
|
+
weights = self.p ** values
|
|
25
|
+
weights = weights / jnp.sum(weights)
|
|
26
|
+
idx = jax.random.choice(key, values, p=weights)
|
|
27
|
+
return idx
|
|
28
|
+
else:
|
|
29
|
+
return jax.random.randint(key, (), lower_bound, self.max_size + 1)
|
|
30
|
+
|
|
31
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
32
|
+
def trivial(self, label):
|
|
33
|
+
start = 0
|
|
34
|
+
transitions = jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens))
|
|
35
|
+
labels = jnp.zeros((self.max_size,), dtype=bool).at[start].set(label)
|
|
36
|
+
return DFAx(start=start,
|
|
37
|
+
transitions=transitions,
|
|
38
|
+
labels=labels)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# Reach sampler
|
|
42
|
+
@struct.dataclass
|
|
43
|
+
class ReachSampler(DFASampler):
|
|
44
|
+
prob_stutter: float = 0.9
|
|
45
|
+
|
|
46
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
47
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
48
|
+
key, subkey = jax.random.split(key)
|
|
49
|
+
n = self.sample_n(subkey, lower_bound=2)
|
|
50
|
+
success = n-1
|
|
51
|
+
transitions = jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens))
|
|
52
|
+
labels = jnp.zeros(self.max_size, dtype=bool)
|
|
53
|
+
labels = labels.at[success].set(True)
|
|
54
|
+
transitions = transitions.at[success, :].set(success)
|
|
55
|
+
|
|
56
|
+
def body_fn(i, carry):
|
|
57
|
+
transitions, labels, key = carry
|
|
58
|
+
key, k1, k2, k3 = jax.random.split(key, 4)
|
|
59
|
+
perm = jax.random.permutation(k1, jnp.arange(self.n_tokens))
|
|
60
|
+
row = jnp.full(self.n_tokens, i, dtype=jnp.int32)
|
|
61
|
+
row = row.at[perm[0]].set(i+1)
|
|
62
|
+
rest = perm[1:]
|
|
63
|
+
r = jax.random.uniform(k2, (self.n_tokens-1,))
|
|
64
|
+
choice = jax.random.bernoulli(k3, 0.5, (self.n_tokens-1,))
|
|
65
|
+
dest = jnp.where(r <= self.prob_stutter, i, i+1)
|
|
66
|
+
row = row.at[rest].set(dest)
|
|
67
|
+
transitions = transitions.at[i].set(row)
|
|
68
|
+
return (transitions, labels, key)
|
|
69
|
+
|
|
70
|
+
transitions, labels, _ = jax.lax.cond(
|
|
71
|
+
n == 0,
|
|
72
|
+
lambda _: (jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens)), jnp.zeros(self.max_size, dtype=bool).at[0].set(True), key),
|
|
73
|
+
lambda _: jax.lax.fori_loop(0, n-1, body_fn, (transitions, labels, key)),
|
|
74
|
+
operand=None
|
|
75
|
+
)
|
|
76
|
+
return DFAx(start=0, transitions=transitions, labels=labels).minimize()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# Reach-Avoid sampler
|
|
80
|
+
@struct.dataclass
|
|
81
|
+
class ReachAvoidSampler(DFASampler):
|
|
82
|
+
prob_stutter: float = 0.9
|
|
83
|
+
|
|
84
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
85
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
86
|
+
key, subkey = jax.random.split(key)
|
|
87
|
+
n = self.sample_n(subkey, lower_bound=3)
|
|
88
|
+
success, fail = n-2, n-1
|
|
89
|
+
transitions = jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens))
|
|
90
|
+
labels = jnp.zeros(self.max_size, dtype=bool)
|
|
91
|
+
labels = labels.at[success].set(True)
|
|
92
|
+
transitions = transitions.at[success, :].set(success)
|
|
93
|
+
transitions = transitions.at[fail, :].set(fail)
|
|
94
|
+
|
|
95
|
+
def body_fn(i, carry):
|
|
96
|
+
transitions, labels, key = carry
|
|
97
|
+
key, k1, k2, k3 = jax.random.split(key, 4)
|
|
98
|
+
perm = jax.random.permutation(k1, jnp.arange(self.n_tokens))
|
|
99
|
+
row = jnp.full(self.n_tokens, i, dtype=jnp.int32)
|
|
100
|
+
row = row.at[perm[0]].set(i+1)
|
|
101
|
+
row = row.at[perm[1]].set(fail)
|
|
102
|
+
rest = perm[2:]
|
|
103
|
+
r = jax.random.uniform(k2, (self.n_tokens-2,))
|
|
104
|
+
choice = jax.random.bernoulli(k3, 0.5, (self.n_tokens-2,))
|
|
105
|
+
dest = jnp.where(r <= self.prob_stutter, i, jnp.where(choice, i+1, fail))
|
|
106
|
+
row = row.at[rest].set(dest)
|
|
107
|
+
transitions = transitions.at[i].set(row)
|
|
108
|
+
return (transitions, labels, key)
|
|
109
|
+
|
|
110
|
+
transitions, labels, _ = jax.lax.cond(
|
|
111
|
+
n == 0,
|
|
112
|
+
lambda _: (jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens)), jnp.zeros(self.max_size, dtype=bool).at[0].set(True), key),
|
|
113
|
+
lambda _: jax.lax.fori_loop(0, n-2, body_fn, (transitions, labels, key)),
|
|
114
|
+
operand=None
|
|
115
|
+
)
|
|
116
|
+
return DFAx(start=0, transitions=transitions, labels=labels).minimize()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# Reach-Avoid with random mutations
|
|
120
|
+
@struct.dataclass
|
|
121
|
+
class RADSampler(DFASampler):
|
|
122
|
+
p: float | None = 0.5
|
|
123
|
+
prob_stutter: float = 0.9
|
|
124
|
+
max_mutations: int = 5
|
|
125
|
+
|
|
126
|
+
@partial(jax.jit, static_argnums=(0,))
|
|
127
|
+
def sample(self, key: chex.PRNGKey) -> DFAx:
|
|
128
|
+
key, subkey = jax.random.split(key)
|
|
129
|
+
n = self.sample_n(subkey, lower_bound=3)
|
|
130
|
+
success, fail = n-2, n-1
|
|
131
|
+
transitions = jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens))
|
|
132
|
+
labels = jnp.zeros(self.max_size, dtype=bool)
|
|
133
|
+
labels = labels.at[success].set(True)
|
|
134
|
+
transitions = transitions.at[success, :].set(success)
|
|
135
|
+
transitions = transitions.at[fail, :].set(fail)
|
|
136
|
+
|
|
137
|
+
def body_fn(i, carry):
|
|
138
|
+
transitions, labels, key = carry
|
|
139
|
+
key, k1, k2, k3, k4 = jax.random.split(key, 5)
|
|
140
|
+
perm = jax.random.permutation(k1, jnp.arange(self.n_tokens))
|
|
141
|
+
row = jnp.full(self.n_tokens, i, dtype=jnp.int32)
|
|
142
|
+
row = row.at[perm[0]].set(i+1)
|
|
143
|
+
is_avoid_problem = jax.random.bernoulli(k2, 0.5)
|
|
144
|
+
row = row.at[perm[1]].set(jnp.where(is_avoid_problem, fail, i))
|
|
145
|
+
rest = perm[2:]
|
|
146
|
+
r = jax.random.uniform(k3, (self.n_tokens-2,))
|
|
147
|
+
choice = jax.random.bernoulli(k4, 0.5, (self.n_tokens-2,))
|
|
148
|
+
dest = jnp.where(r <= self.prob_stutter, i, jnp.where(choice, i+1, fail))
|
|
149
|
+
row = row.at[rest].set(dest)
|
|
150
|
+
transitions = transitions.at[i].set(row)
|
|
151
|
+
return (transitions, labels, key)
|
|
152
|
+
|
|
153
|
+
transitions, labels, _ = jax.lax.cond(
|
|
154
|
+
n == 0,
|
|
155
|
+
lambda _: (jnp.tile(jnp.arange(self.max_size).reshape(-1, 1), (1, self.n_tokens)), jnp.zeros(self.max_size, dtype=bool).at[0].set(True), key),
|
|
156
|
+
lambda _: jax.lax.fori_loop(0, n-2, body_fn, (transitions, labels, key)),
|
|
157
|
+
operand=None
|
|
158
|
+
)
|
|
159
|
+
candidate = DFAx(start=0, transitions=transitions, labels=labels).minimize()
|
|
160
|
+
|
|
161
|
+
key, subkey = jax.random.split(key)
|
|
162
|
+
n_mutations = jax.random.choice(subkey, self.max_mutations + 1)
|
|
163
|
+
|
|
164
|
+
def derive(i, carry):
|
|
165
|
+
k, cand = carry
|
|
166
|
+
k, sk = jax.random.split(k)
|
|
167
|
+
new_cand = cand.mutate(sk).sink_accepts().minimize()
|
|
168
|
+
cand = jax.lax.cond(
|
|
169
|
+
new_cand.n_states <= 1,
|
|
170
|
+
lambda _: cand,
|
|
171
|
+
lambda _: new_cand,
|
|
172
|
+
operand=None
|
|
173
|
+
)
|
|
174
|
+
return k, cand
|
|
175
|
+
|
|
176
|
+
_, candidate = jax.lax.fori_loop(0, n_mutations, derive, (key, candidate))
|
|
177
|
+
|
|
178
|
+
return candidate
|
|
179
|
+
|
dfax-0.1.0/dfax/utils.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
from dfa import DFA
|
|
3
|
+
from dfax import DFAx
|
|
4
|
+
import networkx as nx
|
|
5
|
+
import jax.numpy as jnp
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
import matplotlib.patches as patches
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def dfa2dfax(dfa: DFA) -> DFAx:
|
|
11
|
+
states = dfa.states()
|
|
12
|
+
inputs = dfa.inputs
|
|
13
|
+
start = dfa.start
|
|
14
|
+
transitions = jnp.array([[dfa._transition(s, a) for a in inputs] for s in states])
|
|
15
|
+
labels = jnp.array([dfa._label(s) for s in states])
|
|
16
|
+
tmp = DFAx(
|
|
17
|
+
start=start,
|
|
18
|
+
transitions=transitions,
|
|
19
|
+
labels=labels
|
|
20
|
+
)
|
|
21
|
+
return tmp
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def dfax2dfa(dfax: DFAx) -> DFA:
|
|
25
|
+
inputs = set(range(dfax.transitions.shape[1]))
|
|
26
|
+
|
|
27
|
+
def transition(s, a):
|
|
28
|
+
return int(dfax.transitions[s, a])
|
|
29
|
+
|
|
30
|
+
def label(s):
|
|
31
|
+
return bool(dfax.labels[s])
|
|
32
|
+
|
|
33
|
+
return DFA(
|
|
34
|
+
start=int(dfax.start),
|
|
35
|
+
inputs=inputs,
|
|
36
|
+
transition=transition,
|
|
37
|
+
label=label,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@jax.jit
|
|
42
|
+
def batch2graph(batch):
|
|
43
|
+
if batch["node_features"].ndim == 2 and batch["edge_features"].ndim == 2 and batch["edge_index"].ndim == 2 and batch["current_state"].ndim == 1:
|
|
44
|
+
return batch
|
|
45
|
+
|
|
46
|
+
batch_size, n_nodes, _ = batch["node_features"].shape
|
|
47
|
+
node_features = jnp.concatenate(batch["node_features"])
|
|
48
|
+
edge_features = jnp.concatenate(batch["edge_features"])
|
|
49
|
+
offset = (jnp.arange(batch_size, dtype=jnp.int32) * n_nodes)
|
|
50
|
+
edge_index = jnp.concatenate(batch["edge_index"] + offset[:, None, None], axis=1)
|
|
51
|
+
current_state = (batch["current_state"].reshape(batch_size, -1) + offset[:, None]).flatten()
|
|
52
|
+
n_states = jnp.concatenate(batch["n_states"])
|
|
53
|
+
|
|
54
|
+
graph = {
|
|
55
|
+
"node_features": node_features,
|
|
56
|
+
"edge_features": edge_features,
|
|
57
|
+
"edge_index": edge_index,
|
|
58
|
+
"current_state": current_state,
|
|
59
|
+
"n_states": n_states
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return graph
|
|
63
|
+
|
|
64
|
+
@jax.jit
|
|
65
|
+
def list2batch(graphs):
|
|
66
|
+
node_features_batch = jnp.stack([graph["node_features"] for graph in graphs], axis=0)
|
|
67
|
+
node_features_batch = node_features_batch if node_features_batch.ndim == 3 else jnp.concatenate(node_features_batch, axis=0)
|
|
68
|
+
|
|
69
|
+
edge_features_batch = jnp.stack([graph["edge_features"] for graph in graphs], axis=0)
|
|
70
|
+
edge_features_batch = edge_features_batch if edge_features_batch.ndim == 3 else jnp.concatenate(edge_features_batch, axis=0)
|
|
71
|
+
|
|
72
|
+
edge_index_batch = jnp.stack([graph["edge_index"] for graph in graphs], axis=0)
|
|
73
|
+
edge_index_batch = edge_index_batch if edge_index_batch.ndim == 3 else jnp.concatenate(edge_index_batch, axis=0)
|
|
74
|
+
|
|
75
|
+
current_state_batch = jnp.stack([graph["current_state"] for graph in graphs], axis=0)
|
|
76
|
+
current_state_batch = current_state_batch if current_state_batch.ndim == 2 else jnp.concatenate(current_state_batch, axis=0)
|
|
77
|
+
|
|
78
|
+
n_states_batch = jnp.stack(jnp.array([graph["n_states"] for graph in graphs]), axis=0)
|
|
79
|
+
n_states_batch = n_states_batch if n_states_batch.ndim == 2 else jnp.concatenate(n_states_batch, axis=0)
|
|
80
|
+
|
|
81
|
+
batch = {
|
|
82
|
+
"node_features": node_features_batch,
|
|
83
|
+
"edge_features": edge_features_batch,
|
|
84
|
+
"edge_index": edge_index_batch,
|
|
85
|
+
"current_state": current_state_batch,
|
|
86
|
+
"n_states": n_states_batch,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return batch
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def visualize(dfax, label_states=False, save_path=None):
|
|
93
|
+
n_states, n_tokens = dfax.transitions.shape
|
|
94
|
+
|
|
95
|
+
G = nx.DiGraph()
|
|
96
|
+
for s in range(n_states):
|
|
97
|
+
if dfax.is_reach[s]:
|
|
98
|
+
G.add_node(s, label=str(s))
|
|
99
|
+
|
|
100
|
+
edges = {}
|
|
101
|
+
for s in range(n_states):
|
|
102
|
+
s = int(s)
|
|
103
|
+
for a in range(n_tokens):
|
|
104
|
+
a = int(a)
|
|
105
|
+
t = int(dfax.transitions[s, a])
|
|
106
|
+
if s != t:
|
|
107
|
+
if (s, t) not in edges:
|
|
108
|
+
edges[(s, t)] = [str(a)]
|
|
109
|
+
else:
|
|
110
|
+
edges[(s, t)].append(str(a))
|
|
111
|
+
|
|
112
|
+
for (s, t) in edges:
|
|
113
|
+
G.add_edge(s, t, label=edges[(s, t)])
|
|
114
|
+
|
|
115
|
+
# dummy start node
|
|
116
|
+
dummy_start = ""
|
|
117
|
+
G.add_node(dummy_start)
|
|
118
|
+
G.add_edge(dummy_start, int(dfax.start))
|
|
119
|
+
|
|
120
|
+
pos = nx.shell_layout(G)
|
|
121
|
+
# pos = nx.planar_layout(G)
|
|
122
|
+
start_pos = pos[int(dfax.start)]
|
|
123
|
+
pos[dummy_start] = (start_pos[0] - 0.5, start_pos[1])
|
|
124
|
+
|
|
125
|
+
accept_nodes = [s for s in G.nodes() if s != dummy_start and dfax.labels[s]]
|
|
126
|
+
reject_nodes = [s for s in G.nodes() if s != dummy_start and not dfax.labels[s] and jnp.all(dfax.transitions[s] == s)]
|
|
127
|
+
undecd_nodes = [s for s in G.nodes() if s != dummy_start and not dfax.labels[s]]
|
|
128
|
+
|
|
129
|
+
# draw nodes
|
|
130
|
+
nx.draw_networkx_nodes(G, pos, nodelist=undecd_nodes, node_size=1200,
|
|
131
|
+
node_color="white", edgecolors="black", linewidths=2)
|
|
132
|
+
nx.draw_networkx_nodes(G, pos, nodelist=accept_nodes, node_size=1200,
|
|
133
|
+
node_color="#88E788", edgecolors="black", linewidths=2)
|
|
134
|
+
nx.draw_networkx_nodes(G, pos, nodelist=reject_nodes, node_size=1200,
|
|
135
|
+
node_color="#FF746C", edgecolors="black", linewidths=2)
|
|
136
|
+
|
|
137
|
+
if label_states:
|
|
138
|
+
nx.draw_networkx_labels(G, pos, font_size=20, font_weight="bold")
|
|
139
|
+
|
|
140
|
+
ax = plt.gca()
|
|
141
|
+
|
|
142
|
+
# draw edges
|
|
143
|
+
for (u, v) in G.edges():
|
|
144
|
+
if u == dummy_start:
|
|
145
|
+
nx.draw_networkx_edges(G, pos, edgelist=[(u, v)], arrows=True, arrowsize=20, node_size=1200)
|
|
146
|
+
continue
|
|
147
|
+
|
|
148
|
+
if G.has_edge(v, u):
|
|
149
|
+
rad = 0.25
|
|
150
|
+
else:
|
|
151
|
+
rad = 0.0
|
|
152
|
+
|
|
153
|
+
nx.draw_networkx_edges(G, pos, edgelist=[(u, v)], arrows=True, arrowsize=20,
|
|
154
|
+
connectionstyle=f"arc3,rad={rad}", node_size=1200)
|
|
155
|
+
|
|
156
|
+
# --- draw tokens along the curved edge ---
|
|
157
|
+
x0, y0 = pos[u]
|
|
158
|
+
x1, y1 = pos[v]
|
|
159
|
+
n = len(edges[(u, v)])
|
|
160
|
+
for i, a in enumerate(edges[(u, v)]):
|
|
161
|
+
ratio = (i + 1) / (n + 1)
|
|
162
|
+
|
|
163
|
+
# Compute midpoint along arc3 curve
|
|
164
|
+
if rad != 0:
|
|
165
|
+
# Control point for the quadratic Bezier
|
|
166
|
+
xm_ctrl = (x0 + x1) / 2 + rad * (y1 - y0)
|
|
167
|
+
ym_ctrl = (y0 + y1) / 2 - rad * (x1 - x0)
|
|
168
|
+
|
|
169
|
+
# Quadratic Bezier formula
|
|
170
|
+
xm = (1 - ratio) ** 2 * x0 + 2 * (1 - ratio) * ratio * xm_ctrl + ratio ** 2 * x1
|
|
171
|
+
ym = (1 - ratio) ** 2 * y0 + 2 * (1 - ratio) * ratio * ym_ctrl + ratio ** 2 * y1
|
|
172
|
+
else:
|
|
173
|
+
xm = x0 * (1 - ratio) + x1 * ratio
|
|
174
|
+
ym = y0 * (1 - ratio) + y1 * ratio
|
|
175
|
+
|
|
176
|
+
circle = patches.Circle((xm, ym), 0.08, facecolor="gold", edgecolor="orange", lw=1.5, zorder=5)
|
|
177
|
+
ax.add_patch(circle)
|
|
178
|
+
ax.text(xm, ym, a, ha="center", va="center", fontsize=16, color="black", weight="bold", zorder=6)
|
|
179
|
+
|
|
180
|
+
plt.axis("equal")
|
|
181
|
+
plt.tight_layout()
|
|
182
|
+
plt.axis("off")
|
|
183
|
+
|
|
184
|
+
if save_path:
|
|
185
|
+
plt.savefig(save_path, bbox_inches="tight", dpi=300)
|
|
186
|
+
else:
|
|
187
|
+
plt.show()
|
|
188
|
+
plt.close()
|
|
189
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dfax"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python library for modeling DFAs, Moore Machines, and Transition Systems in JAX."
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Beyazit Yalcinkaya", email = "beyazit@berkeley.edu" }
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[build-system]
|
|
13
|
+
requires = ["hatchling"]
|
|
14
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
absl-py==2.3.1
|
|
2
|
+
attrs==25.4.0
|
|
3
|
+
bidict==0.23.1
|
|
4
|
+
bitarray==2.9.3
|
|
5
|
+
chex==0.1.90
|
|
6
|
+
contourpy==1.3.2
|
|
7
|
+
cycler==0.12.1
|
|
8
|
+
dfa==4.7.1
|
|
9
|
+
etils==1.13.0
|
|
10
|
+
flax==0.10.4
|
|
11
|
+
fonttools==4.60.1
|
|
12
|
+
fsspec==2025.10.0
|
|
13
|
+
funcy==2.0
|
|
14
|
+
humanize==4.14.0
|
|
15
|
+
importlib-resources==6.5.2
|
|
16
|
+
jax==0.4.38
|
|
17
|
+
jaxlib==0.4.38
|
|
18
|
+
kiwisolver==1.4.9
|
|
19
|
+
markdown-it-py==4.0.0
|
|
20
|
+
matplotlib==3.10.7
|
|
21
|
+
mdurl==0.1.2
|
|
22
|
+
ml-dtypes==0.5.3
|
|
23
|
+
msgpack==1.1.2
|
|
24
|
+
nest-asyncio==1.6.0
|
|
25
|
+
networkx==3.4.2
|
|
26
|
+
numpy==2.2.6
|
|
27
|
+
opt-einsum==3.4.0
|
|
28
|
+
optax==0.2.5
|
|
29
|
+
orbax-checkpoint==0.11.5
|
|
30
|
+
packaging==25.0
|
|
31
|
+
pillow==12.0.0
|
|
32
|
+
protobuf==6.33.0
|
|
33
|
+
pygments==2.19.2
|
|
34
|
+
pyparsing==3.2.5
|
|
35
|
+
python-dateutil==2.9.0.post0
|
|
36
|
+
pyyaml==6.0.3
|
|
37
|
+
rich==14.2.0
|
|
38
|
+
scipy==1.15.3
|
|
39
|
+
simplejson==3.20.2
|
|
40
|
+
six==1.17.0
|
|
41
|
+
tensorstore==0.1.78
|
|
42
|
+
toolz==1.1.0
|
|
43
|
+
treescope==0.1.10
|
|
44
|
+
typing-extensions==4.15.0
|
|
45
|
+
zipp==3.23.0
|
dfax-0.1.0/test.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import random
|
|
3
|
+
import jax.numpy as jnp
|
|
4
|
+
from dfax import dfa2dfax, dfax2dfa, DFAx
|
|
5
|
+
from dfax.utils import visualize
|
|
6
|
+
from dfax.samplers import ReachSampler, ReachAvoidSampler, RADSampler
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
key = jax.random.PRNGKey(0)
|
|
10
|
+
|
|
11
|
+
sampler = ReachAvoidSampler(max_size=5, p=None)
|
|
12
|
+
# sampler = ReachSampler()
|
|
13
|
+
# sampler = ReachAvoidSampler()
|
|
14
|
+
|
|
15
|
+
n = 1_000
|
|
16
|
+
|
|
17
|
+
for i in range(n):
|
|
18
|
+
key, subkey = jax.random.split(key)
|
|
19
|
+
dfax = sampler.sample(subkey)
|
|
20
|
+
dfa = dfax2dfa(dfax)
|
|
21
|
+
|
|
22
|
+
inputs = list(dfa.inputs)
|
|
23
|
+
|
|
24
|
+
w = dfa.find_word()
|
|
25
|
+
|
|
26
|
+
for a in w:
|
|
27
|
+
dfa = dfa.advance([a]).minimize()
|
|
28
|
+
dfax = dfax.advance(a).minimize()
|
|
29
|
+
assert dfax == dfa2dfax(dfa).canonicalize()
|
|
30
|
+
assert dfax.reward() == dfa2dfax(dfa).reward()
|
|
31
|
+
assert dfax2dfa(dfax) == dfa
|
|
32
|
+
|
|
33
|
+
print(f"Test completed for {i + 1} samples.", end="\r")
|
|
34
|
+
|
|
35
|
+
print(f"Test completed for {n} samples.")
|
|
36
|
+
|