Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
New VRAM Monitoring throws errors on an AMD powered install
As the title says, after updating to the latest branch, stable diffusion stopped worked. After some debugging and rudimentary coding around, removing any references to the Memmon.py allows for the webui to work as usual again.

Perhaps an option to outright disable the memory monitoring?


Discussion:
What's the error?
There's a setting for `VRAM usage polls per second during generation. Set to 0 to disable.`, but depending on what AMD doesn't support, it's likely to still error, since there's still portions of code that get called even with polling off. I can fix it so 0 turns more code off.
Sorry for the sparse details. Tomorrow I'll test a little more and come back to you with some more precise data rather than 'there's an error lol'. But basically It makes a call for a function in torch that doesn't actually exist (in the AMD compatible version anyway). The program actually still technically works, (as in images are still generated) it's just that it bugs out the UI, preventing you from seeing the result unless you actually go into the outputs folder. 

Resolving patch:
diff --git a/modules/memmon.py b/modules/memmon.py
--- a/modules/memmon.py
+++ b/modules/memmon.py
@@ -22,6 +22,13 @@ def __init__(self, name, device, opts):
         self.run_flag = threading.Event()
         self.data = defaultdict(int)
 
+        try:
+            torch.cuda.mem_get_info()
+            torch.cuda.memory_stats(self.device)
+        except Exception as e:  # AMD or whatever
+            print(f"Warning: caught exception '{e}', memory monitor disabled")
+            self.disabled = True
+
     def run(self):
         if self.disabled:
             return
@@ -62,13 +69,14 @@ def monitor(self):
         self.run_flag.set()
 
     def read(self):
-        free, total = torch.cuda.mem_get_info()
-        self.data["total"] = total
-
-        torch_stats = torch.cuda.memory_stats(self.device)
-        self.data["active_peak"] = torch_stats["active_bytes.all.peak"]
-        self.data["reserved_peak"] = torch_stats["reserved_bytes.all.peak"]
-        self.data["system_peak"] = total - self.data["min_free"]
+        if not self.disabled:
+            free, total = torch.cuda.mem_get_info()
+            self.data["total"] = total
+
+            torch_stats = torch.cuda.memory_stats(self.device)
+            self.data["active_peak"] = torch_stats["active_bytes.all.peak"]
+            self.data["reserved_peak"] = torch_stats["reserved_bytes.all.peak"]
+            self.data["system_peak"] = total - self.data["min_free"]
 
         return self.data
 
diff --git a/modules/ui.py b/modules/ui.py
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -119,7 +119,9 @@ def save_files(js_data, images, index):
 
 def wrap_gradio_call(func):
     def f(*args, **kwargs):
-        shared.mem_mon.monitor()
+        run_memmon = opts.memmon_poll_rate > 0 and not shared.mem_mon.disabled
+        if run_memmon:
+            shared.mem_mon.monitor()
         t = time.perf_counter()
 
         try:
@@ -136,17 +138,20 @@ def f(*args, **kwargs):
 
         elapsed = time.perf_counter() - t
 
-        mem_stats = {k: -(v//-(1024*1024)) for k,v in shared.mem_mon.stop().items()}
-        active_peak = mem_stats['active_peak']
-        reserved_peak = mem_stats['reserved_peak']
-        sys_peak = '?' if opts.memmon_poll_rate <= 0 else mem_stats['system_peak']
-        sys_total = mem_stats['total']
-        sys_pct = '?' if opts.memmon_poll_rate <= 0 else round(sys_peak/sys_total * 100, 2)
-        vram_tooltip = "Torch active: Peak amount of VRAM used by Torch during generation, excluding cached data.&#013;" \
-                       "Torch reserved: Peak amount of VRAM allocated by Torch, including all active and cached data.&#013;" \
-                       "Sys VRAM: Peak amount of VRAM allocation across all applications / total GPU VRAM (peak utilization%)."
-
-        vram_html = '' if opts.memmon_poll_rate == 0 else f"<p class='vram' title='{vram_tooltip}'>Torch active/reserved: {active_peak}/{reserved_peak} MiB, <wbr>Sys VRAM: {sys_peak}/{sys_total} MiB ({sys_pct}%)</p>"
+        if run_memmon:
+            mem_stats = {k: -(v//-(1024*1024)) for k, v in shared.mem_mon.stop().items()}
+            active_peak = mem_stats['active_peak']
+            reserved_peak = mem_stats['reserved_peak']
+            sys_peak = mem_stats['system_peak']
+            sys_total = mem_stats['total']
+            sys_pct = round(sys_peak/max(sys_total, 1) * 100, 2)
+            vram_tooltip = "Torch active: Peak amount of VRAM used by Torch during generation, excluding cached data.&#013;" \
+                           "Torch reserved: Peak amount of VRAM allocated by Torch, including all active and cached data.&#013;" \
+                           "Sys VRAM: Peak amount of VRAM allocation across all applications / total GPU VRAM (peak utilization%)."
+
+            vram_html = f"<p class='vram' title='{vram_tooltip}'>Torch active/reserved: {active_peak}/{reserved_peak} MiB, <wbr>Sys VRAM: {sys_peak}/{sys_total} MiB ({sys_pct}%)</p>"
+        else:
+            vram_html = ''
 
         # last item is always HTML
         res[-1] += f"<div class='performance'><p class='time'>Time taken: <wbr>{elapsed:.2f}s</p>{vram_html}</div>"


Test patch: