rome.stream¶
Stream Manager — persistent asynchronous inference and reward streams.
Inference and reward are not one-shot calls in ROME; they are long-lived tasks that sit on their nodes and keep processing requests for the whole campaign. The stream manager owns them: it submits them to asyncflow as services, feeds them requests from anywhere in the host workflow, collects their outputs, and — the part that closes ROME's loop — hot-swaps their weights whenever the training manager publishes a newer checkpoint.
The workflow supplies the actual inference and reward code. ROME supplies the loop around it::
await rome.stream.start(StreamConfig(
name="generate",
kind=StreamKind.INFERENCE,
load_func=lambda path, ctx: load_my_model(path),
process_func=lambda prompts, ctx: ctx.model.generate(prompts),
num_streams=4,
num_gpus=1,
))
A workflow that would rather own its own loop passes stream_func instead
and gets the same context object, including the reload signal.
REQUEST_NS
module-attribute
¶
Sub-namespace holding one key per pending request, inside a group's DDict.
STATUS_NS
module-attribute
¶
Sub-namespace holding one key per replica's status.
DEFAULT_STREAM_DDICT_KWARGS
module-attribute
¶
DEFAULT_STREAM_DDICT_KWARGS: Dict[str, Any] = {'managers_per_node': 1, 'n_nodes': 1, 'total_mem': 256 * 1024 ** 2}
Defaults for a stream group's DDict.
Much smaller than the manager's, because a group only ever holds requests in
flight and results not yet collected -- it does not accumulate. Override per
group via StreamConfig.ddict_kwargs.
StreamStatus
¶
Bases: Enum
Status of a single inference/reward stream task.
StreamKind
¶
Bases: str, Enum
What a stream is for.
Both kinds run the same loop; the distinction is for routing and reporting. It matters because reward streams are the ones whose outputs feed the data manager.
StreamConfig
dataclass
¶
StreamConfig(name: str = 'default', kind: StreamKind = INFERENCE, process_func: Optional[Callable] = None, load_func: Optional[Callable] = None, stream_func: Optional[Callable] = None, model_path: Optional[str] = None, num_streams: int = 1, num_gpus: int = 1, num_nodes: int = 1, task_description: Optional[Dict[str, Any]] = None, as_service: bool = True, batch_size: int = 1, poll_interval: float = 0.1, auto_reload: bool = True, drain_on_stop: bool = True, on_output: Optional[Callable[[Dict[str, Any]], None]] = None, load_kwargs: Dict[str, Any] = dict(), ddict: Any = None, ddict_kwargs: Optional[Dict[str, Any]] = None)
Configuration for a group of identical stream tasks.
Parameters:
-
name(str, default:'default') –Identifies the stream group. Requests are routed by name, so a workflow can run several independent groups side by side.
-
kind(StreamKind, default:INFERENCE) –INFERENCEorREWARD. -
process_func(Callable, default:None) –The workflow's per-batch work:
(inputs, ctx) -> results, whereinputsis a list of payloads andresultsis a list of the same length. May be sync or async. A plain(inputs)function is accepted too, so existing code needs no signature change. -
load_func(Optional[Callable], default:None) –(model_path, ctx) -> model, called at startup and on every reload. The result becomesctx.model. Omit for streams that need no model (most reward functions). -
stream_func(Optional[Callable], default:None) –Full override:
(ctx) -> None, replacing the managed loop. The implementation must honourctx.should_stop()and callctx.maybe_reload()between batches.process_funcis ignored. -
model_path(Optional[str], default:None) –Initial checkpoint. When
Nonethe stream starts from whatever the training manager has already published, if anything. -
num_streams(int, default:1) –How many identical tasks to run for this group.
-
num_gpus(int, default:1) –Per-task resources, turned into an asyncflow
task_description. -
num_nodes(int, default:1) –Per-task resources, turned into an asyncflow
task_description. -
task_description(Optional[dict], default:None) –Backend-specific resource request, passed through to asyncflow verbatim. Overrides
num_gpus/num_nodeswhen set. -
as_service(bool, default:True) –Submit as an asyncflow service task (default). Streams outlive individual workflow stages, which is exactly what services are for.
-
batch_size(int, default:1) –Maximum requests handed to
process_funcin one call. -
poll_interval(float, default:0.1) –Seconds to idle when the request queue is empty.
-
auto_reload(bool, default:True) –Watch the published model version and reload without being asked — "the model is reloaded when the Stream Manager sees that a new model checkpoint is available". Turn off to reload only on an explicit
Stream.reload_model. -
drain_on_stop(bool, default:True) –Finish already-queued requests before stopping.
-
on_output(Optional[Callable[[dict], None]], default:None) –Called with each output record as it is produced. The manager uses this to pipe reward-stream results into the data manager.
-
load_kwargs(dict, default:dict()) –Extra keyword arguments for
load_func. -
ddict(DDict, default:None) –Dictionary backing this group's request and result queues. When
Nonethe stream manager allocates one and destroys it on stop. -
ddict_kwargs(dict, default:None) –Passed to
DDict(...)when the manager allocates the group's dictionary. Defaults toDEFAULT_STREAM_DDICT_KWARGS.
StreamContext
¶
StreamTask
¶
StreamTask(index: int, config: StreamConfig, ddict: Namespace, root: Namespace, task_fut: Any = None)
One persistent inference or reward stream task.
Holds the asyncflow future for the running task plus its control signals. Status lives in the DDict rather than on the object, so a manager on another node can read it.
Source code in rome/stream.py
ddict
instance-attribute
¶
This stream group's slice of the DDict (requests, results, status).
claim
¶
Pop pending requests for this task.
Popping is what makes a request owned: each key is removed by exactly one claimer, so replicas never double-process a request.
Source code in rome/stream.py
maybe_reload
¶
Load the newest published checkpoint into this task, if there is one.
Source code in rome/stream.py
Stream
¶
Stream(ddict: Namespace, asyncflow: Any)
Stream manager: owns every inference and reward stream in a run.
Each stream group gets its own DDict for requests and results, rather than sharing the manager's. A dictionary has no server-side prefix query, so a scan lists every key in it and filters client-side; sharing one dictionary would make the cost of a replica's poll grow with the corpus, coupling two rates that have nothing to do with each other. Per group, a scan covers only that group's in-flight work, which does not accumulate.
The manager's own dictionary is still used, read-only from here, for the published checkpoint — that is what the streams reload from.
Parameters:
-
ddict(Namespace) –ROME's shared state, holding the published checkpoint.
-
asyncflow(WorkflowEngine) –Engine the stream tasks are submitted to, as services.
Source code in rome/stream.py
start
async
¶
start(config: StreamConfig) -> List[StreamTask]
Start config.num_streams persistent tasks for one stream group.
Call it once per group — typically an inference group and a reward group — against the same manager.
Source code in rome/stream.py
submit
¶
Queue one request and return its id.
Requests round-robin across the group's replicas, so every task gets an even share without needing a shared claim protocol.
Source code in rome/stream.py
submit_batch
¶
get_outputs
¶
Collect finished results, oldest first.
Parameters:
-
stream(str, default:None) –Which group to read. Defaults to the only started group.
-
consume(bool, default:True) –Remove results as they are read (default). Pass
Falseto peek without draining — useful for progress displays.
Source code in rome/stream.py
get_output
async
¶
get_output(request_id: str, stream: Optional[str] = None, timeout: Optional[float] = None, poll_interval: float = 0.05) -> Optional[Dict[str, Any]]
Await one specific request's result.
Returns None on timeout rather than raising, so a caller polling
several requests can keep going.
Source code in rome/stream.py
pending
¶
signal_reload
¶
signal_reload(model_path: Optional[str] = None, stream: Optional[str] = None) -> List[StreamTask]
Raise the reload flag on the targeted tasks without waiting.
Each task swaps weights between batches on its own schedule, so this never interrupts an in-flight inference call.
Source code in rome/stream.py
reload_model
async
¶
reload_model(model_path: Optional[str] = None, stream: Optional[str] = None, wait_for_reload: bool = True, timeout: float = 300.0) -> None
Tell the streams to pick up new weights.
Called explicitly by the workflow, or implicitly by on_checkpoint
when the training manager publishes. model_path=None means "reload
whatever is currently published".
Source code in rome/stream.py
on_checkpoint
¶
Training-manager hook: a new checkpoint just landed.
Registered by Manager, so publishing a checkpoint
and hot-swapping the streams are the same event. Non-blocking by design.
Source code in rome/stream.py
get_status
¶
get_status(stream: Optional[str] = None) -> List[StreamStatus]
Status of every task, or of one group's tasks.
report
¶
Per-group summary for dashboards and logs.
Source code in rome/stream.py
stop
async
¶
Signal the stream tasks to stop, optionally waiting for them to finish.
Stopping does not release the group dictionaries — results drained on
the way down stay readable, which is the whole point of
drain_on_stop. Call close once those have been collected.
Source code in rome/stream.py
close
¶
Release the dictionaries this manager allocated for its groups.
Separate from stop so a workflow can collect what the streams
drained on the way down before their storage goes away. Idempotent.
Source code in rome/stream.py
run_stream_task
async
¶
The persistent loop: load a model, then process until told to stop.