学术
模块更新于 2026-11-21· 浏览 1· 下载 0

DPW 固定基底适配

对预训练的 V 投影做 SVD,得到一组固定的降维基底,只训练后续升维和可选缩放,把可训练参数压得很低;适配结果再与调用侧的前缀响应融合。

模型训练计算机视觉视觉语言模型持续学习参数高效微调

结构图

DPW 固定基底适配结构图
DPW 固定基底适配结构图

对预训练的 V 投影做 SVD,得到一组固定的降维基底,只训练后续升维和可选缩放,把可训练参数压得很低;适配结果再与调用侧的前缀响应融合。

论文信息

  • 论文:Enhancing Continual Learning of Vision-Language Models via Dynamic Prefix Weighting
  • 出处:CVPR 2026

核心痛点

视觉语言模型做持续学习时,逐任务全量微调代价太高;前缀或适配器方法又很难在固定参数预算下稳定复用预训练投影。

模块亮点

  • 对预训练的 V 投影做 SVD,得到一组固定的降维基底,只训练后续升维和可选缩放,把可训练参数压得很低;适配结果再与调用侧的前缀响应融合。

适用场景

适合视觉语言模型的域类增量持续学习,作为原前缀框架里的低预算适配部件。单独抽出时不含动态前缀权重和逐 Token 门控,其他任务需单独验证。

插入位置建议

  • 放在原前缀的适配器位置,与冻结骨干并联;需传入真实的预训练 QKV 权重来构造基底,并核对投影尺寸、瓶颈秩和输入末维是否匹配,残差开关按具体配置使用。

适用任务

  • 视觉语言模型的域类增量持续学习
  • 作为原前缀框架里的低预算适配部件。单独抽出时不含动态前缀权重和逐 Token 门控
  • 其他任务需单独验证

完整代码

dpw.py
"""107_DPW_CVPR2026: 官方核心模块的单文件提取。

官方源码: https://github.com/YonseiML/dpw/blob/a2f1ffc8ef440b5b5952dc58d25f9502e1baa4b1/clip/attention_reduced_param.py
源码 SHA256: 8e06d5f9216e39a506421950abd7c7e239d87c1776de50f5c5c31c57c10d3215
提取核心: Adapter
整理说明: 保留官方预训练 V 权重 SVD 固定降维基与可训练升维。移除强制 CUDA/float16,修复无投影分支 fixed_down 属性缺失;示例权重只用于接口检查,实际需传入预训练 QKV 权重。
形状验证只说明接口可用,不代表已复现论文指标。初始化/辅助输入遵循原仓库;
需要语义特征、掩码、时间等条件的模块不能省略对应条件。
"""
from __future__ import annotations

import torch
from torch import nn
import torch.nn.functional as F
import math


class Adapter(torch.nn.Module):

    def __init__(self, in_dim, out_dim, bottleneck=None, adapter_scalar=None, lr=0.01, dropout=0.0, in_proj_weight=None):
        super().__init__()
        self.register_buffer("fixed_down", None)
        fixed_down = None
        if bottleneck is not None:
            self.bottleneck = True
            down_lora = torch.nn.Linear(in_dim, bottleneck, dtype=torch.float32)
            up_lora = torch.nn.Linear(bottleneck, out_dim, dtype=torch.float32)
            with torch.no_grad():
                if in_proj_weight is not None:
                    v_matrix = in_proj_weight[in_dim * 2:].clone().to(torch.float32)
                    U, S, V = torch.svd(v_matrix.t())
                    fixed_down = U[:, :bottleneck].to(device=in_proj_weight.device, dtype=torch.float32)
                    self.fixed_down = fixed_down
                    torch.nn.init.normal_(up_lora.weight, mean=0.0, std=0.02)
                    torch.nn.init.zeros_(up_lora.bias)
                    self.lora = torch.nn.Sequential(torch.nn.Dropout(p=dropout), up_lora)
                else:
                    torch.nn.init.kaiming_uniform_(down_lora.weight, a=math.sqrt(5))
                    torch.nn.init.zeros_(up_lora.weight)
                    torch.nn.init.zeros_(down_lora.bias)
                    torch.nn.init.zeros_(up_lora.bias)
                    self.lora = torch.nn.Sequential(down_lora, torch.nn.ReLU(), torch.nn.Dropout(p=dropout), up_lora)
        else:
            self.lora = torch.nn.Linear(in_dim, out_dim, dtype=torch.float32)
            with torch.no_grad():
                torch.nn.init.zeros_(self.lora.weight)
                torch.nn.init.zeros_(self.lora.bias)
        if adapter_scalar == 'learnable_scalar':
            self.scale = torch.nn.Parameter(torch.empty(in_dim, dtype=torch.float32))
            torch.nn.init.uniform_(self.scale, -1, 1)
        else:
            self.scale = None
        self.lr = lr
        self.dropout = dropout

    def forward(self, x, residual=False):
        if self.fixed_down is not None:
            x = torch.matmul(x.clone(), self.fixed_down)
        out = self.lora(x)
        if self.scale is not None:
            scale = F.sigmoid(torch.matmul(x, self.scale.unsqueeze(1)))
            out = out * scale
        if residual:
            output = (out + x) * self.lr
        else:
            output = out * self.lr
        return output


def main():
    """最小使用示例:创建模块,验证输入与输出形状。"""
    torch.manual_seed(0)
    pretrained_qkv_weight = torch.randn(96,32)
    module = Adapter(32,32,bottleneck=8,in_proj_weight=pretrained_qkv_weight)
    input = torch.randn(2,16,32)
    output = module(input)
    assert input.shape == output.shape, (input.shape, output.shape)
    assert torch.isfinite(output).all(), "output contains NaN/Inf"
    print("input shape:", tuple(input.shape))
    print("output shape:", tuple(output.shape))
    return module, input, output


if __name__ == "__main__":
    main()

环境与依赖

Python 3.9+

继续浏览

下一篇DSAT 非平稳Transformer双模块互补设计:无参数序列平稳化模块归一化输入提升可预测性,去平稳注意力将原始序列的非平稳信息回注到注意力计算,完美解决过平稳化问题

相关资源