S-exp GPU
All pages
Docs · Function transformsMarkdown

Apply explicit model weights

(functional-call function bindings argument...) calls a function or model with its parameter reads rebound to bindings, a plist from full parameter-path keywords to tensors. Every parameter the call reads must have a binding with the declared shape and kind. A missing binding is E-FUNC-001; an incompatible value is E-FUNC-002. An omitted value never falls back to initialized weights.

Bindings apply through nested model calls and scans. The result has the function's ordinary structure, including parameters returned directly. The call changes no model weight. Frozen values and buffers that the function reads also need explicit bindings; parameters supplies the trainable subset only. A model with no buffers and no frozen parameters needs only the parameters tree.

Save the scalar model as scalar.sx, then run:

sexpgpu probe scalar.sx --expr '(let ((p (tree-map (lambda (w) (* w 0.5)) (parameters model)))) (functional-call model p (full [] 3.0)))' --max-elements 100

The result is 6.0; an ordinary call still uses the original weight 4.0.

Buffers and frozen values

Save this as buffered.sx:

(defmodel buffered ()
  (defparam weight (full [] 3.0))
  (defparam scale (full [] 4.0) :trainable false)
  (defbuffer offset (full [] 2.0) :after-step (lambda (old ctx) old))
  (lambda (x) (+ (* weight scale x) offset)))
(defvar model (buffered))

Combine the trainable, buffer and frozen trees explicitly with tree-merge:

sexpgpu probe buffered.sx --expr '(let ((all (tree-merge (parameters model) (buffers model) (frozen-parameters model)))) (functional-call model all (full [] 2.0)))' --max-elements 100

The result is 26.0: 3.0 * 4.0 * 2.0 + 2.0. Bind a different offset tensor to :model.offset to use a different buffer value for this call. When taking grad, pass only parameters as the differentiated argument and capture the other two trees outside it. All three queries must wait until model paths are assigned; query inside a function or lambda. An early query is E-FUNC-003.

A missing binding names the trainable parameter, buffer or frozen parameter and shows how to combine the trees. The call reads only the explicit bindings.

A functional call must return its data rather than emit metrics, diagnostics, counters, guards, records or training declarations. Such effects are refused with E-TRANSFORM-003; return observations as auxiliary values and report them outside the transformed call.