[4/n] Support gpt oss 20b lora (#21570)
This commit is contained in:
@@ -629,7 +629,6 @@ class RowParallelLinearWithLoRA(BaseLayerWithLoRA):
|
||||
return lora_output
|
||||
|
||||
def forward(self, input_: torch.Tensor, skip_all_reduce=False):
|
||||
# duplicate the logic in RowParallelLinear
|
||||
if self.base_layer.input_is_parallel:
|
||||
input_parallel = input_
|
||||
else:
|
||||
@@ -638,8 +637,14 @@ class RowParallelLinearWithLoRA(BaseLayerWithLoRA):
|
||||
input_, num_partitions=self.base_layer.tp_size
|
||||
)
|
||||
input_parallel = splitted_input[tp_rank].contiguous()
|
||||
|
||||
bias_ = (
|
||||
None
|
||||
if (self.base_layer.tp_rank > 0 or self.base_layer.skip_bias_add)
|
||||
else self.base_layer.bias
|
||||
)
|
||||
output_parallel = self.base_layer.quant_method.apply(
|
||||
self.base_layer, input_parallel
|
||||
self.base_layer, input_parallel, bias=bias_
|
||||
)
|
||||
|
||||
should_reduce = (
|
||||
@@ -668,17 +673,8 @@ class RowParallelLinearWithLoRA(BaseLayerWithLoRA):
|
||||
else:
|
||||
output_ = output_parallel
|
||||
|
||||
if not self.base_layer.skip_bias_add:
|
||||
output = (
|
||||
output_ + self.base_layer.bias
|
||||
if self.base_layer.bias is not None
|
||||
else output_
|
||||
)
|
||||
output_bias = None
|
||||
else:
|
||||
output = output_
|
||||
output_bias = self.base_layer.bias
|
||||
return output, output_bias
|
||||
output_bias = self.base_layer.bias if self.base_layer.skip_bias_add else None
|
||||
return output_, output_bias
|
||||
|
||||
def slice_lora_a_weights(self, A: torch.Tensor, tp_rank: int):
|
||||
shard_size = self.base_layer.input_size_per_partition
|
||||
@@ -719,6 +715,9 @@ class FusedMoEWithLoRA(BaseLayerWithLoRA):
|
||||
self.intermediate_size_per_partition = getattr(
|
||||
base_layer, "intermediate_size_per_partition", None
|
||||
)
|
||||
self._uses_interleaved_gate_up = (
|
||||
getattr(base_layer.moe_runner_config, "gemm1_alpha", None) is not None
|
||||
)
|
||||
|
||||
# initialize triton_lora moe runner for batches with lora enabled
|
||||
from sglang.srt.layers.moe.moe_runner.runner import MoeRunner
|
||||
@@ -895,7 +894,10 @@ class FusedMoEWithLoRA(BaseLayerWithLoRA):
|
||||
gate_up_proj_moe B: [intermediate_size*2, rank] — output matches sharded base w13
|
||||
down_proj_moe B: [hidden_size, rank] — output is all-reduced, no slice
|
||||
"""
|
||||
if self.tp_size <= 1:
|
||||
needs_processing = (self.tp_size > 1) or (
|
||||
target_module == "gate_up_proj_moe" and self._uses_interleaved_gate_up
|
||||
)
|
||||
if not needs_processing:
|
||||
return B
|
||||
if target_module != "gate_up_proj_moe":
|
||||
return B
|
||||
@@ -923,6 +925,8 @@ class FusedMoEWithLoRA(BaseLayerWithLoRA):
|
||||
full_inter = B.shape[0] // 2
|
||||
gate_b = B[start:end, :]
|
||||
up_b = B[full_inter + start : full_inter + end, :]
|
||||
if self._uses_interleaved_gate_up:
|
||||
return torch.stack([gate_b, up_b], dim=1).reshape(-1, B.shape[-1])
|
||||
return torch.cat([gate_b, up_b], dim=0).contiguous()
|
||||
return B
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ class LoRAMemoryPool:
|
||||
# MoE expert version (4D)
|
||||
moe_key = f"{module_name}_moe"
|
||||
buffer[moe_key] = [
|
||||
torch.empty(
|
||||
torch.zeros(
|
||||
get_lora_shape_fn(
|
||||
moe_key, base_model, self.max_lora_rank, idx
|
||||
),
|
||||
@@ -327,7 +327,7 @@ class LoRAMemoryPool:
|
||||
else:
|
||||
# Standard allocation for unambiguous modules
|
||||
buffer[module_name] = [
|
||||
torch.empty(
|
||||
torch.zeros(
|
||||
get_lora_shape_fn(
|
||||
module_name,
|
||||
base_model,
|
||||
@@ -347,7 +347,7 @@ class LoRAMemoryPool:
|
||||
):
|
||||
target_modules = target_modules & set(EMBEDDING_NAMES)
|
||||
for module_name in target_modules:
|
||||
buffer[module_name] = torch.empty(
|
||||
buffer[module_name] = torch.zeros(
|
||||
get_lora_shape_fn(
|
||||
module_name,
|
||||
base_model,
|
||||
@@ -359,7 +359,7 @@ class LoRAMemoryPool:
|
||||
)
|
||||
|
||||
if self.lora_added_tokens_size > 0:
|
||||
self.new_embeddings_buffer["input_embeddings"] = torch.empty(
|
||||
self.new_embeddings_buffer["input_embeddings"] = torch.zeros(
|
||||
(
|
||||
self.max_loras_per_batch,
|
||||
self.lora_added_tokens_size,
|
||||
|
||||
@@ -88,9 +88,17 @@ def get_hidden_dim(
|
||||
elif module_name == "down_proj":
|
||||
return config.intermediate_size, config.hidden_size
|
||||
elif module_name == "gate_up_proj_moe":
|
||||
return config.hidden_size, config.moe_intermediate_size * 2
|
||||
moe_inter = (
|
||||
getattr(config, "moe_intermediate_size", None)
|
||||
or config.intermediate_size
|
||||
)
|
||||
return config.hidden_size, moe_inter * 2
|
||||
elif module_name == "down_proj_moe":
|
||||
return config.moe_intermediate_size, config.hidden_size
|
||||
moe_inter = (
|
||||
getattr(config, "moe_intermediate_size", None)
|
||||
or config.intermediate_size
|
||||
)
|
||||
return moe_inter, config.hidden_size
|
||||
elif module_name == "embed_tokens":
|
||||
# For embedding: input is vocab_size (as embedding lookup), output is hidden_size
|
||||
# if contain extra tokens will be added; otherwise is 0.
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
from collections.abc import Iterable
|
||||
from functools import partial
|
||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
||||
@@ -651,6 +652,13 @@ class GptOssModel(nn.Module):
|
||||
class GptOssForCausalLM(nn.Module):
|
||||
fall_back_to_pt_during_load = False
|
||||
|
||||
_lora_pattern_moe = re.compile(
|
||||
r"^(?:model\.layers\.\d+\.(?:self_attn\.(?:qkv_proj|o_proj)|mlp\.experts)|lm_head|model\.embed_tokens)$"
|
||||
)
|
||||
|
||||
def should_apply_lora(self, module_name: str) -> bool:
|
||||
return bool(self._lora_pattern_moe.match(module_name))
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: GptOssConfig,
|
||||
|
||||
Reference in New Issue
Block a user