Interpreter¶
run_program runs a MonadicProgram on the
reference machine under the handler stack
(Plotkin and Pretnar, 2009; Bauer and Pretnar, 2015). It encodes the program as a Quivers Indexed Effect Core (QIEC) computation,
installs the stacked handlers as lexically scoped handlers of the program's random and score
instances, and runs the program's Python steps through Compute handlers over PyTorch. A draw
receives its value from its handler, and an
observation adds its score as
the handler interprets it. A caller that wants an
execution trace stacks a
TraceHandler first.
interpreter
¶
Running a MonadicProgram on the reference machine under a handler stack.
run_program encodes the program as a kernel computation, installs the
handlers on the thread-local stack as lexical handlers of the program's
random and score instances, serves the program's host steps
through Compute handlers over torch, and evaluates the whole on the
reference machine. The value it returns is the program's output;
handlers accumulate state on the side. A caller that wants a Trace
stacks a TraceHandler before run_program and reads its trace
attribute afterwards; the thin quivers.inference.trace.trace wrapper
does exactly this.
The nesting of the installed handlers, outermost first, is fixed by the
kernel's rules rather than by the order of the stack alone: the run's
score accumulator, then every stack handler of the score instance in
stack order, then the default scoring draw and the run's observations,
then every stack handler of the random instance in stack order, then
the parameter store with every stack handler of the param instance
inside it, then the host steps. A density is a Score.add the answering
handler emits outward, so a score transformer anywhere on the stack
reaches every site, and an inner random handler sees the answer an
outer one gives.
HostValue
¶
HostValue = Tensor | int | float | bool | str | TorchSampleable | tuple[HostValue, ...] | None
A value a host step produces or consumes: a tensor, a number, a
string, a sampleable, None, or a tuple of such values.
RunValue
¶
A let or score value computed from one run's bindings: a tensor or a tuple of them.
host_value
¶
host_value(value: object) -> TypeIs[HostValue]
Whether a value is one a host step may produce or consume.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True for tensors, numbers, strings, sampleables, |
Source code in src/quivers/effects/interpreter.py
check_host_data
¶
check_host_data(program: MonadicProgram | CheckedProgram, data: Mapping[str, object], runs: int | None = None) -> dict[str, bool]
Refuse host data that does not inhabit the program's checked types.
The checked elaboration types every free name a program reads, and
every target the module renders to reads the name at that type. A
datum whose shape or element type differs would make the run compute
a model the checked module does not describe, so it is refused. A
dimension the checked type leaves as an extent variable takes the
length of the first datum it shapes, and every later datum it shapes
must agree. A run over an input batch replicates the whole program
once per batch entry, so a datum may also carry one leading axis of
the batch's length, one value of its checked type per run. A datum
that fills a parameter of a plated draw directly, listed in the
program's host_data_shared, may also omit the plate axis its
checked type leads with: it is then one value every row shares.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program, whose
TYPE:
|
data
|
The supplied host data, by name.
TYPE:
|
runs
|
The length of the input batch, whose axis a datum may lead with;
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, bool]
|
For each checked datum supplied, whether it leads with the input batch's axis, one value per run, rather than one value every run shares. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a datum's shape differs from the shape its checked type names, or an extent variable is fixed at two different lengths. |
TypeError
|
If a datum's element type is not the one its checked type names. |
Source code in src/quivers/effects/interpreter.py
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 | |
run_program
¶
run_program(program: MonadicProgram, x: Tensor, observations: Mapping[str, Tensor] | None = None) -> Tensor | dict[str, Tensor]
Execute a program on the reference machine under the handler stack.
An input of B rows is B independent runs of the whole program.
Each run reads its slice of every value given one per run (a leading
axis of length B) and the whole of every value the runs share;
the handlers on the stack are installed on every run, and the runs'
outputs stack along a leading run axis.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
Program to execute.
TYPE:
|
x
|
Program input. Shape
TYPE:
|
observations
|
Values to condition observed sites on, keyed by site name. Undeclared keys are host data the program's steps read by name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor or dict[str, Tensor]
|
The program's return value: the single returned variable, or the returned variables keyed by their labels. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an observation names one variable of a destructuring draw, or a value is given for an occurrence of a scanned site the run never reaches. |
Source code in src/quivers/effects/interpreter.py
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 | |
sample_program
¶
sample_program(program: MonadicProgram, x: Tensor, sample_shape: Size = Size(), observations: Mapping[str, Tensor] | None = None) -> Tensor | dict[str, Tensor]
Run a program forward under the handler stack, with sample dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
x
|
Program input. Shape
TYPE:
|
sample_shape
|
Leading sample dimensions. Each element of the shape is one independent run of the program; the returned values stack the runs ahead of their batch axis. |
observations
|
Values to condition observed sites on, keyed by site name, and host data the steps read by name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor or dict[str, Tensor]
|
The program's return value, with |
Source code in src/quivers/effects/interpreter.py
cast_dict
¶
Read a program's dictionary return.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A program's return value. |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
The value, which is a dictionary. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the value is a single tensor. |
Source code in src/quivers/effects/interpreter.py
log_joint
¶
log_joint(program: MonadicProgram, x: Tensor, intermediates: Mapping[str, Tensor]) -> Tensor
The joint log density of a program at values for all of its draws.
The program runs on the reference machine under the handler stack
with every draw conditioned on its given value. An input of B
rows is B independent runs of the whole program, and the joint
is one number per run: the sum of that run's site densities. The
joint of all the runs together is the sum of the result.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
x
|
Program input. Shape
TYPE:
|
intermediates
|
A value for every draw the program makes, keyed by variable
name or by return label, together with any host data its steps
read by name. A value is given either once per run, along a
leading axis of length
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The joint log density of each run, shaped |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If a draw the program makes is given no value. |
Source code in src/quivers/effects/interpreter.py
Cited sources¶
Gordon Plotkin and Matija Pretnar. Handlers of Algebraic Effects. In Programming Languages and Systems, volume 5502 of Lecture Notes in Computer Science, 80–94. Springer, 2009. URL: https://link.springer.com/chapter/10.1007/978-3-642-00590-9_7, doi:10.1007/978-3-642-00590-9_7.
Andrej Bauer and Matija Pretnar. Programming with Algebraic Effects and Handlers. Journal of Logical and Algebraic Methods in Programming, 84(1):108–123, 2015. URL: https://www.sciencedirect.com/science/article/pii/S2352220814000194, doi:10.1016/j.jlamp.2014.02.001.