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,
piis 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
funwith abstract scalar coordinates carrying that same interface. The loops are represented by oneScancontaining nestedVMapoperations; 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,
initmust be supplied as a keyword argument.Each call to
funmust return exactly oneir.RVcreated during that call. The result must depend onprevious. 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 coercedinit, which need not be this interface wheninitis already an RV.After coercion, the shape of
initmust match one complete state assembled fromfun. Forfun(previous, t, i, j), letSbe the shape returned by one call tofun. Witht=T,i=I, andj=J,initmust 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;
sfordoes not slice it along the parallel axes.- Parameters:
fun (Callable[..., ir.RV] | None) – Step function, or
Nonein 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 namedinitorinterface. 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
funover 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
funis supplied, anir.RVcontaining every state produced by the scan, not including the coercedinit. If the sequential size isT,result[t]is the state produced at coordinatetandresult.shape == (T,) + init.shape. Whenfunis omitted, a decorator that accepts it and returns such anir.RV. The result has the coerced initial state’s interface; it is not required to carry this interface.- Raises:
TypeError – If
funis not callable or lacks the required signature, a size is not an integer, orfundoes not return exactly oneir.RV.ValueError – If a parameter is named
initorinterface, the function has no sequential coordinate, sizes are missing, unknown, or nonpositive, the result does not depend onprevious, an intermediate Op is random, or the mapped state does not match the shape or interface of the coercedinit, 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