解决Stable Diffusion TensorRT转换模型报错cpu and cuda:0! (when checking argument for argume

记录Stable Diffusion webUI TensorRT插件使用过程的报错:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat1 in method wrapper_CUDA_addmm)

拷贝下面的代码覆盖extensions\stable-diffusion-webui-tensorrt里的export_onnx.py文件,将模型和相关的张量移动到GPU,即可解决。

import os

from modules import sd_hijack, sd_unet
from modules import shared, devices
import torch


def export_current_unet_to_onnx(filename, opset_version=17):
    if torch.cuda.is_available():  
       print("CUDA is available")  
    else:  
        print("CUDA is not available")
    device = 'cuda' if torch.cuda.is_available() else 'cpu'  # 根据CUDA是否可用选择设备  
    shared.sd_model.model.diffusion_model.to(device)
    x = torch.randn(1, 4, 16, 16).to(devices.device, devices.dtype)
    timesteps = torch.zeros((1,)).to(devices.device, devices.dtype) + 500
    context = torch.randn(1, 77, 768).to(devices.device, devices.dtype)
    x = x.to(device)  
    timesteps = timesteps.to(device)  
    context = context.to(device)  
    print(x.device, timesteps.device, context.device)
    def disable_checkpoint(self):
        if getattr(self, 'use_checkpoint', False) == True:
            self.use_checkpoint = False
        if getattr(self, 'checkpoint', False) == True:
            self.checkpoint = False

    shared.sd_model.model.diffusion_model.apply(disable_checkpoint)

    sd_unet.apply_unet("None")
    sd_hijack.model_hijack.apply_optimizations('None')

    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with devices.autocast():
        torch.onnx.export(
            shared.sd_model.model.diffusion_model,
            (x, timesteps, context),
            filename,
            export_params=True,
            opset_version=opset_version,
            do_constant_folding=True,
            input_names=['x', 'timesteps', 'context'],
            output_names=['output'],
            dynamic_axes={
                'x': {0: 'batch_size', 2: 'height', 3: 'width'},
                'timesteps': {0: 'batch_size'},
                'context': {0: 'batch_size', 1: 'sequence_length'},
                'output': {0: 'batch_size'},
            },
        )

    sd_hijack.model_hijack.apply_optimizations()
    sd_unet.apply_unet()

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值