-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathflaggems.py
More file actions
276 lines (227 loc) · 7.39 KB
/
Copy pathflaggems.py
File metadata and controls
276 lines (227 loc) · 7.39 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Copyright (c) 2026 BAAI. All rights reserved.
"""
FlagGems backend implementation.
This backend provides operator implementations using the FlagGems library.
"""
from __future__ import annotations
from typing import Optional, Union
import torch
import os
from vllm_fl.dispatch.backends.base import Backend
class FlagGemsBackend(Backend):
"""
FlagGems backend for operator implementations.
This backend uses the flag_gems library to provide high-performance
operator implementations.
"""
_available: Optional[bool] = None
@property
def name(self) -> str:
return "flagos"
def is_available(self) -> bool:
"""Check if FlagGems is available."""
if FlagGemsBackend._available is None:
try:
import flag_gems # noqa F401
FlagGemsBackend._available = True
except ImportError:
FlagGemsBackend._available = False
return FlagGemsBackend._available
# ==================== Operator Implementations ====================
def silu_and_mul(self, obj, x: torch.Tensor) -> torch.Tensor:
"""
SiLU activation followed by element-wise multiplication.
Args:
obj: The calling obj (for interface consistency)
x: Input tensor of shape [..., 2*d]
Returns:
Output tensor of shape [..., d]
"""
from .impl.activation import silu_and_mul_flaggems
return silu_and_mul_flaggems(obj, x)
def gelu_and_mul(self, obj, x: torch.Tensor) -> torch.Tensor:
"""
GELU activation followed by element-wise multiplication.
Args:
obj: The calling obj (for interface consistency)
x: Input tensor of shape [..., 2*d]
Returns:
Output tensor of shape [..., d]
"""
from .impl.activation import gelu_and_mul_flaggems
return gelu_and_mul_flaggems(obj, x)
def rms_norm(
self,
obj,
x: torch.Tensor,
residual: Optional[torch.Tensor] = None,
) -> Union[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
"""
RMS normalization.
Args:
obj: The calling obj (e.g., RMSNorm layer)
x: Input tensor
residual: Optional residual tensor
Returns:
Normalized tensor, or tuple of (normalized, residual) if residual is provided
"""
from .impl.normalization import rms_norm_flaggems
return rms_norm_flaggems(obj, x, residual)
def rotary_embedding(
self,
obj,
query: torch.Tensor,
key: torch.Tensor,
cos: torch.Tensor,
sin: torch.Tensor,
position_ids: torch.Tensor,
rotary_interleaved: bool = False,
inplace: bool = True,
) -> tuple[torch.Tensor, torch.Tensor]:
"""
Apply rotary position embedding.
Args:
obj: The calling obj (for interface consistency)
query: Query tensor
key: Key tensor
cos: Cosine cache
sin: Sine cache
position_ids: Position indices
rotary_interleaved: Whether to use interleaved rotary
inplace: Whether to modify tensors in-place
Returns:
Tuple of (embedded_query, embedded_key)
"""
from .impl.rotary import rotary_embedding_flaggems
return rotary_embedding_flaggems(
obj,
query,
key,
cos,
sin,
position_ids,
rotary_interleaved=rotary_interleaved,
inplace=inplace,
)
def attention_backend(self, use_mla: bool = False, use_sparse: bool = False) -> str:
"""
Get the attention backend class path for FlagGems.
Args:
use_mla: Whether to use Multi-head Latent Attention (MLA)
use_sparse: Whether to use Deepseek Sparse Attention (DSA)
Returns:
Fully qualified class path string
"""
from vllm.v1.attention.backends.registry import AttentionBackendEnum
# TritonAttentionBackend requires CUDA, check if available
if not torch.cuda.is_available():
raise RuntimeError(
"TritonAttentionBackend requires CUDA but CUDA is not available. "
"Falling back to vendor implementation."
)
if use_mla:
raise NotImplementedError("NOT support mla now!")
if use_sparse:
raise ValueError("use_sparse=True requires use_mla=True.")
use_flaggems_attn = os.environ.get(
"VLLM_FL_USE_FLAGGEMS_ATTN", "0"
).lower() in ("1", "true", "yes")
if use_flaggems_attn:
print("Using FlagGems attention backend.")
return "vllm_fl.dispatch.backends.flaggems.impl.attention.AttentionFLBackend"
return AttentionBackendEnum.TRITON_ATTN.get_path()
def moe_align_block_size(
self,
topk_ids: torch.Tensor,
block_size: int,
num_experts: int,
expert_map: Optional[torch.Tensor] = None,
pad_sorted_ids: bool = False,
ignore_invalid_experts: bool = False,
):
from .impl.fused_moe import moe_align_block_size_flaggems
return moe_align_block_size_flaggems(
topk_ids,
block_size,
num_experts,
expert_map,
pad_sorted_ids,
ignore_invalid_experts,
)
def moe_sum(self, inp, out):
from .impl.fused_moe import moe_sum_flaggems
moe_sum_flaggems(inp, out)
def topk_softmax(
self,
topk_weights,
topk_indices,
token_expert_indices,
gating_output,
renormalize=False,
):
from .impl.fused_moe import topk_softmax_flaggems
return topk_softmax_flaggems(
topk_weights, topk_indices, token_expert_indices, gating_output, renormalize
)
def invoke_fused_moe_triton_kernel(
self,
A,
B,
C,
A_scale,
B_scale,
topk_weights,
sorted_token_ids,
expert_ids,
num_tokens_post_padded,
mul_routed_weight,
top_k,
config,
compute_type,
use_fp8_w8a8,
use_int8_w8a8,
use_int8_w8a16,
use_int4_w4a16,
per_channel_quant,
block_shape=None,
B_bias=None,
):
from .impl.fused_moe import invoke_fused_moe_triton_kernel_flaggems
invoke_fused_moe_triton_kernel_flaggems(
A,
B,
C,
A_scale,
B_scale,
topk_weights,
sorted_token_ids,
expert_ids,
num_tokens_post_padded,
mul_routed_weight,
top_k,
config,
compute_type,
use_fp8_w8a8,
use_int8_w8a8,
use_int8_w8a16,
use_int4_w4a16,
per_channel_quant,
block_shape=block_shape,
B_bias=B_bias,
)
def grouped_topk(
self,
scores,
n_group,
topk_group,
topk,
renormalize,
routed_scaling_factor,
bias,
scoring_func=0,
):
from .impl.fused_moe import grouped_topk_flaggems
return grouped_topk_flaggems(
scores, n_group, topk_group, topk,
renormalize, routed_scaling_factor, bias, scoring_func,
)