File size: 4,165 Bytes
86599c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | from dataclasses import dataclass
import types
import torch
@dataclass
class Layout:
lengths: list
cu: torch.Tensor
chunks: torch.Tensor
offsets: torch.Tensor
positions: torch.Tensor
total: int
maximum: int
@classmethod
def make(cls, lengths, starts=None):
from .stateless_contract import chunk_plan
starts = starts or [0] * len(lengths)
cu = [0]
for n in lengths:
cu.append(cu[-1] + n)
chunks, offsets = chunk_plan(cu)
tensor = lambda x: torch.tensor(x, device="cuda", dtype=torch.int32)
return cls(
lengths,
tensor(cu),
tensor(chunks),
tensor(offsets),
torch.cat(
[torch.arange(s, s + n, device="cuda") for s, n in zip(starts, lengths)]
),
cu[-1],
max(lengths),
)
def gdn_forward(layer, hidden_states):
"""Native Qwen projections, fused causal conv/prep, and chunked delta kernel."""
from .conv_prep_fused import fused_conv_prep
from vllm.third_party.flash_linear_attention.ops import chunk_gated_delta_rule
runner = layer._experiment_runner
meta = runner.current_layout
packed, _ = layer.in_proj_qkvz(hidden_states)
ba, _ = layer.in_proj_ba(hidden_states)
qkv_size = (layer.key_dim * 2 + layer.value_dim) // layer.tp_size
mixed, z = packed.split((qkv_size, layer.value_dim // layer.tp_size), -1)
z = z.reshape(z.shape[0], -1, layer.head_v_dim)
b, a = layer.split_ba(ba)
q, k, v, g, beta = fused_conv_prep(
mixed,
layer.conv1d.weight.view(layer.conv1d.weight.shape[0], -1),
layer.conv1d.bias,
meta.cu,
meta.chunks,
a,
b,
layer.A_log,
layer.dt_bias,
False,
)
out = torch.empty(
(meta.total, layer.num_v_heads // layer.tp_size, layer.head_v_dim),
device=hidden_states.device,
dtype=hidden_states.dtype,
)
if getattr(layer, "_native_fp8_gates", False):
g = g.to(torch.float8_e4m3fn)
beta = beta.to(torch.float8_e4m3fn)
chunk_gated_delta_rule(
q=q[None],
k=k[None],
v=v[None],
g=g[None],
beta=beta[None],
initial_state=None,
output_final_state=False,
cu_seqlens=meta.cu,
chunk_indices=meta.chunks,
chunk_offsets=meta.offsets,
use_qk_l2norm_in_kernel=False,
core_attn_out=out[None],
)
return layer._output_projection(out, z)
def attention_forward(layer, positions, hidden_states):
from vllm.v1.attention.backends.flash_attn import flash_attn_varlen_func
runner = layer._experiment_runner
meta = runner.current_layout
qkv, _ = layer.qkv_proj(hidden_states)
q, k, v, gate = layer._project_qkv_gate(qkv, positions)
keymeta = meta
q = q.reshape(-1, layer.num_heads, layer.head_dim)
k = k.reshape(-1, layer.num_kv_heads, layer.head_dim)
v = v.reshape(-1, layer.num_kv_heads, layer.head_dim)
out = flash_attn_varlen_func(
q=q,
k=k,
v=v,
cu_seqlens_q=meta.cu,
cu_seqlens_k=keymeta.cu,
max_seqlen_q=meta.maximum,
max_seqlen_k=keymeta.maximum,
softmax_scale=layer.scaling,
causal=True,
fa_version=layer.attn.impl.vllm_flash_attn_version,
num_splits=1
if layer.attn.impl.batch_invariant_enabled or layer.attn.impl.fa4_hd256
else 0,
)
if isinstance(out, tuple):
out = out[0]
out = out.reshape(-1, layer.q_size)
if gate is not None:
out = out * torch.sigmoid(gate.reshape_as(out))
return layer.o_proj(out)[0]
class PackedRunner:
def __init__(self, model):
self.backbone = model.model
for block in self.backbone.layers:
recurrent = block.layer_type == "linear_attention"
layer = block.linear_attn if recurrent else block.self_attn
object.__setattr__(layer, "_experiment_runner", self)
layer.forward = types.MethodType(
gdn_forward if recurrent else attention_forward, layer
)
|