fuzzybits-server 0.17.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.
- fuzzybits_server/.dockerignore +64 -0
- fuzzybits_server/__init__.py +15 -0
- fuzzybits_server/app.py +93 -0
- fuzzybits_server/config.py +310 -0
- fuzzybits_server/convergence/__init__.py +100 -0
- fuzzybits_server/convergence/gbas.py +229 -0
- fuzzybits_server/convergence/metrics.py +164 -0
- fuzzybits_server/convergence/policies.py +387 -0
- fuzzybits_server/convergence/runner.py +398 -0
- fuzzybits_server/convergence/sampling.py +409 -0
- fuzzybits_server/convergence/special.py +280 -0
- fuzzybits_server/convergence/tally.py +179 -0
- fuzzybits_server/event_hub.py +58 -0
- fuzzybits_server/event_log.py +16 -0
- fuzzybits_server/fault_tree_payload.py +899 -0
- fuzzybits_server/model_oracle_p.json +44 -0
- fuzzybits_server/native.py +536 -0
- fuzzybits_server/routes.py +408 -0
- fuzzybits_server/state.py +67 -0
- fuzzybits_server/supervisor.py +193 -0
- fuzzybits_server/ui.py +84 -0
- fuzzybits_server/webui/.vite/manifest.json +26 -0
- fuzzybits_server/webui/assets/Playground-B6V8NzhX.css +1 -0
- fuzzybits_server/webui/assets/Playground-CSpFbzwZ.js +5481 -0
- fuzzybits_server/webui/assets/Playground-CSpFbzwZ.js.map +1 -0
- fuzzybits_server/webui/assets/index-BnegRRhI.css +1 -0
- fuzzybits_server/webui/assets/index-C3oGCBE0.js +13 -0
- fuzzybits_server/webui/assets/index-C3oGCBE0.js.map +1 -0
- fuzzybits_server/webui/index.html +24 -0
- fuzzybits_server/worker.py +64 -0
- fuzzybits_server-0.17.0.dist-info/METADATA +281 -0
- fuzzybits_server-0.17.0.dist-info/RECORD +34 -0
- fuzzybits_server-0.17.0.dist-info/WHEEL +4 -0
- fuzzybits_server-0.17.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from fastapi import APIRouter, Body, HTTPException, Query, Request, WebSocket, WebSocketDisconnect
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
|
|
8
|
+
from fuzzybits_server.config import CalculationConfig, parse_watch_value
|
|
9
|
+
from fuzzybits_server.event_hub import EventHub
|
|
10
|
+
from fuzzybits_server.fault_tree_payload import (
|
|
11
|
+
build_dag_bundle_payload,
|
|
12
|
+
build_dag_payload,
|
|
13
|
+
build_execution_plan_payload,
|
|
14
|
+
build_fault_tree_payload,
|
|
15
|
+
build_kernel_dag_payload,
|
|
16
|
+
build_sample_shape_payload,
|
|
17
|
+
build_source_dag_payload,
|
|
18
|
+
list_aralia_xml_inputs,
|
|
19
|
+
resolve_model_xml_path,
|
|
20
|
+
)
|
|
21
|
+
from fuzzybits_server.native import InvalidPcfbError, resolve_uploaded_pcfb, store_uploaded_pcfb
|
|
22
|
+
from fuzzybits_server.supervisor import CalculationSupervisor
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SamplingControlBody(BaseModel):
|
|
26
|
+
model_id: str | None = None
|
|
27
|
+
compilation_level: int | None = Field(default=None, ge=0, le=8)
|
|
28
|
+
expand_atleast_gates: bool | None = None
|
|
29
|
+
expand_xor_gates: bool | None = None
|
|
30
|
+
recognize_atleast_groups: bool | None = None
|
|
31
|
+
recognize_xor_groups: bool | None = None
|
|
32
|
+
ccf: bool | None = None
|
|
33
|
+
keep_null_gates: bool | None = None
|
|
34
|
+
# Total trial budget (0 → auto: unbounded / backend default).
|
|
35
|
+
num_trials: float | None = Field(default=None, ge=0)
|
|
36
|
+
# Optimistic per-node trials-per-pass target (0 → auto: fill device memory).
|
|
37
|
+
requested_trials_per_pass: float | None = Field(default=None, ge=0)
|
|
38
|
+
confidence: float | None = Field(default=None, gt=0, lt=1)
|
|
39
|
+
delta: float | None = Field(default=None, gt=0)
|
|
40
|
+
oracle: float | None = Field(default=None, ge=-1, le=1)
|
|
41
|
+
early_stop: bool | None = None
|
|
42
|
+
# Host-side convergence policy that gates early-stop ("wald" | "bayes" | "gbas").
|
|
43
|
+
policy: str | None = Field(default=None, pattern="^(wald|bayes|gbas)$")
|
|
44
|
+
burn_in: float | None = Field(default=None, ge=0)
|
|
45
|
+
pcfb_id: str | None = None
|
|
46
|
+
watch: list[str] | str | None = None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _pcfb_path_from_id(pcfb_id: str | None) -> str | None:
|
|
50
|
+
if not pcfb_id:
|
|
51
|
+
return None
|
|
52
|
+
try:
|
|
53
|
+
return str(resolve_uploaded_pcfb(pcfb_id))
|
|
54
|
+
except ValueError as exc:
|
|
55
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
56
|
+
except FileNotFoundError as exc:
|
|
57
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _calculation_config(body: SamplingControlBody | None = None) -> CalculationConfig:
|
|
61
|
+
config = CalculationConfig.from_env()
|
|
62
|
+
if body is None:
|
|
63
|
+
return config
|
|
64
|
+
input_files = config.input_files
|
|
65
|
+
pcfb_path = config.pcfb_path
|
|
66
|
+
if body.pcfb_id:
|
|
67
|
+
pcfb_path = _pcfb_path_from_id(body.pcfb_id)
|
|
68
|
+
if body.model_id:
|
|
69
|
+
try:
|
|
70
|
+
input_files = [str(resolve_model_xml_path(body.model_id))]
|
|
71
|
+
except ValueError as exc:
|
|
72
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
73
|
+
except FileNotFoundError as exc:
|
|
74
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
75
|
+
options = _compile_options(
|
|
76
|
+
config.options,
|
|
77
|
+
compilation_level=body.compilation_level,
|
|
78
|
+
expand_atleast_gates=body.expand_atleast_gates,
|
|
79
|
+
expand_xor_gates=body.expand_xor_gates,
|
|
80
|
+
recognize_atleast_groups=body.recognize_atleast_groups,
|
|
81
|
+
recognize_xor_groups=body.recognize_xor_groups,
|
|
82
|
+
ccf=body.ccf,
|
|
83
|
+
keep_null_gates=body.keep_null_gates,
|
|
84
|
+
)
|
|
85
|
+
overrides: dict[str, Any] = {}
|
|
86
|
+
if body.num_trials is not None:
|
|
87
|
+
overrides["num_trials"] = body.num_trials
|
|
88
|
+
if body.requested_trials_per_pass is not None:
|
|
89
|
+
overrides["requested_trials_per_pass"] = body.requested_trials_per_pass
|
|
90
|
+
if body.confidence is not None:
|
|
91
|
+
overrides["confidence"] = body.confidence
|
|
92
|
+
if body.delta is not None:
|
|
93
|
+
overrides["delta"] = body.delta
|
|
94
|
+
if body.oracle is not None:
|
|
95
|
+
overrides["oracle"] = body.oracle
|
|
96
|
+
if body.early_stop is not None:
|
|
97
|
+
overrides["early_stop"] = body.early_stop
|
|
98
|
+
if body.policy is not None:
|
|
99
|
+
overrides["policy"] = body.policy
|
|
100
|
+
if body.burn_in is not None:
|
|
101
|
+
overrides["burn_in"] = body.burn_in
|
|
102
|
+
if body.watch is not None:
|
|
103
|
+
_, names = parse_watch_value(body.watch)
|
|
104
|
+
if names:
|
|
105
|
+
overrides["watch"] = names
|
|
106
|
+
if overrides:
|
|
107
|
+
options.update(overrides)
|
|
108
|
+
if input_files == config.input_files and options == config.options and pcfb_path == config.pcfb_path:
|
|
109
|
+
return config
|
|
110
|
+
return CalculationConfig(input_files=input_files, options=options, pcfb_path=pcfb_path)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def create_router(supervisor: CalculationSupervisor, event_hub: EventHub) -> APIRouter:
|
|
114
|
+
router = APIRouter(prefix="/api/v1")
|
|
115
|
+
|
|
116
|
+
@router.get("/healthz")
|
|
117
|
+
def healthz() -> dict[str, str]:
|
|
118
|
+
return {"status": "ok"}
|
|
119
|
+
|
|
120
|
+
@router.get("/status")
|
|
121
|
+
def status() -> dict[str, Any]:
|
|
122
|
+
return supervisor.state.snapshot(alive=supervisor.alive())
|
|
123
|
+
|
|
124
|
+
@router.get("/model/xml-inputs")
|
|
125
|
+
def xml_inputs() -> dict[str, Any]:
|
|
126
|
+
return list_aralia_xml_inputs()
|
|
127
|
+
|
|
128
|
+
@router.post("/model/pcfb")
|
|
129
|
+
async def upload_pcfb(request: Request) -> dict[str, Any]:
|
|
130
|
+
"""Accept a `.pcfb` circuit, validate with ``fuzzybits.inspect``, and store it.
|
|
131
|
+
|
|
132
|
+
Body: raw `.pcfb` bytes (``Content-Type: application/octet-stream``).
|
|
133
|
+
On success: ``{"id": "<sha256>", "pcfb_id": "<sha256>", "inspect": {...}}``.
|
|
134
|
+
Start a run with ``POST /api/v1/start`` and ``{"pcfb_id": "<sha256>"}``.
|
|
135
|
+
Invalid circuits return HTTP 400 with ``diagnostics`` from ``pcfb::validate``.
|
|
136
|
+
"""
|
|
137
|
+
data = await request.body()
|
|
138
|
+
try:
|
|
139
|
+
digest, path, summary = store_uploaded_pcfb(data)
|
|
140
|
+
except InvalidPcfbError as exc:
|
|
141
|
+
raise HTTPException(
|
|
142
|
+
status_code=400,
|
|
143
|
+
detail={
|
|
144
|
+
"error": str(exc),
|
|
145
|
+
"diagnostics": exc.diagnostics,
|
|
146
|
+
"inspect": exc.inspect,
|
|
147
|
+
},
|
|
148
|
+
) from exc
|
|
149
|
+
return {"id": digest, "pcfb_id": digest, "path": str(path), "inspect": summary}
|
|
150
|
+
|
|
151
|
+
@router.get("/model/fault-tree")
|
|
152
|
+
def fault_tree_model(model_id: str | None = Query(default=None)) -> dict[str, Any]:
|
|
153
|
+
if model_id:
|
|
154
|
+
try:
|
|
155
|
+
path = resolve_model_xml_path(model_id)
|
|
156
|
+
except ValueError as exc:
|
|
157
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
158
|
+
except FileNotFoundError as exc:
|
|
159
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
160
|
+
return build_fault_tree_payload([str(path)])
|
|
161
|
+
return build_fault_tree_payload(CalculationConfig.from_env().input_files)
|
|
162
|
+
|
|
163
|
+
@router.get("/model/dag")
|
|
164
|
+
def dag_model(
|
|
165
|
+
model_id: str | None = Query(default=None),
|
|
166
|
+
pcfb_id: str | None = Query(default=None),
|
|
167
|
+
compilation_level: int | None = Query(default=None, ge=0, le=8),
|
|
168
|
+
expand_atleast_gates: bool | None = Query(default=None),
|
|
169
|
+
expand_xor_gates: bool | None = Query(default=None),
|
|
170
|
+
recognize_atleast_groups: bool | None = Query(default=None),
|
|
171
|
+
recognize_xor_groups: bool | None = Query(default=None),
|
|
172
|
+
ccf: bool | None = Query(default=None),
|
|
173
|
+
keep_null_gates: bool | None = Query(default=None),
|
|
174
|
+
) -> dict[str, Any]:
|
|
175
|
+
config = CalculationConfig.from_env()
|
|
176
|
+
options = _compile_options(
|
|
177
|
+
config.options,
|
|
178
|
+
compilation_level=compilation_level,
|
|
179
|
+
expand_atleast_gates=expand_atleast_gates,
|
|
180
|
+
expand_xor_gates=expand_xor_gates,
|
|
181
|
+
recognize_atleast_groups=recognize_atleast_groups,
|
|
182
|
+
recognize_xor_groups=recognize_xor_groups,
|
|
183
|
+
ccf=ccf,
|
|
184
|
+
keep_null_gates=keep_null_gates,
|
|
185
|
+
)
|
|
186
|
+
pcfb_path = _pcfb_path_from_id(pcfb_id)
|
|
187
|
+
if model_id:
|
|
188
|
+
try:
|
|
189
|
+
path = resolve_model_xml_path(model_id)
|
|
190
|
+
except ValueError as exc:
|
|
191
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
192
|
+
except FileNotFoundError as exc:
|
|
193
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
194
|
+
return build_dag_payload([str(path)], options, pcfb_path=pcfb_path)
|
|
195
|
+
return build_dag_payload(config.input_files, options, pcfb_path=pcfb_path)
|
|
196
|
+
|
|
197
|
+
@router.get("/model/source-dag")
|
|
198
|
+
def source_dag_model(model_id: str | None = Query(default=None)) -> dict[str, Any]:
|
|
199
|
+
if model_id:
|
|
200
|
+
try:
|
|
201
|
+
path = resolve_model_xml_path(model_id)
|
|
202
|
+
except ValueError as exc:
|
|
203
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
204
|
+
except FileNotFoundError as exc:
|
|
205
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
206
|
+
return build_source_dag_payload([str(path)])
|
|
207
|
+
return build_source_dag_payload(CalculationConfig.from_env().input_files)
|
|
208
|
+
|
|
209
|
+
@router.get("/model/kernel-dag")
|
|
210
|
+
def kernel_dag_model(
|
|
211
|
+
model_id: str | None = Query(default=None),
|
|
212
|
+
pcfb_id: str | None = Query(default=None),
|
|
213
|
+
compilation_level: int | None = Query(default=None, ge=0, le=8),
|
|
214
|
+
expand_atleast_gates: bool | None = Query(default=None),
|
|
215
|
+
expand_xor_gates: bool | None = Query(default=None),
|
|
216
|
+
recognize_atleast_groups: bool | None = Query(default=None),
|
|
217
|
+
recognize_xor_groups: bool | None = Query(default=None),
|
|
218
|
+
ccf: bool | None = Query(default=None),
|
|
219
|
+
keep_null_gates: bool | None = Query(default=None),
|
|
220
|
+
) -> dict[str, Any]:
|
|
221
|
+
config = CalculationConfig.from_env()
|
|
222
|
+
options = _compile_options(
|
|
223
|
+
config.options,
|
|
224
|
+
compilation_level=compilation_level,
|
|
225
|
+
expand_atleast_gates=expand_atleast_gates,
|
|
226
|
+
expand_xor_gates=expand_xor_gates,
|
|
227
|
+
recognize_atleast_groups=recognize_atleast_groups,
|
|
228
|
+
recognize_xor_groups=recognize_xor_groups,
|
|
229
|
+
ccf=ccf,
|
|
230
|
+
keep_null_gates=keep_null_gates,
|
|
231
|
+
)
|
|
232
|
+
if model_id:
|
|
233
|
+
try:
|
|
234
|
+
path = resolve_model_xml_path(model_id)
|
|
235
|
+
except ValueError as exc:
|
|
236
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
237
|
+
except FileNotFoundError as exc:
|
|
238
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
239
|
+
return build_kernel_dag_payload([str(path)], options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
240
|
+
return build_kernel_dag_payload(config.input_files, options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
241
|
+
|
|
242
|
+
@router.get("/model/sample-shape")
|
|
243
|
+
def sample_shape_model(
|
|
244
|
+
model_id: str | None = Query(default=None),
|
|
245
|
+
pcfb_id: str | None = Query(default=None),
|
|
246
|
+
compilation_level: int | None = Query(default=None, ge=0, le=8),
|
|
247
|
+
expand_atleast_gates: bool | None = Query(default=None),
|
|
248
|
+
expand_xor_gates: bool | None = Query(default=None),
|
|
249
|
+
recognize_atleast_groups: bool | None = Query(default=None),
|
|
250
|
+
recognize_xor_groups: bool | None = Query(default=None),
|
|
251
|
+
ccf: bool | None = Query(default=None),
|
|
252
|
+
keep_null_gates: bool | None = Query(default=None),
|
|
253
|
+
num_trials: float | None = Query(default=None, ge=0),
|
|
254
|
+
requested_trials_per_pass: float | None = Query(default=None, ge=0),
|
|
255
|
+
) -> dict[str, Any]:
|
|
256
|
+
config = CalculationConfig.from_env()
|
|
257
|
+
options = _compile_options(
|
|
258
|
+
config.options,
|
|
259
|
+
compilation_level=compilation_level,
|
|
260
|
+
expand_atleast_gates=expand_atleast_gates,
|
|
261
|
+
expand_xor_gates=expand_xor_gates,
|
|
262
|
+
recognize_atleast_groups=recognize_atleast_groups,
|
|
263
|
+
recognize_xor_groups=recognize_xor_groups,
|
|
264
|
+
ccf=ccf,
|
|
265
|
+
keep_null_gates=keep_null_gates,
|
|
266
|
+
)
|
|
267
|
+
if num_trials is not None:
|
|
268
|
+
options["num_trials"] = num_trials
|
|
269
|
+
if requested_trials_per_pass is not None:
|
|
270
|
+
options["requested_trials_per_pass"] = requested_trials_per_pass
|
|
271
|
+
if model_id:
|
|
272
|
+
try:
|
|
273
|
+
path = resolve_model_xml_path(model_id)
|
|
274
|
+
except ValueError as exc:
|
|
275
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
276
|
+
except FileNotFoundError as exc:
|
|
277
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
278
|
+
return build_sample_shape_payload([str(path)], options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
279
|
+
return build_sample_shape_payload(config.input_files, options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
280
|
+
|
|
281
|
+
@router.get("/model/execution-plan")
|
|
282
|
+
def execution_plan_model(
|
|
283
|
+
model_id: str | None = Query(default=None),
|
|
284
|
+
pcfb_id: str | None = Query(default=None),
|
|
285
|
+
compilation_level: int | None = Query(default=None, ge=0, le=8),
|
|
286
|
+
expand_atleast_gates: bool | None = Query(default=None),
|
|
287
|
+
expand_xor_gates: bool | None = Query(default=None),
|
|
288
|
+
recognize_atleast_groups: bool | None = Query(default=None),
|
|
289
|
+
recognize_xor_groups: bool | None = Query(default=None),
|
|
290
|
+
ccf: bool | None = Query(default=None),
|
|
291
|
+
keep_null_gates: bool | None = Query(default=None),
|
|
292
|
+
num_trials: float | None = Query(default=None, ge=0),
|
|
293
|
+
requested_trials_per_pass: float | None = Query(default=None, ge=0),
|
|
294
|
+
) -> dict[str, Any]:
|
|
295
|
+
config = CalculationConfig.from_env()
|
|
296
|
+
options = _compile_options(
|
|
297
|
+
config.options,
|
|
298
|
+
compilation_level=compilation_level,
|
|
299
|
+
expand_atleast_gates=expand_atleast_gates,
|
|
300
|
+
expand_xor_gates=expand_xor_gates,
|
|
301
|
+
recognize_atleast_groups=recognize_atleast_groups,
|
|
302
|
+
recognize_xor_groups=recognize_xor_groups,
|
|
303
|
+
ccf=ccf,
|
|
304
|
+
keep_null_gates=keep_null_gates,
|
|
305
|
+
)
|
|
306
|
+
if num_trials is not None:
|
|
307
|
+
options["num_trials"] = num_trials
|
|
308
|
+
if requested_trials_per_pass is not None:
|
|
309
|
+
options["requested_trials_per_pass"] = requested_trials_per_pass
|
|
310
|
+
if model_id:
|
|
311
|
+
try:
|
|
312
|
+
path = resolve_model_xml_path(model_id)
|
|
313
|
+
except ValueError as exc:
|
|
314
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
315
|
+
except FileNotFoundError as exc:
|
|
316
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
317
|
+
return build_execution_plan_payload([str(path)], options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
318
|
+
return build_execution_plan_payload(config.input_files, options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
319
|
+
|
|
320
|
+
@router.get("/model/dag-bundle")
|
|
321
|
+
def dag_bundle_model(
|
|
322
|
+
model_id: str | None = Query(default=None),
|
|
323
|
+
pcfb_id: str | None = Query(default=None),
|
|
324
|
+
compilation_level: int | None = Query(default=None, ge=0, le=8),
|
|
325
|
+
expand_atleast_gates: bool | None = Query(default=None),
|
|
326
|
+
expand_xor_gates: bool | None = Query(default=None),
|
|
327
|
+
recognize_atleast_groups: bool | None = Query(default=None),
|
|
328
|
+
recognize_xor_groups: bool | None = Query(default=None),
|
|
329
|
+
ccf: bool | None = Query(default=None),
|
|
330
|
+
keep_null_gates: bool | None = Query(default=None),
|
|
331
|
+
) -> dict[str, Any]:
|
|
332
|
+
config = CalculationConfig.from_env()
|
|
333
|
+
options = _compile_options(
|
|
334
|
+
config.options,
|
|
335
|
+
compilation_level=compilation_level,
|
|
336
|
+
expand_atleast_gates=expand_atleast_gates,
|
|
337
|
+
expand_xor_gates=expand_xor_gates,
|
|
338
|
+
recognize_atleast_groups=recognize_atleast_groups,
|
|
339
|
+
recognize_xor_groups=recognize_xor_groups,
|
|
340
|
+
ccf=ccf,
|
|
341
|
+
keep_null_gates=keep_null_gates,
|
|
342
|
+
)
|
|
343
|
+
if model_id:
|
|
344
|
+
try:
|
|
345
|
+
path = resolve_model_xml_path(model_id)
|
|
346
|
+
except ValueError as exc:
|
|
347
|
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
348
|
+
except FileNotFoundError as exc:
|
|
349
|
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
350
|
+
return build_dag_bundle_payload([str(path)], options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
351
|
+
return build_dag_bundle_payload(config.input_files, options, pcfb_path=_pcfb_path_from_id(pcfb_id))
|
|
352
|
+
|
|
353
|
+
@router.post("/reset")
|
|
354
|
+
def reset_calculation(body: SamplingControlBody | None = Body(default=None)) -> dict[str, Any]:
|
|
355
|
+
return supervisor.restart(_calculation_config(body))
|
|
356
|
+
|
|
357
|
+
@router.post("/start")
|
|
358
|
+
def start_sampling(body: SamplingControlBody | None = Body(default=None)) -> dict[str, Any]:
|
|
359
|
+
return supervisor.start_sampling(_calculation_config(body))
|
|
360
|
+
|
|
361
|
+
@router.post("/stop")
|
|
362
|
+
def stop_sampling() -> dict[str, Any]:
|
|
363
|
+
return supervisor.stop_sampling()
|
|
364
|
+
|
|
365
|
+
@router.websocket("/ws")
|
|
366
|
+
async def websocket_stream(websocket: WebSocket) -> None:
|
|
367
|
+
replay = websocket.query_params.get("replay", "").lower() in {"1", "true", "yes", "on"}
|
|
368
|
+
await event_hub.connect(
|
|
369
|
+
websocket,
|
|
370
|
+
supervisor.state.snapshot(alive=supervisor.alive()),
|
|
371
|
+
replay=replay,
|
|
372
|
+
)
|
|
373
|
+
try:
|
|
374
|
+
while True:
|
|
375
|
+
await websocket.receive_text()
|
|
376
|
+
except WebSocketDisconnect:
|
|
377
|
+
event_hub.disconnect(websocket)
|
|
378
|
+
|
|
379
|
+
return router
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def _compile_options(
|
|
383
|
+
base: dict[str, Any],
|
|
384
|
+
*,
|
|
385
|
+
compilation_level: int | None,
|
|
386
|
+
expand_atleast_gates: bool | None,
|
|
387
|
+
expand_xor_gates: bool | None,
|
|
388
|
+
recognize_atleast_groups: bool | None,
|
|
389
|
+
recognize_xor_groups: bool | None,
|
|
390
|
+
ccf: bool | None,
|
|
391
|
+
keep_null_gates: bool | None,
|
|
392
|
+
) -> dict[str, Any]:
|
|
393
|
+
options = dict(base)
|
|
394
|
+
if compilation_level is not None:
|
|
395
|
+
options["compilation_level"] = compilation_level
|
|
396
|
+
if expand_atleast_gates is not None:
|
|
397
|
+
options["expand_atleast_gates"] = expand_atleast_gates
|
|
398
|
+
if expand_xor_gates is not None:
|
|
399
|
+
options["expand_xor_gates"] = expand_xor_gates
|
|
400
|
+
if recognize_atleast_groups is not None:
|
|
401
|
+
options["recognize_atleast_groups"] = recognize_atleast_groups
|
|
402
|
+
if recognize_xor_groups is not None:
|
|
403
|
+
options["recognize_xor_groups"] = recognize_xor_groups
|
|
404
|
+
if ccf is not None:
|
|
405
|
+
options["ccf"] = ccf
|
|
406
|
+
if keep_null_gates is not None:
|
|
407
|
+
options["keep_null_gates"] = keep_null_gates
|
|
408
|
+
return options
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
import time
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CalculationState:
|
|
9
|
+
def __init__(self) -> None:
|
|
10
|
+
self._lock = threading.Lock()
|
|
11
|
+
self.status = "not_started"
|
|
12
|
+
self.pid: int | None = None
|
|
13
|
+
self.started_at: float | None = None
|
|
14
|
+
self.finished_at: float | None = None
|
|
15
|
+
self.update_count = 0
|
|
16
|
+
self.last_update: dict[str, Any] | None = None
|
|
17
|
+
self.last_result: dict[str, Any] | None = None
|
|
18
|
+
self.last_error: str | None = None
|
|
19
|
+
|
|
20
|
+
def snapshot(self, alive: bool | None = None) -> dict[str, Any]:
|
|
21
|
+
with self._lock:
|
|
22
|
+
return {
|
|
23
|
+
"status": self.status,
|
|
24
|
+
"pid": self.pid,
|
|
25
|
+
"alive": alive,
|
|
26
|
+
"started_at": self.started_at,
|
|
27
|
+
"finished_at": self.finished_at,
|
|
28
|
+
"update_count": self.update_count,
|
|
29
|
+
"last_update": self.last_update,
|
|
30
|
+
"last_result": self.last_result,
|
|
31
|
+
"last_error": self.last_error,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def mark_started(self, pid: int) -> None:
|
|
35
|
+
with self._lock:
|
|
36
|
+
self.status = "running"
|
|
37
|
+
self.pid = pid
|
|
38
|
+
self.started_at = time.time()
|
|
39
|
+
self.finished_at = None
|
|
40
|
+
self.update_count = 0
|
|
41
|
+
self.last_update = None
|
|
42
|
+
self.last_result = None
|
|
43
|
+
self.last_error = None
|
|
44
|
+
|
|
45
|
+
def record_progress(self, update: dict[str, Any]) -> None:
|
|
46
|
+
with self._lock:
|
|
47
|
+
self.update_count += 1
|
|
48
|
+
self.last_update = update
|
|
49
|
+
|
|
50
|
+
def mark_complete(self, result: dict[str, Any]) -> None:
|
|
51
|
+
with self._lock:
|
|
52
|
+
self.status = "completed"
|
|
53
|
+
self.finished_at = time.time()
|
|
54
|
+
self.last_result = result
|
|
55
|
+
|
|
56
|
+
def mark_failed(self, error: str) -> None:
|
|
57
|
+
with self._lock:
|
|
58
|
+
self.status = "failed"
|
|
59
|
+
self.finished_at = time.time()
|
|
60
|
+
self.last_error = error
|
|
61
|
+
|
|
62
|
+
def mark_stopped(self) -> None:
|
|
63
|
+
with self._lock:
|
|
64
|
+
self.status = "stopped"
|
|
65
|
+
self.finished_at = time.time()
|
|
66
|
+
self.pid = None
|
|
67
|
+
self.last_error = None
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import multiprocessing as mp
|
|
4
|
+
import queue
|
|
5
|
+
import threading
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from fuzzybits_server.config import CalculationConfig
|
|
9
|
+
from fuzzybits_server.event_hub import EventHub
|
|
10
|
+
from fuzzybits_server.event_log import emit
|
|
11
|
+
from fuzzybits_server.fault_tree_payload import build_fault_tree_payload
|
|
12
|
+
from fuzzybits_server.state import CalculationState
|
|
13
|
+
from fuzzybits_server.worker import calculation_main
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CalculationSupervisor:
|
|
17
|
+
def __init__(self, event_hub: EventHub) -> None:
|
|
18
|
+
self._ctx = mp.get_context("spawn")
|
|
19
|
+
self._process: mp.Process | None = None
|
|
20
|
+
self._drainer: threading.Thread | None = None
|
|
21
|
+
self._generation = 0
|
|
22
|
+
self._restart_lock = threading.Lock()
|
|
23
|
+
self.state = CalculationState()
|
|
24
|
+
self._event_hub = event_hub
|
|
25
|
+
|
|
26
|
+
def start(self, config: CalculationConfig) -> None:
|
|
27
|
+
with self._restart_lock:
|
|
28
|
+
self._start_locked(config)
|
|
29
|
+
|
|
30
|
+
def _start_locked(self, config: CalculationConfig) -> None:
|
|
31
|
+
if self._process and self._process.is_alive():
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
events: mp.Queue = self._ctx.Queue()
|
|
35
|
+
self._generation += 1
|
|
36
|
+
generation = self._generation
|
|
37
|
+
self._process = self._ctx.Process(
|
|
38
|
+
target=calculation_main,
|
|
39
|
+
args=(config, events),
|
|
40
|
+
name="fuzzybits-calculation",
|
|
41
|
+
)
|
|
42
|
+
self._process.start()
|
|
43
|
+
process = self._process
|
|
44
|
+
self.state.mark_started(self._process.pid or -1)
|
|
45
|
+
emit("server_started_calculation", pid=self._process.pid, input_files=config.input_files)
|
|
46
|
+
self._event_hub.publish_from_thread({
|
|
47
|
+
"event": "server_started_calculation",
|
|
48
|
+
"pid": self._process.pid,
|
|
49
|
+
"input_files": config.input_files,
|
|
50
|
+
"options": config.options,
|
|
51
|
+
})
|
|
52
|
+
self._event_hub.publish_from_thread({
|
|
53
|
+
"event": "model_loaded",
|
|
54
|
+
"model": build_fault_tree_payload(config.input_files),
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
self._drainer = threading.Thread(
|
|
58
|
+
target=self._drain_events,
|
|
59
|
+
args=(generation, events, process),
|
|
60
|
+
daemon=True,
|
|
61
|
+
)
|
|
62
|
+
self._drainer.start()
|
|
63
|
+
|
|
64
|
+
def stop(self) -> None:
|
|
65
|
+
if self._process and self._process.is_alive():
|
|
66
|
+
self._process.terminate()
|
|
67
|
+
self._process.join(timeout=10)
|
|
68
|
+
if self._process.is_alive():
|
|
69
|
+
self._process.kill()
|
|
70
|
+
self._process.join(timeout=5)
|
|
71
|
+
|
|
72
|
+
def restart(self, config: CalculationConfig) -> dict[str, Any]:
|
|
73
|
+
if not self._restart_lock.acquire(blocking=False):
|
|
74
|
+
return {
|
|
75
|
+
"accepted": False,
|
|
76
|
+
"reason": "restart already in progress",
|
|
77
|
+
"status": self.state.snapshot(alive=self.alive()),
|
|
78
|
+
}
|
|
79
|
+
try:
|
|
80
|
+
self._generation += 1
|
|
81
|
+
self.stop()
|
|
82
|
+
self._event_hub.clear_replay()
|
|
83
|
+
self._event_hub.publish_from_thread({"event": "calculation_reset"})
|
|
84
|
+
self._start_locked(config)
|
|
85
|
+
return {
|
|
86
|
+
"accepted": True,
|
|
87
|
+
"status": self.state.snapshot(alive=self.alive()),
|
|
88
|
+
}
|
|
89
|
+
finally:
|
|
90
|
+
self._restart_lock.release()
|
|
91
|
+
|
|
92
|
+
def start_sampling(self, config: CalculationConfig) -> dict[str, Any]:
|
|
93
|
+
if not self._restart_lock.acquire(blocking=False):
|
|
94
|
+
return {
|
|
95
|
+
"accepted": False,
|
|
96
|
+
"reason": "control operation already in progress",
|
|
97
|
+
"status": self.state.snapshot(alive=self.alive()),
|
|
98
|
+
}
|
|
99
|
+
try:
|
|
100
|
+
if self._process and self._process.is_alive():
|
|
101
|
+
return {
|
|
102
|
+
"accepted": False,
|
|
103
|
+
"reason": "calculation already running",
|
|
104
|
+
"status": self.state.snapshot(alive=True),
|
|
105
|
+
}
|
|
106
|
+
self._generation += 1
|
|
107
|
+
self._event_hub.clear_replay()
|
|
108
|
+
self._event_hub.publish_from_thread({"event": "calculation_reset"})
|
|
109
|
+
self._start_locked(config)
|
|
110
|
+
return {
|
|
111
|
+
"accepted": True,
|
|
112
|
+
"status": self.state.snapshot(alive=self.alive()),
|
|
113
|
+
}
|
|
114
|
+
finally:
|
|
115
|
+
self._restart_lock.release()
|
|
116
|
+
|
|
117
|
+
def stop_sampling(self) -> dict[str, Any]:
|
|
118
|
+
if not self._restart_lock.acquire(blocking=False):
|
|
119
|
+
return {
|
|
120
|
+
"accepted": False,
|
|
121
|
+
"reason": "control operation already in progress",
|
|
122
|
+
"status": self.state.snapshot(alive=self.alive()),
|
|
123
|
+
}
|
|
124
|
+
try:
|
|
125
|
+
if not (self._process and self._process.is_alive()):
|
|
126
|
+
self.state.mark_stopped()
|
|
127
|
+
return {
|
|
128
|
+
"accepted": False,
|
|
129
|
+
"reason": "calculation is not running",
|
|
130
|
+
"status": self.state.snapshot(alive=False),
|
|
131
|
+
}
|
|
132
|
+
self._generation += 1
|
|
133
|
+
self.stop()
|
|
134
|
+
self.state.mark_stopped()
|
|
135
|
+
status = self.state.snapshot(alive=False)
|
|
136
|
+
self._event_hub.publish_from_thread({
|
|
137
|
+
"event": "calculation_stopped",
|
|
138
|
+
"status": status,
|
|
139
|
+
})
|
|
140
|
+
return {
|
|
141
|
+
"accepted": True,
|
|
142
|
+
"status": status,
|
|
143
|
+
}
|
|
144
|
+
finally:
|
|
145
|
+
self._restart_lock.release()
|
|
146
|
+
|
|
147
|
+
def alive(self) -> bool | None:
|
|
148
|
+
return self._process.is_alive() if self._process else None
|
|
149
|
+
|
|
150
|
+
def _drain_events(self, generation: int, events: mp.Queue, process: mp.Process) -> None:
|
|
151
|
+
while True:
|
|
152
|
+
if generation != self._generation:
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
if not process.is_alive() and events.empty():
|
|
156
|
+
if process.exitcode not in (0, None) and self.state.snapshot()["status"] == "running":
|
|
157
|
+
error = f"calculation process exited with code {process.exitcode}"
|
|
158
|
+
self.state.mark_failed(error)
|
|
159
|
+
self._event_hub.publish_from_thread({
|
|
160
|
+
"event": "calculation_failed",
|
|
161
|
+
"error": error,
|
|
162
|
+
})
|
|
163
|
+
return
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
event = events.get(timeout=0.2)
|
|
167
|
+
except queue.Empty:
|
|
168
|
+
continue
|
|
169
|
+
|
|
170
|
+
if generation != self._generation:
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
match event.get("type"):
|
|
174
|
+
case "progress":
|
|
175
|
+
self.state.record_progress(event["update"])
|
|
176
|
+
self._event_hub.publish_from_thread({
|
|
177
|
+
"event": "progress",
|
|
178
|
+
"sequence": event["sequence"],
|
|
179
|
+
"timestamp": event["timestamp"],
|
|
180
|
+
"update": event["update"],
|
|
181
|
+
})
|
|
182
|
+
case "completed":
|
|
183
|
+
self.state.mark_complete(event["result"])
|
|
184
|
+
self._event_hub.publish_from_thread({
|
|
185
|
+
"event": "calculation_completed",
|
|
186
|
+
"result": event["result"],
|
|
187
|
+
})
|
|
188
|
+
case "failed":
|
|
189
|
+
self.state.mark_failed(event["error"])
|
|
190
|
+
self._event_hub.publish_from_thread({
|
|
191
|
+
"event": "calculation_failed",
|
|
192
|
+
"error": event["error"],
|
|
193
|
+
})
|