Skip to content

rome.utils

Shared helpers for laying ROME state out inside a Dragon DDict.

Every ROME component reads and writes the same distributed dictionary, which is what lets a task on one node add training data that a training task on another node picks up. The one rule that makes that safe is:

**never read-modify-write a shared container.**

d = ddict["corpus"]; d[uid] = rec; ddict["corpus"] = d loses records the moment two nodes do it at once. So ROME gives every record, request and result its own key and rebuilds collections by scanning a key prefix — single key writes are atomic in a DDict, so nothing is ever clobbered.

Namespace is the small amount of glue that makes that layout pleasant to work with. It is a view, not a store: it holds no state of its own and several views over the same DDict compose freely.

SEP module-attribute

SEP = '|'

Separator between namespace segments. Unlikely to appear in a uid or a name.

MODEL_PATH_KEY module-attribute

MODEL_PATH_KEY = 'model_path'

Key holding the checkpoint the workflow should currently be using.

MODEL_VERSION_KEY module-attribute

MODEL_VERSION_KEY = 'model_version'

Key holding the monotonically increasing checkpoint version.

Stream tasks poll this to decide when to hot-swap weights.

Namespace

Namespace(ddict: MutableMapping, prefix: str = '')

Bases: MutableMapping

A prefixed view over a Dragon DDict (or any mapping).

Every operation goes through a handle owned by the calling thread — see thread_handle — so views may be shared freely across the manager and its stream workers.

Parameters:

  • ddict (MutableMapping) –

    The shared dictionary. Normally a dragon.data.ddict.DDict; any mapping works, which is what makes the components unit-testable.

  • prefix (str, default: '' ) –

    Prepended to every key this view touches.

Source code in rome/utils.py
def __init__(self, ddict: MutableMapping, prefix: str = ""):
    self._ddict = ddict
    self._prefix = prefix
    # Serialized once, here, rather than per access: serialize() is itself
    # a client operation, and calling it from many threads would be the
    # very race this is meant to avoid.
    self._serialized = ddict.serialize() if hasattr(ddict, "serialize") else None

ddict property

ddict: MutableMapping

The underlying shared dictionary.

namespace

namespace(*segments: Any) -> 'Namespace'

Derive a child view: ns.namespace("record") -> keys record|….

Source code in rome/utils.py
def namespace(self, *segments: Any) -> "Namespace":
    """Derive a child view: ``ns.namespace("record")`` -> keys ``record|…``."""
    suffix = SEP.join(str(s) for s in segments)
    child = Namespace.__new__(Namespace)
    child._ddict = self._ddict
    child._prefix = f"{self._prefix}{suffix}{SEP}"
    # Inherited rather than recomputed: serialize() is a client operation,
    # and child views are created on hot paths.
    child._serialized = self._serialized
    return child

get

get(key: str, default: Any = None) -> Any

Read key, returning default when absent.

Goes through __getitem__ rather than DDict.get so the behaviour is identical whichever mapping is underneath.

Source code in rome/utils.py
def get(self, key: str, default: Any = None) -> Any:
    """Read ``key``, returning ``default`` when absent.

    Goes through ``__getitem__`` rather than ``DDict.get`` so the behaviour
    is identical whichever mapping is underneath.
    """
    try:
        return self._target[self._full(key)]
    except (KeyError, TypeError):
        return default

pop

pop(key: str, default: Any = _MISSING) -> Any

Remove key and return its value.

This is ROME's claim protocol: whoever pops a key owns that item, so two stream replicas draining the same queue never process a request twice. Correctness rests on the underlying pop being atomic, which is why it delegates rather than doing read-then-delete — a Dragon DDict resolves the contention manager-side, and measurably: four threads racing over 120 keys claim 120 with zero duplicates.

The single-argument form is deliberate. dict.pop(k, default) returns the default while DDict.pop(k, default) raises DDictKeyError regardless, so asking for the default would behave differently on the two backings; catching KeyError (which DDictKeyError subclasses) behaves the same on both.

Source code in rome/utils.py
def pop(self, key: str, default: Any = _MISSING) -> Any:
    """Remove ``key`` and return its value.

    This is ROME's claim protocol: whoever pops a key owns that item, so
    two stream replicas draining the same queue never process a request
    twice. Correctness rests on the underlying pop being atomic, which is
    why it delegates rather than doing read-then-delete — a Dragon DDict
    resolves the contention manager-side, and measurably: four threads
    racing over 120 keys claim 120 with zero duplicates.

    The single-argument form is deliberate. ``dict.pop(k, default)``
    returns the default while ``DDict.pop(k, default)`` raises
    ``DDictKeyError`` regardless, so asking for the default would behave
    differently on the two backings; catching ``KeyError`` (which
    ``DDictKeyError`` subclasses) behaves the same on both.
    """
    try:
        return self._target.pop(self._full(key))
    except (KeyError, TypeError):
        if default is _MISSING:
            raise KeyError(key)
        return default

increment

increment(key: str, amount: int = 1) -> int

Bump an integer counter and return the new value.

Only safe when a single component owns the counter — ROME keeps to that (the training manager owns model_version, and so on).

Source code in rome/utils.py
def increment(self, key: str, amount: int = 1) -> int:
    """Bump an integer counter and return the new value.

    Only safe when a single component owns the counter — ROME keeps to
    that (the training manager owns ``model_version``, and so on).
    """
    new = int(self.get(key, 0)) + amount
    self[key] = new
    return new

keys

keys(prefix: str = '') -> List[str]

Namespace-relative keys, optionally filtered by a further prefix.

Source code in rome/utils.py
def keys(self, prefix: str = "") -> List[str]:  # type: ignore[override]
    """Namespace-relative keys, optionally filtered by a further prefix."""
    full_prefix = self._full(prefix)
    cut = len(self._prefix)
    return sorted(k[cut:] for k in self._raw_keys() if k.startswith(full_prefix))

items

items(prefix: str = '') -> List[Tuple[str, Any]]

(key, value) pairs under prefix, skipping keys that vanish mid-scan.

Source code in rome/utils.py
def items(self, prefix: str = "") -> List[Tuple[str, Any]]:  # type: ignore[override]
    """``(key, value)`` pairs under ``prefix``, skipping keys that vanish mid-scan."""
    out: List[Tuple[str, Any]] = []
    for key in self.keys(prefix):
        value = self.get(key, _MISSING)
        if value is not _MISSING:
            out.append((key, value))
    return out

drain

drain(prefix: str = '', limit: Optional[int] = None) -> List[Tuple[str, Any]]

Pop and return up to limit (key, value) pairs under prefix.

Source code in rome/utils.py
def drain(self, prefix: str = "", limit: Optional[int] = None) -> List[Tuple[str, Any]]:
    """Pop and return up to ``limit`` ``(key, value)`` pairs under ``prefix``."""
    out: List[Tuple[str, Any]] = []
    for key in self.keys(prefix):
        if limit is not None and len(out) >= limit:
            break
        value = self.pop(key, _MISSING)
        if value is not _MISSING:
            out.append((key, value))
    return out

clear

clear(prefix: str = '') -> int

Delete every key under prefix; returns how many were removed.

Source code in rome/utils.py
def clear(self, prefix: str = "") -> int:  # type: ignore[override]
    """Delete every key under ``prefix``; returns how many were removed."""
    return len(self.drain(prefix))

snapshot

snapshot(prefix: str = '') -> Dict[str, Any]

A plain dict copy of this view — handy when logging or debugging.

Source code in rome/utils.py
def snapshot(self, prefix: str = "") -> Dict[str, Any]:
    """A plain ``dict`` copy of this view — handy when logging or debugging."""
    return dict(self.items(prefix))

thread_handle

thread_handle(ddict: MutableMapping, serialized: Optional[str]) -> MutableMapping

Return a DDict client handle owned by the calling thread.

A Dragon DDict handle multiplexes an FLI channel and is not safe to share between threads: concurrent use corrupts the channel and surfaces as dragon.fli.DragonFLIEOT, usually from an unrelated later operation. It is not a hypothetical — ROME runs the manager's event loop and its asyncio.to_thread stream workers in one process against one dictionary, which reproduces it within a few hundred operations.

Dragon's own remedy is that each consumer attaches its own handle, so that is what this does, once per thread and cached. Attaching is cheap and the thread pool is bounded, so the cache stays small.

serialized is None means the backing is a plain mapping (the unit-test and single-process case), which is returned unchanged.

Source code in rome/utils.py
def thread_handle(ddict: MutableMapping, serialized: Optional[str]) -> MutableMapping:
    """Return a DDict client handle owned by the calling thread.

    A Dragon DDict handle multiplexes an FLI channel and is **not** safe to
    share between threads: concurrent use corrupts the channel and surfaces as
    ``dragon.fli.DragonFLIEOT``, usually from an unrelated later operation. It
    is not a hypothetical — ROME runs the manager's event loop and its
    ``asyncio.to_thread`` stream workers in one process against one dictionary,
    which reproduces it within a few hundred operations.

    Dragon's own remedy is that each consumer attaches its own handle, so that
    is what this does, once per thread and cached. Attaching is cheap and the
    thread pool is bounded, so the cache stays small.

    ``serialized is None`` means the backing is a plain mapping (the unit-test
    and single-process case), which is returned unchanged.
    """
    if serialized is None:
        return ddict
    cache = getattr(_thread_local, "handles", None)
    if cache is None:
        cache = _thread_local.handles = {}
    handle = cache.get(serialized)
    if handle is None:
        handle = type(ddict).attach(serialized)
        cache[serialized] = handle
    return handle

submit_task

submit_task(asyncflow, func, *args, task_description: Optional[Dict[str, Any]] = None, service: bool = False, executable: bool = False, **kwargs)

Submit func to the workflow engine as a task.

ROME schedules nothing itself — asyncflow is the execution backend, and this is the single place ROME talks to it.

Parameters:

  • asyncflow (WorkflowEngine) –

    The engine the host workflow handed to ROME.

  • func (coroutine function) –

    The task body. asyncflow only accepts async def. For a function task a blocking trainer or inference call belongs inside an asyncio.to_thread; for an executable task func instead returns the shell command string to run (the same shape IMPRESS uses).

  • task_description (dict, default: None ) –

    Backend-specific resource request (ranks, GPUs, placement policy, …). Passed straight through — ROME does not interpret it, because the keys depend on which execution backend the workflow is running.

  • service (bool, default: False ) –

    Mark the task as a long-running service. ROME's inference and reward streams are services; a training round is not. Ignored for executable tasks.

  • executable (bool, default: False ) –

    Submit as an executable (command) task rather than a function task. func's return value is then the command line to run on the backend, not a Python result. This is how a training round runs as its own process — see rome.train.base.TrainTask.as_command.

Returns:

  • Future –

    Resolves when the task completes (to its return value for a function task; to the backend's execution result for an executable one).

Source code in rome/utils.py
def submit_task(
    asyncflow,
    func,
    *args,
    task_description: Optional[Dict[str, Any]] = None,
    service: bool = False,
    executable: bool = False,
    **kwargs,
):
    """Submit ``func`` to the workflow engine as a task.

    ROME schedules nothing itself — asyncflow is the execution backend, and
    this is the single place ROME talks to it.

    Parameters
    ----------
    asyncflow : WorkflowEngine
        The engine the host workflow handed to ROME.
    func : coroutine function
        The task body. asyncflow only accepts ``async def``. For a *function*
        task a blocking trainer or inference call belongs inside an
        ``asyncio.to_thread``; for an *executable* task ``func`` instead returns
        the shell command string to run (the same shape IMPRESS uses).
    task_description : dict, optional
        Backend-specific resource request (ranks, GPUs, placement policy, …).
        Passed straight through — ROME does not interpret it, because the
        keys depend on which execution backend the workflow is running.
    service : bool
        Mark the task as a long-running service. ROME's inference and reward
        streams are services; a training round is not. Ignored for executable
        tasks.
    executable : bool
        Submit as an executable (command) task rather than a function task.
        ``func``'s return value is then the command line to run on the backend,
        not a Python result. This is how a training round runs as its own
        process — see :meth:`rome.train.base.TrainTask.as_command`.

    Returns
    -------
    asyncio.Future
        Resolves when the task completes (to its return value for a function
        task; to the backend's execution result for an executable one).
    """
    if executable:
        decorated = asyncflow.executable_task()(func)
    else:
        decorated = asyncflow.function_task(service=service)(func)
    return decorated(*args, task_description=task_description or {}, **kwargs)

resource_description

resource_description(gpus: int = 0, nodes: int = 1, extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]

Build a default task_description from ROME's coarse resource fields.

The concrete keys an execution backend understands are its own business, so this only fills in the two that the RADICAL backends share, and anything a workflow passes in extra wins. Workflows on an exotic backend should set task_description explicitly on the config and ignore gpus/nodes.

Source code in rome/utils.py
def resource_description(
    gpus: int = 0, nodes: int = 1, extra: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
    """Build a default ``task_description`` from ROME's coarse resource fields.

    The concrete keys an execution backend understands are its own business, so
    this only fills in the two that the RADICAL backends share, and anything a
    workflow passes in ``extra`` wins. Workflows on an exotic backend should set
    ``task_description`` explicitly on the config and ignore ``gpus``/``nodes``.
    """
    desc: Dict[str, Any] = {}
    if nodes and nodes != 1:
        desc["ranks"] = nodes
    if gpus:
        desc["gpus_per_rank"] = gpus
    if extra:
        desc.update(extra)
    return desc