Ir.AssignmentsAssignment computations over tensor nodes.
type init_data = | Broadcast of Base.floatHost-backed scalar fill of the inferred shape, including its committed padding. Unlike Reshape, this does not constrain the number of elements.
| Reshape of Ndarray.t| Keep_shape_no_padding of Ndarray.t| Padded of {data : Ndarray.t;padding : Ops.axis_padding Base.array;padded_value : Base.float;}val sexp_of_init_data : init_data -> Sexplib0.Sexp.tval sexp_of_buffer : buffer -> Sexplib0.Sexp.ttype fetch_op = | Constant of Base.float| Constant_bits of Base.int64| Constant_fill of Base.float Base.array| Range_over_offsets| Slice of {batch_idx : Indexing.static_symbol;sliced : Tnode.t;}| Embed_symbol of Indexing.static_symbol| Embed_self_id| Embed_dim of Indexing.variable_refval sexp_of_fetch_op : fetch_op -> Sexplib0.Sexp.ttype accum_rhs = | Ternop of {op : Ops.ternop;rhs1 : buffer;rhs2 : buffer;rhs3 : buffer;}| Binop of {}| Unop of {}| Block of {}| Rev_sides of {}Reverse scatter: None skips a target without removing its operand position or projection. Skipped targets are neither read nor written, including initialization.
val sexp_of_accum_rhs : accum_rhs -> Sexplib0.Sexp.ttype t = | Noop| Seq of t * t| Block_comment of Base.string * t| Accum_op of {initialize_neutral : Base.bool;accum : Ops.binop;lhs : Tnode.t;rhs : accum_rhs;projections : Indexing.projections Utils.Lazy.t;projections_debug : Base.string;}| Set_vec_unop of {op : Ops.vec_unop;lhs : Tnode.t;rhs : buffer;projections : Indexing.projections Utils.Lazy.t;projections_debug : Base.string;}| Fetch of {array : Tnode.t;fetch_op : fetch_op;dims : Base.int Base.array Utils.Lazy.t;}val sexp_of_t : t -> Sexplib0.Sexp.tFolds f over the LEAF statements (Accum_op, Set_vec_unop, Fetch) in execution order, skipping the Noop / Seq / Block_comment scaffolding. Descending is what this module's queries and the tests' finders used to each write for themselves, and forgetting to descend through a Block_comment loses statements silently -- so the recursion is written once. f still matches on t (the leaf kinds mean different things to different queries), and its scaffolding arm is unreachable (gh-ocannl-818 would give the leaves a type of their own).
fold_leaves for a f that only has effects.
val sexp_of_side : side -> Sexplib0.Sexp.tval side_of_sexp : Sexplib0.Sexp.t -> sidemodule Fragment : sig ... endA code fragment generated from a tensor, identified by its owner node and side (gh-ocannl-1724): (t.value, Fwd) is t's forward code; (t.grad, Bprop) is t's backprop code, the code propagating from t.grad into its children's gradients.
type comp = {asgns : t;embedded_nodes : Base.Set.M(Ir.Tnode).t;owned : Base.Set.M(Fragment).t;The tensor-generated code fragments this computation carries. Context.compile refuses a computation owning a fragment some routine of its compilation lineage already owns.
requires : Base.Set.M(Fragment).t;The backprop fragments its gradient writes flow into that it does not carry itself. Context.run refuses the routine unless a routine of its own lineage (never a sibling) owns each one.
zeroes : Base.Set.M(Ir.Tnode).t;The gradients whose tensor-generated zeroing this computation carries (gh-ocannl-1744): a tensor's zero_grads and whatever is sequenced from it.
accumulates : Base.Set.M(Ir.Tnode).t;The gradients its tensor-generated backprop accumulates into (gh-ocannl-1744): the operands' gradients a tensor's own gradient code writes, and its embedded children's. Context.compile refuses a computation accumulating into a gradient that another routine of its lineage accumulates into, unless a zeroing compiled before both, and carried by neither, resets it; and one accumulating into a gradient whose propagation (its Bprop fragment) an earlier routine of the lineage already carries.
accumulation_runs : Base.int Base.Map.M(Ir.Tnode).t;For a gradient in accumulates and not in zeroes, the runs of this computation one reset of the gradient deliberately spans (gh-ocannl-1781); absent means one. Context.run refuses a routine that has already accumulated into such a gradient's buffer that many times since the buffer was last reset -- by a run of a routine zeroing it, or by a write from outside a run. Train.grad_update ~accum_steps:k states k: the explicit opt-in for gradient accumulation over micro-steps.
persists : Base.Set.M(Ir.Tnode).t;The nodes whose written value this computation means to last across runs -- an optimizer step (Train.sgd_one marks its parameter; persisting marks a hand-written one), gh-ocannl-2027. Context.compile refuses a computation that itself re-establishes such a node (persists_undone). Context.run refuses a run of a routine re-establishing such a node's buffer from its forward code without reading it (forward_recomputed, over a node whose Fwd fragment the routine owns) while the buffer still holds a persisting write's value, which that run would silently undo; only a write from outside a run replaces the value deliberately.
}Computations based on assignments. Tensor construction fills in all five sets; zeroing owns and requires nothing (it fills zeroes only), and initialization and hand-written %cd assignments fill none: a hand-written write to a gradient neither contributes nor resets.
val sexp_of_comp : comp -> Sexplib0.Sexp.tval comp :
?owned:Base.Set.M(Fragment).t ->
?requires:Base.Set.M(Fragment).t ->
?zeroes:Base.Set.M(Ir.Tnode).t ->
?accumulates:Base.Set.M(Ir.Tnode).t ->
?accumulation_runs:Base.int Base.Map.M(Ir.Tnode).t ->
?persists:Base.Set.M(Ir.Tnode).t ->
embedded:Base.Set.M(Ir.Tnode).t ->
t ->
compThe computation constructor; owned, requires, zeroes, accumulates, accumulation_runs and persists default to empty.
persisting nodes c adds nodes to c's persists: the writes c makes to them are meant to last, as a hand-written %cd optimizer step's are.
val empty_comp : compval context_nodes : plc:Tnode.Placements.t -> t -> Tnode.t_setval guess_materialized : t -> Tnode.t_setThe materialization guess: nodes written and not read elsewhere in a sequence, plus every Fetch.Slice's parent and view so a parent can back a zero-copy alias view (gh-ocannl-293). A slice parent read elsewhere in the sequence is subtracted too. This preserves the historical recursive Seq subtraction, not a source-value dependency analysis: use collect_value_reads for dependencies and collect_mentioned for every node named by the code.
val collect_mentioned : t -> Tnode.t_setEvery node the code mentions, including slice parents, views and written destinations, but excluding Merge_buffer operands. Syntactic: forces no dims, projections or precision.
val collect_value_reads : t -> Tnode.t_setThe source-value dependencies of the code (gh-ocannl-2205): the nodes whose values it reads as operands. These are an Accum_op's Node right-hand-side buffers, or for Rev_sides its lhs (the source; its buffers are written); a Set_vec_unop's Node operand; and a Fetch.Slice's parent sliced, whether the slice lowers to a copy or to an alias view -- the view itself is defined by that Fetch, not read, so a reader of the view depends on the view and the view on its parent. A node the code writes and then reads is included: callers subtract what they provide or embed.
Not included: a Merge_buffer tn operand, which reads another stream's transferred buffer and not tn's value in this program; and the prior value an accumulation without initialize_neutral reads from its own lhs, which is a write (collect_written), so a gradient accumulation is not a dependency on its target. Syntactic: it forces no dims, projections or precision and marks no alias, so it is usable before shape inference closes. The materialization guess guess_materialized is unaffected by it.
val collect_written : t -> Tnode.t_setWhether the Slice of sliced into array lowers to an alias view of its parent rather than to a copy (gh-ocannl-293). Lineage-independent; forces both nodes' shapes, so it belongs where lowering follows, not where shape inference may still be open.
val forward_fetched :
?fetched_elsewhere:(Tnode.t -> Base.bool) ->
?view_aliases:(array:Tnode.t -> sliced:Tnode.t -> Base.bool) ->
comp ->
Base.Set.M(Ir.Tnode).tThe nodes whose forward code comp carries as a Fetch -- a Fetch into a node whose forward fragment comp owns (gh-ocannl-1876). Only Tensor.term emits such a Fetch for a leaf (a Tensor.term_init or literal with at most limit_constant_fill_size values, a range, an embedded symbol or id), as that leaf's whole forward code, and Operation.slice for a view; a gradient's zeroing Fetch has no forward fragment, and a parameter's initializer owns none (Tensor.init_params). These are the nodes every run re-establishes, so a write to one does not last: Context.compile's refusal and Train.sgd_update's are this one rule.
A Slice counts unless the write through it lasts: a view view_aliases (by default, any) over a parent this comp does not fetch and fetched_elsewhere (by default, none) does not name. Forward code orders the parent first. A write through an alias reaches the parent and stays over data, and over a fetched parent is undone when the parent is fetched again; a copy is copied again whatever its parent. Pass slice_alias_eligible once shapes are settled; a caller before that leaves the copies to one after.
val accumulates_from_zero : comp -> Base.Set.M(Ir.Tnode).tThe nodes comp first touches with an accumulation whose neutral element is zero and which resets nothing: =+, =-, =|| (gh-ocannl-2013). The operator says the node builds on what it holds, and on a fresh buffer that is the neutral element, so the program states its own start. A reduction, =:+, is not among them: it owes its init, and reading its target first means it lost it. Neither is a node something reads before accumulating into it, or that its own right-hand side reads.
val forward_recomputed : comp -> Base.Set.M(Ir.Tnode).tThe nodes comp re-establishes without using the value they had (gh-ocannl-1998): a node it writes whose resulting value is not built from its incoming value, directly or through the nodes it wrote on the way. Forward code that recomputes a node, or fetches one, is what this tells from a read-modify-write, a running statistic, which builds on whatever it found. Order counts: a read after a reset uses the new value, and a read of the old value that the node's own result does not reach (a delay line, y =: s; s =: x) does not save it. Takes the operators at their word: an operand Ops.binop_conditionality leaves unevaluated is no use of a value, and assignments are not simplified, so s =: s *. 0 does use s. Every write counts as covering its node, as whether a projection does cannot be asked before shapes settle.
A Slice counts exactly when its parent does, the view being taken as an alias of it; whether it is one is forward_fetched's question, for a caller with settled shapes.
Nothing here asks who owns a write, so a gradient's zeroing or a parameter's initializer in comp counts. Context asks it of each compiled routine, keeping the nodes whose Fwd fragment the routine owns -- the ones its forward code recomputes -- for persists' run check (gh-ocannl-2027); persists_undone asks it of a computation persisting a node.
Runs the computations in order, uniting their nodes, fragments and gradient sets; a gradient two members state different accumulation_runs for keeps the smaller, a member accumulating into it without zeroing it and stating nothing counting as one. Raises Utils.User_error, naming the fragment, when two members own the same fragment (gh-ocannl-1746): owned is a set, so the copy would otherwise run twice unseen -- for a backprop fragment, a double-counted gradient.
val persists_undone : comp -> Base.Set.M(Ir.Tnode).tThe nodes in comp's persists that comp itself re-establishes without reading them (forward_recomputed, a view counting as its parent): every run of comp undoes the previous run's persisting write, or this run's own, whichever order its code takes. Context.compile refuses a computation with any (gh-ocannl-2027).
val collect_neutral_elem : t -> Base.float Base.optionval to_low_level :
?static_indices:Indexing.static_symbol Base.list ->
t ->
Low_level.tval to_doc :
?name:Base.string ->
?static_indices:Indexing.static_symbol Base.list ->
Base.unit ->
t ->
PPrint.documentval to_string : t -> Base.stringThe code rendered as to_doc renders it, at the layout get_name_exn's refusal prints.
val derived_name : t -> Base.string Base.optionThe routine name the code's Block_comments derive (joined with _then_ across a Seq), or None when they derive none -- typically a bare t.Tensor.forward, which Train.forward wraps in a comment naming the routine. Renders nothing.
val nameless_code_excerpt : t -> Base.stringThe code as to_string renders it, cut to its first lines with a count of the rest: what a refusal of a nameless routine prints after its first line, to identify the comp.
val get_name_exn : t -> Base.stringderived_name, raising Invalid_argument where it is None. The message's first line names the fix, Train.forward, and the compile's ?name; a line The code: and the nameless_code_excerpt follow it. A compile entry point taking no ?name refuses through derived_name instead, with only the fixes it can take (Train.to_routine).
val lower :
?rewrite_target:Rewrites.target ->
?blends_value_selects:Base.bool ->
?accum_prec:(Ops.prec -> Ops.prec) ->
Low_level.optimize_ctx ->
unoptim_ll_source:(PPrint.document -> Base.unit) Base.option ->
ll_source:(PPrint.document -> Base.unit) Base.option ->
cd_source:(PPrint.document -> Base.unit) Base.option ->
name:Base.string ->
Indexing.static_symbol Base.list ->
t ->
Low_level.optimizedLowers and optimizes t. blends_value_selects is whether the backend charges the union of a speculable select's arms, passed on to Low_level.optimize (gh-ocannl-1775), and accum_prec its accumulator-residency resolver (gh-ocannl-2444); Backends.lower_assignments always states both.