Mikubill / sd-webui-controlnet

WebUI extension for ControlNet
GNU General Public License v3.0
17.11k stars 1.97k forks source link

[Bug]: RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same #1649

Closed bishalw closed 1 year ago

bishalw commented 1 year ago

Is there an existing issue for this?

What happened?

RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same

This is the error I get when I try to use the depth_leres++ preprocessor.

Steps to reproduce the problem

  1. Go to ControlNet
  2. Upload Image
  3. Try to use depth_leres++

What should have happened?

Should've gotten the depth map.

Commit where the problem happens

webui: 1.3.2 controlnet: 1.1.224

What browsers do you use to access the UI ?

Google Chrome

Command Line Arguments

export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --opt-sub-quad-attention --use-cpu interrogate"

This is what i currently used, but I've tried it without it as well.

List of enabled extensions

Screenshot 2023-06-16 at 4 15 53 AM

Console logs

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

################################################################
Running on john user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
Create and activate python venv
################################################################

################################################################
Launching launch.py...
################################################################
Python 3.10.12 (main, Jun  7 2023, 00:38:32) [Clang 14.0.3 (clang-1403.0.22.14.1)]
Version: v1.3.2
Commit hash: baf6946e06249c5af9851c60171692c44ef633e0
Installing requirements

Launching Web UI with arguments: --skip-torch-cuda-test --upcast-sampling --opt-sub-quad-attention --use-cpu interrogate
No module 'xformers'. Proceeding without it.
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
2023-06-16 04:29:20,196 - ControlNet - INFO - ControlNet v1.1.224
ControlNet preprocessor location: /Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/downloads
2023-06-16 04:29:20,244 - ControlNet - INFO - ControlNet v1.1.224
Image Browser: ImageReward is not installed, cannot be used.
Loading weights [52768d2bc4] from /Users/john/stable-diffusion-webui/models/Stable-diffusion/toonyou_beta3.safetensors
Creating model from config: /Users/john/stable-diffusion-webui/configs/v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 5.0s (import torch: 0.6s, import gradio: 0.6s, import ldm: 0.2s, other imports: 0.5s, load scripts: 2.0s, initialize extra networks: 0.2s, create ui: 0.7s, gradio launch: 0.2s).
Loading VAE weights specified in settings: /Users/john/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors
Applying optimization: sub-quadratic... done.
Textual inversion embeddings loaded(12): bad-artist, bad-artist-anime, bad-hands-5, bad-image-v2-39000, bad_pictures, bad_prompt_version2, bad_prompt_version2 (1), CyberRealistic_Negative-neg, EasyNegativeV2, negative_hand-neg, ng_deepnegative_v1_75t, verybadimagenegative_v1.3
Model loaded in 2.7s (load weights from disk: 0.2s, create model: 0.8s, apply weights to model: 0.7s, apply half(): 0.3s, move model to device: 0.5s).
2023-06-16 04:29:56,199 - ControlNet - INFO - Preview Resolution = 512
Traceback (most recent call last):
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/routes.py", line 422, in run_predict
    output = await app.get_blocks().process_api(
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/blocks.py", line 1323, in process_api
    result = await self.call_function(
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/blocks.py", line 1051, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet_ui/controlnet_ui_group.py", line 632, in run_annotator
    result, is_image = preprocessor(
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/utils.py", line 76, in decorated_func
    return cached_func(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/utils.py", line 64, in cached_func
    return func(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/global_state.py", line 35, in unified_preprocessor
    return preprocessor_modules[preprocessor_name](*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/processor.py", line 211, in leres
    result = model_leres(img, thr_a, thr_b, boost=boost)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/__init__.py", line 79, in apply_leres
    depth = estimateboost(input_image, model, 0, pix2pixmodel, max(width, height))
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/depthmap.py", line 418, in estimateboost
    whole_estimate = doubleestimate(img, net_receptive_field_size, whole_image_optimal_size, pix2pixsize, model, model_type, pix2pixmodel)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/depthmap.py", line 132, in doubleestimate
    estimate1 = singleestimate(img, size1, model, net_type)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/depthmap.py", line 156, in singleestimate
    return estimateleres(img, model, msize, msize)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/depthmap.py", line 43, in estimateleres
    prediction = model.depth_model(img_torch)
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/multi_depth_model_woauxi.py", line 32, in forward
    lateral_out = self.encoder_modules(x)
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/network_auxi.py", line 96, in forward
    x = self.encoder(x)  # 1/32, 1/16, 1/8, 1/4
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/Resnext_torch.py", line 223, in forward
    return self._forward_impl(x)
  File "/Users/john/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/leres/Resnext_torch.py", line 199, in _forward_impl
    x = self.conv1(x)
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/john/stable-diffusion-webui/extensions-builtin/Lora/lora.py", line 415, in lora_Conv2d_forward
    return torch.nn.Conv2d_forward_before_lora(self, input)
  File "/Users/john/stable-diffusion-webui/extensions/a1111-sd-webui-lycoris/lycoris.py", line 753, in lyco_Conv2d_forward
    return torch.nn.Conv2d_forward_before_lyco(self, input)
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 463, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/Users/john/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 459, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same

Additional information

I ran this on M1 Pro Max

Bavoch commented 1 year ago

same problem

luoluoluolin commented 1 year ago

same problem

jerrychoux commented 1 year ago

same problem

bala1802 commented 1 year ago

RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same

Encountered this error on M2

TylerBizz commented 1 year ago

RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same

I'm running into the same issue on Mac M2 Max when using controlnet depth_leres++ and depth_leres preprocessors

Belib commented 1 year ago

Same error M1 Max

quinlansls commented 1 year ago

Same issue, M1

misato2099 commented 1 year ago

Same error M1 Pro

AA8j commented 1 year ago

Same error M1 Max

frankRenlf commented 1 year ago

you need to set: net.to(devices) then it will be ok

AA8j commented 1 year ago

you need to set: net.to(devices) then it will be ok

More details?please

frankRenlf commented 1 year ago

The error means that you put data in mps but sht net still in cpu, so you need to put net in mps also.

def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
               devices=torch.device('mps')):
    # """用多GPU进行模型训练"""
    timer, num_batches = d2l.Timer(), len(train_iter)
    animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
                            legend=['train loss', 'train acc', 'test acc'])
    # net = nn.DataParallel(net, device_ids=devices).to(devices[0])
    net.to(devices)
    for epoch in range(num_epochs):
        # 4个维度:储存训练损失,训练准确度,实例数,特点数
        metric = d2l.Accumulator(4)
        for i, (features, labels) in enumerate(train_iter):
            timer.start()
            l, acc = train_batch_ch13(
                net, features, labels, loss, trainer, devices)
            metric.add(l, acc, labels.shape[0], labels.numel())
            timer.stop()
            if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
                animator.add(epoch + (i + 1) / num_batches,
                             (metric[0] / metric[2], metric[1] / metric[3],
                              None))
        test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
        animator.add(epoch + 1, (None, None, test_acc))
    print(f'loss {metric[0] / metric[2]:.3f}, train acc '
          f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
    print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
          f'{str(devices)}')
owspace commented 1 year ago

same error M1 Max

ga780586 commented 1 year ago

The error means that you put data in mps but sht net still in cpu, so you need to put net in mps also.

def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
               devices=torch.device('mps')):
    # """用多GPU进行模型训练"""
    timer, num_batches = d2l.Timer(), len(train_iter)
    animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
                            legend=['train loss', 'train acc', 'test acc'])
    # net = nn.DataParallel(net, device_ids=devices).to(devices[0])
    net.to(devices)
    for epoch in range(num_epochs):
        # 4个维度:储存训练损失,训练准确度,实例数,特点数
        metric = d2l.Accumulator(4)
        for i, (features, labels) in enumerate(train_iter):
            timer.start()
            l, acc = train_batch_ch13(
                net, features, labels, loss, trainer, devices)
            metric.add(l, acc, labels.shape[0], labels.numel())
            timer.stop()
            if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
                animator.add(epoch + (i + 1) / num_batches,
                             (metric[0] / metric[2], metric[1] / metric[3],
                              None))
        test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
        animator.add(epoch + 1, (None, None, test_acc))
    print(f'loss {metric[0] / metric[2]:.3f}, train acc '
          f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
    print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
          f'{str(devices)}')

請問這些代碼要放在哪裡?

ga780586 commented 1 year ago

I've found the solution: stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres/init.py

code: import cv2 import numpy as np import torch import os from modules import devices, shared from annotator.annotator_path import models_path from torchvision.transforms import transforms

AdelaiDepth/LeReS imports

from .leres.depthmap import estimateleres, estimateboost from .leres.multi_depth_model_woauxi import RelDepthModel from .leres.net_tools import strip_prefix_if_present

pix2pix/merge net imports

from .pix2pix.options.test_options import TestOptions from .pix2pix.models.pix2pix4depth_model import Pix2Pix4DepthModel

base_model_path = os.path.join(models_path, "leres") old_modeldir = os.path.dirname(os.path.realpath(file))

remote_model_path_leres = "https://huggingface.co/lllyasviel/Annotators/resolve/main/res101.pth" remote_model_path_pix2pix = "https://huggingface.co/lllyasviel/Annotators/resolve/main/latest_net_G.pth"

model = None pix2pixmodel = None

def unload_leres_model(): global model, pix2pixmodel if model is not None: model = model.cpu() if pix2pixmodel is not None: pix2pixmodel = pix2pixmodel.unload_network('G')

def apply_leres(input_image, thr_a, thr_b, boost=False): global model, pix2pixmodel

定義 device 變量

if torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('mps')

if model is None:
    model_path = os.path.join(base_model_path, "res101.pth")
    old_model_path = os.path.join(old_modeldir, "res101.pth")

    if os.path.exists(old_model_path):
        model_path = old_model_path
    elif not os.path.exists(model_path):
        from basicsr.utils.download_util import load_file_from_url
        load_file_from_url(remote_model_path_leres, model_dir=base_model_path)

    if torch.cuda.is_available():
        checkpoint = torch.load(model_path)
    else:
        checkpoint = torch.load(model_path, map_location=torch.device('cpu'))

    model = RelDepthModel(backbone='resnext101')
    model.load_state_dict(strip_prefix_if_present(checkpoint['depth_model'], "module."), strict=True)
    del checkpoint

if boost and pix2pixmodel is None:
    pix2pixmodel_path = os.path.join(base_model_path, "latest_net_G.pth")
    if not os.path.exists(pix2pixmodel_path):
        from basicsr.utils.download_util import load_file_from_url
        load_file_from_url(remote_model_path_pix2pix, model_dir=base_model_path)

    opt = TestOptions().parse()
    if not torch.cuda.is_available():
        device = torch.device('mps')  # 定義 device 變量
        opt.gpu_ids = []  # cpu mode
    pix2pixmodel = Pix2Pix4DepthModel(opt)
    pix2pixmodel.save_dir = base_model_path
    pix2pixmodel.load_networks('latest')
    pix2pixmodel.eval()

model = model.to(device)  # 使用正確的 device 變量

if devices.get_device_for("controlnet").type != 'mps':
    model = model.to(devices.get_device_for("controlnet"))

assert input_image.ndim == 3
height, width, dim = input_image.shape

with torch.no_grad():
    if boost:
        depth = estimateboost(input_image, model, 0, pix2pixmodel, max(width, height))
    else:
        depth = estimateleres(input_image, model, width, height)

    numbytes=2
    depth_min = depth.min()
    depth_max = depth.max()
    max_val = (2**(8*numbytes))-1

    if depth_max - depth_min > np.finfo("float").eps:
        out = max_val * (depth - depth_min) / (depth_max - depth_min)
    else:
        out = np.zeros(depth.shape)

    depth_image = out.astype("uint16")
    depth_image = cv2.convertScaleAbs(depth_image, alpha=(255.0/65535.0))

    if thr_a != 0:
        thr_a = ((thr_a/100)*255) 
        depth_image = cv2.threshold(depth_image, thr_a, 255, cv2.THRESH_TOZERO)[1]

    depth_image = cv2.bitwise_not(depth_image)

    if thr_b != 0:
        thr_b = ((thr_b/100)*255)
        depth_image = cv2.threshold(depth_image, thr_b, 255, cv2.THRESH_TOZERO)[1]

    return depth_image
stevesketch commented 1 year ago

I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)

ga780586 commented 1 year ago

I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)

NEW solution found:

open stable-diffusion-webui/webui-user.sh

in export COMMANDLINE_ARGS=

ADD --no-half

DONE!!!

Minyall commented 11 months ago

Tried this on my M1. The --no-half commandline arg didn't make a difference, however changing the code in the leres __init__.py did fix the issue. For those who don't get the code, just download the replacement file from this link, then navigate to

stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres

Rename __init__.py to old__init__.py or similar just in case you need to revert this. Then drop the downloaded version of __init__.py into the folder. Restart Automatic1111 and you should be good to go, so long as you select the leres preprocessor for Controlnet.

Credit to @ga780586 for actually writing the code. I just implemented it and shared my updated file.

LorenzoOrlandi commented 9 months ago

Thanks @ga780586 and @Minyall this actually worked! wish it was implemented into offical CN, as of v1.1.440 it's still not working out of the box

2000pd3rvr commented 9 months ago

I'm having the same error. But I'm only really a basic coder so I'm confused as to which code in which file needs to be updated. Could you explain with a bit more detail please? :)

The way this question has been answered, makes it seem like the issue is specific to the model mentioned, but it isn't. Porting any model from CUDA to mps will most likely require this change. And in response, also for the sake of other coders who may be less familiar with various platforms, I think a generic answer for this would be, aside adding images and labels to device, also add your model in same way. Example below;

images=images.to(devc) labels=labels.to(devc) model = model.to(devc)

before doing; outputs = model(images)

PinoYu commented 7 months ago

@Minyall I do did as you said but the issue still remain. Why is that? Please help.

PS: This issue happens while I am using the “controlnet-canny” function.

ddyaoshang commented 7 months ago

在我的 M1 上尝试过这个。 --no-half 命令行参数没有什么区别,但是更改 leres 中的代码__init__.py确实解决了问题。对于那些没有获得代码的人,只需从此链接下载替换文件,然后导航到

stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/leres

重命名__init__.pyold__init__.py或类似名称,以防万一您需要恢复此名称。然后将下载的版本放入__init__.py文件夹中。重新启动Automatic1111,只要您为Controlnet 选择leres 预处理器,您就可以开始了。

归功于@ga780586用于实际编写代码。我刚刚实现了它并分享了我更新的文件。

Thank you very much for solving my problem in the way you said

Shahriyar-Moradi commented 3 months ago

RuntimeError: Input type (MPSFloatType) and weight type (torch.FloatTensor) should be the same

device = torch.device("cuda:0" if torch.cuda.is_available() else "mps")inputs = inputs.to(device)

labels = labels.to(device) model=model.to(device) outputs = model(inputs).to(device) It will work