Skip to content

Runs and records

runner

Running experiments and storing their records under their hashes.

Each point of an experiment is one run, stored as a record in the project's artifact store (ArtifactStore, namespace experiment) under the point's hash and the engine's identity. Running an experiment again returns the stored record without computing anything: the record is the constructive trace of the point, keyed by everything that determines it. A changed input changes the point hash, so the point runs again. The computation itself goes through FitCache, so a run or fit another experiment (or qvr run --cache) computed is reused too.

Every data file is read and checked against its pin on every invocation, including a cached one: a file whose content no longer matches its pin is refused rather than silently answered from the store.

A record holds the experiment's inputs (entry, content hashes, data digests, values, method, seed, links, handlers, and the source files with their Software Heritage identifiers) and its outputs: for a run, the value and the trace; for a fit, the posterior draws and their summaries.

RECORD_FORMAT module-attribute

RECORD_FORMAT = 'qvr-experiment-record/v1'

The format of an experiment record.

RECORD_NAMESPACE module-attribute

RECORD_NAMESPACE = 'experiment'

The artifact-store namespace experiment records are kept in.

PointRun didactic-model

PointRun(*, point: ExperimentPoint, key: str, hit: bool, record: JsonValue = field(opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

The record of one point of an experiment.

PARAMETER DESCRIPTION
point

The point.

TYPE: ExperimentPoint

key

The record's store key: the point hash with the engine identity.

TYPE: str

hit

Whether the record came from the store.

TYPE: bool

record

The record as mutable JSON data, compared by identity.

TYPE: JsonValue DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

record_key

record_key(point_hash: str, engine: str) -> str

The store key of a point's record.

PARAMETER DESCRIPTION
point_hash

The point hash.

TYPE: str

engine

The engine identity digest.

TYPE: str

RETURNS DESCRIPTION
str

The digest of both: a record is reused only by the engine that computed it.

Source code in src/quivers/experiments/runner.py
def record_key(point_hash: str, engine: str) -> str:
    """The store key of a point's record.

    Parameters
    ----------
    point_hash : str
        The point hash.
    engine : str
        The engine identity digest.

    Returns
    -------
    str
        The digest of both: a record is reused only by the engine that
        computed it.
    """
    return tagged_digest("experiment-record", [point_hash, engine])

read_data

read_data(plan: ExperimentPlan) -> dict[str, JsonValue]

Read and check every data file of an experiment.

PARAMETER DESCRIPTION
plan

The experiment.

TYPE: ExperimentPlan

RETURNS DESCRIPTION
dict[str, JsonValue]

Each data clause's value, by the name it is given to.

RAISES DESCRIPTION
ExperimentError

If a file cannot be read or does not match its pin.

Source code in src/quivers/experiments/runner.py
def read_data(plan: ExperimentPlan) -> dict[str, JsonValue]:
    """Read and check every data file of an experiment.

    Parameters
    ----------
    plan : ExperimentPlan
        The experiment.

    Returns
    -------
    dict[str, JsonValue]
        Each data clause's value, by the name it is given to.

    Raises
    ------
    ExperimentError
        If a file cannot be read or does not match its pin.
    """
    values: dict[str, JsonValue] = {}
    for item in plan.data:
        _, value = read_pinned(
            Path(item.path),
            item.pin,
            origin=plan.file,
            line=item.line,
            column=item.column,
        )
        values[item.name] = value
    return values

stored_records

stored_records(plan: ExperimentPlan, *, store: ArtifactStore | None = None, engine: str | None = None) -> tuple[PointRun | None, ...]

The stored records of an experiment's points, without running any.

PARAMETER DESCRIPTION
plan

The experiment.

TYPE: ExperimentPlan

store

Where records are kept; the project's store when None.

TYPE: ArtifactStore | None DEFAULT: None

engine

The engine identity; engine_identity when None.

TYPE: str | None DEFAULT: None

RETURNS DESCRIPTION
tuple[PointRun | None, ...]

One entry per point, None for a point with no stored record.

RAISES DESCRIPTION
StoreError

If a stored record is corrupt.

Source code in src/quivers/experiments/runner.py
def stored_records(
    plan: ExperimentPlan,
    *,
    store: ArtifactStore | None = None,
    engine: str | None = None,
) -> tuple[PointRun | None, ...]:
    """The stored records of an experiment's points, without running any.

    Parameters
    ----------
    plan : ExperimentPlan
        The experiment.
    store : ArtifactStore | None
        Where records are kept; the project's store when None.
    engine : str | None
        The engine identity; `engine_identity` when None.

    Returns
    -------
    tuple[PointRun | None, ...]
        One entry per point, None for a point with no stored record.

    Raises
    ------
    StoreError
        If a stored record is corrupt.
    """
    artifacts = _store(store, plan)
    identity = engine_identity() if engine is None else engine
    found: list[PointRun | None] = []
    for point in plan.points:
        key = record_key(point.hash, identity)
        stored = artifacts.get(RECORD_NAMESPACE, key)
        found.append(
            None
            if stored is None
            else PointRun(point=point, key=key, hit=True, record=json.loads(stored))
        )
    return tuple(found)

run_experiment

run_experiment(plan: ExperimentPlan, *, store: ArtifactStore | None = None, engine: str | None = None, rerun: bool = False) -> tuple[PointRun, ...]

Run every point of an experiment, reusing stored records.

PARAMETER DESCRIPTION
plan

The experiment.

TYPE: ExperimentPlan

store

Where records are kept; the project's store when None.

TYPE: ArtifactStore | None DEFAULT: None

engine

The engine identity; engine_identity when None.

TYPE: str | None DEFAULT: None

rerun

Compute every point even when its record is stored, and replace the record.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[PointRun, ...]

One record per point, in sweep order.

RAISES DESCRIPTION
ExperimentError

If a data file does not match its pin or cannot be read.

ExecutionFailure

If a run fails on the reference machine.

FitCacheError

If a run or fit has no content identity.

StoreError

If a stored record is corrupt.

Source code in src/quivers/experiments/runner.py
def run_experiment(
    plan: ExperimentPlan,
    *,
    store: ArtifactStore | None = None,
    engine: str | None = None,
    rerun: bool = False,
) -> tuple[PointRun, ...]:
    """Run every point of an experiment, reusing stored records.

    Parameters
    ----------
    plan : ExperimentPlan
        The experiment.
    store : ArtifactStore | None
        Where records are kept; the project's store when None.
    engine : str | None
        The engine identity; `engine_identity` when None.
    rerun : bool
        Compute every point even when its record is stored, and replace the
        record.

    Returns
    -------
    tuple[PointRun, ...]
        One record per point, in sweep order.

    Raises
    ------
    ExperimentError
        If a data file does not match its pin or cannot be read.
    ExecutionFailure
        If a run fails on the reference machine.
    FitCacheError
        If a run or fit has no content identity.
    StoreError
        If a stored record is corrupt.
    """
    artifacts = _store(store, plan)
    identity = engine_identity() if engine is None else engine
    data = read_data(plan)
    cache = FitCache(artifacts, engine=identity)
    runs: list[PointRun] = []
    for point in plan.points:
        key = record_key(point.hash, identity)
        stored = None if rerun else artifacts.get(RECORD_NAMESPACE, key)
        if stored is not None:
            runs.append(
                PointRun(point=point, key=key, hit=True, record=json.loads(stored))
            )
            continue
        given = {**data, **_values(plan, point)}
        started = datetime.now(UTC)
        clock = time.perf_counter()
        before = len(cache.log)
        outputs = (
            _run_outputs(plan, given, cache)
            if plan.kind == "run"
            else _fit_outputs(plan, given, cache)
        )
        ended = datetime.now(UTC)
        log = [
            f"{event.kind} {event.key}: {'reused' if event.hit else 'computed'}"
            for event in cache.log[before:]
        ]
        log.append(f"{plan.method} took {time.perf_counter() - clock:.3f} s")
        record = _record(plan, point, identity, outputs, started, ended, log)
        artifacts.put(
            RECORD_NAMESPACE,
            key,
            json.dumps(record, sort_keys=True, allow_nan=False).encode("utf-8"),
            label=f"experiment {plan.name} {point.label}".rstrip(),
        )
        runs.append(PointRun(point=point, key=key, hit=False, record=record))
    return tuple(runs)