pangolin.interface.Interface.sfor

Contents

pangolin.interface.Interface.sfor#

Interface.sfor(fun: Callable[[RV, RV], RV], /, *, init: RVLike, **sizes: int) → RV[source]#
Interface.sfor(fun: Callable[[RV, RV, RV], RV], /, *, init: RVLike, **sizes: int) → RV
Interface.sfor(fun: Callable[[RV, RV, RV, RV], RV], /, *, init: RVLike, **sizes: int) → RV
Interface.sfor(fun: Callable[[RV, RV, RV, RV, RV], RV], /, *, init: RVLike, **sizes: int) → RV
Interface.sfor(fun: Callable[[ir.RV, ir.RV, ir.RV, ir.RV, ir.RV, ir.RV, *Rest], ir.RV], /, *, init: RVLike, **sizes: int) → RV
Interface.sfor(fun: None = None, /, *, init: RVLike, **sizes: int) → SForDecorator

Build a state sequence with named sequential and parallel coordinates.

In the examples below, pi is this interface. This combinator constructs the result directly. A scalar call of the form:

x = pi.sfor(fun, init=init, t=T)

has these semantics:

previous = init if isinstance(init, ir.RV) else pi.constant(init)
states = []
for t in range(T):
    t_rv = pi.constant(t)
    next_state = fun(previous, t_rv)
    states.append(next_state)
    previous = next_state
x = stack(states)

For example, if:

fun = lambda previous, t: normal(previous, 1)

then:

x[0] ~ normal(init, 1)
x[t] ~ normal(x[t-1], 1), t > 0

A call with parallel coordinates:

x = pi.sfor(fun, init=init, t=T, i=I, j=J)

has these semantics:

previous = init if isinstance(init, ir.RV) else pi.constant(init)
states = []
for t in range(T):
    t_rv = pi.constant(t)
    next_state = empty((I, J))
    for i in range(I):
        i_rv = pi.constant(i)
        for j in range(J):
            j_rv = pi.constant(j)
            next_state[i, j] = fun(previous, t_rv, i_rv, j_rv)
    states.append(next_state)
    previous = next_state
x = stack(states)

This pseudocode describes the meaning of the loops, not the graph constructed internally. Internally, each coordinate range is a constant RV array with this interface, and the combinators trace fun with abstract scalar coordinates carrying that same interface. The loops are represented by one Scan containing nested VMap operations; they are not executed in Python. Function-parameter order determines the nesting, while keyword order has no effect.

For example, if:

fun = lambda previous, t, i, j: normal(a[i, j] * previous[i, j] + b[t, i, j], c[i, j])

then:

x[0, i, j] ~ normal(a[i, j] * init[i, j] + b[0, i, j], c[i, j])
x[t, i, j] ~ normal(a[i, j] * x[t-1, i, j] + b[t, i, j], c[i, j]), t > 0

Finally, decorator form:

@pi.sfor(init=init, t=T)
def x(previous, t):
    return normal(previous, 1)

is equivalent to:

def fun(previous, t):
    return normal(previous, 1)

x = pi.sfor(fun, init=init, t=T)

In both forms, init must be supplied as a keyword argument.

Each call to fun must return exactly one ir.RV created during that call. The result must depend on previous. Any other RVs created during the call that contribute to the result must have deterministic Ops. The assembled state must have the same interface as the coerced init, which need not be this interface when init is already an RV.

After coercion, the shape of init must match one complete state assembled from fun. For fun(previous, t, i, j), let S be the shape returned by one call to fun. With t=T, i=I, and j=J, init must have shape (I, J, *S), and the result has shape (T, I, J, *S). Equivalently, result.shape == (T,) + init.shape. The output axes appear in function-parameter order; keyword order is ignored.

Each parallel call receives the complete previous state; sfor does not slice it along the parallel axes.

Parameters:
  • fun (Callable[..., ir.RV] | None) – Step function, or None in decorator form. It must have a fixed positional signature with at least two parameters and no defaults, variadic parameters, or keyword-only parameters. The first parameter receives the complete previous state; the second receives the scalar sequential coordinate; any later parameters receive scalar parallel coordinates. No parameter may be named init or interface. The function must satisfy the return, carry-dependence, determinism, shape, and interface requirements above.

  • init (RVLike) – Keyword-only complete state before the first step. An existing RV is preserved, including its interface; other values become constant RVs owned by this interface. The resulting RV’s shape and interface must match the state assembled by mapping fun over every parallel coordinate.

  • **sizes (int) – Exactly one positive integer for every coordinate parameter: the sequential parameter and all parallel parameters. Names must match the function parameters. Values determine axis lengths; function-parameter order, not keyword order, determines axis order.

Returns:

result_or_decorator (ir.RV | decorator) – When fun is supplied, an ir.RV containing every state produced by the scan, not including the coerced init. If the sequential size is T, result[t] is the state produced at coordinate t and result.shape == (T,) + init.shape. When fun is omitted, a decorator that accepts it and returns such an ir.RV. The result has the coerced initial state’s interface; it is not required to carry this interface.

Raises:
  • TypeError – If fun is not callable or lacks the required signature, a size is not an integer, or fun does not return exactly one ir.RV.

  • ValueError – If a parameter is named init or interface, the function has no sequential coordinate, sizes are missing, unknown, or nonpositive, the result does not depend on previous, an intermediate Op is random, or the mapped state does not match the shape or interface of the coerced init, or a non-RV initial value is not a valid constant.

Return type:

RV | SForDecorator

Examples

>>> from pangolin.interface import Interface
>>> pi = Interface()
>>> walk = pi.sfor(lambda previous, t: pi.normal(previous, 1), init=0, t=4)
>>> walk.shape
(4,)
>>> walk.interface is pi
True

The initial RV and output can belong to a different interface:

>>> other = Interface()
>>> start = other.constant(0)
>>> walk = pi.sfor(lambda previous, t: other.normal(previous, 1), init=start, t=3)
>>> walk.interface is other
True