Documentation

RoleFlow.Flow.Print

The JAX printer (trusted) #

A typed program P of the flow signature, with its roles ρ, is printed as two JAX functions that also return the log-weight: the forward function from the lines of P, and the inverse from the lines of the inverse program P† (Prog.inverse, the schedule of the recipes of the derivation). The inverse program is typed too, with its own roles ρ† (inferred), and both are printed by the same code (emitFwd): the log-weight is L_θ, the sum of ℓ_o over the spine steps, accumulated in ld with one ld += ℓ_o on each spine step's row, the ; aligned. For P† it is the log-weight of Prog.density for P†; that it agrees a.e. with -L_θ at the values P† recomputes (Prog.inverse_density) is not proved (TODO.md), but checked by scripts/compare.py. Every variable is a Python expression; structural operations (split, the changes of sort) emit slices or nothing, the others bind a fresh variable; a spine step that prints nothing gets a row pass ; ld += ….

Each stage is a definition (Linked.add) printed as {name}_fwd, {name}_inv over its own weights. A model is printed as one program flow_fwd (and its inverse program flow_inv), so the stage pipeline is a typed program rather than a Python composition: its stages inlined (Flow.modelProg), or, with calls, a call of each stage (Flow.callProg), a row y, l = {name}_fwd(w[a:b], x) ; ld += l. A call is a spine step like any other, and the inverse program calls {name}_inv. The roles are inferred (Prog.infer) on each stage and on the model. writeModule refuses a model or a stage that is not typed, or whose inverse program is not.

The leaf table (one JAX expression per operation and per nonzero log-weight) is trusted: it is written to agree with Flow.semOp and Flow.ℓOp. Functions act on one example; the caller batches with jax.vmap.

The printer's state #

  • lines : Array (String × String)

    The rows so far: the code, and the log-weight statement after its ; ("": none).

  • next : ℕ
  • permChecks : Array (String × ℕ × List ℕ)

    Each permutation's expression (on x, of length n) with its index list, for <module>_perms.py.

Instances For
    @[reducible, inline]
    Equations
    Instances For
      Equations
      Instances For

        Bind an expression to a fresh variable.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          Permutations #

          A permutation is printed as its steps (Flow.PermS), one numpy call each, applied in order; its inverse as the steps of PermS.inv. writeModule also writes each permutation's expression with its index list (PermS.idxList) to <module>_perms.py, which checks that numpy agrees.

          The comma-separated numbers.

          Equations
          Instances For

            A step applied to the expression x; the result keeps the step's shape (reshape reads row-major, so the next step can reshape it directly).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              The steps applied to the flat vector x, in order, flattened at the end; steps that do nothing (PermStep.isId) are left out.

              Equations
              Instances For

                The empty vector is printed as bool, which jnp.concatenate promotes to the dtype of what it is joined with.

                Equations
                Instances For

                  Several values as one Python value: a tuple, or the value itself.

                  Equations
                  Instances For

                    Leaves #

                    def RoleFlow.Flow.Print.emitOp {as bs : List FS} :
                    FOp as bs → List String → M (List String)

                    An operation's results from its arguments, one expression per result; agrees with Flow.semOp.

                    Equations
                    Instances For
                      def RoleFlow.Flow.Print.ldOp {as bs : List FS} :
                      FOp as bs → Option (List String → String)

                      The log-weight ℓ_o on the arguments, where it is not 0; agrees with Flow.ℓOp (rebase r r' has log ρ_{r'} - log ρ_r, a term for each density that is not 1; oddpow_logdet p x is Σ log p x^(p-1), which opowLogdet sets to 0 on the null set of the axes; triu_logdet c n U = n Σ_a U_aa is triuLogdet c n U).

                      Equations
                      Instances For

                        Put ld += t after the row the current line printed, if it printed one (the rows were n before it), or else on a row pass of its own.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Calls #

                          The weights at the positions ps, as a list: a slice w[a:b] if they are the function's own weights a, …, b - 1.

                          Equations
                          Instances For
                            def RoleFlow.Flow.Print.emitCall (name : String) (inv : Bool) (ws x : String) (nb : ℕ) :

                            A row calling {name}_fwd (or _inv) on the weights ws and the values x, binding its nb results and its log-weight; returns their expressions.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              Programs #

                              @[reducible, inline]

                              An operation of the signature the printed program is over: of the flow signature, or a call (Linked.view).

                              Equations
                              Instances For
                                def RoleFlow.Flow.Print.emitLines {Sg : Sig FS} (view : View Sg) (env : Array String) (ls : List (Line Sg)) (ρ : List Role) :

                                Print the lines from the expressions of the context they start in, each spine step with its ℓ_o (for a call, the log-weight it returns); returns the expressions of all variables, by position.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  The column of the ;: after the longest row that has a log-weight statement, ignoring rows longer than this.

                                  Equations
                                  Instances For

                                    def name(w, x) returning (value, ld), from the body's rows: ld starts at 0, and the log-weight statements are aligned at one column.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      The inputs of a function taking n values as x.

                                      Equations
                                      Instances For
                                        def RoleFlow.Flow.Print.emitFwd {Sg : Sig FS} (view : View Sg) (P : Prog Sg) (ρ : List Role) (env₀ : List String) :

                                        The function of P from the expressions of its weights and inputs: the returned expressions, after the rows that compute them and accumulate the log-weight.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          def RoleFlow.Flow.Print.emitPair {Sg : Sig FS} (view : View Sg) (name : String) (P : Prog Sg) (ρ ρi : List Role) :

                                          {name}_fwd from P with its roles ρ, and {name}_inv from P† = P.inverse ρ with its roles ρi, both with the log-weight. The weights are the list w.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For

                                            Modules #

                                            Helpers the leaf table refers to, matching Flow.FlowOps.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For

                                              The postamble: the model's forward/inverse (the model as one program) and init_weights.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                def RoleFlow.Flow.Print.emitStage {Sg : Sig FS} (view : View Sg) (s : Stage) (Q : Prog Sg) (ρ ρi : List Role) :

                                                A stage's two functions (over its own weights), and its weights' dimensions and inits.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For

                                                  The definitions of a tower, in order, each with the dimensions and inits of the weights of the stage of its name.

                                                  Equations
                                                  Instances For

                                                    A certified model: its stages, as the definitions of a tower L (each over its own weights), and the model as one program P at the top of the tower with its roles ρ and the roles ρi of P.inverse ρ: the stages inlined, or a program of calls of them.

                                                    Instances For

                                                      The module: the stages, the model (flow_fwd from P, flow_inv from P†), and the Python interface; with each permutation's expression and index list.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For

                                                        A script that checks each permutation's numpy expression, applied to 0, …, n - 1, against its index list (as Lean computes it).

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          def RoleFlow.Flow.Print.inferBoth {Sg : Sig FS} (what : String) (P : Prog Sg) :
                                                          Except String ((ρ : List Role) × (ρi : List Role) ×' P.Typed ρ ∧ (P.inverse ρ).Typed ρi)

                                                          The roles of P and of its inverse program P.inverse ρ, by inference, with the typings; what names P in the error.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For

                                                            The operation, if it is a permutation that is not PermS.valid (its index map is then not a permutation, and the model does not read it as the steps).

                                                            Equations
                                                            Instances For

                                                              Certify a model: add each stage, after the front end (swapArgs), as a definition of a tower (Linked.add, with its roles and its inverse program's, inferred), then infer the roles of the model as one program and of its inverse program: the stages inlined (modelProg, after the front end), or, with calls, a call of each (callProg). Fails if something is not typed, if two stages have one name, or if a permutation is not valid.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                Certify a model (certify) and write its module, and <module>_perms.py (permCheckScript).

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For