pangolin.interface.Interface.vfor#
- Interface.vfor(fun: Callable[[], Output], /, **sizes: int) Output[source]#
- Interface.vfor(fun: Callable[[ir.RV], Output], /, **sizes: int) Output
- Interface.vfor(fun: Callable[[ir.RV, ir.RV], Output], /, **sizes: int) Output
- Interface.vfor(fun: Callable[[ir.RV, ir.RV, ir.RV], Output], /, **sizes: int) Output
- Interface.vfor(fun: Callable[[ir.RV, ir.RV, ir.RV, ir.RV], Output], /, **sizes: int) Output
- Interface.vfor(fun: Callable[[ir.RV, ir.RV, ir.RV, ir.RV, ir.RV], Output], /, **sizes: int) Output
- Interface.vfor(fun: Callable[[ir.RV, ir.RV, ir.RV, ir.RV, ir.RV, ir.RV, *Rest], Output], /, **sizes: int) Output
- Interface.vfor(fun: None = None, /, **sizes: int) VForDecorator
Vectorize a function over named integer ranges.
funmust have a fixed positional signature. Each parameter receives a scalarir.RVrepresenting an integer coordinate from zero through one less than its named size.funmust return a PyTree of newly createdir.RVobjects; it cannot return a coordinate or an existing RV captured through a closure. With no coordinate parameters,funis called directly and its RV outputs need not be newly created.Axis sizes are matched to function parameters by name. Output axes occur in function-parameter order, regardless of the order in which keyword arguments are supplied.
Coordinate RVs carry this interface. Output RVs retain the interfaces chosen by
fun; they are not required to carry this interface.This combinator supports direct calls, decorator factories, and multiline decorator syntax.
- Parameters:
fun (Callable[..., PyTree[ir.RV]] | None) – Function to vectorize, or
Nonewhen creating a decorator. It must have a fixed positional signature, accept one scalarir.RVcoordinate per parameter, and return a PyTree of newly createdir.RVobjects. No parameter may be namedinterface.**sizes (int) – Positive integer size for every parameter of
fun, supplied under the corresponding parameter name.
- Returns:
output_or_decorator (PyTree[ir.RV] | decorator) – The vectorized output PyTree when
funis supplied, otherwise a decorator that acceptsfunand produces that output.- Raises:
TypeError – If
funis not callable, has no fixed positional signature, a size is not an integer, or the output is not a PyTree ofir.RVobjects.ValueError – If an axis size is missing, an unknown axis name is supplied, a size is not positive, a parameter is named
interface, or an output RV was not created byfun.
- Return type:
Output | VForDecorator
Notes
Static type checking preserves the number of coordinate arguments and exact PyTree structure of the output, and requires the first six arguments to be
ir.RVobjects. Runtime validation still applies to every argument.Examples
Direct call:
>>> from pangolin import interface as pi >>> x = pi.vfor(lambda i, j: 10 * i + j, i=2, j=3) >>> x.shape (2, 3)
Keyword order does not affect axis order:
>>> y = pi.vfor(lambda i, j: 10 * i + j, j=3, i=2) >>> y.shape (2, 3) >>> ir.rv_equal(x, y) True
Decorator factory:
>>> make_x = pi.vfor(i=2, j=3) >>> x = make_x(lambda i, j: 10 * i + j) >>> x.shape (2, 3)
Multiline decorator form:
>>> @pi.vfor(i=2, j=3) ... def x(i, j): ... return 10 * i + j >>> x.shape (2, 3)
Every function parameter must have a named size:
>>> pi.vfor(lambda i, j: i + j, i=2) Traceback (most recent call last): ... ValueError: vfor missing size for axis 'j'
Unknown axis names are rejected:
>>> pi.vfor(lambda i, j: i + j, i=2, j=3, k=4) Traceback (most recent call last): ... ValueError: vfor got size for unknown axis 'k'; expected ('i', 'j')
Python defaults are not interpreted as axis sizes:
>>> pi.vfor(lambda i=3: i + 1) Traceback (most recent call last): ... TypeError: <lambda> has no fixed positional signature: 'i' has a default