-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[2/n] lora - Shared outer experts and support qwen3_30b_a3b_instruct #21466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
9f602f9
4f30d8c
add28dc
767655c
3ea7296
8fe34b6
a8ffbd0
a0b904b
ce6f45e
b396208
0f43488
b0daab1
8293a93
a59478b
32db555
44a26f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,17 +71,22 @@ | |
| class LoRAInfo: | ||
| """LoRA weights and dispatch info for MoE computation.""" | ||
|
|
||
| # LoRA weights: [num_loras, num_experts, dim1, dim2] | ||
| # LoRA weights: [num_loras, num_experts_or_1, dim1, dim2] | ||
| # When experts_shared_outer_loras=True: | ||
| # gate_up_lora_a: [num_loras, 1, max_rank, hidden_dim] (shared) | ||
| # down_lora_b: [num_loras, 1, hidden_dim, max_rank] (shared) | ||
| gate_up_lora_a_weights: ( | ||
| torch.Tensor | ||
| ) # [num_loras, num_experts, max_rank, hidden_dim] | ||
| ) # [num_loras, num_experts_or_1, max_rank, hidden_dim] | ||
| gate_up_lora_b_weights: ( | ||
| torch.Tensor | ||
| ) # [num_loras, num_experts, gate_up_dim, max_rank] | ||
| down_lora_a_weights: ( | ||
| torch.Tensor | ||
| ) # [num_loras, num_experts, max_rank, intermediate_dim] | ||
| down_lora_b_weights: torch.Tensor # [num_loras, num_experts, hidden_dim, max_rank] | ||
| down_lora_b_weights: ( | ||
| torch.Tensor | ||
| ) # [num_loras, num_experts_or_1, hidden_dim, max_rank] | ||
|
|
||
| # Indice pointers of each segment in shape (num_segments + 1, ) | ||
| seg_indptr: torch.Tensor | ||
|
|
@@ -95,6 +100,7 @@ class LoRAInfo: | |
| max_lora_rank: int # Maximum LoRA rank across all adapters | ||
|
|
||
| num_experts: int | ||
| experts_shared_outer_loras: bool = False | ||
|
|
||
| fully_sharded: bool = False | ||
| tp_size: int = 1 | ||
|
|
@@ -469,16 +475,11 @@ def _add_lora_gate_up_delta( | |
|
|
||
| r = lora_info.max_lora_rank | ||
| gate_up_a = lora_info.gate_up_lora_a_weights | ||
| if lora_info.experts_shared_outer_loras: | ||
| gate_up_a = gate_up_a.expand(-1, lora_info.num_experts, -1, -1) | ||
|
Comment on lines
450
to
+479
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. link 4 later. This seems suboptimal perf wise
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than expanding, we might create a new kernel that points all the expert loras into the same position |
||
| gate_up_b = lora_info.gate_up_lora_b_weights | ||
| inter_size = gate_up_b.shape[2] // 2 | ||
|
|
||
| # Split packed gate_up weights into separate gate and up slices. | ||
| # gate_up_lora_a has shape [max_loras, num_experts, 2*r, hidden_dim] | ||
| # where the first r rows are gate_lora_a and the next r are up_lora_a. | ||
| # gate_up_lora_b has shape [max_loras, num_experts, 2*inter_size, r] | ||
| # where the first inter_size rows are gate_lora_b and the rest up_lora_b. | ||
| # Using num_slices=2 lets the kernel handle gate and up independently, | ||
| # keeping the rank dimension at r so shrink and expand both match. | ||
| lora_a_stacked = [gate_up_a[:, :, :r, :], gate_up_a[:, :, r : 2 * r, :]] | ||
| lora_b_stacked = [ | ||
| gate_up_b[:, :, :inter_size, :], | ||
|
|
@@ -542,8 +543,12 @@ def _add_lora_down_delta( | |
| if lora_info.max_lora_rank == 0: | ||
| return | ||
|
|
||
| down_lora_b = lora_info.down_lora_b_weights | ||
| if lora_info.experts_shared_outer_loras: | ||
| down_lora_b = down_lora_b.expand(-1, lora_info.num_experts, -1, -1) | ||
|
|
||
| lora_a_stacked = [lora_info.down_lora_a_weights] | ||
| lora_b_stacked = [lora_info.down_lora_b_weights] | ||
| lora_b_stacked = [down_lora_b] | ||
|
|
||
| if lora_info.fully_sharded and lora_info.tp_size > 1: | ||
| shard_size = lora_info.hidden_size // lora_info.tp_size | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.