RuntimeError: “softmax_lastdim_kernel_impl“ not implemented for ‘Half‘

该段代码展示了如何将CLIP模型的权重从浮点数转换为半精度(fp16)浮点数,涉及到卷积层、线性层和MultiheadAttention层的权重处理。转换过程遍历模型的所有层,对适用于fp16的数据类型进行转换,以减小模型内存占用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CLIP/clip/model.py
第371行函数注释,如下

def _convert_weights_to_fp16(l):
        """
        if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)):
            l.weight.data = l.weight.data.half()
            if l.bias is not None:
                l.bias.data = l.bias.data.half()

        if isinstance(l, nn.MultiheadAttention):
            for attr in [*[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v"]:
                tensor = getattr(l, attr)
                if tensor is not None:
                    tensor.data = tensor.data.half()

        for name in ["text_projection", "proj"]:
            if hasattr(l, name):
                attr = getattr(l, name)
                if attr is not None:
                    attr.data = attr.data.half()
        """
        model.apply(_convert_weights_to_fp16)

参考 https://github.com/nerdyrodent/VQGAN-CLIP/issues/70

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值