[Model] Add Inkling LoRA support [4/N] (#48884)
Signed-off-by: Woosuk Kwon <woosuk@inferact.ai> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Giancarlo Delfin <32987265+TheEpicDolphin@users.noreply.github.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Isotr0py <mozf@inferact.ai> Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
co-authored by
Bugen Zhao
Giancarlo Delfin
Isotr0py
Isotr0py
Jee Jee Li
Roger Wang
Yifan Qiao
Claude Fable 5
OpenAI Codex
parent
fe784ff22e
commit
f3e9497e92
@@ -1,6 +1,22 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Inkling logits processor with muP output scaling."""
|
||||
"""Inkling logits processor (muP + LoRA aware).
|
||||
|
||||
Inkling divides the final logits by a muP width multiplier
|
||||
(``logits_mup_width_multiplier``). This applies it two ways, depending on
|
||||
whether an lm_head LoRA is attached:
|
||||
|
||||
* No LoRA: fold ``1/mup`` into the lm_head GEMM alpha (fp32 epilogue) -- no
|
||||
separate elementwise kernel, no extra rounding, no weight mutation.
|
||||
* LoRA attached: the LoRA manager wraps this layer in
|
||||
``LogitsProcessorWithLoRA``, whose ``forward`` calls
|
||||
``type(base_layer).forward(self=wrapper)`` -- so this ``forward`` runs with
|
||||
``self`` bound to the wrapper. We detect that via ``base_layer`` and take the
|
||||
LoRA path: run the wrapper's ``_get_logits`` (base logits + the lm_head LoRA
|
||||
delta), then divide the full logits by the multiplier so the delta is scaled
|
||||
too. muP thus composes with the LoRA delta, with the dispatch as the only
|
||||
model-side branching.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -47,6 +63,46 @@ class InklingLogitsProcessor(LogitsProcessor):
|
||||
lm_head: VocabParallelEmbedding,
|
||||
hidden_states: torch.Tensor,
|
||||
embedding_bias: torch.Tensor | None = None,
|
||||
) -> torch.Tensor | None:
|
||||
# ``base_layer`` exists only on the LogitsProcessorWithLoRA wrapper,
|
||||
# which calls this forward with ``self`` bound to the wrapper. The
|
||||
# wrapper is not an ``InklingLogitsProcessor`` instance, so dispatch
|
||||
# ``_lora_forward`` explicitly through the base_layer's class (it
|
||||
# provides ``_get_logits``/``logits_as_input``; only ``_lora_forward``
|
||||
# lives on this class).
|
||||
if hasattr(self, "base_layer"):
|
||||
return type(self.base_layer)._lora_forward(
|
||||
self, lm_head, hidden_states, embedding_bias
|
||||
)
|
||||
return self._base_forward(lm_head, hidden_states, embedding_bias)
|
||||
|
||||
def _lora_forward(
|
||||
self,
|
||||
lm_head: VocabParallelEmbedding,
|
||||
hidden_states: torch.Tensor,
|
||||
embedding_bias: torch.Tensor | None = None,
|
||||
) -> torch.Tensor | None:
|
||||
# ``self`` is the LogitsProcessorWithLoRA wrapper here: ``_get_logits``
|
||||
# returns the base logits plus the lm_head LoRA delta. Apply the muP
|
||||
# divisor on the full logits so the LoRA delta is scaled too.
|
||||
mup_multiplier = self.base_layer.logits_mup_width_multiplier
|
||||
mup = 1.0 / mup_multiplier if mup_multiplier else None
|
||||
if self.logits_as_input:
|
||||
logits = hidden_states
|
||||
else:
|
||||
logits = self._get_logits(hidden_states, lm_head, embedding_bias)
|
||||
# TODO: fuse this multiplication
|
||||
if logits is not None and mup:
|
||||
assert self.base_layer.soft_cap is None
|
||||
assert self.base_layer.scale == 1.0
|
||||
logits = logits * mup
|
||||
return logits
|
||||
|
||||
def _base_forward(
|
||||
self,
|
||||
lm_head: VocabParallelEmbedding,
|
||||
hidden_states: torch.Tensor,
|
||||
embedding_bias: torch.Tensor | None = None,
|
||||
) -> torch.Tensor | None:
|
||||
mup = self.logits_mup_width_multiplier
|
||||
if not mup:
|
||||
|
||||
@@ -24,6 +24,7 @@ from vllm.model_executor.layers.quantization import QuantizationConfig
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead
|
||||
from vllm.model_executor.models.interfaces import (
|
||||
MultiModalEmbeddings,
|
||||
SupportsLoRA,
|
||||
SupportsMultiModal,
|
||||
SupportsPP,
|
||||
)
|
||||
@@ -360,7 +361,7 @@ class InklingModel(nn.Module):
|
||||
return self.norm(hidden_states)
|
||||
|
||||
|
||||
class _TmlForCausalLMBase(nn.Module, SupportsPP):
|
||||
class _TmlForCausalLMBase(nn.Module, SupportsPP, SupportsLoRA):
|
||||
"""Shared text-backbone causal-LM scaffolding for both entry classes."""
|
||||
|
||||
hf_to_vllm_mapper = WeightsMapper(
|
||||
@@ -380,6 +381,8 @@ class _TmlForCausalLMBase(nn.Module, SupportsPP):
|
||||
"model.llm.embed": "model.embed_tokens",
|
||||
"model.llm.norm": "model.norm",
|
||||
"model.llm.unembed": "lm_head",
|
||||
"language_model.layers.": "model.layers.",
|
||||
"language_model.lm_head.": "lm_head.",
|
||||
},
|
||||
orig_to_new_suffix={
|
||||
# NVFP4 scale
|
||||
@@ -389,6 +392,13 @@ class _TmlForCausalLMBase(nn.Module, SupportsPP):
|
||||
".w2_weight.scale2": ".w2_weight_scale_2",
|
||||
},
|
||||
)
|
||||
packed_modules_mapping = {
|
||||
"qkvr": ["wq_du", "wk_dv", "wv_dv", "wr_du"],
|
||||
"w13": ["w1", "w3"],
|
||||
}
|
||||
embedding_modules = {
|
||||
"lm_head": "output_embeddings",
|
||||
}
|
||||
|
||||
def _build(
|
||||
self,
|
||||
|
||||
@@ -343,6 +343,71 @@ class InklingSinkExperts(nn.Module):
|
||||
return h @ self.w2_weight.T # (T, D)
|
||||
|
||||
|
||||
class InklingSinkExpertsLinear(nn.Module):
|
||||
"""LoRA-capable implementation of the Inkling sink experts."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
n_experts: int,
|
||||
d_model: int,
|
||||
d_mlp: int,
|
||||
*,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
from vllm.model_executor.layers.linear import (
|
||||
MergedColumnParallelLinear,
|
||||
RowParallelLinear,
|
||||
)
|
||||
|
||||
self.n_experts = n_experts
|
||||
self.d_mlp = d_mlp
|
||||
total = n_experts * d_mlp
|
||||
self.w13 = MergedColumnParallelLinear(
|
||||
input_size=d_model,
|
||||
output_sizes=[total, total],
|
||||
bias=False,
|
||||
prefix=f"{prefix}.w13",
|
||||
)
|
||||
self.w2 = RowParallelLinear(
|
||||
input_size=total,
|
||||
output_size=d_model,
|
||||
bias=False,
|
||||
reduce_results=False,
|
||||
prefix=f"{prefix}.w2",
|
||||
)
|
||||
self._w2_input_pp = self.w2.input_size_per_partition
|
||||
self._col_expert: torch.Tensor | None = None
|
||||
|
||||
def _gamma_expand(self, gammas: torch.Tensor) -> torch.Tensor:
|
||||
if self._col_expert is None or self._col_expert.device != gammas.device:
|
||||
local = self._w2_input_pp
|
||||
start = get_tensor_model_parallel_rank() * local
|
||||
cols = torch.arange(start, start + local, device=gammas.device)
|
||||
self._col_expert = (cols // self.d_mlp).long()
|
||||
return gammas[:, self._col_expert]
|
||||
|
||||
def load_weight(self, key: str, weight: torch.Tensor) -> list[str]:
|
||||
if key == "w13_weight":
|
||||
d_model = weight.shape[-1]
|
||||
gate = weight[:, 0::2, :].reshape(-1, d_model).contiguous()
|
||||
up = weight[:, 1::2, :].reshape(-1, d_model).contiguous()
|
||||
self.w13.weight_loader(self.w13.weight, gate, 0)
|
||||
self.w13.weight_loader(self.w13.weight, up, 1)
|
||||
return ["w13.weight"]
|
||||
w = weight.permute(1, 0, 2).reshape(weight.shape[1], -1).contiguous()
|
||||
self.w2.weight_loader(self.w2.weight, w)
|
||||
return ["w2.weight"]
|
||||
|
||||
def forward(self, x: torch.Tensor, gammas: torch.Tensor) -> torch.Tensor:
|
||||
gate_up, _ = self.w13(x)
|
||||
gate, up = gate_up.chunk(2, dim=-1)
|
||||
hidden_states = torch.nn.functional.silu(gate) * up
|
||||
hidden_states = (hidden_states * self._gamma_expand(gammas)).to(x.dtype)
|
||||
output, _ = self.w2(hidden_states)
|
||||
return output
|
||||
|
||||
|
||||
class InklingMoE(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -417,7 +482,12 @@ class InklingMoE(nn.Module):
|
||||
layer_id
|
||||
), f"layer {layer_id}: NVFP4 shared experts are not supported"
|
||||
|
||||
self.sink_experts = InklingSinkExperts(
|
||||
sink_experts_cls = (
|
||||
InklingSinkExpertsLinear
|
||||
if get_current_vllm_config().lora_config is not None
|
||||
else InklingSinkExperts
|
||||
)
|
||||
self.sink_experts = sink_experts_cls(
|
||||
n_experts=n_shared,
|
||||
d_model=config.hidden_size,
|
||||
d_mlp=config.intermediate_size,
|
||||
|
||||
Reference in New Issue
Block a user