﻿# JAX buffer donation: stop using an input after handing its storage to the computation

Use JAX buffer donation with explicit input ownership, compatible outputs and preserved diagnostics, rather than treating donation as a harmless cache setting.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-jax-donation-input-ownership/
Language: en

## Identify the value that is being replaced

An optimizer step receives current model parameters and returns their replacements. That is a natural place to consider reusing storage because the old version may no longer be needed. The [JAX buffer-donation guide](https://docs.jax.dev/en/latest/buffer_donation.html) describes allowing a computation to reuse donated input buffers for outputs. Donation is an ownership decision about particular inputs, not a general instruction to clear unused GPU memory elsewhere in the process.

## Remove later reads from the caller

The [JIT API](https://docs.jax.dev/en/latest/_autosummary/jax.jit.html) states that donated buffers must not be reused by the caller. Review code after the call, including error reporting, checkpoint preparation and comparisons against prior parameters. A second Python name pointing to the same array does not create a preserved copy of its contents. If a diagnostic requires the previous state, arrange an appropriate independent representation before donating the value it needs.

## Inspect nested state as a collection of leaves

Model state often contains nested containers rather than a single array. The donation guide explains that donating a pytree applies to its component buffers. The [pytree guide](https://docs.jax.dev/en/latest/pytrees.html) describes the corresponding container and leaf structure. Review all affected leaves, including optimizer statistics that a monitoring routine may still read. A convenient top-level argument name can hide several different lifetimes inside one donated state object.

## Verify that reuse is actually usable

Compatible output storage is necessary for donation to be useful; the guide documents warnings for unusable donated buffers. Treat such a warning as information about the actual computation rather than proof that a global memory limit is wrong. Start with a representative step and inspect the warnings and output contract. Donation does not promise that every eligible input will be reused or that all temporary allocations disappear from the calculation.

## Keep recovery independent of invalidated inputs

A failure path should not depend on reading an input that the successful call was allowed to consume. Preserve the required recovery state deliberately and document when ownership changes. The guides on [activation checkpoint recomputation](/journal/activation-checkpoint-recomputation-graph/) and [stream-ordered allocation lifetimes](/journal/learn-stream-ordered-allocation-lifetime/) provide related memory concepts. The distinct point here is caller ownership: returning from a function does not restore permission to use a donated array's previous contents.
