ComfyUI Memory Calculation Bug with Patch
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
### Custom Node Testing
- [x] I have tried disabling custom nodes and the issue persists (see [how to disable custom nodes](https://docs.comfy.org/troubleshooting/custom-node-issues#step-1%3A-test-with-all-custom-nodes-disabled) if you need help)
### Expected Behavior
An image generates in Flux on Jetson Thor.
### Actual Behavior
OOM memory error on system with 128GB of memory with 124GB of free memory.
### Steps to Reproduce
Run the reference Flux2 pipeline.
### Debug Logs
```powershell
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/execution.py", line 329, in get_output_data
return_values = await _async_map_node_over_list(prompt_id, unique_id, obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb, v3_data=v3_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/execution.py", line 303, in _async_map_node_over_list
await process_inputs(input_dict, i)
File "/home/matt/Local/ComfyUI/execution.py", line 291, in process_inputs
result = f(**inputs)
^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy_api/internal/__init__.py", line 149, in wrapped_func
return method(locked_class, **inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy_api/latest/_io.py", line 1513, in EXECUTE_NORMALIZED
to_return = cls.execute(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy_extras/nodes_custom_sampler.py", line 911, in execute
samples = guider.sample(noise.generate_noise(latent), latent_image, sampler, sigmas, denoise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=noise.seed)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/samplers.py", line 1035, in sample
output = executor.execute(noise, latent_image, sampler, sigmas, denoise_mask, callback, disable_pbar, seed, latent_shapes=latent_shapes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/patcher_extension.py", line 112, in execute
return self.original(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/samplers.py", line 984, in outer_sample
self.inner_model, self.conds, self.loaded_models = comfy.sampler_helpers.prepare_sampling(self.model_patcher, noise.shape, self.conds, self.model_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/sampler_helpers.py", line 130, in prepare_sampling
return executor.execute(model, noise_shape, conds, model_options=model_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/patcher_extension.py", line 112, in execute
return self.original(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/sampler_helpers.py", line 138, in _prepare_sampling
comfy.model_management.load_models_gpu([model] + models, memory_required=memory_required + inference_memory, minimum_memory_required=minimum_memory_required + inference_memory)
File "/home/matt/Local/ComfyUI/comfy/model_management.py", line 703, in load_models_gpu
loaded_model.model_load(lowvram_model_memory, force_patch_weights=force_patch_weights)
File "/home/matt/Local/ComfyUI/comfy/model_management.py", line 508, in model_load
self.model_use_more_vram(use_more_vram, force_patch_weights=force_patch_weights)
File "/home/matt/Local/ComfyUI/comfy/model_management.py", line 538, in model_use_more_vram
return self.model.partially_load(self.device, extra_memory, force_patch_weights=force_patch_weights)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/model_patcher.py", line 983, in partially_load
raise e
File "/home/matt/Local/ComfyUI/comfy/model_patcher.py", line 980, in partially_load
self.load(device_to, lowvram_model_memory=current_used + extra_memory, force_patch_weights=force_patch_weights, full_load=full_load)
File "/home/matt/Local/ComfyUI/comfy/model_patcher.py", line 782, in load
x[2].to(device_to)
File "/home/matt/Local/ComfyUI/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1371, in to
return self._apply(convert)
^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/ops.py", line 631, in _apply
self.register_parameter(key, torch.nn.Parameter(fn(param), requires_grad=False))
^^^^^^^^^
File "/home/matt/Local/ComfyUI/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1357, in convert
return t.to(
^^^^^
File "/home/matt/Local/ComfyUI/comfy/quant_ops.py", line 205, in __torch_dispatch__
return _GENERIC_UTILS[func](func, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/quant_ops.py", line 321, in generic_to_dtype_layout
return _handle_device_transfer(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/matt/Local/ComfyUI/comfy/quant_ops.py", line 272, in _handle_device_transfer
new_q_data = qt._qdata.to(device=target_device)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
torch.OutOfMemoryError: Allocation on device
Got an OOM, unloading all loaded models.
```
### Other
# ComfyUI Memory Management Bug Fix for Jetson/Unified Memory Devices
## Issue Summary
ComfyUI displayed incorrect VRAM values (1 PB / 1000+ TB) and crashed with Out of Memory errors when loading models on devices with unified memory architecture, specifically NVIDIA Jetson Thor with 128GB unified memory.
## Symptoms
1. **Incorrect VRAM Display**: Console showed absurd memory values like:
```
loaded completely; 95367431640625005117571072.00 MB usable, 17180.60 MB loaded, full load: True
```
(This displays as ~95 exabytes or ~1 petabyte instead of actual 128GB)
2. **Out of Memory Crashes**: Models failed to load with `torch.OutOfMemoryError` despite having sufficient memory available:
```
torch.OutOfMemoryError: Allocation on device
```
## Root Cause
The bug was caused by a sentinel value `1e32` (10^32 bytes) being used internally to represent "unlimited VRAM" when `lowvram_model_memory == 0`. This sentinel value:
1. Was displayed directly to users as available VRAM (`1e32 / (1024 * 1024) = 95367431640625005117571072 MB`)
2. Was used in actual memory allocation calculations, causing the memory manager to think it had unlimited memory
3. Led to incorrect memory management decisions and OOM crashes on memory-constrained devices
### Code Flow
1. `comfy/model_management.py:687-701` - Calculates `lowvram_model_memory` based on VRAM state
2. When certain conditions aren't met, `lowvram_model_memory` remains `0`
3. `comfy/model_management.py:504-505` - Sets `use_more_vram = 1e32` when `lowvram_model_memory == 0`
4. This huge value propagates through the memory management system
5. `comfy/model_patcher.py:794` - Logs the value, displaying the absurd number
6. Memory allocation attempts use this unrealistic value, causing OOM
## Fix Applied
Two changes were made to address both the display issue and the underlying memory management bug:
### 1. Display Fix (comfy/model_patcher.py:790-799)
**File**: `comfy/model_patcher.py`
**Lines**: 790-799
Added sanity check to cap displayed memory at actual total VRAM:
```python
# Cap displayed memory at actual total VRAM to avoid showing sentinel values like 1e32
display_memory = lowvram_model_memory
if display_memory > 1e30: # Detect sentinel value
display_memory = comfy.model_management.get_total_memory(device_to)
if lowvram_counter > 0:
logging.info("loaded partially; {:.2f} MB usable, ...".format(display_memory / (1024 * 1024), ...))
else:
logging.info("loaded completely; {:.2f} MB usable, ...".format(display_memory / (1024 * 1024), ...))
```
### 2. Memory Management Fix (comfy/model_management.py:504-508)
**File**: `comfy/model_management.py`
**Lines**: 504-508
Replaced sentinel value with actual free memory:
```python
use_more_vram = lowvram_model_memory
if use_more_vram == 0:
# Use actual available memory instead of 1e32 sentinel value
# to prevent OOM on devices with limited memory (e.g., Jetson)
use_more_vram = get_free_memory(self.device)
```
## Before vs After
### Before (Buggy Behavior)
```
loaded completely; 95367431640625005117571072.00 MB usable, 17180.60 MB loaded, full load: True
...
torch.OutOfMemoryError: Allocation on device
```
### After (Fixed Behavior)
```
loaded completely; 125772.18 MB usable, 17180.60 MB loaded, full load: True
[Model loads successfully]
```
## Impact
- **Fixes**: OOM crashes on Jetson and other unified memory devices
- **Fixes**: Misleading VRAM display showing petabytes instead of actual memory
- **Improves**: Memory management accuracy across all device types
- **Compatibility**: No breaking changes, works with existing workflows
## Testing Environment
- **Device**: NVIDIA Jetson Thor (Tegra platform)
- **Memory**: 128GB unified memory (shared CPU/GPU)
- **CUDA**: Device reports as "NVIDIA Thor" with compute capability 11.0
- **Model**: Flux2 (~30GB) and Flux2TEModel
- **Python**: 3.12
- **PyTorch**: Latest with CUDA support
## Files Modified
1. `comfy/model_patcher.py` - Lines 790-799 (display fix)
2. `comfy/model_management.py` - Lines 504-508 (memory calculation fix)
## Patch Diff
### comfy/model_patcher.py
```diff
@@ -787,10 +787,17 @@ class ModelPatcher:
for param in params:
self.pin_weight_to_device("{}.{}".format(n, param))
+ # Cap displayed memory at actual total VRAM to avoid showing sentinel values like 1e32
+ display_memory = lowvram_model_memory
+ if display_memory > 1e30: # Detect sentinel value
+ display_memory = comfy.model_management.get_total_memory(device_to)
+
if lowvram_counter > 0:
- logging.info("loaded partially; {:.2f} MB usable, {:.2f} MB loaded, {:.2f} MB offloaded, {:.2f} MB buffer reserved, lowvram patches: {}".format(lowvram_model_memory / (1024 * 1024), mem_counter / (1024 * 1024), lowvram_mem_counter / (1024 * 1024), offload_buffer / (1024 * 1024), patch_counter))
+ logging.info("loaded partially; {:.2f} MB usable, {:.2f} MB loaded, {:.2f} MB offloaded, {:.2f} MB buffer reserved, lowvram patches: {}".format(display_memory / (1024 * 1024), mem_counter / (1024 * 1024), lowvram_mem_counter / (1024 * 1024), offload_buffer / (1024 * 1024), patch_counter))
self.model.model_lowvram = True
else:
- logging.info("loaded completely; {:.2f} MB usable, {:.2f} MB loaded, full load: {}".format(lowvram_model_memory / (1024 * 1024), mem_counter / (1024 * 1024), full_load))
+ logging.info("loaded completely; {:.2f} MB usable, {:.2f} MB loaded, full load: {}".format(display_memory / (1024 * 1024), mem_counter / (1024 * 1024), full_load))
self.model.model_lowvram = False
```
### comfy/model_management.py
```diff
@@ -502,7 +502,9 @@ class LoadedModel:
# if self.model.loaded_size() > 0:
use_more_vram = lowvram_model_memory
if use_more_vram == 0:
- use_more_vram = 1e32
+ # Use actual available memory instead of 1e32 sentinel value
+ # to prevent OOM on devices with limited memory (e.g., Jetson)
+ use_more_vram = get_free_memory(self.device)
self.model_use_more_vram(use_more_vram, force_patch_weights=force_patch_weights)
```
## Related Context
This issue was introduced in recent commits that modified memory management logic, specifically commit `f17251bec65b5760cfedec29eace7d77f4b35130` which changed the minimum memory requirement from `128 * 1024 * 1024` to `0`, exposing the sentinel value issue.
## Recommendation
This fix should be merged to prevent OOM crashes on Jetson devices and other systems with unified memory. The changes are minimal, non-breaking, and improve memory management accuracy across all device types.
---
**Report submitted by**: RAMBOROGERS
**Date**: 2025-12-14
**ComfyUI Version**: v0.4.0 (fc657f47)
Contributor guide
Assessment
This issue has not been assessed yet.