#017

linear_sigmoid_mul

bf16 sglang · both · sglang.srt.models.qwen2_moe · importance 0.7%

Reference Implementation

reference.py
import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):

    def __init__(self) -> None:
        super().__init__()

    def forward(self, hidden_states: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor, post_mul_mat: torch.Tensor) -> torch.Tensor:
        gate = F.linear(hidden_states, weight, bias)
        out = torch.sigmoid(gate.float()) * post_mul_mat.float()
        return out.to(post_mul_mat.dtype)

Shapes

TSOL hardware:
# token_counthidden_sizeout_features TSOL(XPU-A)TProdS
0 803740964096 1.16 ms 1.60 ms 72.3%
1 671740964096 968.89 us 1.27 ms 76.5%
2 400140964096 577.12 us 2.06 ms 28.0%
3 1538140964096 2.22 ms 7.21 ms 30.8%
4 657640964096 948.55 us 1.26 ms 75.3%
5 120484096 3.17 us 20.90 us 15.2%
6 9020484096 6.49 us 32.20 us 20.2%
7 1320484096 3.22 us 17.30 us 18.6%
8 274840964096 396.38 us 564.50 us 70.2%

Input Generation

input.py
import torch

def _make_inputs(token_count: int, hidden_size: int, out_features: int) -> dict[str, torch.Tensor]:
    hidden_states = torch.randn(token_count, hidden_size, dtype=torch.bfloat16, device='cuda') * 0.02
    weight = torch.randn(out_features, hidden_size, dtype=torch.bfloat16, device='cuda') * hidden_size ** (-0.5)
    bias = torch.randn(out_features, dtype=torch.bfloat16, device='cuda') * 0.02
    post_mul_mat = torch.randn(token_count, out_features, dtype=torch.bfloat16, device='cuda') * 0.02
    return {'hidden_states': hidden_states, 'weight': weight, 'bias': bias, 'post_mul_mat': post_mul_mat}