Copy
SparseAdam
- class tensorplay.optim.sparse_adam.SparseAdam(params: Iterable[TensorBase] | Iterable[dict[str, Any]] | Iterable[tuple[str, TensorBase]], lr: float | TensorBase = 0.001, betas: tuple[float, float] = (0.9, 0.999), eps: float = 1e-08, maximize: bool = False)[source]
SparseAdam implements a masked version of the Adam algorithm suitable for sparse gradients. Currently, due to implementation constraints (explained below), SparseAdam is only intended for a narrow subset of use cases, specifically parameters of a dense layout with gradients of a sparse layout. This occurs in a special case where the module backwards produces grads already in a sparse layout. One example NN module that behaves as such is
nn.Embedding(sparse=True).SparseAdam approximates the Adam algorithm by masking out the parameter and moment updates corresponding to the zero values in the gradients. Whereas the Adam algorithm will update the first moment, the second moment, and the parameters based on all values of the gradients, SparseAdam only updates the moments and parameters corresponding to the non-zero values of the gradients.
A simplified way of thinking about the intended implementation is as such:
Create a mask of the non-zero values in the sparse gradients. For example, if your gradient looks like [0, 5, 0, 0, 9], the mask would be [0, 1, 0, 0, 1].
Apply this mask over the running moments and do computation on only the non-zero values.
Apply this mask over the parameters and only apply an update on non-zero values.
In actuality, we use sparse layout Tensors to optimize this approximation, which means the more gradients that are masked by not being materialized, the more performant the optimization. Since we rely on using sparse layout tensors, we infer that any materialized value in the sparse layout is non-zero and we do NOT actually verify that all values are not zero! It is important to not conflate a semantically sparse tensor (a tensor where many of its values are zeros) with a sparse layout tensor (a tensor where
.is_sparsereturnsTrue). The SparseAdam approximation is intended for semantically sparse tensors and the sparse layout is only an implementation detail. A clearer implementation would be to use MaskedTensors, but those are experimental.Note
If you suspect your gradients are semantically sparse (but do not have sparse layout), this variant may not be the best for you. Ideally, you want to avoid materializing anything that is suspected to be sparse in the first place, since needing to convert all your grads from dense layout to sparse layout may outweigh the performance gain. Here, using Adam may be the best alternative, unless you can easily rig up your module to output sparse grads similar to
nn.Embedding(sparse=True). If you insist on converting your grads, you can do so by manually overriding your parameters’.gradfields with their sparse equivalents before calling.step().- Parameters:
params (iterable) – iterable of parameters or named_parameters to optimize or iterable of dicts defining parameter groups. When using named_parameters, all parameters in all groups should be named
lr (float, Tensor, optional) – learning rate (default: 1e-3)
betas (Tuple[float, float], optional) – coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999))
eps (float, optional) – term added to the denominator to improve numerical stability (default: 1e-8)
maximize (bool, optional) – maximize the objective with respect to the params, instead of minimizing (default: False)
- step(closure=None)[source]
Perform a single optimization step.
- Parameters:
closure (Callable, optional) – A closure that reevaluates the model and returns the loss.
Help improve this page
Found an error, an unclear step, or a missing example?
