lshqqytiger / stable-diffusion-webui-amdgpu-forge

Forge for stable-diffusion-webui-amdgpu (formerly stable-diffusion-webui-directml)
GNU Affero General Public License v3.0
55 stars 4 forks source link

[BUG]: Error loading script: preprocessor_inpaint.py #13

Open TheFerumn opened 2 months ago

TheFerumn commented 2 months ago

SD launched with DirectML, works and launches normally with this bug but inpainting doesn't work

*** Error loading script: preprocessor_inpaint.py

Traceback (most recent call last):
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\modules\scripts.py", line 525, in load_scripts
    script_module = script_loading.load_module(scriptfile.path)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\modules\script_loading.py", line 13, in load_module
    module_spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\scripts\preprocessor_inpaint.py", line 13, in <module>
    from annotator.lama.saicinpainting.training.trainers import load_checkpoint
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\trainers\__init__.py", line 3, in <module>
    from annotator.lama.saicinpainting.training.trainers.default import DefaultInpaintingTrainingModule
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\trainers\default.py", line 11, in <module>
    from annotator.lama.saicinpainting.training.trainers.base import BaseInpaintingTrainingModule, make_multiscale_noise
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\trainers\base.py", line 16, in <module>
    from annotator.lama.saicinpainting.training.modules import make_generator  #, make_discriminator
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\modules\__init__.py", line 3, in <module>
    from annotator.lama.saicinpainting.training.modules.ffc import FFCResNetGenerator
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\modules\ffc.py", line 11, in <module>
    from annotator.lama.saicinpainting.training.modules.spatial_transform import LearnableSpatialTransformWrapper
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\extensions-builtin\forge_preprocessor_inpaint\annotator\lama\saicinpainting\training\modules\spatial_transform.py", line 4, in <module>
    from kornia.geometry.transform import rotate
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\__init__.py", line 11, in <module>
    from . import augmentation, color, contrib, core, enhance, feature, io, losses, metrics, morphology, tracking, utils, x
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\augmentation\__init__.py", line 1, in <module>
    from kornia.augmentation._2d import (
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\augmentation\_2d\__init__.py", line 3, in <module>
    from kornia.augmentation._2d.mix import *
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\augmentation\_2d\mix\__init__.py", line 1, in <module>
    from kornia.augmentation._2d.mix.cutmix import RandomCutMix, RandomCutMixV2
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\augmentation\_2d\mix\cutmix.py", line 7, in <module>
    from kornia.augmentation._2d.mix.base import MixAugmentationBase, MixAugmentationBaseV2
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\augmentation\_2d\mix\base.py", line 10, in <module>
    from kornia.geometry.boxes import Boxes
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\kornia\geometry\boxes.py", line 582, in <module>
    class Boxes3D:
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_script.py", line 1375, in script
    _compile_and_register_class(obj, _rcb, qualified_name)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_recursive.py", line 61, in _compile_and_register_class
    script_class = torch._C._jit_script_class_compile(
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_recursive.py", line 1007, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_script.py", line 1395, in script
    fn = torch._C._jit_script_compile(
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_recursive.py", line 1007, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_script.py", line 1395, in script
    fn = torch._C._jit_script_compile(
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_recursive.py", line 1007, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\_script.py", line 1392, in script
    ast = get_jit_def(obj, obj.__name__)
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\jit\frontend.py", line 331, in get_jit_def
    parsed_def = parse_def(fn) if not isinstance(fn, _ParsedDef) else fn
  File "C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\venv\lib\site-packages\torch\_sources.py", line 128, in parse_def
    raise RuntimeError(
RuntimeError: Expected a single top-level function: C:\Stable Diffusion\stable-diffusion-webui-amdgpu-forge\modules\dml\amp\autocast_mode.py:43
TheFerumn commented 2 months ago

I have no idea what i am doing but after some debugging with ChatGPT it suggested me to edit autocast_mode.py into this:

import importlib
from typing import Any, Optional
import torch

ops = ["torch.Tensor.__matmul__", "torch.addbmm", ...]  # truncated for brevity

supported_cast_pairs = {
    torch.float16: (torch.float32,),
    torch.float32: (torch.float16,),
}

def forward(op, *args):
    if not torch.dml.is_autocast_enabled:
        return op(*args)
    args = list(map(cast, args))
    return op(*args)

def cast(tensor: torch.Tensor):
    if not torch.is_tensor(tensor):
        return tensor
    dtype: torch.dtype = tensor.dtype
    if dtype not in supported_cast_pairs or (torch.dml.autocast_gpu_dtype != dtype and torch.dml.autocast_gpu_dtype not in supported_cast_pairs[dtype]):
        return tensor
    return tensor.type(torch.dml.autocast_gpu_dtype)

def cond(op: str):
    if isinstance(op, str):
        func_path = op.split('.')
        for i in range(len(func_path)-1, -1, -1):
            try:
                resolved_obj = importlib.import_module('.'.join(func_path[:i]))
                break
            except ImportError:
                pass
        for attr_name in func_path[i:-1]:
            resolved_obj = getattr(resolved_obj, attr_name)
        op = getattr(resolved_obj, func_path[-1])

        def forward_op(*args):
            return forward(op, *args)

        setattr(resolved_obj, func_path[-1], forward_op)

for o in ops:
    cond(o)

class autocast:
    prev: bool
    fast_dtype: torch.dtype = torch.float16

    def __init__(self, dtype: Optional[torch.dtype] = torch.float16):
        self.fast_dtype = dtype

    def __enter__(self):
        self.prev = torch.dml.is_autocast_enabled
        self.prev_fast_dtype = torch.dml.autocast_gpu_dtype
        torch.dml.is_autocast_enabled = True
        torch.dml.autocast_gpu_dtype = self.fast_dtype

    def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any):
        torch.dml.is_autocast_enabled = self.prev
        torch.dml.autocast_gpu_dtype = self.prev_fast_dtype

It works like a charm an no more errors but still inpainting doesn't work. I tries to generate. No errors but no effects.

TheFerumn commented 2 months ago

Changing commands into --no-half --no-half-vae instead of --upcast-sampling fixes it. As far as i see its known issue.

TheFerumn commented 1 month ago

Issue is still here but i can't fix it the same way as before. BTW: I am not sure what preprocessor_inpaint.py is for but inpainting works even with this error. I will just ignore it for now.

TheFerumn commented 1 month ago

When i try to upscale image with img2img tab i am having this error

Traceback (most recent call last):
  File "C:\Stable Diffusion\new-forge\modules_forge\main_thread.py", line 30, in work
    self.result = self.func(*self.args, **self.kwargs)
  File "C:\Stable Diffusion\new-forge\modules\img2img.py", line 246, in img2img_function
    processed = modules.scripts.scripts_img2img.run(p, *args)
  File "C:\Stable Diffusion\new-forge\modules\scripts.py", line 792, in run
    processed = script.run(p, *script_args)
  File "C:\Stable Diffusion\new-forge\scripts\sd_upscale.py", line 43, in run
    img = upscaler.scaler.upscale(init_img, scale_factor, upscaler.data_path)
  File "C:\Stable Diffusion\new-forge\modules\upscaler.py", line 68, in upscale
    img = self.do_upscale(img, selected_model)
  File "C:\Stable Diffusion\new-forge\extensions-builtin\SwinIR\scripts\swinir_model.py", line 52, in do_upscale
    img = upscaler_utils.upscale_2(
  File "C:\Stable Diffusion\new-forge\modules\upscaler_utils.py", line 181, in upscale_2
    output = tiled_upscale_2(
  File "C:\Stable Diffusion\new-forge\modules\upscaler_utils.py", line 142, in tiled_upscale_2
    out_patch = model(in_patch)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\spandrel\__helpers\model_descriptor.py", line 472, in __call__
    output = self._call_fn(self.model, image)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\spandrel\__helpers\model_descriptor.py", line 439, in <lambda>
    self._call_fn = call_fn or (lambda model, image: model(image))
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\spandrel\architectures\SwinIR\arch\SwinIR.py", line 1044, in forward
    x = self.conv_first(x)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\conv.py", line 460, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "C:\Stable Diffusion\new-forge\venv\lib\site-packages\torch\nn\modules\conv.py", line 456, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
  File "C:\Stable Diffusion\new-forge\modules\dml\amp\autocast_mode.py", line 43, in <lambda>
    setattr(resolved_obj, func_path[-1], lambda *args, **kwargs: forward(op, args, kwargs))
  File "C:\Stable Diffusion\new-forge\modules\dml\amp\autocast_mode.py", line 15, in forward
    return op(*args, **kwargs)
RuntimeError: Cannot set version_counter for inference tensor
Cannot set version_counter for inference tensor

Interesting thing is upscaling works in txt2img when using Hires. fix autocast_mode.py is broken and i am hoping for some fix soon

TheFerumn commented 3 weeks ago

I found this issue https://github.com/lshqqytiger/stable-diffusion-webui-amdgpu/issues/503 and there is solution to do downgrade to fix it but is there any way to fix it in Forge ? @lshqqytiger ?