Funsors¶
Basic Funsors¶
-
class
Funsor(inputs, output, fresh=None, bound=None)[source]¶ Bases:
objectAbstract base class for immutable functional tensors.
Concrete derived classes must implement
__init__()methods taking hashable*argsand no optional**kwargsso as to support cons hashing.Derived classes with
.freshvariables must implement aneager_subs()method. Derived classes with.boundvariables must implement an_alpha_convert()method.Parameters: - inputs (OrderedDict) – A mapping from input name to domain. This can be viewed as a typed context or a mapping from free variables to domains.
- output (Domain) – An output domain.
-
dtype¶
-
shape¶
-
requires_grad¶
-
approximate(op, guide, approx_vars=None)[source]¶ Approximate wrt and all or a subset of inputs.
Parameters:
-
sample(sampled_vars, sample_inputs=None, rng_key=None)[source]¶ Create a Monte Carlo approximation to this funsor by replacing functions of
sampled_varswithDeltas.The result is a
Funsorwith the same.inputsand.outputas the original funsor (plussample_inputsif provided), so that self can be replaced by the sample in expectation computations:y = x.sample(sampled_vars) assert y.inputs == x.inputs assert y.output == x.output exact = (x.exp() * integrand).reduce(ops.add) approx = (y.exp() * integrand).reduce(ops.add)
If
sample_inputsis provided, this creates a batch of samples.Parameters: - sampled_vars (str, Variable, or set or frozenset thereof.) – A set of input variables to sample.
- sample_inputs (OrderedDict) – An optional mapping from variable
name to
Domainover which samples will be batched. - rng_key (None or JAX's random.PRNGKey) – a PRNG state to be used by JAX backend to generate random samples
-
align(names)[source]¶ Align this funsor to match given
names. This is mainly useful in preparation for extracting.dataof afunsor.tensor.Tensor.Parameters: names (tuple) – A tuple of strings representing all names but in a new order. Returns: A permuted funsor equivalent to self. Return type: Funsor
-
eager_subs(subs)[source]¶ Internal substitution function. This relies on the user-facing
__call__()method to coerce non-Funsors to Funsors. Once all inputs are Funsors,eager_subs()implementations can recurse to callSubs.
-
to_funsor(x, output=None, dim_to_name=None, **kwargs)[source]¶ Convert to a
Funsor. OnlyFunsors and scalars are accepted.Parameters: - x – An object.
- output (funsor.domains.Domain) – An optional output hint.
- dim_to_name (OrderedDict) – An optional mapping from negative batch dimensions to name strings.
Returns: A Funsor equivalent to
x.Return type: Raises: ValueError
-
to_data(x, name_to_dim=None, **kwargs)[source]¶ Extract a python object from a
Funsor.Raises a
ValueErrorif free variables remain or if the funsor is lazy.Parameters: - x – An object, possibly a
Funsor. - name_to_dim (OrderedDict) – An optional inputs hint.
Returns: A non-funsor equivalent to
x.Raises: ValueError if any free variables remain.
Raises: PatternMissingError if funsor is not fully evaluated.
- x – An object, possibly a
-
class
Variable(name, output)[source]¶ Bases:
funsor.terms.FunsorFunsor representing a single free variable.
Parameters: - name (str) – A variable name.
- output (funsor.domains.Domain) – A domain.
-
class
Subs(arg, subs)[source]¶ Bases:
funsor.terms.FunsorLazy substitution of the form
x(u=y, v=z).Parameters: - arg (Funsor) – A funsor being substituted into.
- subs (tuple) – A tuple of
(name, value)pairs, wherenameis a string andvaluecan be coerced to aFunsorviato_funsor().
-
class
Unary(op, arg)[source]¶ Bases:
funsor.terms.FunsorLazy unary operation.
Parameters: - op (Op) – A unary operator.
- arg (Funsor) – An argument.
-
class
Binary(op, lhs, rhs)[source]¶ Bases:
funsor.terms.FunsorLazy binary operation.
Parameters:
-
class
Reduce(op, arg, reduced_vars)[source]¶ Bases:
funsor.terms.FunsorLazy reduction over multiple variables.
The user-facing interface is the
Funsor.reduce()method.Parameters: - op (AssociativeOp) – An associative operator.
- arg (funsor) – An argument to be reduced.
- reduced_vars (frozenset) – A set of variables over which to reduce.
-
class
Scatter(op, subs, source, reduced_vars)[source]¶ Bases:
funsor.terms.FunsorTranspose of structurally linear
Subs, followed byReduce.For injective scatter operations this should satisfy the equation:
if destin = Scatter(op, subs, source, frozenset()) then source = Subs(destin, subs)
The
reduced_varsis merely for computational efficiency, and could always be split out into a separate.reduce(). For example in the following equation, the left hand side uses much less memory than the right hand side:Scatter(op, subs, source, reduced_vars) == Scatter(op, subs, source, frozenset()).reduce(op, reduced_vars)
Warning
This is currently implemented only for injective scatter operations. In particular, this does not allow accumulation behavior like scatter-add.
Note
Scatter(ops.add, ...)is the funsor analog ofnumpy.add.at()ortorch.index_put()orjax.lax.scatter_add(). For injective substitutions,Scatter(ops.add, ...)is roughly equivalent to the tensor operation:result = zeros(...) # since zero is the additive unit result[subs] = source
Parameters:
-
class
Approximate(op, model, guide, approx_vars)[source]¶ Bases:
funsor.terms.FunsorInterpretation-specific approximation wrt a set of variables.
The default eager interpretation should be exact. The user-facing interface is the
Funsor.approximate()method.Parameters:
-
class
Number(data, dtype=None)[source]¶ Bases:
funsor.terms.FunsorFunsor backed by a Python number.
Parameters: - data (numbers.Number) – A python number.
- dtype – A nonnegative integer or the string “real”.
-
class
Slice(name, start, stop, step, dtype)[source]¶ Bases:
funsor.terms.FunsorSymbolic representation of a Python
sliceobject.Parameters:
-
class
Stack(name, parts)[source]¶ Bases:
funsor.terms.FunsorStack of funsors along a new input dimension.
Parameters:
-
class
Cat(name, parts, part_name=None)[source]¶ Bases:
funsor.terms.FunsorConcatenate funsors along an existing input dimension.
Parameters:
-
class
Lambda(var, expr)[source]¶ Bases:
funsor.terms.FunsorLazy inverse to
ops.getitem.This is useful to simulate higher-order functions of integers by representing those functions as arrays.
Parameters: - var (Variable) – A variable to bind.
- expr (funsor) – A funsor.
-
class
Independent(fn, reals_var, bint_var, diag_var)[source]¶ Bases:
funsor.terms.FunsorCreates an independent diagonal distribution.
This is equivalent to substitution followed by reduction:
f = ... # a batched distribution assert f.inputs['x_i'] == Reals[4, 5] assert f.inputs['i'] == Bint[3] g = Independent(f, 'x', 'i', 'x_i') assert g.inputs['x'] == Reals[3, 4, 5] assert 'x_i' not in g.inputs assert 'i' not in g.inputs x = Variable('x', Reals[3, 4, 5]) g == f(x_i=x['i']).reduce(ops.add, 'i')
Parameters:
Delta¶
-
solve(expr, value)[source]¶ Tries to solve for free inputs of an
exprsuch thatexpr == value, and computes the log-abs-det-Jacobian of the resulting substitution.Parameters: Returns: A tuple
(name, point, log_abs_det_jacobian)Return type: Raises: ValueError
-
class
Delta(terms)[source]¶ Bases:
funsor.terms.FunsorNormalized delta distribution binding multiple variables.
There are three syntaxes supported for constructing Deltas:
Delta(((name1, (point1, log_density1)), (name2, (point2, log_density2)), (name3, (point3, log_density3))))
or for a single name:
Delta(name, point, log_density)
or for default
log_density == 0:Delta(name, point)
Parameters: terms (tuple) – A tuple of tuples of the form (name, (point, log_density)).
Tensor¶
-
class
Tensor(data, inputs=None, dtype='real')[source]¶ Bases:
funsor.terms.FunsorFunsor backed by a PyTorch Tensor or a NumPy ndarray.
This follows the
torch.distributionsconvention of arranging named “batch” dimensions on the left and remaining “event” dimensions on the right. The output shape is determined by all remaining dims. For example:data = torch.zeros(5,4,3,2) x = Tensor(data, {"i": Bint[5], "j": Bint[4]}) assert x.output == Reals[3, 2]
Operators like
matmuland.sum()operate only on the output shape, and will not change the named inputs.Parameters: -
requires_grad¶
-
new_arange(name, *args, **kwargs)[source]¶ Helper to create a named
torch.arange()ornp.arange()funsor. In some cases this can be replaced by a symbolicSlice.Parameters: Return type:
-
-
align_tensor(new_inputs, x, expand=False)[source]¶ Permute and add dims to a tensor to match desired
new_inputs.Parameters: - new_inputs (OrderedDict) – A target set of inputs.
- x (funsor.terms.Funsor) – A
TensororNumber. - expand (bool) – If False (default), set result size to 1 for any input
of
xnot innew_inputs; if True expand tonew_inputssize.
Returns: a number or
torch.Tensorornp.ndarraythat can be broadcast to other tensors with inputsnew_inputs.Return type: int or float or torch.Tensor or np.ndarray
-
align_tensors(*args, **kwargs)[source]¶ Permute multiple tensors before applying a broadcasted op.
This is mainly useful for implementing eager funsor operations.
Parameters: - *args (funsor.terms.Funsor) – Multiple
Tensors andNumbers. - expand (bool) – Whether to expand input tensors. Defaults to False.
Returns: a pair
(inputs, tensors)where tensors are alltorch.Tensors ornp.ndarrays that can be broadcast together to a single data with giveninputs.Return type: - *args (funsor.terms.Funsor) – Multiple
-
class
Function(fn, output, args)[source]¶ Bases:
funsor.terms.FunsorFunsor wrapped by a native PyTorch or NumPy function.
Functions are assumed to support broadcasting and can be eagerly evaluated on funsors with free variables of int type (i.e. batch dimensions).
Functions are usually created via thefunction()decorator.Parameters:
-
function(*signature)[source]¶ Decorator to wrap a PyTorch/NumPy function, using either type hints or explicit type annotations.
Example:
# Using type hints: @funsor.tensor.function def matmul(x: Reals[3, 4], y: Reals[4, 5]) -> Reals[3, 5]: return torch.matmul(x, y) # Using explicit type annotations: @funsor.tensor.function(Reals[3, 4], Reals[4, 5], Reals[3, 5]) def matmul(x, y): return torch.matmul(x, y) @funsor.tensor.function(Reals[10], Reals[10, 10], Reals[10], Real) def mvn_log_prob(loc, scale_tril, x): d = torch.distributions.MultivariateNormal(loc, scale_tril) return d.log_prob(x)
To support functions that output nested tuples of tensors, specify a nested
Tupleof output types, for example:@funsor.tensor.function def max_and_argmax(x: Reals[8]) -> Tuple[Real, Bint[8]]: return torch.max(x, dim=-1)
Parameters: *signature – A sequence if input domains followed by a final output domain or nested tuple of output domains.
-
Einsum(equation, *operands)[source]¶ Wrapper around
torch.einsum()ornp.einsum()to operate on real-valued Funsors.Note this operates only on the
outputtensor. To perform sum-product contractions on named dimensions, instead use+andReduce.Parameters: - equation (str) – An
torch.einsum()ornp.einsum()equation. - operands (tuple) – A tuple of input funsors.
- equation (str) – An
-
tensordot(x, y, dims)[source]¶ Wrapper around
torch.tensordot()ornp.tensordot()to operate on real-valued Funsors.Note this operates only on the
outputtensor. To perform sum-product contractions on named dimensions, instead use+andReduce.Arguments should satisfy:
len(x.shape) >= dims len(y.shape) >= dims dims == 0 or x.shape[-dims:] == y.shape[:dims]
Parameters: Return type:
Gaussian¶
-
class
BlockVector(shape)[source]¶ Bases:
objectJit-compatible helper to build blockwise vectors. Syntax is similar to
torch.zeros()x = BlockVector((100, 20)) x[..., 0:4] = x1 x[..., 6:10] = x2 x = x.as_tensor() assert x.shape == (100, 20)
-
class
BlockMatrix(shape)[source]¶ Bases:
objectJit-compatible helper to build blockwise matrices. Syntax is similar to
torch.zeros()x = BlockMatrix((100, 20, 20)) x[..., 0:4, 0:4] = x11 x[..., 0:4, 6:10] = x12 x[..., 6:10, 0:4] = x12.transpose(-1, -2) x[..., 6:10, 6:10] = x22 x = x.as_tensor() assert x.shape == (100, 20, 20)
-
align_gaussian(new_inputs, old, expand=False)[source]¶ Align data of a Gaussian distribution to a new
inputsshape.
-
class
Gaussian(white_vec, prec_sqrt, inputs)[source]¶ Bases:
funsor.terms.FunsorFunsor representing a batched Gaussian log-density function.
Gaussians are the internal representation for joint and conditional multivariate normal distributions and multivariate normal likelihoods. Mathematically, a Gaussian represents the quadratic log density function:
f(x) = -0.5 * || x @ prec_sqrt - white_vec ||^2 = -0.5 * < x @ prec_sqrt - white_vec | x @ prec_sqrt - white_vec > = -0.5 * < x | prec_sqrt @ prec_sqrt.T | x> + < x | prec_sqrt | white_vec > - 0.5 ||white_vec||^2
Internally Gaussians use a square root information filter (SRIF) representation consisting of a square root of the precision matrix
prec_sqrtand a vector in the whitened spacewhite_vec. This representation allows space-efficient construction of Gaussians with incomplete information, i.e. with zero eigenvalues in the precision matrix. These incomplete log densities arise when making low-dimensional observations of higher-dimensional hidden state. Sampling and marginalization are supported only for full-rank Gaussians or full-rank subsets of Gaussians. See therank()andis_full_rank()properties.Note
Gaussians are not normalized probability distributions, rather they are canonicalized to evaluate to zero log density at their maximum:f(prec_sqrt \ white_vec) = 0. Not only are Gaussians non-normalized, but they may be rank deficient and non-normalizable, in which case sampling and marginalization are supported only un full-rank subsets of variables.Parameters: - white_vec (torch.Tensor) – An batched white noise vector, where
white_vec = prec_sqrt.T @ mean. Alternatively you can specify one of the kwargsmeanorinfo_vec, which will be converted towhite_vec. - prec_sqrt (torch.Tensor) – A batched square root of the positive
semidefinite precision matrix. This need not be square, and typically
has shape
prec_sqrt.shape == white_vec.shape[:-1] + (dim, rank), wheredimis the total flattened size of real inputs andrank = white_vec.shape[-1]. Alternatively you can specify one of the kwargsprecision,covariance, orscale_tril, which will be converted toprec_sqrt. - inputs (OrderedDict) – Mapping from name to
Domain.
-
compression_threshold= 2¶
-
classmethod
set_compression_threshold(threshold: float)[source]¶ Context manager to set rank compression threshold.
To save space Gaussians compress wide
prec_sqrtmatrices down to square. However compression uses a QR decomposition which can be expensive and which has unstable gradients when the resulting precision matrix is rank deficient. To balance space and time costs and numerical stability, compression is trigger only onprec_sqrtmatrices whose width to height ratio is greater thanthreshold.Parameters: threshold (float) – Defaults to 2. To optimize for space and deterministic computations, set threshold = 1. To optimize for fewest QR decompositions and numerical stability, setthreshold = math.inf.
-
rank¶
-
is_full_rank¶
- white_vec (torch.Tensor) – An batched white noise vector, where
Joint¶
Contraction¶
-
class
Contraction(red_op, bin_op, reduced_vars, terms)[source]¶ Bases:
funsor.terms.FunsorDeclarative representation of a finitary sum-product operation.
After normalization via the
normalize()interpretation contractions will canonically order their terms by type:Delta, Number, Tensor, Gaussian
-
GaussianMixture¶ alias of
funsor.cnf.Contraction
Integrate¶
-
class
Integrate(log_measure, integrand, reduced_vars)[source]¶ Bases:
funsor.terms.FunsorFunsor representing an integral wrt a log density funsor.
Parameters:
Constant¶
-
class
ConstantMeta(name, bases, dct)[source]¶ Bases:
funsor.terms.FunsorMetaWrapper to convert
const_inputsto a tuple.
-
class
Constant(const_inputs, arg)[source]¶ Bases:
funsor.terms.FunsorFunsor that is constant wrt
const_inputs.Constantcan be used for provenance tracking.Examples:
a = Constant(OrderedDict(x=Real, y=Bint[3]), Number(0)) a(y=1) # returns Constant(OrderedDict(x=Real), Number(0)) a(x=2, y=1) # returns Number(0) d = Tensor(torch.tensor([1, 2, 3]))["y"] a + d # returns Constant(OrderedDict(x=Real), d) c = Constant(OrderedDict(x=Bint[3]), Number(1)) c.reduce(ops.add, "x") # returns Number(3)
Parameters: - const_inputs (dict) – A mapping from input name (str) to datatype (
funsor.domain.Domain). - arg (funsor) – A funsor that is constant wrt to const_inputs.
- const_inputs (dict) – A mapping from input name (str) to datatype (