lora_patch ¶
The part of a LoRA adapter that is not a low-rank product.
A LoRA states a weight delta as B @ A. That form needs two things to be true: the base checkpoint must already contain the parameter, and the delta must actually be low rank. Distilled video checkpoints break both often enough that dropping whatever does not fit silently loses real signal.
Two payload kinds cover the gap, named after the convention ComfyUI's loader already reads so one file works in both places:
<module>.diff / <module>.diff_b An exact additive delta for a parameter the base model has. Used where a rank-r factorization buys nothing or cannot be formed at all -- RMSNorm vectors, biases, and matrices whose smaller dimension is already at or below the rank that would be chosen. Factoring a length-n vector into rank r costs r(1 + n) > n.
<module>.set_weight A whole parameter the base model does not carry, so no delta is expressible. MiniMax H3's VSA to_gate_compress is the case that motivated this: it exists only under the sparse-attention backend, and :func:load_model_from_full_model_state_dict otherwise zero-initializes it, which is exactly the "gate contributes nothing" state a VSA-distilled student was trained away from.
Both kinds are applied to the unsharded tensor while the checkpoint is streaming in, so FSDP and tensor-parallel placement come from the surrounding loader instead of being reimplemented here. That ordering is not a preference: maybe_load_fsdp_model shards the model on the meta device before any weight is read, so there is no earlier window, and patching afterwards would mean gathering and redistributing every affected parameter one at a time.
Because the adapter has to be known when the transformer loads, this path applies the adapter a pipeline is constructed with. Swapping to a different adapter later still goes through :meth:LoRAPipeline.set_lora_adapter, which handles the low-rank half only.
Classes¶
fastvideo.models.loader.lora_patch.DenseLoRAPatch ¶
DenseLoRAPatch(files: list[str], additive: dict[str, tuple[str, str]], replacement: dict[str, tuple[str, str]], strength: float = 1.0)
Adapter keys that address a whole parameter rather than a factor of one.
Construction only reads the safetensors headers, so the tensors themselves stay on disk until the loader asks for one. That keeps peak host memory at a single parameter even for adapters whose dense half is several GiB, which the VSA gates alone are.
Source code in fastvideo/models/loader/lora_patch.py
Attributes¶
fastvideo.models.loader.lora_patch.DenseLoRAPatch.replacement_parameters property ¶
Names of the parameters this adapter supplies outright.
Lets a caller decide what the adapter needs from the runtime -- an H3 adapter carrying to_gate_compress only works under the VSA backend -- without reading any tensor or guessing from the file name.
Methods:¶
fastvideo.models.loader.lora_patch.DenseLoRAPatch.apply_to ¶
apply_to(param_name: str, tensor: Tensor) -> Tensor
Add this parameter's .diff/.diff_b delta, if the adapter has one.
The sum is taken in float32 whatever the operands are: both sides arrive in bfloat16, whose 8-bit significand would quantize away a delta three orders of magnitude below the base weight. The caller casts back to the target dtype.
Source code in fastvideo/models/loader/lora_patch.py
fastvideo.models.loader.lora_patch.DenseLoRAPatch.from_adapter classmethod ¶
from_adapter(lora_path: str | None, param_names_mapping: Callable[[str], tuple[str, Any, Any]] | None = None, *, strength: float = 1.0) -> DenseLoRAPatch | None
Build a patch from an adapter, or None when it carries no dense payload.
param_names_mapping is the same callable the checkpoint loader uses, so adapter keys are resolved into the model's own parameter names by the identical rules -- an adapter written against the published checkpoint layout needs no separate conversion table.
Source code in fastvideo/models/loader/lora_patch.py
fastvideo.models.loader.lora_patch.DenseLoRAPatch.provides ¶
fastvideo.models.loader.lora_patch.DenseLoRAPatch.replacement_for ¶
replacement_for(param_name: str) -> Tensor | None
The adapter's whole-tensor value for a parameter, or None.
Source code in fastvideo/models/loader/lora_patch.py
fastvideo.models.loader.lora_patch.DenseLoRAPatch.report_unapplied ¶
Warn about dense keys that never reached a parameter.
An adapter key the loader silently ignores is the failure mode this whole path exists to fix, so it is said out loud rather than left to be inferred from a model that merely generates worse.
Source code in fastvideo/models/loader/lora_patch.py
Functions:¶
fastvideo.models.loader.lora_patch.normalize_lora_key ¶
Rewrite an adapter key to the <module>.lora_A|lora_B|lora_alpha spelling.
Returns None for keys the low-rank merge path deliberately does not handle -- the dense payload above, which lands during checkpoint load, and bookkeeping like .dora_scale. Callers use that to tell "not mine" apart from "mine and unmatched", which is the difference between a quiet skip and a warning.