[XPU] Dispatch nn.LayerNorm to fused SYCL kernel via CustomOp (#57172)
Signed-off-by: Michal Ganczarenko <michal.ganczarenko@intel.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
Harry Mellor
parent
a4c4bb418e
commit
df5668e5c9
@@ -71,6 +71,7 @@ from vllm.model_executor.models.transformers.utils import (
|
||||
named_state,
|
||||
replace_conv_class,
|
||||
replace_embedding_class,
|
||||
replace_layernorm_class,
|
||||
replace_linear_class,
|
||||
)
|
||||
from vllm.model_executor.models.utils import (
|
||||
@@ -466,6 +467,7 @@ class Base(
|
||||
- `nn.Conv2d` / `nn.Conv3d` with vLLM's `Conv2d` / `Conv3d`
|
||||
- Vocab `nn.Embedding`s with vLLM's `VocabParallelEmbedding`
|
||||
- RMSNorm (detected from their dataflow) with vLLM's `RMSNorm`or `GemmaRMSNorm`
|
||||
- `nn.LayerNorm` with vLLM's `LayerNorm`
|
||||
"""
|
||||
tp_plan = self.model.tp_plan or {}
|
||||
|
||||
@@ -556,6 +558,8 @@ class Base(
|
||||
new_module = replace_embedding_class(
|
||||
child_module, self.quant_config, prefix=qual_name
|
||||
)
|
||||
elif isinstance(child_module, nn.LayerNorm):
|
||||
new_module = replace_layernorm_class(child_module)
|
||||
elif child_module_fusers := fusers[child_module]:
|
||||
for fuser in child_module_fusers:
|
||||
register_fusion(fuser, qual_name, child_module)
|
||||
|
||||
@@ -28,6 +28,7 @@ from torch import nn
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.conv import Conv2dLayer, Conv3dLayer
|
||||
from vllm.model_executor.layers.layernorm import LayerNorm
|
||||
from vllm.model_executor.layers.linear import (
|
||||
ColumnParallelLinear,
|
||||
ReplicatedLinear,
|
||||
@@ -187,6 +188,34 @@ def replace_conv_class(conv: TorchConv) -> VllmConv | TorchConv:
|
||||
)
|
||||
|
||||
|
||||
def replace_layernorm_class(layernorm: nn.LayerNorm) -> nn.Module:
|
||||
"""Replace a standard (mean-centered) `nn.LayerNorm` with vLLM's
|
||||
`LayerNorm`.
|
||||
|
||||
Args:
|
||||
layernorm: `nn.LayerNorm` to be replaced.
|
||||
|
||||
Returns:
|
||||
The new `LayerNorm`. If the layernorm is not supported (a subclass with
|
||||
its own behavior, multi-dim `normalized_shape`, or not both
|
||||
elementwise-affine and biased), returns the original module unchanged.
|
||||
|
||||
"""
|
||||
if (
|
||||
type(layernorm) is not nn.LayerNorm
|
||||
or len(layernorm.normalized_shape) != 1
|
||||
or not layernorm.elementwise_affine
|
||||
or layernorm.bias is None
|
||||
):
|
||||
return layernorm
|
||||
|
||||
return LayerNorm(
|
||||
layernorm.normalized_shape[0],
|
||||
eps=layernorm.eps,
|
||||
dtype=layernorm.weight.dtype,
|
||||
)
|
||||
|
||||
|
||||
def attrsetter(attr: str) -> Callable[[object, object], None]:
|
||||
"""Set a possibly nested attribute, like the inverse of attrgetter."""
|
||||
parent, _, name = attr.rpartition(".")
|
||||
|
||||
Reference in New Issue
Block a user