Skip to content

Commit 2c4415e

Browse files
committed
add op
1 parent f2fab11 commit 2c4415e

2 files changed

Lines changed: 94 additions & 256 deletions

File tree

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

Lines changed: 72 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import torch
99

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

1212
from .impl import (
1313
rmsnorm_fwd_fl,
@@ -28,11 +28,9 @@
2828
moe_permute_bwd_fl,
2929
)
3030

31-
3231
def _check_flagos_available() -> bool:
3332
return True
3433

35-
3634
class FlagOSBackend(TEFLBackendBase):
3735
@staticmethod
3836
def check_available() -> bool:
@@ -41,15 +39,9 @@ def check_available() -> bool:
4139
def is_available(self) -> bool:
4240
return _check_flagos_available()
4341

44-
def get_flash_attention_class(self):
45-
from .attention.dot_product_attention.backends import FlashAttentionFL
46-
47-
return FlashAttentionFL
48-
4942
def get_attention_backend(self, attention_params=None):
5043
from packaging.version import Version as PkgVersion
5144
from ...logger_manager import get_logger
52-
5345
logger = get_logger()
5446

5547
# Read environment variables to determine which backends to enable
@@ -79,17 +71,18 @@ def get_attention_backend(self, attention_params=None):
7971
available_backends,
8072
)
8173

74+
##### transformer_engine/pytorch/csrc/extensions/pybind.cpp #####
8275
def generic_gemm(
8376
self,
84-
A: torch.Tensor,
77+
A: Any,
8578
transA: bool,
86-
B: torch.Tensor,
79+
B: Any,
8780
transB: bool,
88-
D: torch.Tensor,
81+
D: Any,
8982
quantizer: Any,
90-
output_dtype: torch.dtype,
83+
output_dtype: Optional[DType],
9184
bias: Optional[torch.Tensor],
92-
bias_type: Any,
85+
bias_type: DType,
9386
gelu: bool,
9487
gelu_in: Optional[torch.Tensor],
9588
grad: bool,
@@ -98,79 +91,53 @@ def generic_gemm(
9891
accumulate: bool,
9992
use_split_accumulator: bool,
10093
comm_overlap: Optional[Any] = None,
101-
comm_type: Optional[Any] = None,
94+
comm_type: Optional[CommOverlapType] = None,
10295
extra_output: Optional[torch.Tensor] = None,
10396
bulk_overlap: bool = False,
10497
alpha: float = 1.0,
10598
beta: Optional[float] = None,
106-
) -> Any:
99+
) -> List[Any]:
107100
return generic_gemm_fl(
108-
A,
109-
transA,
110-
B,
111-
transB,
112-
D,
113-
quantizer,
114-
output_dtype,
115-
bias,
116-
bias_type,
117-
gelu,
118-
gelu_in,
119-
grad,
120-
workspace,
121-
workspace_size,
122-
accumulate,
123-
use_split_accumulator,
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,
101+
A, transA, B, transB, D, quantizer, output_dtype,
102+
bias, bias_type, gelu, gelu_in, grad, workspace, workspace_size,
103+
accumulate, use_split_accumulator, comm_overlap, comm_type,
104+
extra_output, bulk_overlap, alpha, beta
130105
)
131106

107+
# Other granular functions
132108
def rmsnorm_fwd(
133109
self,
134-
input: torch.Tensor,
135-
weight: torch.Tensor,
110+
input: Any,
111+
weight: Any,
136112
eps: float,
137-
ln_out: Optional[torch.Tensor],
113+
ln_out: Any,
138114
quantizer: Any,
139-
otype: torch.dtype,
115+
otype: DType,
140116
sm_margin: int,
141117
zero_centered_gamma: bool,
142-
) -> Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor]:
118+
) -> List[Any]:
143119
return rmsnorm_fwd_fl(
144-
input=input,
145-
weight=weight,
146-
eps=eps,
147-
ln_out=ln_out,
148-
quantizer=quantizer,
149-
odtype=otype,
150-
sm_margin=sm_margin,
151-
zero_centered_gamma=zero_centered_gamma,
120+
input=input, weight=weight, eps=eps, ln_out=ln_out,
121+
quantizer=quantizer, odtype=otype,
122+
sm_margin=sm_margin, zero_centered_gamma=zero_centered_gamma,
152123
)
153-
154124
def rmsnorm_bwd(
155125
self,
156-
dy: torch.Tensor,
126+
dz: torch.Tensor,
157127
x: torch.Tensor,
158128
rsigma: torch.Tensor,
159129
gamma: torch.Tensor,
160-
sm_margin: int = 0,
161-
zero_centered_gamma: bool = False,
162-
eps: float = 1e-5,
163-
) -> Tuple[torch.Tensor, torch.Tensor]:
130+
sm_margin: int,
131+
zero_centered_gamma: bool,
132+
) -> List[Any]:
164133
return rmsnorm_bwd_fl(
165-
dy=dy,
166-
x=x,
167-
rsigma=rsigma,
168-
gamma=gamma,
169-
sm_margin=sm_margin,
170-
zero_centered_gamma=zero_centered_gamma,
171-
eps=eps,
134+
dy=dz, x=x, rsigma=rsigma, gamma=gamma,
135+
sm_margin=sm_margin, zero_centered_gamma=zero_centered_gamma
172136
)
137+
def get_fused_attn_backend(self, *args, **kwargs) -> int:
138+
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
173139

140+
# multi-tensor functions
174141
def multi_tensor_scale(
175142
self,
176143
chunk_size: int,
@@ -179,61 +146,64 @@ def multi_tensor_scale(
179146
scale: float,
180147
) -> None:
181148
return multi_tensor_scale_fl(chunk_size, noop_flag, tensor_lists, scale)
182-
183149
def multi_tensor_l2norm(
184150
self,
185151
chunk_size: int,
186152
noop_flag: torch.Tensor,
187153
tensor_lists: List[List[torch.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
192-
154+
per_tensor: Optional[bool] = False,
155+
) -> Tuple[torch.Tensor, torch.Tensor]:
156+
return multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor)
193157
def multi_tensor_adam(
194158
self,
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
159+
chunk_size: int,
160+
noop_flag: torch.Tensor,
161+
tensor_lists: List[List[torch.Tensor]],
162+
lr: float,
163+
beta1: float,
164+
beta2: float,
165+
epsilon: float,
166+
step: int,
167+
mode: int,
168+
bias_correction: int,
169+
weight_decay: float,
170+
) -> None:
209171
return multi_tensor_adam_fl(
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,
172+
chunk_size, noop_flag, tensor_lists, lr, beta1, beta2, epsilon,
173+
step, mode, bias_correction, weight_decay,
174+
)
175+
def multi_tensor_adam_param_remainder(
176+
self,
177+
chunk_size: int,
178+
noop_flag: torch.Tensor,
179+
tensor_lists: List[List[torch.Tensor]],
180+
lr: float,
181+
beta1: float,
182+
beta2: float,
183+
epsilon: float,
184+
step: int,
185+
mode: int,
186+
bias_correction: int,
187+
weight_decay: float,
188+
) -> None:
189+
return multi_tensor_adam_param_remainder_fl(
190+
chunk_size, noop_flag, tensor_lists,
191+
lr, beta1, beta2, epsilon,
192+
step, mode, bias_correction, weight_decay,
221193
)
222194

195+
# Misc
223196
def get_cublasLt_version(self) -> int:
224197
return 110000
225-
226198
def get_cudnn_version(self) -> int:
227199
return 90000
228-
229200
def get_num_cublas_streams(self) -> int:
230201
return 0
231202

232-
def get_fused_attn_backend(self, *args, **kwargs) -> int:
233-
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
234-
235-
def create_fp8_tensor_meta(self) -> FP8TensorMeta:
236-
return FP8TensorMeta()
203+
############## class func #################################
204+
def get_flash_attention_class(self):
205+
from .attention.dot_product_attention.backends import FlashAttentionFL
206+
return FlashAttentionFL
237207

238208
def gelu(self, input: torch.Tensor, quantizer: Any) -> Any:
239209
return gelu_fl(input, quantizer)

0 commit comments

Comments
 (0)