LoRALinear¶
- class torchtune.modules.peft.LoRALinear(in_dim: int, out_dim: int, rank: int, alpha: float, dropout: float = 0.0, use_bias: bool = False, quantize_base: bool = False, **quantization_kwargs)[source]¶
LoRA 线性层,如 LoRA: Large Language Models 的低秩自适应 中介绍的那样。
LoRA 通过低秩近似扰动给定的层,其中只有秩分解矩阵是可训练的。在线性层中,LoRALinear 层不是 \(x \mapsto W_0x\),而是定义为 \(x \mapsto W_0x + (\alpha / r)BAx\),其中 \(r\) 是矩阵 \(A\) 和 \(B\) 的秩,\(\alpha\) 是比例因子。与原始实现一样,我们支持在与低秩矩阵相乘之前进行 dropout。
- 参数:
in_dim (int) – 输入维度
out_dim (int) – 输出维度
rank (int) – 低秩近似的秩
alpha (float) – 低秩近似的比例因子
dropout (float) – dropout 概率。默认值:0.0
use_bias (bool) – 是否在原始线性层中包含偏置。默认值:False
quantize_base (bool) – 是否量化基础线性权重。默认值:False
**quantization_kwargs – 量化基础线性权重时传递给 to_nf4 的关键字参数。有效参数的示例包括 block_size 和 scaler_block_size,它们分别控制权重量化和标量量化的粒度。这仅在 quantize_base 为 True 时使用。默认值 None
- Raises:
ValueError – 如果
quantize_base
为 False,但提供了量化 kwargs。
- adapter_params() List[str] [source]¶
返回与来自适配器的模型中
nn.Parameter
的名称相对应的字符串列表。对于 LoRA,这意味着 lora_a.weight 和 lora_b.weight。
- forward(x: Tensor) Tensor [source]¶
- 参数:
x (torch.Tensor) – 输入张量,形状为
(..., in_dim)
- Returns:
输出张量,形状为
(..., out_dim)
- Return type: