Skip to content

feat: auto fit tensors across devices to guarantee optimal load#1736

Merged
leejet merged 9 commits into
leejet:masterfrom
pwilkin:auto-fit
Jul 4, 2026
Merged

feat: auto fit tensors across devices to guarantee optimal load#1736
leejet merged 9 commits into
leejet:masterfrom
pwilkin:auto-fit

Conversation

@pwilkin

@pwilkin pwilkin commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Part 3 of the split from #1470, contains the auto-fitting logic.

Requires #1734, #1735

Related Issue / Discussion

#1470

pwilkin and others added 9 commits July 3, 2026 16:21
A --backend module assignment can now list several devices separated by
'&'. The module's transformer blocks are partitioned into contiguous
ranges sized proportionally to each device's free memory (minus a fixed
compute headroom) and registered with the ModelManager with per-tensor
compute backends; the existing allocation/staging/LoRA/residency
machinery handles the weights unchanged. The module's graphs execute on
a ggml_backend_sched spanning the devices, pinning each node to the
device of the most recently consumed weight (view ops are never pinned)
and splitting each graph exactly once. Supported for the diffusion and
te modules; for te the dominant encoder (t5xxl or the LLM) splits while
small sub-runners stay on the main device. Graph-cut segmentation and
--stream-layers are disabled for split modules. Adds --list-devices to
print the ggml device names accepted by the backend specs.

Manual placement only; row/tensor split and auto-fit are follow-ups.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ode row)

--split-mode selects how a module assigned multiple runtime devices
distributes its weights: layer (default) or row. In row mode the module
keeps executing on its main device while its transformer-block matmul
weights are allocated in the backend's row-split buffer type
(resolved through the "ggml_backend_split_buffer_type" proc, CUDA only
for now), which slices each weight's rows across the devices in
proportion to free memory and runs the matmuls multi-GPU internally.

The ModelManager owns the split buffer types (set_split_buffer_type):
params_buffer_type_for returns the split type for eligible tensors when
params live on the compute backend, and the staging path groups by
(backend, buffer type) and allocates with
ggml_backend_alloc_ctx_tensors_from_buft so cpu/disk params residency
stages straight into split buffers. Eligibility is limited to
contiguous 2D weights of at least 256x256 inside transformer blocks:
anything else is consumed by non-matmul ops or sliced into views,
which split buffers do not support. Direct LoRA application skips
row-split tensors; the automatic LoRA mode selects at_runtime when row
split is active. Falls back to a layer split when the backend has no
split buffer type or the devices span registries.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
--auto-fit derives the diffusion/te/vae placements from the model
metadata and per-device memory budgets, then feeds them through the
existing --backend / --params-backend assignment mechanism (the plan
and the emitted specs are printed). Budgets reuse --max-vram: positive
values cap a device, negative values mean free memory minus that many
GiB, unset defaults to free minus a 512 MiB margin.

When all components fit resident they are spread across the GPUs;
otherwise the plan switches to time-share, giving the heavy components
disk params residency (load per phase, free after) and splitting a
component too large for any single device across all GPUs via the
layer/row split mechanisms (--split-mode still selects which).
Components that fit nowhere fall back to the CPU. A VAE decode that
still runs out of memory retries once with tiling enabled (temporal
for the LTX video VAE, spatial otherwise).

Backend initialization now happens after model metadata is loaded so
the planner can size components before any backend exists. auto_fit
defaults to off.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
# Conflicts:
#	docs/backend.md
#	examples/common/common.cpp
#	examples/common/common.h
#	include/stable-diffusion.h
#	src/conditioning/conditioner.hpp
#	src/core/ggml_extend.hpp
#	src/core/ggml_extend_backend.cpp
#	src/core/ggml_extend_backend.h
#	src/core/util.cpp
#	src/model_manager.cpp
#	src/model_manager.h
#	src/stable-diffusion.cpp
@leejet leejet merged commit b11c95a into leejet:master Jul 4, 2026
11 checks passed
@leejet

leejet commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants