Skip to content
class

XLoraClassifier

extendsModule
XLoraClassifier(config: XLoraConfig, n_classes: int, n_layers: int, dtype: DTypeLike, device: str)

Predicts, for each token, how much each LoRA expert should weigh.

A stack of xlora_depth linear layers (ReLU and dropout between them) over the hidden states, (batch, tokens, hidden_size) to (batch, tokens, n_classes) — or n_classes * n_layers outputs with layerwise_scalings, spread over the adapted layers. The result is (batch, tokens, n_layers, n_classes), softmax-normalised over the experts (at softmax_temperature) unless enable_softmax is off. It reads its settings from the config it is given, so changing the config's softmax_temperature or scaling_pass_value takes effect at the next call.

Parameters

The classifier's shape (hidden_size set) and behaviour.
n_classesint
How many experts it weighs.
n_layersint
How many adapted layers the weights are for.
dtypelucid.dtype
The dtype of its parameters and of the weights it returns (the adapter's: the base model's, float32 for a half-precision one).
devicestr
The device it lives on (the base model's).

Attributes

The linear layers; their keys (layers.0.weight ...) are the adapter file's.
log_scalingslist of Tensor
The scalings of each call, while scalings_logging is on.

Examples

>>> import lucid
>>> from lucid.peft import XLoraConfig
>>> from lucid.peft.tuners.xlora._classifier import XLoraClassifier
>>> config = XLoraConfig(hidden_size=8, adapters={"a": "x", "b": "y"})
>>> classifier = XLoraClassifier(config, n_classes=2, n_layers=3, dtype=lucid.float32, device="cpu")
>>> classifier(lucid.randn(1, 5, 8)).shape
(1, 5, 3, 2)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: XLoraConfig, n_classes: int, n_layers: int, dtype: DTypeLike, device: str)

Build the layers for config and move them to device as dtype.

Instance methods

2
fn

dummy_scalings

→Tensor
dummy_scalings(batch: int, tokens: int, device: str)

The scalings of the scalings pass: scaling_pass_value for every token, layer and expert.

fn

forward

→Tensor
forward(hidden: Tensor)

The (batch, tokens, n_layers, n_classes) weights of the experts for hidden.