TrainableTokensLayer
_ConfigLayer_WeightRewriteTrainableTokensLayer(base_layer: nn.Module, adapter_name: str, config: C)Adapter bookkeeping and the row replacement shared by the trainable-tokens layers.
Per adapter: trainable_tokens_delta (the trained rows), trainable_tokens_original
(the table's rows when the adapter was made) and the read-only mapping token_indices.
Several adapters on one layer must name disjoint tokens.
Parameters
base_ layernn.Modulebase_layer, its parameters shared.adapter_ namestrconfigTrainableTokensConfigtoken_indices are this layer's.Raises
TypeErrorbase_layer is not the module type the layer class adapts.ValueErrortoken_indices is empty or names a row the table lacks, or base_layer builds
parameters on its first input.Used by 2
Properties
3The adapters the forward pass applies, as a list.
The base layer's bias (a buffer on a quantized base), or None.
The base layer's weight (unchanged unless an adapter is merged into it).
Attributes
3Name (or names) of the adapter the forward pass applies.
Whether the forward pass runs the base layer alone.
Whether an adapter is folded into the base weight (merged_adapters non-empty).
Instance methods
12Remove adapter_name: its trained rows and the originals it kept.
Apply the adapter in the forward pass (True) or run the base alone (False).
Disabling a merged layer unmerges it first, here rather than on the next call, so the forward pass never writes a weight — a compiled forward must not replay that write.
Parameters
enabledboolThe active adapter, whether it is merged and whether adapters are disabled.
Run the layer on the table with the active adapters' rows in place.
The original table when adapters are disabled, every active adapter is merged or none
applies; two adapters that train the same token raise ValueError. A layer merged
for some of its adapters puts the rest's rows into the table that holds the merged ones.
Return the wrapped module, whose parameters are the base model's own.
One step down: where another tuner layer stands between, that layer, not the user's
module (_root goes all the way).
Return the change merging adapter makes to the table: trained minus original rows, zero elsewhere.
Parameters
adapterstrReturns
TensorShaped like the base weight.
Write the active adapters' rows into the table.
Parameters
safe_ mergebool= Falseadapter_ nameslist of str= NoneRaises
ValueErrormerge.set_adapter
→Nonefrom _BaseTunerLayerset_adapter(adapter_names: str | list[str], inference_mode: bool = False)Apply the named adapters, and train exactly those unless inference_mode.
The names this layer holds nothing for are skipped by the forward pass. A merged layer is unmerged first, since the active adapters are no longer the merged ones.
Parameters
adapter_ namesstr or list of strinference_ modebool= Falseset_requires_grad
→Nonefrom _BaseTunerLayerset_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)Set requires_grad on the parameters of the named adapters this layer holds.
supports_lora_conversion
→boolfrom _BaseTunerLayersupports_lora_conversion(adapter_name: str = 'default')Whether adapter_name on this layer can be converted to a plain LoRA update.
Take every merged update back out of the base weights, in place.
The result equals the original weights up to the rounding of the round trip.
Create adapter adapter_name from config.
The adapter is built whole before the layer holds any of it. A layer
that holds an adapter already keeps applying the adapters it applied:
the new one is inactive until set_adapter names it; a layer with
none starts applying this one.
Parameters
adapter_ namestrconfigCRaises
ValueErrorconfig does not fit the layer.