Open TheFerumn opened 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.
Changing commands into --no-half --no-half-vae instead of --upcast-sampling fixes it. As far as i see its known issue.
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.
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
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 ?
SD launched with DirectML, works and launches normally with this bug but inpainting doesn't work