Skip to content

fix: restore runtime LoRA tensor counts - #2400

Merged
leejet merged 2 commits into
masterfrom
fix/dgl-runtime-lora-counts
Nov 22, 2026
Merged

leejet merged 2 commits into
masterfrom
fix/dgl-runtime-lora-counts

Conversation

@leejet

@leejet leejet commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Restore runtime LoRA tensor counts in DGL. Log the applied count by model component and adapter file after execution, and warn when incompatible tensors are skipped.
  • Reset the counters after reporting so each sampling run produces one summary per adapter.

Related Issue / Discussion

N/A

Additional Information

N/A

Checklist

Summary by CodeRabbit

  • Improvements
    • Automatic LoRA mode now applies unquantized weights immediately when using the compute backend without row splitting, and applies LoRA at runtime for quantized, offloaded, or row-split weights.
    • LoRA adapter reports show clearer counts of applied and incompatible tensors, with optional component labels.
    • Runtime reporting captures adapter statistics when tensors were applied or skipped as incompatible.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Automatic LoRA mode selection now uses row-split status, backend placement, and tensor metadata. Model integrations and documentation use the resolved selection rules. LoRA execution also updates statistics reporting and tracking cleanup.

Changes: LoRA Mode and Execution

Layer / File(s) Summary
Mode resolution and model integration
src/dgl/models/lora.h, src/dgl/models/lora.cpp, src/dgl/models/model_runtime.cpp, src/dgl/models/model_base.h, src/dgl/models/model_base.cpp, src/dgl/model_value.cpp, src/dgl/models/detail_detector.cpp, src/dgl/models/upscaler.cpp, docs/dgl.md, docs/models/lora.md, examples/common/common.cpp
Automatic mode selection now considers row splitting, backend placement, and tensor quantization metadata. Runtime model setup, detector and upscaler integrations, and the related documentation use the resolved mode rules.
Statistics reporting
src/model/adapter/lora.hpp
LoraModel::stat accepts an optional component label and reports compatible and incompatible tensor counts. Unused-tensor and incomplete-application warnings remain limited to non-runtime calls.
Execution lifecycle
src/dgl/models/lora.h, src/dgl/models/lora.cpp
prepare stores the backend module. At execution end, the code records statistics and clears applied and skipped-incompatible tensor tracking when an adapter exists and either list is nonempty, then ends each model runner.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ModelRuntime
  participant ModelLoader
  participant SDBackendManager
  participant resolve_lora_mode
  ModelRuntime->>ModelLoader: inspect stored tensor metadata
  ModelRuntime->>SDBackendManager: inspect backend placement
  ModelRuntime->>resolve_lora_mode: provide mode and row-split status
  resolve_lora_mode-->>ModelRuntime: return resolved mode
Loading

Suggested reviewers: wbruna

Merge Risk: Minimal · up to 4cba5

This change unifies automatic LoRA mode selection, updates the documentation to match, and adds per-component LoRA tensor count reporting. No concrete merge-blocking issue was found. Normal build and runtime checks are still advisable.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix: restore runtime LoRA tensor counts" names the main change. The raw summary and PR description both show the change restores runtime LoRA tensor count logging by model component and ada…
Description check Passed The description follows the repository template. It includes Summary, Related Issue / Discussion, Additional Information, and Checklist sections. The Summary explains the change: runtime LoRA tensor c…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix: restore runtime LoRA tensor counts" names the main change. The raw summary and PR description both show the change restores runtime LoRA tensor count logging by model component and adapter, with warnings for skipped incompatible tensors and counter reset after reporting. The title is specific and concise. The summary also covers a mode-resolution refactor (resolve_lora_mode, auto mode selection) from the commit message, which the title does not mention. That gap is partial, not a mismatch.

Full details: Description check

Explanation

The description follows the repository template. It includes Summary, Related Issue / Discussion, Additional Information, and Checklist sections. The Summary explains the change: runtime LoRA tensor count logging per component and adapter, incompatible-tensor warnings, and counter reset after each run. Related Issue is marked N/A, and Additional Information is marked N/A. Both checklist items are checked. The description does not mention the auto-mode resolution refactor, the documentation updates for auto mode, or the removal of the persistent argument. These omissions are non-critical for the pass decision but reduce reviewer context.

Resolution

Optionally add a note to the Summary that describes the auto LoRA mode unification (resolve_lora_mode) and the related documentation changes, because the diff changes user-facing auto-mode behavior.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Cppcheck (2.22.0)

Cppcheck (2.22.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

src/dgl/models/model_runtime.cpp

Cppcheck interrupted; analysis of this file is incomplete

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7a424ebb-f293-432c-9358-6ecdfa2bdfcf
Commits

Reviewing files that changed from the base of the PR and between 7001abe and 4cba5db.

Files selected for processing: 11

  • docs/dgl.md
  • docs/models/lora.md
  • examples/common/common.cpp
  • src/dgl/model_value.cpp
  • src/dgl/models/detail_detector.cpp
  • src/dgl/models/lora.cpp
  • src/dgl/models/lora.h
  • src/dgl/models/model_base.cpp
  • src/dgl/models/model_base.h
  • src/dgl/models/model_runtime.cpp
  • src/dgl/models/upscaler.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@leejet
leejet merged commit 0d71f96 into master Nov 22, 2026
10 checks passed
@leejet
leejet deleted the fix/dgl-runtime-lora-counts branch November 22, 2026 18:44
filve pushed a commit to filve/diffusione-in-tempo-reale that referenced this pull request Nov 24, 2026
…art 2)

Brings in the Diffusion Graph Language runtime (leejet#2396) and the commits
after it: MOSS-Image edit LoRAs (leejet#2203), runtime LoRA counts (leejet#2400),
immediate LoRA merges for INT8 weights (leejet#2403), one server context with
on-demand model loading (leejet#2404), Qwen Image 2.1 prefix caching (leejet#2405)
and idle TTLs for model weights (leejet#2406). The server changes of leejet#2404
and leejet#2406 come in the same merge so that sd-server is ported once.

src/pipeline and src/stable-diffusion.cpp are gone; the fork's
additions move into the graph runtime:
- CPU fallback: ModelRuntime::configure_backends() falls back to the cpu
  backend, with a warning, when a module's backend cannot be
  initialized; disable_backend_fallback (also a DGL context option)
  makes it fail with SD_ERROR_BACKEND instead.
- memory_guard is a DGL context option for the context's ModelManager.
  ModelManager::ResourceHandle, the residency manager the models use,
  forwards the guard hooks and honours the prefetch suspension.
- layer_split_fill is applied when the context is created; multi_gpu is
  checked there. The multi-GPU planner, which upstream no longer calls,
  picks the device of each module when auto-fit is on, more than one
  GPU is present and no backend was given (planned_backend_spec() in
  src/api/generation_program.cpp); the run plan places the weights.
- The SD2 v-parameterization probe reports a failed compute instead of
  aborting.
- sd_ctx_create, sd_ctx_set_model_options*, sd_generate_image and
  sd_generate_video record the error code again; a load failure with no
  deeper cause is model_file.

sd-server keeps the ContextKeeper as the owner of the context:
- requests pick their model as upstream does (recipe and models in the
  body); the switch goes through ContextKeeper::load(), so the profile
  ladder and the recovery cover it too;
- sd_ctx_mutex serializes model switches, generations and DGL scripts;
  a DGL script runs on the keeper's context, or on a temporary one while
  no model is loaded (ContextKeeper::with_context()); the idle TTL
  release does not wait for a running generation (try_with_context());
- every context the keeper creates gets the server's file-access policy
  (create_context_from_params() + apply_server_file_access());
- /sdcpp/v1/models/load and /unload stay, rebuilt on
  resolve_model_selection(), as do /sdcpp/v1/health and /resources;
- /sdcpp/v1/upscale and the server-side ADetailer go, as upstream did:
  both are DGL scripts now.

Tests: memory_guard_engine_test follows the new capacity hook;
server_recovery_test checks with_context() and try_with_context()
(red with both behaviours broken); api_robustness expects a missing
model file at the first generation, where the weights load now;
server_cancel_e2e.py uses 100 steps, the server's maximum.

docs: the C API section of docs/integrazione_progetti.md uses the
opaque options; backend.md says what the multi-GPU planner decides
with DGL; api.md documents the kept load/unload routes.

Verified with a CPU build: the library, sd-cli and sd-server build; all
nine unit-test programs pass, server_recovery_test eight times in a row
and twice under TSan with no warnings; with a synthetic SD 1.5 model
(random weights) sd-cli generates, falls back to the cpu backend for
--backend vulkan0 and fails as backend with --disable-backend-fallback,
and test/server_cancel_e2e.py passes its 35 checks against a real
sd-server. A scratch smoke test of the server (20 checks: no model, DGL
jobs with and without a model, models/load and unload, a job that picks
its model, the idle release) passes too.

Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
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.

1 participant