1
0
Fork 0
mirror of https://github.com/p-e-w/heretic.git synced 2026-12-03 18:42:57 +07:00

[GH-ISSUE #156] [Feature request] Sharded LoRA merge #92

Closed
opened 2026-11-01 04:17:26 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kabachuha on GitHub (Feb 14, 2026).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/156

Currently, the export algorithm requires the full model to be loaded into RAM before the LoRA merge and saving to drive.

However, it can be frustrating if the model size is much more than the RAM capability.

Yet, it is possible to apply LoRA per model shard. For each o_proj and mlp_proj component lora is simply a set of two low-rank matrices A and B, which are being added to the model's weight processing at inference. It is possible to fuse them into the weights using the simple algorithm.

    lora_weight = (lora_module.lora_A.weight @ lora_module.lora_B.weight) * lora_module.scaling
    module.weight += lora_weight.to(module.weight.dtype)

What I propose is to

  1. For the sharded transformer model like model-00001-of-00010.safetensors load each shard one by one.
  2. Iterate over the tensors in the shard.
  3. For the shard's target components, find the corresponding A and B matrices in the LoRA.
  4. Modify the weights by adding the matrix product.

This way will allow exporting very large models with smaller RAM.

Most quantization frameworks like llama.cpp read the model layer by layer and therefore too can operate in low RAM.

Originally created by @kabachuha on GitHub (Feb 14, 2026). Original GitHub issue: https://github.com/p-e-w/heretic/issues/156 Currently, the export algorithm requires the full model to be loaded into RAM before the LoRA merge and saving to drive. However, it can be frustrating if the model size is much more than the RAM capability. Yet, it is possible to apply LoRA per model shard. For each o_proj and mlp_proj component lora is simply a set of two low-rank matrices A and B, which are being added to the model's weight processing at inference. It is possible to fuse them into the weights using the simple algorithm. ```python lora_weight = (lora_module.lora_A.weight @ lora_module.lora_B.weight) * lora_module.scaling module.weight += lora_weight.to(module.weight.dtype) ``` What I propose is to 1. For the sharded transformer model like `model-00001-of-00010.safetensors` load each shard one by one. 2. Iterate over the tensors in the shard. 3. For the shard's target components, find the corresponding A and B matrices in the LoRA. 4. Modify the weights by adding the matrix product. This way will allow exporting very large models with smaller RAM. Most quantization frameworks like llama.cpp read the model layer by layer and therefore too can operate in low RAM.
Author
Owner

@p-e-w commented on GitHub (May 9, 2026):

LoRA export is now working, so we don't need such complicated logic anymore. If the user wants a special merge strategy they can export the LoRA and do it manually with a tool of their choice.

<!-- gh-comment-id:4412227789 --> @p-e-w commented on GitHub (May 9, 2026): LoRA export is now working, so we don't need such complicated logic anymore. If the user wants a special merge strategy they can export the LoRA and do it manually with a tool of their choice.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
p-e-w/heretic#92
No description provided.