Interpreter¶
Running a program on the reference machine under a handler stack.
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.
host_value
¶
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
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.
| 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. |
Source code in src/quivers/effects/interpreter.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
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, and the joint is the sum of the site densities the run accumulated, one term per site, over the input's batch axis.
| 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.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The joint log density, shaped |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If a draw the program makes is given no value. |