beanmachine.ppl.compiler.runtime module

A builder for the BeanMachine Graph language

The Beanstalk compiler has, at a high level, five phases.

  • First, it transforms a Python model into a semantically equivalent program “single assignment” (SA) form that uses only a small subset of Python features.

  • Second, it transforms that program into a “lifted” form. Portions of the program which do not involve samples are executed normally, but any computation that involves a stochastic node in any way is instead turned into a graph node.

    Jargon note:

    Every graph of a model will have some nodes that represent random samples and some which do not. For instance, we might have a simple coin flip model with three nodes: a sample, a distribution, and a constant probability:

    def flip():

    return Bernoulli(0.5)

    sample –> Bernoulli –> 0.5

    We’ll refer to the nodes which somehow involve a sample, either directly or indirectly, as “stochastic” nodes.

  • Third, we actually execute the lifted program and accumulate the graph.

  • Fourth, the accumulated graph tracks the type information of the original Python program. We mutate the accumulated graph into a form where it obeys the rules of the BMG type system.

  • Fifth, we either create actual BMG nodes in memory via native code interop, or we emit a program in Python or C++ which does so.

This module implements the graph builder that is called during execution of the lifted program; it implements phases three, four and five.

class beanmachine.ppl.compiler.runtime.BMGRuntime

Bases: object

accumulate_graph(queries: List[beanmachine.ppl.model.rv_identifier.RVIdentifier], observations: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, Any]) beanmachine.ppl.compiler.bm_graph_builder.BMGraphBuilder
handle_dot_get(operand: Any, name: str) Any
handle_dot_set(operand: Any, name: str, value: Any) None
handle_for(iter: Any) None
handle_function(function: Any, arguments: List[Any], kwargs: Optional[Dict[str, Any]] = None) Any
handle_if(test: Any) None
handle_index(left: Any, right: Any) Any
handle_not_in(input: Any, other: Any) Any
handle_slice(left: Any, lower: Any, upper: Any, step: Any) Any
handle_subscript_assign(target: Any, index: Any, stop: Any, step: Any, value: Any) None
in_flight: Set[beanmachine.ppl.utils.memoize.MemoizationKey]
lifted_map: Dict[Callable, Callable]
rv_map: Dict[beanmachine.ppl.utils.memoize.MemoizationKey, beanmachine.ppl.compiler.bmg_nodes.BMGNode]