Skip to content

Program encoding

The kernel encoding of a MonadicProgram.

program_module

The kernel encoding of a MonadicProgram.

A MonadicProgram is a sequence of host steps: a draw from a torch morphism at an input built from earlier bindings, a deterministic binding computed by a host closure, or a score contributed by one. The kernel has no term for a host function, and does not need one: each step's host part is an instance of the prelude's Compute interface, performed on the values it reads (the arguments of a draw, the names a closure declares, or every value bound so far when it declares none), and each draw is the canonical Random.sample request on the sampleable the host part returns.

The computation this module builds therefore makes the program's data flow and effect structure explicit while every tensor operation stays in the host, behind a handler a runtime provider installs. The user's effect handlers are lexical handlers of the same random and score instances, so conditioning, intervention, replay, reweighting, and tracing compose by the kernel's own rules.

HostStep dataclass

HostStep(instance: EffectInstanceId, effect: EffectRef, kind: str, spec: Step | None, environment: tuple[str, ...], answer_type: TypeExpr, site: str | None = None)

One host part of the program, as the runtime provider serves it.

PARAMETER DESCRIPTION
instance

The Compute instance the step performs on.

TYPE: EffectInstanceId

effect

The applied Compute[X, A] interface of the instance.

TYPE: EffectRef

kind

"input" for the program's parameter split, "draw" for a morphism applied to its input, "apply" for a categorical morphism's tensor contracted against its input, "bind" for a destructuring split, "let" for a deterministic binding, "score" for a scored binding.

TYPE: str

spec

The program's step record, or None for the input split.

TYPE: Step | None

environment

The names of the locals the step reads, in the order the environment tuple carries them.

TYPE: tuple[str, ...]

answer_type

The type the host part returns.

TYPE: TypeExpr

site

The site label of a draw step, else None.

TYPE: str | None DEFAULT: None

ProgramKernel dataclass

ProgramKernel(module: QiecModule, program: MonadicProgram, steps: tuple[HostStep, ...], sites: tuple[str, ...], site_types: dict[str, TypeExpr], data: tuple[str, ...], random: NamedEffectInstance, score: NamedEffectInstance, param: NamedEffectInstance, result_type: TypeExpr, locals: dict[str, Local] = dict())

A program's computation with the record the runtime needs to serve it.

PARAMETER DESCRIPTION
module

The kernel module: the interfaces, the instances, and the entry computation, without handlers; a run adds its own.

TYPE: QiecModule

program

The program encoded.

TYPE: MonadicProgram

steps

The host steps, in body order.

TYPE: tuple[HostStep, ...]

sites

The site label of every draw, in order.

TYPE: tuple[str, ...]

site_types

The value type of each site, by label.

TYPE: dict[str, TypeExpr]

data

The names of the host data inputs after the program's input, in parameter order.

TYPE: tuple[str, ...]

random

The canonical Random instance.

TYPE: NamedEffectInstance

score

The canonical Score instance.

TYPE: NamedEffectInstance

param

The canonical Param instance.

TYPE: NamedEffectInstance

result_type

The entry computation's result type.

TYPE: TypeExpr

space_type

space_type(space: SetObject | ContinuousSpace) -> TypeExpr

The kernel type of a value of a space.

PARAMETER DESCRIPTION
space

A program's domain, codomain, or a morphism's codomain.

TYPE: SetObject | ContinuousSpace

RETURNS DESCRIPTION
TypeExpr

Int for a finite set, Tensor[Real]([dim]) for a continuous space of that dimension, and the product of the component types for a product space.

Source code in src/quivers/effects/program_module.py
def space_type(space: SetObject | ContinuousSpace) -> TypeExpr:
    """The kernel type of a value of a space.

    Parameters
    ----------
    space : SetObject | ContinuousSpace
        A program's domain, codomain, or a morphism's codomain.

    Returns
    -------
    TypeExpr
        ``Int`` for a finite set, ``Tensor[Real]([dim])`` for a
        continuous space of that dimension, and the product of the
        component types for a product space.
    """
    if isinstance(space, ProductSpace):
        return product_type(*(space_type(component) for component in space.components))
    if isinstance(space, ContinuousSpace):
        return tensor_type(REAL, (IndexLiteral(int(space.dim), NAT),))
    return INT

program_kernel

program_kernel(program: MonadicProgram, data: Sequence[str] = ()) -> ProgramKernel

Encode a program as a kernel module.

PARAMETER DESCRIPTION
program

The program.

TYPE: MonadicProgram

data

The names of the host data inputs a run supplies beside the program's input, in parameter order.

TYPE: Sequence[str] DEFAULT: ()

RETURNS DESCRIPTION
ProgramKernel

The module and the record the runtime serves it by.

Source code in src/quivers/effects/program_module.py
def program_kernel(program: MonadicProgram, data: Sequence[str] = ()) -> ProgramKernel:
    """Encode a program as a kernel module.

    Parameters
    ----------
    program : MonadicProgram
        The program.
    data : Sequence[str]
        The names of the host data inputs a run supplies beside the
        program's input, in parameter order.

    Returns
    -------
    ProgramKernel
        The module and the record the runtime serves it by.
    """
    return _Builder(program, data).build()