Repository navigation
Add recompute_checkpoint/2 to compute a result again at every use - #1913
Draft
blasphemetheus wants to merge 15 commits into
Draft
blasphemetheus wants to merge 15 commits into
blasphemetheus wants to merge 15 commits into
Conversation
Ports the October per-use experiment (751d120) to current main. checkpoint/2 builds an Nx.block with an Nx.Block.Checkpoint struct. The result of that block is not kept: * The evaluator keeps the evaluated inputs and runs the body again for every consumer. * EXLA lowers the block again for every consumer. Each copy reads its inputs through an optimization barrier shared with the consumer's other operands, and the consumer reads those operands from the same barrier. Inputs that are checkpoint blocks are lowered again too, so a chain of checkpoints recomputes everything before each use. Gradients use the existing block rule. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The plain function already fuses the matrix into both uses there. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The construct has its own name and block struct, Nx.Block.Recompute, so it does not share a name with gradient checkpointing. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…luator EXLA gave a tied copy only to consumers lowered through the generic operator path. A while, another block, or an element of a tuple result got one copy that XLA merged with the others. The copies are now made in recur_operator for every kind of consumer, and the element of a tuple result is no longer cached. The evaluator kept the evaluated inputs of a recompute block, so an input that was itself a recompute block ran once. It now keeps a recipe for such inputs and runs it again on every use, as EXLA does. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…than a tuple Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Ports the cases written for checkpoint/2 that compare values and gradients with the plain function: cond, while, custom_grad, stop_grad, containers, vectorization, shared weights, nesting, higher-order gradients and random keys. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Run counts for a vectorized result, a side effect in the function, and scratch memory for a large result used inside cond and inside a while body. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The gradient of a while keeps no state per iteration, so one block per iteration saves nothing and a chain of blocks inside one iteration does. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Nx.Block.Recompute becomes Nx.Block.RecomputeCheckpoint. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Drops the cases carried over from the gradient checkpointing tests that only vary the body of the function. recompute_checkpoint/2 has no gradient rule of its own, so they exercised the general block gradient. Keeps one test per distinct path: uses, chains, nesting, tuple results, vectorized inputs, cond, while, side effects and input validation. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
On CUDA the plain function already computes the exponential once per use in the block consumer case, and the gradient of the loop needs the same scratch memory with or without recompute_checkpoint. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
Author
|
Right now in |
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it does
Without
recompute_checkpoint,Nx.exp(x)runs once andystays in memory until the last line. With it,Nx.exp(x)runs forNx.sum(y), the result is dropped, and it runs again fory * first.An input that is itself a
recompute_checkpointresult is computed again too:zis used twice, soNx.tanhruns twice, and each of those runs needsy, soNx.expruns twice as well. In a chain ofnblocks one use of the last result runs alln.Under grad
The backward pass is the main place a result has a late second use: the forward pass uses an activation, and much later the gradient of the next layer uses it again.
There is no gradient rule for it.
graddifferentiates the block like any other, and the saving comes from how the compiler lowers each use.For the gradient of 16 such blocks (two dense layers each, width 1024, batch 32768), peak scratch memory on CUDA is 0.93 GB against 4.45 GB plain.
grad_checkpoint/2from #1865 needs 2.47 GB. The cost is that inputs are rebuilt at every use: the compiled gradient has 575 matrix products against 95 plain, and that count grows with the square of the depth.On the host client the same gradient needs 6.57 GB against 8.46 GB plain, where
grad_checkpoint/2needs 2.42 GB. So this is the lower-memory option on a GPU only, and the slower one everywhere.How it works
recompute_checkpoint/2builds anNx.blockwith anNx.Block.RecomputeCheckpointstruct.Nx.Defn.Evaluatorstores a recipe for the block in place of its result and runs the recipe for every consumer.Limits
Two uses that do not depend on each other share one copy
Computing a result again saves memory when there is a gap between an early use and a late one, during which the result would otherwise be held. Here there is no gap. Neither use needs anything from the other, so the compiler may run them together and one copy serves both. EXLA compiles
Nx.exponce for this function, and twice when the second use waits on the first (first = Nx.sum(y)thenNx.sum(y * first)).Nx.Defn.Evaluatorruns the function once per use in both cases. The values agree; the amount of work differs between the two compilers.A
whiletakes one copy and carries itThe variables of a loop are stored values.
yis computed once when the loop starts, and from then onaccis an ordinary tensor the loop holds. To compute again on each iteration, callrecompute_checkpointinside the loop body.One block per iteration saves nothing in the gradient of a
whilegraddoes not store the state of each iteration of awhile. It rebuilds each state from the initial one when the backward pass needs it, so the gradient of a loop holds the intermediates of one iteration at a time. Nothing is kept across iterations for a block to give back.With one block of two dense layers per iteration, the gradient needs 472 MB of scratch memory on the host client with or without
recompute_checkpoint, and the same withgrad_checkpoint/2. When one iteration is a chain of four blocks, it drops from 1279 MB to 1080 MB, because there are activations inside the iteration that would otherwise be held. On CUDA that loop needs 809 MB either way, so inside awhilethe saving is on the host client only. In the gradient of a loop, the copies are tied only to values that exist when the iteration starts (the loop state and slices of the weights), so on CUDA they are computed at the start of the body and held.A side effect in the function happens once per use
The function runs for every use of its result, so this prints twice where the plain function prints once.
Tests
nx/test/nx/defn/recompute_checkpoint_test.exs(17 tests) counts runs of the function in the evaluator for two uses, a chain, a nested call, a tuple result, a vectorized result,condandwhile, and checks gradients against the plain function for the same cases.exla/test/exla/defn/recompute_checkpoint_test.exs(17 tests) counts instructions in the compiled program for the same consumers plus another block, and checks scratch memory forward and undergrad.🤖 Generated with Claude Code