dfax 0.1.0__py3-none-any.whl
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/__init__.py +2 -0
- dfax/dfax.py +356 -0
- dfax/samplers.py +179 -0
- dfax/utils.py +189 -0
- dfax-0.1.0.dist-info/METADATA +136 -0
- dfax-0.1.0.dist-info/RECORD +8 -0
- dfax-0.1.0.dist-info/WHEEL +4 -0
- dfax-0.1.0.dist-info/licenses/LICENSE +21 -0
dfax/__init__.py
ADDED
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
|
+
|
dfax/samplers.py
ADDED
|
@@ -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/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,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
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
dfax/__init__.py,sha256=Z7ML6_mY6woYNNoO8G-dqRwj9iLITiiArFP_PFNhmVY,48
|
|
2
|
+
dfax/dfax.py,sha256=dwBrsjSgrhBikvERw8aPL1fqTfQajr3YNOCTe1rsDFw,12486
|
|
3
|
+
dfax/samplers.py,sha256=r9b48rioT54T6rmzakDLmzbKUZJ59JtwPjQutsW5vxg,7263
|
|
4
|
+
dfax/utils.py,sha256=tEoruuxANe6ZIuNC7cqgTRef10VNbva7HNbgIXJGdGo,6732
|
|
5
|
+
dfax-0.1.0.dist-info/METADATA,sha256=wzgpWzITxHhyy4Okwe7Jp3CH9WtW8SpnsOpabCA3cbk,3523
|
|
6
|
+
dfax-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
7
|
+
dfax-0.1.0.dist-info/licenses/LICENSE,sha256=Cvu0BZqt3rcFFv70hcFDgD_y8ryOKW85F-qGRfYI4iM,1071
|
|
8
|
+
dfax-0.1.0.dist-info/RECORD,,
|
|
@@ -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.
|