Skip to content
fn

cast_mixed_precision_params

→None
cast_mixed_precision_params(model: nn.Module, dtype: lucid.dtype)

Cast the model's frozen parameters to dtype and its trainable ones to float32.

For half-precision mixed-precision training with an adapter: the frozen base weights, which are most of the model, are kept in the smaller type to save memory, and the adapter's weights stay in full precision, which keeps their updates stable. The change is in place.

Parameters

The model whose parameters are cast.
dtypelucid.dtype
The type for the parameters that do not require a gradient — lucid.float16 or lucid.bfloat16.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, cast_mixed_precision_params, get_peft_model
>>> peft = get_peft_model(nn.Sequential(nn.Linear(4, 4)), LoraConfig(r=2, target_modules=["0"]))
>>> cast_mixed_precision_params(peft, lucid.float16)
>>> peft.get_base_model()[0].base_layer.weight.dtype
lucid.float16
>>> peft.get_base_model()[0].lora_A["default"].weight.dtype
lucid.float32

Used by 1