Skip to content

Commit f2fab11

Browse files
committed
Supports moe_permute related operators
1 parent 47e8ee7 commit f2fab11

6 files changed

Lines changed: 881 additions & 95 deletions

File tree

transformer_engine/plugin/core/backends/flagos/flagos.py

Lines changed: 104 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77

88
import torch
99

10-
from ...ops import *
10+
from ...ops import TEFLBackendBase, FP8TensorMeta, NVTE_Fused_Attn_Backend
1111

1212
from .impl import (
1313
rmsnorm_fwd_fl,
1414
rmsnorm_bwd_fl,
1515
multi_tensor_scale_fl,
1616
multi_tensor_adam_fl,
17-
multi_tensor_adam_param_remainder_fl,
1817
multi_tensor_l2_norm_fl,
1918
generic_gemm_fl,
19+
gelu_fl,
20+
geglu_fl,
21+
qgelu_fl,
22+
qgeglu_fl,
23+
relu_fl,
24+
reglu_fl,
25+
moe_permute_fwd_fl,
26+
moe_unpermute_bwd_fl,
27+
moe_unpermute_fwd_fl,
28+
moe_permute_bwd_fl,
2029
)
2130

2231

@@ -32,6 +41,11 @@ def check_available() -> bool:
3241
def is_available(self) -> bool:
3342
return _check_flagos_available()
3443

44+
def get_flash_attention_class(self):
45+
from .attention.dot_product_attention.backends import FlashAttentionFL
46+
47+
return FlashAttentionFL
48+
3549
def get_attention_backend(self, attention_params=None):
3650
from packaging.version import Version as PkgVersion
3751
from ...logger_manager import get_logger
@@ -65,18 +79,17 @@ def get_attention_backend(self, attention_params=None):
6579
available_backends,
6680
)
6781

68-
##### transformer_engine/pytorch/csrc/extensions/pybind.cpp #####
6982
def generic_gemm(
7083
self,
71-
A: Any,
84+
A: torch.Tensor,
7285
transA: bool,
73-
B: Any,
86+
B: torch.Tensor,
7487
transB: bool,
75-
D: Any,
88+
D: torch.Tensor,
7689
quantizer: Any,
77-
output_dtype: Optional[DType],
90+
output_dtype: torch.dtype,
7891
bias: Optional[torch.Tensor],
79-
bias_type: DType,
92+
bias_type: Any,
8093
gelu: bool,
8194
gelu_in: Optional[torch.Tensor],
8295
grad: bool,
@@ -85,12 +98,12 @@ def generic_gemm(
8598
accumulate: bool,
8699
use_split_accumulator: bool,
87100
comm_overlap: Optional[Any] = None,
88-
comm_type: Optional[CommOverlapType] = None,
101+
comm_type: Optional[Any] = None,
89102
extra_output: Optional[torch.Tensor] = None,
90103
bulk_overlap: bool = False,
91104
alpha: float = 1.0,
92105
beta: Optional[float] = None,
93-
) -> List[Any]:
106+
) -> Any:
94107
return generic_gemm_fl(
95108
A,
96109
transA,
@@ -108,26 +121,25 @@ def generic_gemm(
108121
workspace_size,
109122
accumulate,
110123
use_split_accumulator,
111-
comm_overlap,
112-
comm_type,
113-
extra_output,
114-
bulk_overlap,
115-
alpha,
116-
beta,
124+
comm_overlap=comm_overlap,
125+
comm_type=comm_type,
126+
extra_output=extra_output,
127+
bulk_overlap=bulk_overlap,
128+
alpha=alpha,
129+
beta=beta,
117130
)
118131

119-
# Other granular functions
120132
def rmsnorm_fwd(
121133
self,
122-
input: Any,
123-
weight: Any,
134+
input: torch.Tensor,
135+
weight: torch.Tensor,
124136
eps: float,
125-
ln_out: Any,
137+
ln_out: Optional[torch.Tensor],
126138
quantizer: Any,
127-
otype: DType,
139+
otype: torch.dtype,
128140
sm_margin: int,
129141
zero_centered_gamma: bool,
130-
) -> List[Any]:
142+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor]:
131143
return rmsnorm_fwd_fl(
132144
input=input,
133145
weight=weight,
@@ -141,26 +153,24 @@ def rmsnorm_fwd(
141153

142154
def rmsnorm_bwd(
143155
self,
144-
dz: torch.Tensor,
156+
dy: torch.Tensor,
145157
x: torch.Tensor,
146158
rsigma: torch.Tensor,
147159
gamma: torch.Tensor,
148-
sm_margin: int,
149-
zero_centered_gamma: bool,
150-
) -> List[Any]:
160+
sm_margin: int = 0,
161+
zero_centered_gamma: bool = False,
162+
eps: float = 1e-5,
163+
) -> Tuple[torch.Tensor, torch.Tensor]:
151164
return rmsnorm_bwd_fl(
152-
dy=dz,
165+
dy=dy,
153166
x=x,
154167
rsigma=rsigma,
155168
gamma=gamma,
156169
sm_margin=sm_margin,
157170
zero_centered_gamma=zero_centered_gamma,
171+
eps=eps,
158172
)
159173

160-
def get_fused_attn_backend(self, *args, **kwargs) -> int:
161-
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
162-
163-
# multi-tensor functions
164174
def multi_tensor_scale(
165175
self,
166176
chunk_size: int,
@@ -175,67 +185,41 @@ def multi_tensor_l2norm(
175185
chunk_size: int,
176186
noop_flag: torch.Tensor,
177187
tensor_lists: List[List[torch.Tensor]],
178-
per_tensor: Optional[bool] = False,
179-
) -> Tuple[torch.Tensor, torch.Tensor]:
180-
return multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor)
188+
per_tensor: bool = False,
189+
) -> Union[torch.Tensor, List[torch.Tensor]]:
190+
result, _ = multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor)
191+
return result
181192

182193
def multi_tensor_adam(
183194
self,
184-
chunk_size: int,
185-
noop_flag: torch.Tensor,
186-
tensor_lists: List[List[torch.Tensor]],
187-
lr: float,
188-
beta1: float,
189-
beta2: float,
190-
epsilon: float,
191-
step: int,
192-
mode: int,
193-
bias_correction: int,
194-
weight_decay: float,
195-
) -> None:
195+
chunk_size: int = None,
196+
noop_flag: torch.Tensor = None,
197+
tensor_lists: List[List[torch.Tensor]] = None,
198+
lr: float = None,
199+
beta1: float = None,
200+
beta2: float = None,
201+
eps: float = None,
202+
step: int = None,
203+
mode: int = None,
204+
bias_correction: int = None,
205+
weight_decay: float = None,
206+
):
207+
if chunk_size is None:
208+
return multi_tensor_adam_fl
196209
return multi_tensor_adam_fl(
197-
chunk_size,
198-
noop_flag,
199-
tensor_lists,
200-
lr,
201-
beta1,
202-
beta2,
203-
epsilon,
204-
step,
205-
mode,
206-
bias_correction,
207-
weight_decay,
208-
)
209-
210-
def multi_tensor_adam_param_remainder(
211-
self,
212-
chunk_size: int,
213-
noop_flag: torch.Tensor,
214-
tensor_lists: List[List[torch.Tensor]],
215-
lr: float,
216-
beta1: float,
217-
beta2: float,
218-
epsilon: float,
219-
step: int,
220-
mode: int,
221-
bias_correction: int,
222-
weight_decay: float,
223-
) -> None:
224-
return multi_tensor_adam_param_remainder_fl(
225-
chunk_size,
226-
noop_flag,
227-
tensor_lists,
228-
lr,
229-
beta1,
230-
beta2,
231-
epsilon,
232-
step,
233-
mode,
234-
bias_correction,
235-
weight_decay,
210+
chunk_size=chunk_size,
211+
noop_flag=noop_flag,
212+
tensor_lists=tensor_lists,
213+
lr=lr,
214+
beta1=beta1,
215+
beta2=beta2,
216+
eps=eps,
217+
step=step,
218+
mode=mode,
219+
bias_correction=bias_correction,
220+
weight_decay=weight_decay,
236221
)
237222

238-
# Misc
239223
def get_cublasLt_version(self) -> int:
240224
return 110000
241225

@@ -245,8 +229,38 @@ def get_cudnn_version(self) -> int:
245229
def get_num_cublas_streams(self) -> int:
246230
return 0
247231

248-
############## class func #################################
249-
def get_flash_attention_class(self):
250-
from .attention.dot_product_attention.backends import FlashAttentionFL
232+
def get_fused_attn_backend(self, *args, **kwargs) -> int:
233+
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
251234

252-
return FlashAttentionFL
235+
def create_fp8_tensor_meta(self) -> FP8TensorMeta:
236+
return FP8TensorMeta()
237+
238+
def gelu(self, input: torch.Tensor, quantizer: Any) -> Any:
239+
return gelu_fl(input, quantizer)
240+
241+
def geglu(self, input: torch.Tensor, quantizer: Any) -> Any:
242+
return geglu_fl(input, quantizer)
243+
244+
def qgelu(self, input: torch.Tensor, quantizer: Any) -> Any:
245+
return qgelu_fl(input, quantizer)
246+
247+
def qgeglu(self, input: torch.Tensor, quantizer: Any) -> Any:
248+
return qgeglu_fl(input, quantizer)
249+
250+
def relu(self, input: torch.Tensor, quantizer: Any) -> Any:
251+
return relu_fl(input, quantizer)
252+
253+
def reglu(self, input: torch.Tensor, quantizer: Any) -> Any:
254+
return reglu_fl(input, quantizer)
255+
256+
def moe_permute_fwd(self, *args, **kwargs) -> Any:
257+
return moe_permute_fwd_fl(*args, **kwargs)
258+
259+
def moe_unpermute_bwd(self, *args, **kwargs) -> Any:
260+
return moe_unpermute_bwd_fl(*args, **kwargs)
261+
262+
def moe_unpermute_fwd(self, *args, **kwargs) -> Any:
263+
return moe_unpermute_fwd_fl(*args, **kwargs)
264+
265+
def moe_permute_bwd(self, *args, **kwargs) -> Any:
266+
return moe_permute_bwd_fl(*args, **kwargs)

transformer_engine/plugin/core/backends/flagos/impl/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
from .rmsnorm import *
77
from .fused_adam import *
88
from .multi_tensor import *
9+
from .activation import *
10+
from .moe_permute import *
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import torch
2+
from typing import Any
3+
import flag_gems
4+
5+
6+
def gelu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
7+
return flag_gems.gelu(input, approximate="tanh")
8+
9+
10+
def geglu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
11+
a, b = input.chunk(2, dim=-1)
12+
return flag_gems.gelu(a, approximate="tanh") * b
13+
14+
15+
def qgelu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
16+
return input * flag_gems.sigmoid(1.702 * input)
17+
18+
19+
def qgeglu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
20+
a, b = input.chunk(2, dim=-1)
21+
return a * flag_gems.sigmoid(1.702 * a) * b
22+
23+
24+
def relu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
25+
return flag_gems.relu(input)
26+
27+
28+
def reglu_fl(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
29+
a, b = input.chunk(2, dim=-1)
30+
return flag_gems.relu(a) * b

0 commit comments

Comments
 (0)