In this repo, when loading a model, is there some code to split a qkv tensor into separate q, k, v ?
Assistant
I will search for "qkv" in the codebase to find relevant code sections that might be splitting these tensors.
Assistant
I will examine comfy/utils.py to see how it handles the mapping and splitting of QKV tensors during model loading.
Assistant
I will search for the usages of mmdit_to_diffusers to understand how the key_map is used during model loading.
Assistant
I will search for the code that handles the slice tuple (target_key, (dim, start, length)) to see how it's used during state dict loading.
Assistant
I will search for the slicing logic, specifically looking for narrow or the pattern (dim, start, length) in the code.
Assistant
I will examine comfy/model_detection.py around line 1057 to see how narrow is used to split weights.
Assistant
I will read comfy/diffusers_convert.py to see how it handles QKV weights and if it performs any splitting.
Assistant
I will search comfy/sd.py for loading logic that might involve splitting QKV tensors.
Assistant
I will search for the term split in comfy/sd.py to see if there are any matches.
Assistant
I will search for the term qkv in comfy/sd.py.
Assistant
I will examine comfy/sd.py around lines 449 and 1054 to see how QKV weights are handled.
Assistant
I will examine comfy/sd.py around line 1054.
Assistant
I will search for "state_dict" and "split" in comfy/utils.py.
Assistant
I will search for functions in comfy/utils.py that take a state_dict and modify it.
User
Don't use grep because it's a large repo
Assistant
I'll examine comfy/diffusers_load.py to see if it handles splitting QKV tensors during model loading.
Assistant
I will search for "qkv" in comfy/supported_models.py to see if there are any specific handling or mapping rules for QKV tensors.
Assistant
I will read comfy/model_detection.py around line 1031 to identify the function containing the logic for splitting QKV tensors.
Assistant
I'll read comfy/diffusers_convert.py to check for any logic that converts state dictionaries to the Diffusers format.
Assistant
I'll read comfy/lora.py around line 386 to understand how narrow is used in load_lora.
Assistant
I'll read the beginning of comfy/utils.py to check for any splitting logic in load_torch_file.
Assistant
Yes, there is code that handles the splitting (and merging) of QKV tensors, primarily located in and , using mapping definitions from .
comfy/model_detection.py
comfy/lora.py
comfy/utils.py
ComfyUI generally stores QKV weights as a single combined tensor for efficiency. Therefore, "loading" code usually performs a merge (combining separate Q, K, V from a file into one QKV tensor) or a slice (accessing just the Q part of a combined tensor to apply a LoRA).
This file contains the logic to convert separate Diffusers keys (e.g., to_q, to_k) into a combined ComfyUI QKV tensor. It uses narrow() to slice the destination tensor and fill it with the source weights.
File:comfy/model_detection.py
Context:convert_diffusers_mmdit and similar functions.
Code:
python
# approx line 1057
w = old_weight.narrow(offset[0], offset[1], offset[2])
w[:] = fun(weight)
Here, old_weight is the combined QKV tensor. narrow creates a view (slice) for just the Q, K, or V part, and the incoming weight is copied into that slice.
2. LoRA Patching Logic (comfy/lora.py)
When loading a LoRA that targets only one component (e.g., Query) of a combined QKV tensor, this code splits the model's weight using narrow() to apply the patch.
File:comfy/lora.py
Context:calculate_weight function.
Code:
python
# approx line 386
if offset is not None:
old_weight = weight
weight = weight.narrow(offset[0], offset[1], offset[2])
3. Mapping Definitions (comfy/utils.py)
This file defines how to split/merge the tensors (the offset values used above).