Tensor.SchemeThe low-level, explicit layer of shape schemes, which the %op integration (gh-ocannl-1751) wraps. A scheme is the shape constraints a tensor function imposes, inferred once by a trace and instantiated at each application:
Scheme.trace, at the definition;Scheme.instantiate adds the scheme's constraints, renamed, relating it to given argument and result shapes, without running the function: the shapes close as an inline application of the function would have closed them.What is shared and what is per-application follows ML's value restriction: the dimensions of a tensor created before the trace -- a shared parameter, lifted to the function's unit-parameter scope -- stay fixed across applications, and everything the function creates (its intermediates, a parameter created inside it, a capture reference made inside it) is fresh per application. So create the shared parameters before Scheme.trace. The design note is the gh-ocannl-404 issue's reconciled-plan comment; docs/shape_inference.md has the summary.
type tensor := tval label : t -> Base.stringval arity : t -> Base.intval num_results : t -> Base.intval to_string : t -> Base.stringto_string s prints s's boundary: <label> : <arguments> => <results>, each shape as batch|input->output under the trace's solution, with ..v.. for a row variable and () for an empty row, and _ for a scheme with no results. Variables are named by first appearance, so the text does not depend on the session's id counters: a generalized variable (renamed per application) as 'a, 'b, ...; a fixed one (shared with what the function closes over, such as a parameter lifted to its () scope) as '_a, '_b, ..., the way OCaml prints weak type variables. Then the indented lines: the broadcasting inequalities r ⊑ o1, o2, ... (the result side refines each operand side) among the boundary's variables and the fixed ones, grouped by left side; then _ ⊑ o1, o2, ... for the variables some eliminated intermediate refines together when no printed variable does (gh-ocannl-1988: arguments consumed together by a reduction, whose common refinement the result no longer carries); then the lower bounds of the boundary's variables, b ⊑ v, the variables such a bound names related like the boundary's. All under the trace's final environment, with the function's intermediate variables eliminated along monotone chains. The order is canonical: the boundary's variables by their appearance in the header, a fixed one by its relations to them -- never the solver's traversal order. Other constraints (totals, concatenations) are not printed. test/operations/reusable_tensor_programs.ml shows one.
trace ~label n f applies f to n tracing tensors whose shapes are fresh row variables, inside with_rewind, and returns the scheme of the constraints it registered over them and over its results (a flat list: flatten a structure). The trace leaves nothing in the session. Raises Row.Shape_error prefixed "tracing the shape scheme <label>: " on a shape error in f (including one only the end-of-trace check of f's trailing Shape.set_dim and the like finds), when f forces dims or projections (inference cannot close during a trace), and for what a scheme cannot carry: Shape.set_dim and the like on a capture reference made outside f. Random number generation inside f is carried (gh-ocannl-1890): see Shape.Scheme.trace for how its coefficient reads each call site's precision. A capture reference made outside f that f's operations bind is an input of the scheme (gh-ocannl-1791): each application relates it to the reference's state then, and instantiate binds it, as the body would.
val instantiate :
?application:Base.string ->
?here:Ppx_here_lib.position ->
t ->
args:Shape.t Base.list ->
results:Shape.t Base.list ->
Base.unitinstantiate s ~args ~results adds s's constraints for one application of its function to args with results (fresh, inferred shapes, or the shapes of tensors computing the results), without running the function. An argument-dependent shape error raises Row.Shape_error prefixed "applying the shape scheme <label> to <application>: " (with ~here:[%here], "... to <application> at <file>:<line>: ": gh-ocannl-2007) and taints the session, as a failed construction does; the constraints carry the application's name (and here), so a failure at the closure names it too. The next closure applies the solution to args and results and closes them. It finalizes the scheme's rows but derives no projections for the body's steps, so projection-time refusals do not run: an Affine or Concat over a Sym that cannot be sized, window verification, and shared symbolic iterators. An instance can therefore close where the body's inline application is refused (gh-ocannl-2020). Automatic %op schemes are unaffected: their body always runs. Valid across closures, unless the scheme depends on open state no shape it used holds (then refused after a closure: trace again). A scheme whose function draws random numbers is refused: the size of a draw's counter is the precision its conversion node settles, and an instance, unlike the function's own application, has none.
%op functions (gh-ocannl-1751)%op gives a function a scheme when every parameter of the function it returns is a tensor parameter (docs/syntax_extensions.md says which qualify). The closure built at the () application -- or, for a function without a () stage, its first application -- traces the scheme. How each application uses it is the op_shape_schemes setting:
check (the default): each application checks the scheme against its arguments' shapes, in a window that is always rewound, before the body runs and registers as it always does. The applications in the body of a checked application are not checked themselves: its scheme holds their constraints, so an argument error in one is reported by the outer check, naming the outer function (gh-ocannl-1814). Only a function's first application there still traces its presence patterns, which the outer scheme does not hold. Under an opted-out, refused or otherwise unchecked function they are checked as usual;off: no schemes.A shape error that does not depend on the arguments raises at the trace, prefixed "tracing the shape scheme <function>: " -- or, inside an application written in %op, "tracing the shape scheme <function> in the %op application at <file>:<line>: " (gh-ocannl-2164). One that does raises at the application, before the body runs, prefixed "applying the shape scheme <function> to <arguments>: " -- or, for an application evaluated inside an application written in %op code, the innermost such, "applying the shape scheme <function> to <arguments> in the %op application at <file>:<line>: " (gh-ocannl-2007). The expansion records that site; an application written in plain OCaml outside any %op application has none (OCaml has no caller location), so its message names the function and the arguments only. A trace a scheme cannot carry (setting a capture reference made outside the function, forcing dims, or a body that cannot run on tracers) leaves the function unschemed, as it was before, once its rewind succeeded, and says so on stderr -- once per function and reason in a process (gh-ocannl-1791), without an application site. If rewind is refused, the error names the function followed by the active %op application site, when present, and the scheme opt-out remedy. A capture reference made outside the function that its operations bind is a scheme input, checked against each application's state of it. Ids are unaffected: traces and checks allocate tensor ids, node uids and shape-variable ids from reserved ranges.
Option-valued tensor parameters (gh-ocannl-1792), annotated ~(p : Tensor.t option) or ?(p : Tensor.t option): the function gets one scheme per presence pattern -- which of them are Some -- each traced, checked and refused on its own, under the label <function> [p, no q] (just <function> without such parameters). The closure built at () traces every pattern; a function without a () stage traces every pattern at its first application; afterwards each pattern re-traces on its own when stale.
An automatic scheme's results (gh-ocannl-1801) are what the traced body returns, when it is a tensor the body made (or a copy of its record, { t with forward = ... }) or one of its arguments, or a tuple (or record) of such: one result per component. Any other value -- a function, a list, a tensor made before the body ran -- gives the scheme no results. The checks relate the arguments only, so the results serve to_string and instantiate. traced_by returns the schemes a closure traced: the ones its applications are checked against, not a re-trace.
val status : handle -> Base.string"pending", "schemed", or "unschemed: <reason>"; with option-valued parameters, one "<label>: <status>" per pattern, separated by "; ".
type event = | Traced of tA function's scheme (a pattern's, under its label) was traced: the scheme its applications are checked against.
*)| Unschemed of Base.string * Base.stringA function (a pattern, by its label) was left unschemed, with the reason.
*)| Checked of tThe scheme against which an application was checked.
*)val registration_counts : Base.unit -> Shape.registration_countsProcess-wide registration statistics since the last reset (gh-ocannl-1752's attribution): see Shape.registration_counts.
val registration_times : Base.unit -> Shape.registration_timesProcess-wide shape-registration wall time since the last reset (gh-ocannl-1752): see Shape.registration_times.
val traced_by : (Base.unit -> 'a) -> 'a * t Base.listtraced_by (fun () -> block conf ()) runs the function and returns its result with the automatic schemes traced while it ran, innermost first: those of the closures it built at their () stage (every presence pattern of each), of the functions without a () stage it first applied, and of the stale ones it re-traced. They are the schemes the closures use -- their applications are checked against them -- so to_string prints the scheme in use, with no re-trace (gh-ocannl-1801). The scheme's label names its function (and pattern).
val check_definition :
name:Base.string ->
(Base.unit -> Base.unit) ->
t Base.listcheck_definition ~name (fun () -> ignore (block conf ())) runs a trial application of a %op block definition inside a window that is always rewound and allocates no session ids, so the () traces of the block and of the sub-blocks it builds run, and with them any shape error that does not depend on the arguments, prefixed "checking the definition <name>: ". Returns the schemes traced, as traced_by does (every presence pattern of a closure built at () in the trial: see above), innermost first; the session is unchanged afterwards, and the schemes stay printable. A function without a () stage that the trial only builds is not traced. It catches errors that hold for every configuration and those of the trial configuration only; it costs the trial's construction and traces. Prefer it to a bare block conf (), which leaves the trial's parameters in the session.
module For_ppx : sig ... endThe runtime the %op expansion calls. Not for direct use.