Commit dfaba22
Ref-based object store with dict trace and three-layer architecture (#24)
* Implement ref-based data passing between actors
Convert data passing to use Ray ObjectRefs throughout, eliminating
unnecessary serialization through the driver. Key changes:
- NodeWrapper.sample() now returns List[Dict[str, ObjectRef]] with
internal chunking based on ray.chunk_size config
- Add _simulate(), _sample_posterior(), _sample_proposal() as internal
methods for actual sampling logic
- Add _convert_rvbatch_to_refs() helper for RVBatch to refs conversion
- MultiplexNodeWrapper updated for ref-based multiplexed sampling
- DeployedGraph gains _resolve_refs_to_trace(), _merge_refs(),
_refs_to_arrays() helpers for graph traversal with refs
- DatasetManagerActor.append_refs() stores ObjectRefs directly
- Add _dump_refs() for lazy disk dumps when store_fraction > 0
- Fix resampling bug by merging proposal_refs with sample_refs
This enables zero-copy data flow between simulation and buffer actors,
supports heterogeneous sample shapes, and makes the buffer shape-agnostic.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Simplify internal sampling interface with (value, logprob) tuples
Replace RVBatch wrapper handling with explicit tuple returns:
- _simulate() now returns (value, logprob) tuple
- _sample_posterior() returns (value, logprob) tuple
- _sample_proposal() returns (value, logprob) tuple
- Rename _convert_rvbatch_to_refs() to _batch_to_refs(value, logprob)
This removes hasattr checks and as_rvbatch() calls, making the
internal interface explicit. RVBatch is still used by estimators
internally, but the boundary is now a simple tuple.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Replace RVBatch with dict-based trace and ref-only outer layer
Three-layer architecture: outer (DeployedGraph/store) passes only
ObjectRefs, NodeWrapper handles ref↔array boundary with batched
ray.get/put, inner (estimators/simulators) sees pure arrays/tensors.
- Remove RVBatch/RV classes entirely
- Estimators return {'value': arr, 'log_prob': arr} dicts
- Flat store keys: theta.value, theta.log_prob (not bare theta)
- NodeWrapper: unified _chunked_sample, _resolve_refs (batched
ray.get), _batch_to_refs (phased puts), condition_refs interface
- MultiplexNodeWrapper: factored _multiplexed_call, slices ref lists
- DeployedGraph: ref-only _execute_graph (no double serialization),
_arrays_to_condition_refs, _extract_value_refs, batched _refs_to_arrays
- Fix cli.py sample_mode for new ref-based return format
- Fix read-only array warning from Ray zero-copy returns
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Remove unnecessary pause/resume during resimulation
sample_proposal interleaves with training via async yield points
(await asyncio.sleep(0) in the training loop). Forward simulation
runs on separate actors concurrently with training. No need to
pause training during the entire resimulation cycle.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Optimize ref resolution: broadcast detection and threaded ray.put
_resolve_refs now detects when all refs are identical (broadcast case,
e.g. repeated observations) and resolves once with np.broadcast_to
instead of fetching N copies. Reduces resolve_refs from ~280ms to ~1ms
for broadcast observations.
_batch_to_refs uses ThreadPoolExecutor(4) for ray.put calls, giving
~1.3x speedup for small arrays and ~4x for large arrays (GIL released
during serialization).
_to_tensor handles read-only numpy arrays (from np.broadcast_to) by
copying before torch.from_numpy conversion.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Avoid materializing broadcast arrays in _to_tensor
Use np.asarray (no-op) instead of np.array (100MB copy) for read-only
broadcast arrays from _resolve_refs. The PyTorch non-writable warning
is harmless since .to(device) always creates a writable copy.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Defer broadcast expansion and background data fetching in core
Lazy broadcast: _resolve_refs now returns compact (1,...) arrays for
broadcast conditions, deferring np.broadcast_to to the point of use in
_simulate. Reduces memory allocation during chunked sampling.
Background fetch: CachedDataLoader.sync() offloads ray.get to a
background thread so the training loop is not blocked. New data is
applied one epoch later; first sync blocks to ensure initial data.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Move np.stack + tensor conversion into background fetch thread
_apply_fetch was taking ~4s per sync (1024 samples) because it ran
np.stack + _to_tensor per-sample on the main thread, blocking GPU
training. Now _checkout_and_fetch does the full pipeline in the
background thread (checkout_refs, ray.get, np.stack, torch.as_tensor),
returning ready-to-use tensors. _apply_fetch on the main thread only
does fast batched scatter/cat (~0.2s). Also makes sync() truly
non-blocking by checking .done() before applying.
Measured: _apply_fetch 4.0s → 0.2s, epoch time 14.2s → 10s.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Fix dtype mismatch in _apply_fetch scatter assignment
Background thread's torch.as_tensor may produce different precision
(e.g., Float vs Double) than the existing cache arrays. Cast new
tensors to match the destination dtype before indexed assignment.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Remove dead code and fix missing joblib import
Cleanup:
- Remove unused call_simulator_method, call_estimator_method (NodeWrapper)
- Remove duplicate shutdown() stub in NodeWrapper
- Remove unused shutdown() in DatasetManagerActor
- Remove unused load_observations() and its imports from utils.py
- Remove leftover debug log in _merge_refs
- Simplify _execute_graph: deduplicate parent loop in conditional
Bugfix:
- Add missing joblib import in raystore.py (used by load_initial_samples)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Suppress Ray blocking ray.get warning in async training loop
buffer.get_stats() does a synchronous ray.get to fetch buffer statistics.
This triggers a Ray warning about blocking in async actors. The call is
fast (<1ms) and only happens once per epoch, so the blocking is negligible.
Added warnings.catch_warnings context manager to suppress this specific
warning at the two call sites in stepwise_estimator.py, with TODO noting
potential future improvements (async get_stats or background caching).
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Suppress Ray 'blocking ray.get inside async actor' warning
Set Ray's internal blocking_get_inside_async_warned flag to True
in NodeWrapper.__init__ to prevent the warning from being logged.
This warning is unavoidable without splitting async training actors
from synchronous sampling actors.
Also removes ineffective warnings.filterwarnings context managers
from stepwise_estimator.py that were previously added but didn't
work (Ray uses logging.warning, not warnings.warn).
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 260d871 commit dfaba22
File tree
8 files changed
+583
-353
lines changed- examples/05_linear_regression
- falcon
- contrib
- core
8 files changed
+583
-353
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| 124 | + | |
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
411 | 411 | | |
412 | 412 | | |
413 | 413 | | |
414 | | - | |
| 414 | + | |
415 | 415 | | |
416 | 416 | | |
417 | | - | |
418 | 417 | | |
419 | | - | |
| 418 | + | |
420 | 419 | | |
421 | 420 | | |
422 | | - | |
423 | | - | |
424 | 421 | | |
425 | | - | |
| 422 | + | |
426 | 423 | | |
427 | 424 | | |
428 | 425 | | |
429 | 426 | | |
430 | | - | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
431 | 433 | | |
432 | | - | |
433 | | - | |
| 434 | + | |
| 435 | + | |
434 | 436 | | |
435 | 437 | | |
436 | 438 | | |
437 | | - | |
| 439 | + | |
| 440 | + | |
438 | 441 | | |
439 | 442 | | |
440 | | - | |
| 443 | + | |
441 | 444 | | |
442 | 445 | | |
443 | 446 | | |
444 | 447 | | |
445 | | - | |
| 448 | + | |
446 | 449 | | |
447 | 450 | | |
448 | 451 | | |
449 | | - | |
| 452 | + | |
450 | 453 | | |
451 | 454 | | |
452 | | - | |
453 | | - | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
454 | 462 | | |
455 | 463 | | |
456 | 464 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
| |||
228 | 227 | | |
229 | 228 | | |
230 | 229 | | |
231 | | - | |
232 | | - | |
| 230 | + | |
| 231 | + | |
233 | 232 | | |
234 | | - | |
| 233 | + | |
235 | 234 | | |
236 | 235 | | |
237 | 236 | | |
| |||
345 | 344 | | |
346 | 345 | | |
347 | 346 | | |
348 | | - | |
| 347 | + | |
349 | 348 | | |
350 | 349 | | |
351 | 350 | | |
352 | 351 | | |
353 | 352 | | |
354 | 353 | | |
355 | | - | |
356 | | - | |
| 354 | + | |
| 355 | + | |
357 | 356 | | |
358 | 357 | | |
359 | 358 | | |
360 | 359 | | |
361 | 360 | | |
362 | | - | |
| 361 | + | |
363 | 362 | | |
364 | 363 | | |
365 | 364 | | |
366 | 365 | | |
367 | 366 | | |
368 | 367 | | |
369 | | - | |
| 368 | + | |
370 | 369 | | |
371 | 370 | | |
372 | 371 | | |
373 | 372 | | |
374 | 373 | | |
375 | | - | |
| 374 | + | |
376 | 375 | | |
377 | 376 | | |
378 | 377 | | |
| |||
393 | 392 | | |
394 | 393 | | |
395 | 394 | | |
396 | | - | |
| 395 | + | |
397 | 396 | | |
398 | 397 | | |
399 | 398 | | |
| |||
405 | 404 | | |
406 | 405 | | |
407 | 406 | | |
408 | | - | |
409 | | - | |
| 407 | + | |
| 408 | + | |
410 | 409 | | |
411 | 410 | | |
412 | 411 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| |||
117 | 116 | | |
118 | 117 | | |
119 | 118 | | |
120 | | - | |
| 119 | + | |
121 | 120 | | |
122 | 121 | | |
123 | 122 | | |
| |||
185 | 184 | | |
186 | 185 | | |
187 | 186 | | |
188 | | - | |
| 187 | + | |
| 188 | + | |
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
| |||
430 | 430 | | |
431 | 431 | | |
432 | 432 | | |
433 | | - | |
| 433 | + | |
434 | 434 | | |
435 | | - | |
| 435 | + | |
436 | 436 | | |
437 | 437 | | |
438 | 438 | | |
| |||
471 | 471 | | |
472 | 472 | | |
473 | 473 | | |
474 | | - | |
475 | | - | |
| 474 | + | |
| 475 | + | |
476 | 476 | | |
477 | | - | |
478 | | - | |
| 477 | + | |
| 478 | + | |
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
604 | | - | |
| 604 | + | |
605 | 605 | | |
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
609 | | - | |
610 | | - | |
| 609 | + | |
| 610 | + | |
611 | 611 | | |
612 | | - | |
| 612 | + | |
613 | 613 | | |
614 | 614 | | |
615 | 615 | | |
616 | 616 | | |
617 | 617 | | |
618 | 618 | | |
619 | 619 | | |
620 | | - | |
| 620 | + | |
621 | 621 | | |
622 | 622 | | |
623 | 623 | | |
624 | 624 | | |
625 | 625 | | |
626 | 626 | | |
627 | 627 | | |
628 | | - | |
| 628 | + | |
629 | 629 | | |
630 | 630 | | |
631 | 631 | | |
632 | 632 | | |
633 | 633 | | |
634 | | - | |
| 634 | + | |
635 | 635 | | |
636 | | - | |
| 636 | + | |
637 | 637 | | |
638 | 638 | | |
639 | 639 | | |
640 | | - | |
| 640 | + | |
641 | 641 | | |
642 | 642 | | |
643 | 643 | | |
644 | | - | |
645 | | - | |
646 | | - | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| |||
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
| 21 | + | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
35 | 34 | | |
36 | 35 | | |
37 | 36 | | |
38 | | - | |
| 37 | + | |
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
| |||
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
47 | | - | |
| 46 | + | |
48 | 47 | | |
49 | 48 | | |
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | | - | |
| 53 | + | |
55 | 54 | | |
56 | 55 | | |
57 | 56 | | |
| |||
60 | 59 | | |
61 | 60 | | |
62 | 61 | | |
63 | | - | |
| 62 | + | |
64 | 63 | | |
65 | 64 | | |
66 | 65 | | |
67 | 66 | | |
68 | 67 | | |
69 | 68 | | |
70 | | - | |
| 69 | + | |
71 | 70 | | |
72 | 71 | | |
73 | 72 | | |
| |||
76 | 75 | | |
77 | 76 | | |
78 | 77 | | |
79 | | - | |
| 78 | + | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
| |||
0 commit comments