Class Grad
java.lang.Object
ai.beestream.jitnn.Grad
Whether new operators record what autograd needs. On by default, per thread.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA try-with-resources scope that restores the previous gradient mode. -
Method Summary
Modifier and TypeMethodDescriptionstatic Tensorcheckpoint(Function<Tensor[], Tensor> segment, Tensor... inputs) Activation checkpointing, as PyTorch'storch.utils.checkpoint: runssegmentoninputsand keeps none of its activations for the backward pass, which runs the segment again to get them.static TensorRescales the gradients ofparamsso that their global L2 norm is at mostmaxNorm, as PyTorch'sclip_grad_norm_: every gradient is multiplied by min(1, maxNorm / (norm + 1e-6)).static Grad.Scopedisabled()Disables gradient recording until the returned scope is closed.static TensorRuns a forward pass for inference: no gradient recording, and every tensor created inside except the returned one is internal, so its buffer is reused as soon as the computation no longer needs it.static Grad.ScopeThe scope form ofinference(Supplier), for code that computes several outputs: no gradients, and intermediates are internal (their buffers are reused and inference fusions apply).static booleanWhether operators recorded now on this thread record what autograd needs.static TensorMarkstas read by the caller: its buffer is never reused.
-
Method Details
-
isEnabled
public static boolean isEnabled()Whether operators recorded now on this thread record what autograd needs. -
checkpoint
Activation checkpointing, as PyTorch'storch.utils.checkpoint: runssegmentoninputsand keeps none of its activations for the backward pass, which runs the segment again to get them. The segment's intermediates are freed after their last forward reader, so a training step holds the activations of one segment at a time instead of all of them, for about one more forward pass of compute. Gradients reach the inputs and the parameters the segment uses. Dropout draws the same masks when the segment runs again; BatchNorm in training mode updates its running statistics twice, as in PyTorch.for (Block b : blocks) { x = Grad.checkpoint(in -> b.forward(in[0]), x); } -
disabled
Disables gradient recording until the returned scope is closed. -
inferenceScope
The scope form ofinference(Supplier), for code that computes several outputs: no gradients, and intermediates are internal (their buffers are reused and inference fusions apply). Pass each output tokeep(Tensor)before the scope closes, or its buffer may be reused. -
keep
-
inference
Runs a forward pass for inference: no gradient recording, and every tensor created inside except the returned one is internal, so its buffer is reused as soon as the computation no longer needs it. Reading those intermediates after the call is not supported. -
clipNorm
Rescales the gradients ofparamsso that their global L2 norm is at mostmaxNorm, as PyTorch'sclip_grad_norm_: every gradient is multiplied by min(1, maxNorm / (norm + 1e-6)). Runs lazily on the device with the rest of the step and returns the norm before clipping as a scalar tensor (reading it is a sync point).
-