main
py 27 lines 834 Bytes
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 import torch
4 import torch.nn.functional as F
5
6 from vllm.model_executor.hw_agnostic.custom_op import CustomOp
7
8
9 @CustomOp.register("silu_and_mul")
10 class SiluAndMul(CustomOp):
11 """SwiGLU: ``x -> silu(x[:d]) * x[d:]`` where ``d = x.shape[-1] // 2``."""
12
13 def forward_native(self, x: torch.Tensor) -> torch.Tensor:
14 d = x.shape[-1] // 2
15 return F.silu(x[..., :d]) * x[..., d:]
16
17
18 # Activation-and-mul ops keyed by HF activation name.
19 _ACTIVATION_AND_MUL_REGISTRY = {
20 "silu": SiluAndMul,
21 "swish": SiluAndMul,
22 }
23
24
25 def get_act_and_mul_fn(act_fn_name: str) -> CustomOp:
26 """Build the hw-agnostic activation-and-mul op named `act_fn_name`."""
27 return _ACTIVATION_AND_MUL_REGISTRY[act_fn_name.lower()]()