Repository navigation
Studio: release a streamed encoder's safetensors mapping once its weights are pinned - #16914
Conversation
…ghts are pinned load_plain_prequant_safetensors returned safetensors' tensors as views of one whole-file mapping. Registered as buffers (Int8ConvRotLinear's weight and weight_scale), they keep that view base through the .data re-point that moves a streamed weight onto its pinned copy, so the whole file stayed mapped and resident beside the pinned copy for the life of the model (8.7 GiB for Qwen-Image-2.1's int8 text encoder under a VRAM budget). Hand back base-less aliases of the same storage instead (no copy).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…rs; test file deletion too build_text_encoder kept a single-file encoder's int8 ConvRot codes as views of the safetensors mapping (codes.to(torch.int8) returns the view), so the streaming pin's .data re-point kept the whole file mapped. Load base-less aliases instead. The regression tests now also delete the file after the re-point, which an open mapping refuses on Windows.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Real host RAM (
MemAvailable) does not change with this PR. The memory released is a clean, file-backed page-cache mapping that the kernel could already reclaim. What drops is the process's own RSS / PSS: the memory Studio appears to use intop, task managers and PSS-based tooling. The open mapping of the model file is also released.Under a VRAM budget, Studio streams the int8 text encoder from a pinned host copy. The encoder's original safetensors file then stayed mapped and fully resident for the life of the model, on top of the pinned copy. On Qwen-Image-2.1 that is the whole 8.7 GiB int8 encoder file, so the process showed about 28.6 GiB PSS at 8 and 12 GiB budgets when about 19.9 GiB is actually needed.
This PR makes the plain pre-quant loader return tensors that do not hold that mapping. Once the weights are pinned, the file is unmapped. Outputs are bit-identical and per-step speed is unchanged.
What / why
load_plain_prequant_safetensorsnow returns each tensor as a base-less alias of the same storage (torch.empty(0).set_(storage, offset, size, stride), no copy)._mapped_tensorsin the same file already builds its tensors this way.build_text_encoderindiffusion_comfy_components.py(single-file int8 ConvRot text encoders) had the same leak:codes.to(torch.int8)returns the view itself. It now loads base-less aliases too. The MiniMax-H3 conditioner is not affected:pin_module_in_placealready replaces_parameters/_buffersentries instead of using.data, for this reason.safe_open(...).get_tensor()returns views of one storage that maps the whole file.Int8ConvRotLinearregistersweightandweight_scaleas buffers.load_state_dict(assign=True)installs those views as they are.tensor.data = pinned(_GroupPinner, the pin arena). That swaps the storage but keeps the view's_base, so every int8 weight and scale kept the old file storage alive.nn.Parameter(view)already drops the base. That is why the DiT never leaked and only the buffer-based encoder did.MemAvailabledid not move: the kernel could reclaim those pages.Evidence (Colab RTX PRO 6000 G4, one VM, base
ccc8a827d2vs head, fresh process per cell)Qwen-Image-2.1 t2i 1024^2, 25 steps, VRAM capped by ballast. Cells ran in the order B H H B per budget. Steady-state numbers are taken after 3 renders.
/proc/self/smapson base shows the encoder's 8.71 GiB safetensors blob mapped and fully resident. On head it is gone.Where the remaining 19.9 GiB at 8 GiB goes (head, smaps):
Simulation (CPU, merge base
ccc8a827d2vs head)A synthetic plain pre-quant file was loaded by the base and head
load_plain_prequant_safetensors. It held int8 2-D, fp32(N, 1), bf16, fp8, a misaligned int8, an empty tensor, a root tensor, and anlm_headloaded with and withoutskip_names. The run was repeated on torch 2.6.0 + safetensors 0.4.5, torch 2.12.1 and torch 2.14.1 (safetensors 0.8.0)._base is not None(True -> False, per tensor) and "file still mapped after the.data =re-point" (True -> False).requires_gradtorch.savesize and round tripsave_fileof the loaded dict (succeeds on both)add_(the tensor changes and the file is untouched, copy-on-write on both)load_plain_prequant_safetensorsarediffusion_te_prequant.load_prequant_text_encoderandvideo_ltx25_te_worker. Neither reads._base/is_view, and both only cast (copy) orload_state_dict(assign=True). The text-encoder release snapshot copies before saving (tensor.to("cpu", copy=True)). No code instudio/backend/corereads._baseof a tensor.os.remove), so on Windows they fail on base and pass on head; Linux also checks/proc/self/maps.Tests
test_int8_convrot_encoder_releases_the_file_once_its_weights_are_repointedintest_diffusion_comfy_components.py. It fails on base and passes on head, with the state dict unchanged.test_a_buffer_repointed_at_its_pinned_copy_releases_the_file_mappinginstudio/backend/tests/test_prequant_safetensors.py. It loads a plain checkpoint, registers the tensor as a buffer, re-points it with.data =the way the pinner does, and checks/proc/self/maps.test_differential.pyagainst the merge base: DISCRIMINATING, fixed=2, unchanged=107, head_fail=0.Not proven
/proc/self/maps.MemAvailableheadroom is unchanged. What changes is the reported footprint and the open mapping.