mirror of
https://github.com/vladmandic/automatic
synced 2026-09-19 17:24:32 +02:00
update directml
This commit is contained in:
@@ -34,6 +34,7 @@ def wrap_gradio_gpu_call(func, extra_outputs=None):
|
||||
progress.record_results(id_task, res)
|
||||
except Exception as e:
|
||||
shared.log.error(f"Exception: {e}")
|
||||
errors.display(e, 'gradio call')
|
||||
res[-1] = f"<div class='error'>{html.escape(str(e))}</div>"
|
||||
finally:
|
||||
progress.finish_task(id_task)
|
||||
|
||||
+2
-1
@@ -7,7 +7,8 @@ parser._optionals = parser.add_argument_group('Other options') # pylint: disable
|
||||
group = parser.add_argument_group('Server options')
|
||||
|
||||
# main server args
|
||||
group.add_argument("--config", type=str, default=os.path.join(data_path, 'config.json'), help="Use specific configuration file, default: %(default)s")
|
||||
group.add_argument("--config", type=str, default=os.path.join(data_path, 'config.json'), help="Use specific server configuration file, default: %(default)s")
|
||||
group.add_argument("--ui-config", type=str, default=os.path.join(data_path, 'ui-config.json'), help="Use specific UI configuration file, default: %(default)s")
|
||||
group.add_argument("--medvram", action='store_true', help="Split model stages and keep only active part in VRAM, default: %(default)s")
|
||||
group.add_argument("--lowvram", action='store_true', help="Split model components and keep only active part in VRAM, default: %(default)s")
|
||||
group.add_argument("--ckpt", type=str, default=None, help="Path to model checkpoint to load immediately, default: %(default)s")
|
||||
|
||||
+13
-14
@@ -30,27 +30,22 @@ def get_cuda_device_string():
|
||||
return "cuda"
|
||||
|
||||
|
||||
def get_dml_device_string():
|
||||
if shared.cmd_opts.device_id is not None:
|
||||
return f"privateuseone:{shared.cmd_opts.device_id}"
|
||||
return "privateuseone:0"
|
||||
|
||||
|
||||
def get_optimal_device_name():
|
||||
if shared.cmd_opts.use_ipex:
|
||||
return "xpu"
|
||||
elif torch.cuda.is_available():
|
||||
elif torch.cuda.is_available() and not shared.cmd_opts.use_directml:
|
||||
return get_cuda_device_string()
|
||||
if has_mps():
|
||||
return "mps"
|
||||
try:
|
||||
import torch_directml # pylint: disable=import-error
|
||||
if shared.cmd_opts.use_directml:
|
||||
import torch_directml
|
||||
if torch_directml.is_available():
|
||||
return get_dml_device_string()
|
||||
torch.cuda.is_available = lambda: False
|
||||
if shared.cmd_opts.device_id is not None:
|
||||
return f"privateuseone:{shared.cmd_opts.device_id}"
|
||||
return torch_directml.device()
|
||||
else:
|
||||
return "cpu"
|
||||
except:
|
||||
return "cpu"
|
||||
|
||||
|
||||
def get_optimal_device():
|
||||
@@ -120,6 +115,8 @@ def set_cuda_params():
|
||||
global dtype, dtype_vae, dtype_unet, unet_needs_upcast # pylint: disable=global-statement
|
||||
# set dtype
|
||||
ok = test_fp16()
|
||||
if shared.cmd_opts.use_directml:
|
||||
shared.opts.no_half = True
|
||||
if ok and shared.opts.cuda_dtype == 'FP32':
|
||||
shared.log.info('CUDA FP16 test passed but desired mode is set to FP32')
|
||||
if shared.opts.cuda_dtype == 'FP16' and ok:
|
||||
@@ -187,10 +184,12 @@ def autocast(disable=False):
|
||||
|
||||
|
||||
def without_autocast(disable=False):
|
||||
if disable:
|
||||
return contextlib.nullcontext()
|
||||
if shared.cmd_opts.use_ipex:
|
||||
return torch.autocast("xpu", enabled=False) if torch.is_autocast_enabled() and not disable else contextlib.nullcontext()
|
||||
return torch.autocast("xpu", enabled=False) if torch.is_autocast_enabled() else contextlib.nullcontext()
|
||||
else:
|
||||
return torch.autocast("cuda", enabled=False) if torch.is_autocast_enabled() and not disable else contextlib.nullcontext()
|
||||
return torch.autocast("cuda", enabled=False) if torch.is_autocast_enabled() else contextlib.nullcontext()
|
||||
|
||||
|
||||
class NansException(Exception):
|
||||
|
||||
@@ -6,8 +6,8 @@ import modules.dml.hijack
|
||||
from .optimizer.unknown import UnknownOptimizer
|
||||
|
||||
class DirectML():
|
||||
def get_optimizer(device: torch.device):
|
||||
assert(device.type == 'privateuseone')
|
||||
def get_optimizer(self, device: torch.device):
|
||||
assert device.type == 'privateuseone'
|
||||
try:
|
||||
device_name = torch_directml.device_name(device.index)
|
||||
if 'NVIDIA' in device_name or 'GeForce' in device_name:
|
||||
@@ -22,8 +22,8 @@ class DirectML():
|
||||
except:
|
||||
return UnknownOptimizer
|
||||
|
||||
def memory_stats(device: torch.device):
|
||||
optimizer = DirectML.get_optimizer(device)
|
||||
def memory_stats(self, device: torch.device):
|
||||
optimizer = DirectML.get_optimizer(self, device)
|
||||
return optimizer.memory_stats(device.index)
|
||||
|
||||
# Alternative of torch.cuda for DirectML.
|
||||
|
||||
@@ -705,6 +705,16 @@ def restart_server(restart=True):
|
||||
log.info('Server will restart')
|
||||
|
||||
|
||||
def restore_defaults(restart=True):
|
||||
if os.path.exists(cmd_opts.config):
|
||||
log.info('Restoring server defaults')
|
||||
os.remove(cmd_opts.config)
|
||||
if os.path.exists(cmd_opts.ui_config):
|
||||
log.info('Restoring UI defaults')
|
||||
os.remove(cmd_opts.ui_config)
|
||||
restart_server(True)
|
||||
|
||||
|
||||
def listfiles(dirname):
|
||||
filenames = [os.path.join(dirname, x) for x in sorted(os.listdir(dirname), key=str.lower) if not x.startswith(".")]
|
||||
return [file for file in filenames if os.path.isfile(file)]
|
||||
|
||||
+3
-1
@@ -1322,6 +1322,7 @@ def create_ui():
|
||||
with gr.Blocks(analytics_enabled=False) as settings_interface:
|
||||
with gr.Row():
|
||||
settings_submit = gr.Button(value="Apply settings", variant='primary', elem_id="settings_submit")
|
||||
defaults_submit = gr.Button(value="Restore defaults", variant='primary', elem_id="defaults_submit")
|
||||
restart_submit = gr.Button(value="Restart server", variant='primary', elem_id="restart_submit")
|
||||
shutdown_submit = gr.Button(value="Shutdown server", variant='primary', elem_id="shutdown_submit")
|
||||
preview_theme = gr.Button(value="Preview theme", variant='primary', elem_id="settings_preview_theme")
|
||||
@@ -1453,6 +1454,7 @@ def create_ui():
|
||||
inputs=components,
|
||||
outputs=[text_settings, result],
|
||||
)
|
||||
defaults_submit.click(fn=lambda x: shared.restore_defaults(restart=True), _js="restart_reload")
|
||||
restart_submit.click(fn=lambda x: shared.restart_server(restart=True), _js="restart_reload")
|
||||
shutdown_submit.click(fn=lambda x: shared.restart_server(restart=False), _js="restart_reload")
|
||||
|
||||
@@ -1531,7 +1533,7 @@ def create_ui():
|
||||
]
|
||||
)
|
||||
|
||||
ui_config_file = cmd_opts.ui_config_file
|
||||
ui_config_file = cmd_opts.ui_config
|
||||
ui_settings = {}
|
||||
settings_count = len(ui_settings)
|
||||
error_loading = False
|
||||
|
||||
Reference in New Issue
Block a user