pangolin.interface.ir_code#
- pangolin.interface.ir_code(fun, namespace=None)#
Return a function that displays a specialized IR graph as Python-like code.
Every supplied input leaf must be an RV. Each occurrence becomes a fresh abstract input with the same shape and interface before calling
funonce. The returned string shows a function building the resulting graph using bare IR names, with no imports or prefixes. Intermediate assignments preserve node sharing, and the return statement preserves the output’s list, tuple, or dictionary structure.- Parameters:
fun (Callable[[P], PyTree[RV]]) – Graph-building function with RV-only input and output trees. Bind fixed Python arguments through a closure or
functools.partialbefore inspection.namespace (Mapping[str, object] | None) – Optional mapping such as
locals()containing every captured RV. Captures appear as free references, such asscaleorparameters['scale'], not function parameters. The first reference whose root name does not conflict with a parameter, the displayed function name, or an IR name is used.
- Returns:
code_for – Function returning the displayed code as a string without printing it.
- Raises:
TypeError – If any supplied input or returned leaf is not an RV.
ValueError – If a captured RV has no unambiguous reference in
namespace.NotImplementedError – If an input or output uses a custom PyTree rather than built-in lists, tuples, or dicts.
- Return type:
Callable[[P], str]
Notes
This describes one traced specialization, not arbitrary Python control flow. Omitted defaults remain fixed during tracing. Captured RVs, including RV defaults, remain free references. Input ancestry is not copied. Interfaces are used during tracing but omitted from the displayed code.
Input names come from the function signature when available; otherwise
*argsand**kwargsare used. Lambdas use the namemodel. Ops use their existingrepr, which may abbreviate constants. This is an inspection tool, not an executable serialization of the graph.Examples
Inspect the precise broadcasting behavior of built-in functions:
>>> from pangolin import interface as pi >>> print(ir_code(pi.normal)(pi.constant(0.0), pi.constant(1.0))) def normal(mu, sigma): a = RV(Normal(), mu, sigma) return a >>> print(ir_code(pi.normal)(pi.constant([0.0, 1.0]), pi.constant(1.0))) def normal(mu, sigma): a = RV(VMap(Normal(), [0, None], 2), mu, sigma) return a
Inspect a pairwise squared-distance matrix built with two named
vforindices:>>> def distances(x): ... return pi.vfor( ... lambda i, j: pi.sum((x[i, :] - x[j, :]) ** 2, axis=0), ... i=x.shape[0], ... j=x.shape[0], ... ) >>> print(ir_code(distances)(pi.constant([[0., 0.], [1., 2.], [3., 1.]]))) def distances(x): a = RV(VMap(VMap(VMap(Sub(), [0, 0], 2), [None, 0], 3), [0, None], 3), x, x) b = RV(Constant(2)) c = RV(VMap(VMap(VMap(Pow(), [0, None], 2), [0, None], 3), [0, None], 3), a, b) d = RV(VMap(VMap(Sum(axis=0), [0], 3), [0], 3), c) return d
Input and output PyTree structure remains visible, including repeated references to an RV:
>>> def model(parameters): ... x = pi.normal(parameters['mean'], parameters['scales'][0]) ... return {'draw': x, 'derived': (pi.exp(x), x)} >>> print(ir_code(model)({'mean': pi.constant(0.0), 'scales': [pi.constant(1.0)]})) def model(parameters): a = RV(Normal(), parameters['mean'], parameters['scales'][0]) b = RV(Exp(), a) return {'draw': a, 'derived': (b, a)}
Captured RVs are referenced through the supplied namespace:
>>> scale = pi.exponential(1.0) >>> def model(mean): ... return pi.normal(mean, scale) >>> print(ir_code(model, locals())(pi.constant(0.0))) def model(mean): a = RV(Normal(), mean, scale) return a
Non-RV inputs are rejected even if the original function accepts them: RV inputs are required so they can be replaced with symbolic inputs during tracing.
>>> pi.normal(0.0, 1.0) RV(Normal(), RV(Constant(0.)), RV(Constant(1.))) >>> ir_code(pi.normal)(0.0, 1.0) Traceback (most recent call last): ... TypeError: ir_code inputs must be a PyTree whose leaves are RV; found float