Core / factor graphs
The directed factor graph beneath the circuits: a graph of factors, each a sampler over its sites.
Abstract base classes
AbstractTiledFactorclassAbstractTiledFactor(
base: AbstractFactor,
n_tiles: int,
weight_tied: bool,
*,
batch_size: int | None = None,
slice_info: bool = False,
)Base implementation for factors tiled across independent replicas.
batch_size chooses the mapping mode: None, 0, or any value >= n_tiles uses jax.vmap (all tiles at once); a smaller value uses jax.lax.map with chunks of that size.
The info argument is the runtime info forwarded to each tile's base.sample. By default the same info is broadcast to every tile; set slice_info=True to instead pass per-tile info.
Arguments:
base: The factor to replicate.n_tiles: Number of tiles.weight_tied: Whether all tiles share one parameter set (see above).batch_size:vmapvslax.mapexecution strategy (see above).slice_info: Whetherinfois sliced per tile (see above).
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Sample every tile and stack the results along a leading n_tiles axis. With return_aux=True each tile's aux is stacked likewise.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Per-tile AbstractFactor.sample_with_references.
Each tile calls base.sample_with_references(n_references) and the results are stacked across tiles. The main output leaves have shape (n_tiles, *base_output_shape) and aux leaves have shape (n_references + 1, n_tiles, *base_aux_leaf_shape).
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]base.init_params when weight_tied, else vmap-ed over n_tiles.
AbstractChainFactorclassAbstractChainFactor(
base: AbstractFactor,
n_steps: int,
feedback_porting_fn: Union[Callable[[PyTree[Array]], Mapping[str, PyTree[Array]]], str],
weight_tied: bool,
*,
slice_info: bool = False,
)Base implementation for factors chained via jax.lax.scan.
At each step the base's input ports split into two disjoint sets:
- Feedback ports: receive
feedback_porting_fn(previous step's main output); at step 0 they take the chain's initial state. - Broadcast ports: get the caller's value unchanged at every step.
feedback_porting_fn is either a port-name str (for lambda main: {str: main}) or a Callable mapping the previous main output to a dict of feedback-port values.
Arguments:
base: The factor applied at every step.n_steps: Number of steps.feedback_porting_fn: Port-namestrorCallablerouting each step's main output into the next step.weight_tied: Whether all steps share one parameter set.slice_info: Whetherinfois sliced per step.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Run the scan and return the final step's output. With return_aux=True additionally returns the per-step aux stacked along a leading axis of size n_steps.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Per-step AbstractFactor.sample_with_references along the scan.
Each step calls base.sample_with_references(n_references); the main output of the step is threaded as the scan carry.The returned aux_trace leaves have shape (n_references + 1, n_steps, *base_aux_leaf_shape).
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]vmap(base.init_params) per step, or one shared init when weight_tied.
AbstractDFGclassAbstractDFG()A Factor built as a DAG of placed factors.
Holds the structure and concretises sample / sample_with_references as an eager topological walk.
The DFG owns a flat namespace of addresses, one per input port and one per Site, and the two sets must be disjoint. Every parents entry and output_name is such an address. A DFG is itself a Factor, so it can nest as the factor of a Site.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], tuple[PyTree[Array], ...]]Run the DAG once and return the output_name value.
Samples each site in topological order, routing parent outputs through its porting_fn.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], tuple[PyTree[Array], ...]]Like sample, but draws each site via its factor's sample_with_references, so every site yields n_references + 1 samples. Returns (output, per_site_auxes).
distribute_paramsmethoddistribute_params(params: PyTree[Array]) -> tuple[PyTree[Array], ...]Scatter the shared params mapping into a per-site tuple.
gather_param_gradsmethodgather_param_grads(
params: PyTree[Array],
site_grads: Mapping[int, PyTree[Array]],
) -> PyTree[Array]Gather per-site parameter gradients into the shared params dict.
distribute_infomethoddistribute_info(info: DFGInfo | None) -> tuple[PyTree, ...]Scatter info.entries into a per-site tuple.
AbstractFactorclassAbstractFactor()Base class for probabilistic factors.
A directed factor is a conditional distribution $P(\text{output} \mid \text{inputs})$.
Factor methods operate with the following:
params: the factor's parameter pytree.info: optional runtime auxiliary info.site_info: optional per-site metadata supplied by the surroundingSite.return_aux: whenTrue, methods additionally return a factor-definedauxpytree.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Draw a "main" sample plus n_references additional samples.
Always returns (main_output, aux). main_output is a single sample. Every aux leaf gains a leading axis of size n_references + 1: position 0 is the main sample's aux, positions 1 .. n_references are the references' auxes.
For a sample with no references and no aux, use sample.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_references: Number of reference samples to draw alongside the main sample.
Returns:
(main_output, aux).
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
AbstractReferenceFactorclassAbstractReferenceFactor()Abstract factor + a concrete sample_with_references.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Generic reference-sampling default.
Draws n_references + 1 samples via sample_multiple and returns the first as the main sample, with all of their auxes stacked.
AbstractHasLogProbabilityclassAbstractHasLogProbability()Capability mixin for factors with a tractable, analytic log_probability(outputs | inputs).
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
log_probabilitymethodlog_probability(
inputs: Mapping[str, PyTree[Array]],
outputs: PyTree[Array],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, ''] | tuple[Float[Array, ''], PyTree[Array]]Scalar log-probability of outputs given inputs.
Arguments:
inputs: Per-port pytree inputs, as insample.outputs: An output pytree matchingoutput_spec.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.return_aux: Whether to additionally return a factor-definedauxpytree.
Returns:
The scalar log P(outputs | inputs), or (log_p, aux) when return_aux=True.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Draw a "main" sample plus n_references additional samples.
Always returns (main_output, aux). main_output is a single sample. Every aux leaf gains a leading axis of size n_references + 1: position 0 is the main sample's aux, positions 1 .. n_references are the references' auxes.
For a sample with no references and no aux, use sample.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_references: Number of reference samples to draw alongside the main sample.
Returns:
(main_output, aux).
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
AbstractEnumerableOutputFactorclassAbstractEnumerableOutputFactor()Factor whose output state space is finite, with a canonical ordering.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Draw a "main" sample plus n_references additional samples.
Always returns (main_output, aux). main_output is a single sample. Every aux leaf gains a leading axis of size n_references + 1: position 0 is the main sample's aux, positions 1 .. n_references are the references' auxes.
For a sample with no references and no aux, use sample.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_references: Number of reference samples to draw alongside the main sample.
Returns:
(main_output, aux).
n_output_statespropertyn_output_statesNumber of output states in the canonical ordering.
get_nth_output_statemethodget_nth_output_state(n: int | Int[Array, '']) -> PyTree[Array]Return the n-th output state in the canonical ordering.
The returned pytree has the same structure as the output of AbstractFactor.sample.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
output_state_to_indexmethodoutput_state_to_index(outputs: PyTree[Array]) -> Float[Array, '']Index of outputs in the canonical ordering, as a scalar float.
AbstractFiniteStateSpaceFactorclassAbstractFiniteStateSpaceFactor()Factor whose input and output state spaces are both finite.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Draw a "main" sample plus n_references additional samples.
Always returns (main_output, aux). main_output is a single sample. Every aux leaf gains a leading axis of size n_references + 1: position 0 is the main sample's aux, positions 1 .. n_references are the references' auxes.
For a sample with no references and no aux, use sample.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_references: Number of reference samples to draw alongside the main sample.
Returns:
(main_output, aux).
n_output_statespropertyn_output_statesNumber of output states in the canonical ordering.
get_nth_output_statemethodget_nth_output_state(n: int | Int[Array, '']) -> PyTree[Array]Return the n-th output state in the canonical ordering.
The returned pytree has the same structure as the output of AbstractFactor.sample.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
output_state_to_indexmethodoutput_state_to_index(outputs: PyTree[Array]) -> Float[Array, '']Index of outputs in the canonical ordering, as a scalar float.
n_input_statespropertyn_input_statesNumber of input states in the canonical ordering.
get_nth_input_statemethodget_nth_input_state(n: int | Int[Array, '']) -> Mapping[str, PyTree[Array]]Return the n-th input state in the canonical ordering.
The returned pytree has the same structure as the inputs argument to AbstractFactor.sample.
input_state_to_indexmethodinput_state_to_index(inputs: Mapping[str, PyTree[Array]]) -> Float[Array, '']Index of inputs in the canonical ordering, as a scalar float.
AbstractHasExplicitOutputDistributionclassAbstractHasExplicitOutputDistribution()Factor whose conditional is available in closed form as an explicit log-prob vector over the enumerable output states.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Draw a "main" sample plus n_references additional samples.
Always returns (main_output, aux). main_output is a single sample. Every aux leaf gains a leading axis of size n_references + 1: position 0 is the main sample's aux, positions 1 .. n_references are the references' auxes.
For a sample with no references and no aux, use sample.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_references: Number of reference samples to draw alongside the main sample.
Returns:
(main_output, aux).
n_output_statespropertyn_output_statesNumber of output states in the canonical ordering.
get_nth_output_statemethodget_nth_output_state(n: int | Int[Array, '']) -> PyTree[Array]Return the n-th output state in the canonical ordering.
The returned pytree has the same structure as the output of AbstractFactor.sample.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
output_state_to_indexmethodoutput_state_to_index(outputs: PyTree[Array]) -> Float[Array, '']Index of outputs in the canonical ordering, as a scalar float.
get_log_output_distributionmethodget_log_output_distribution(
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, 'n_output_states'] | tuple[Float[Array, 'n_output_states'], PyTree[Array]]Log-probabilities of all n_output_states outputs given inputs.
Returns a vector v of shape (n_output_states,) with v[j] = log P(get_nth_output_state(j) | inputs).
Arguments:
inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.return_aux: Whether to additionally return a factor-definedaux.
Returns:
The length-n_output_states log-probability vector, or (v, aux) when return_aux=True.
log_probabilitymethodlog_probability(
inputs: Mapping[str, PyTree[Array]],
outputs: PyTree[Array],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, ''] | tuple[Float[Array, ''], PyTree[Array]]Index get_log_output_distribution at the queried outputs.
AbstractMatrixFactorclassAbstractMatrixFactor()A finite-state factor whose conditional is given explicitly as a matrix.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw a single sample from this factor.
Arguments:
key: PRNG key.inputs: Per-port pytree inputs. Keys must equal theself.input_portskeys.params: Parameter pytree for this factor.info: Runtime auxiliary info.site_info: Static per-site metadata supplied by the surroundingSite.return_aux: IfTrue, return(output, aux).
Returns:
The sampled output (a pytree matching output_spec), or (output, aux) when return_aux=True.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Generic reference-sampling default.
Draws n_references + 1 samples via sample_multiple and returns the first as the main sample, with all of their auxes stacked.
log_probabilitymethodlog_probability(
inputs: Mapping[str, PyTree[Array]],
outputs: PyTree[Array],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, ''] | tuple[Float[Array, ''], PyTree[Array]]Index get_log_output_distribution at the queried outputs.
input_portspropertyinput_portsoutput_specpropertyoutput_specn_input_statespropertyn_input_statesn_output_statespropertyn_output_statesget_nth_input_statemethodget_nth_input_state(n: int | Int[Array, '']) -> Mapping[str, PyTree[Array]]get_nth_output_statemethodget_nth_output_state(n: int | Int[Array, '']) -> PyTree[Array]input_state_to_indexmethodinput_state_to_index(inputs: Mapping[str, PyTree[Array]]) -> Float[Array, '']output_state_to_indexmethodoutput_state_to_index(outputs: PyTree[Array]) -> Float[Array, '']get_log_probability_matrixmethodget_log_probability_matrix(
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
) -> Float[Array, 'n_input_states n_output_states']Return the (n_input_states, n_output_states) log-probability matrix.
Entry [i, j] is log P(output_states[j] | input_states[i]).
Arguments:
params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.
Returns:
The matrix of log-probabilities.
get_log_output_distributionmethodget_log_output_distribution(
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, 'n_output_states'] | tuple[Float[Array, 'n_output_states'], PyTree[Array]]The matrix row selected by the input's canonical index.
Concrete classes
TiledFactorclassTiledFactor(
base: AbstractFactor,
n_tiles: int,
weight_tied: bool,
*,
batch_size: int | None = None,
slice_info: bool = False,
)Replication of a base factor across n_tiles independent tiles.
baseattributebase: AbstractFactorn_tilesattributen_tiles: intweight_tiedattributeweight_tied: boolbatch_sizeattributebatch_size: int | Noneslice_infoattributeslice_info: boolinput_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Sample every tile and stack the results along a leading n_tiles axis. With return_aux=True each tile's aux is stacked likewise.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Per-tile AbstractFactor.sample_with_references.
Each tile calls base.sample_with_references(n_references) and the results are stacked across tiles. The main output leaves have shape (n_tiles, *base_output_shape) and aux leaves have shape (n_references + 1, n_tiles, *base_aux_leaf_shape).
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]base.init_params when weight_tied, else vmap-ed over n_tiles.
ChainFactorclassChainFactor(
base: AbstractFactor,
n_steps: int,
feedback_porting_fn: Union[Callable[[PyTree[Array]], Mapping[str, PyTree[Array]]], str],
weight_tied: bool,
*,
slice_info: bool = False,
)Sequential composition of a base factor via jax.lax.scan.
baseattributebase: AbstractFactorn_stepsattributen_steps: intfeedback_porting_fnattributefeedback_porting_fn: Optional[Callable[[jaxPyTree[Array]], Mapping[str, jaxPyTree[Array]]]]feedback_portsattributefeedback_ports: tuple[str, ...]all_step_input_portsattributeall_step_input_ports: tuple[str, ...]weight_tiedattributeweight_tied: boolslice_infoattributeslice_info: boolinput_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Run the scan and return the final step's output. With return_aux=True additionally returns the per-step aux stacked along a leading axis of size n_steps.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Per-step AbstractFactor.sample_with_references along the scan.
Each step calls base.sample_with_references(n_references); the main output of the step is threaded as the scan carry.The returned aux_trace leaves have shape (n_references + 1, n_steps, *base_aux_leaf_shape).
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]vmap(base.init_params) per step, or one shared init when weight_tied.
SiteclassSite(
name: str,
factor: AbstractFactor,
parents: Any,
porting_fn: Any,
param_key: str | int | None,
info_key: str | None,
site_info: Any,
)The placement of a Factor at one position in a DFG.
Construct a Site.
Arguments:
name: This site's address in the DFG namespace. Unique among sites and disjoint from the input-port names.factor: TheFactorplaced here.parents: Addresses (input ports or site names) feeding this factor;porting_fnmaps them onto its named input ports.porting_fn: How parents route into the factor's input dict: a tuple of port names (1:1 withparents), or a callableparents -> input dictfor non-trivial routing.param_key: Address of this site's parameters within the DFG'sparams.info_key: Likeparam_key, for runtime info;Nonepassesinfo=None.site_info: per-site metadata passed to the factor.
nameattributename: strfactorattributefactor: AbstractFactorparentsattributeparents: tuple[str, ...]porting_fnattributeporting_fn: Union[Callable[[Sequence[jaxPyTree[Array]]], Mapping[str, jaxPyTree[Array]]], tuple[str, ...]]param_keyattributeparam_key: str | int | Noneinfo_keyattributeinfo_key: str | Nonesite_infoattributesite_info: AnyDFGInfoclassDFGInfo(
expose_site_outputs: bool,
entries: Mapping[str, PyTree[Array]] = <factory>,
)DFG-level runtime info.
Separate from per-site info, which lives in entries under each site's info_key; this configures how the DAG itself runs.
Arguments:
expose_site_outputs: WhenTrue(andauxis requested), prepend a name-keyed dict of every site's main output to the aux return.entries: Per-info_keymapping, scattered to sites bydistribute_info. A child-DFGsite's entry is itself aDFGInfo.
expose_site_outputsattributeexpose_site_outputs: boolentriesattributeentries: Mapping[str, jaxPyTree[Array]]DFGclassDFG(
sites: tuple[Site, ...],
input_ports: Mapping[str, PyTree[jax.ShapeDtypeStruct]],
output_name: str,
)A concrete, eagerly-walked DAG of factors.
sitesattributesites: tuple[Site, ...]input_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]output_nameattributeoutput_name: strtopological_orderattributetopological_order: tuple[int, ...]sites_by_nameattributesites_by_name: Mapping[str, int]sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], tuple[PyTree[Array], ...]]Run the DAG once and return the output_name value.
Samples each site in topological order, routing parent outputs through its porting_fn.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], tuple[PyTree[Array], ...]]Like sample, but draws each site via its factor's sample_with_references, so every site yields n_references + 1 samples. Returns (output, per_site_auxes).
distribute_paramsmethoddistribute_params(params: PyTree[Array]) -> tuple[PyTree[Array], ...]Scatter the shared params mapping into a per-site tuple.
gather_param_gradsmethodgather_param_grads(
params: PyTree[Array],
site_grads: Mapping[int, PyTree[Array]],
) -> PyTree[Array]Gather per-site parameter gradients into the shared params dict.
distribute_infomethoddistribute_info(info: DFGInfo | None) -> tuple[PyTree, ...]Scatter info.entries into a per-site tuple.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Initialise parameters once per distinct param_key.
DeterministicFactorclassDeterministicFactor(
fn: Callable[[Mapping[str, PyTree[Array]], Any], PyTree[Array]],
input_ports: Any,
output_spec: PyTree[jax.ShapeDtypeStruct],
)A factor whose output is a deterministic function of its inputs.
Arguments:
fn: A pure functionfn(inputs, site_info) -> outputmapping the per-portinputsdict (and the surroundingSite's staticsite_info) to an output pytree matchingoutput_spec.input_ports: The factor's input-port specs.output_spec: The spec offn's output.
fnattributefn: Callable[[Mapping[str, jaxPyTree[Array]], Any], jaxPyTree[Array]]input_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Return fn(inputs, site_info).
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], PyTree[Array]]Evaluate fn once.
init_paramsmethodinit_params(key: Key[Array, '']) -> PyTree[Array]Return a freshly-initialised parameter pytree.
Arguments:
key: PRNG key.
log_probabilitymethodlog_probability(
inputs: Mapping[str, PyTree[Array]],
outputs: PyTree[Array],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
return_aux: bool = False,
) -> Float[Array, ''] | tuple[Float[Array, ''], PyTree[Array]]0 if outputs == fn(inputs, site_info), else -inf.