In the Penzai V2 API, parameters and state variables are no longer ordinary PyTree nodes but are represented as mutable "variable" objects embedded within the model structure. This allows for sharing values between multiple parts of a model using standard Python reference semantics.
There are two primary types of variables:
Parameter: Used for model parameters that are updated by an optimizer but are not modified during the forward pass. They can be shared between multiple models or model components.StateVariable: A mutable variable intended to be modified during the forward pass.
Important Note on JAX Compatibility: Because these variable objects are mutable, they cannot be passed directly through most JAX transformations. You must use Penzai utilities to identify/extract variables and run model logic in a functional way. To support this, every variable object must have a unique label (either manually specified or automatically generated).
While the variables themselves are mutable leaves, the model layers remain immutable PyTree nodes, allowing for safe model copying and modification.