Skip to content

[FIX] Patch non-writable NumPy arrays in GGUF loader to prevent PyTorch VRAM spikes #8329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion invokeai/backend/quantization/gguf/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def gguf_sd_loader(path: Path, compute_dtype: torch.dtype) -> dict[str, GGMLTens

sd: dict[str, GGMLTensor] = {}
for tensor in reader.tensors:
torch_tensor = torch.from_numpy(tensor.data)
torch_tensor = torch.from_numpy(tensor.data.copy() if not tensor.data.flags.writeable else tensor.data)
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional copy operation creates an unnecessary memory copy for every non-writeable array. Consider checking writeable status once and handling the two cases separately to avoid the overhead of the conditional check and copy operation in the hot path.

Suggested change
torch_tensor = torch.from_numpy(tensor.data.copy() if not tensor.data.flags.writeable else tensor.data)
if not tensor.data.flags.writeable:
data = tensor.data.copy()
else:
data = tensor.data
torch_tensor = torch.from_numpy(data)

Copilot uses AI. Check for mistakes.

shape = torch.Size(tuple(int(v) for v in reversed(tensor.shape)))
if tensor.tensor_type in TORCH_COMPATIBLE_QTYPES:
torch_tensor = torch_tensor.view(*shape)
Expand Down