Skip to content

Commit 12b2077

Browse files
Fix: Resolve parameter mismatch between TE_FL and NVTE functions (flagos-ai#34)
# Description Align TE_FL backend interface signatures with the upstream NVTE (NVIDIA TransformerEngine) C++ pybind API, to resolve parameter mismatches that cause runtime failures.
1 parent b0a5934 commit 12b2077

22 files changed

Lines changed: 4796 additions & 3217 deletions

File tree

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

Lines changed: 64 additions & 80 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, rmsnorm_bwd_fl,
@@ -20,7 +20,6 @@
2020
def _check_flagos_available() -> bool:
2121
return True
2222

23-
2423
class FlagOSBackend(TEFLBackendBase):
2524
@staticmethod
2625
def check_available() -> bool:
@@ -29,10 +28,6 @@ def check_available() -> bool:
2928
def is_available(self) -> bool:
3029
return _check_flagos_available()
3130

32-
def get_flash_attention_class(self):
33-
from .attention.dot_product_attention.backends import FlashAttentionFL
34-
return FlashAttentionFL
35-
3631
def get_attention_backend(self, attention_params=None):
3732
from packaging.version import Version as PkgVersion
3833
from ...logger_manager import get_logger
@@ -65,17 +60,18 @@ def get_attention_backend(self, attention_params=None):
6560
available_backends,
6661
)
6762

63+
##### transformer_engine/pytorch/csrc/extensions/pybind.cpp #####
6864
def generic_gemm(
6965
self,
70-
A: torch.Tensor,
66+
A: Any,
7167
transA: bool,
72-
B: torch.Tensor,
68+
B: Any,
7369
transB: bool,
74-
D: torch.Tensor,
70+
D: Any,
7571
quantizer: Any,
76-
output_dtype: torch.dtype,
72+
output_dtype: Optional[DType],
7773
bias: Optional[torch.Tensor],
78-
bias_type: Any,
74+
bias_type: DType,
7975
gelu: bool,
8076
gelu_in: Optional[torch.Tensor],
8177
grad: bool,
@@ -84,53 +80,53 @@ def generic_gemm(
8480
accumulate: bool,
8581
use_split_accumulator: bool,
8682
comm_overlap: Optional[Any] = None,
87-
comm_type: Optional[Any] = None,
83+
comm_type: Optional[CommOverlapType] = None,
8884
extra_output: Optional[torch.Tensor] = None,
8985
bulk_overlap: bool = False,
9086
alpha: float = 1.0,
9187
beta: Optional[float] = None,
92-
) -> Any:
88+
) -> List[Any]:
9389
return generic_gemm_fl(
9490
A, transA, B, transB, D, quantizer, output_dtype,
95-
bias, bias_type, gelu, gelu_in, grad,
96-
workspace, workspace_size, accumulate, use_split_accumulator,
97-
comm_overlap=comm_overlap, comm_type=comm_type,
98-
extra_output=extra_output, bulk_overlap=bulk_overlap,
99-
alpha=alpha, beta=beta
91+
bias, bias_type, gelu, gelu_in, grad, workspace, workspace_size,
92+
accumulate, use_split_accumulator, comm_overlap, comm_type,
93+
extra_output, bulk_overlap, alpha, beta
10094
)
10195

96+
# Other granular functions
10297
def rmsnorm_fwd(
10398
self,
104-
input: torch.Tensor,
105-
weight: torch.Tensor,
99+
input: Any,
100+
weight: Any,
106101
eps: float,
107-
ln_out: Optional[torch.Tensor],
102+
ln_out: Any,
108103
quantizer: Any,
109-
otype: torch.dtype,
104+
otype: DType,
110105
sm_margin: int,
111106
zero_centered_gamma: bool,
112-
) -> Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor]:
107+
) -> List[Any]:
113108
return rmsnorm_fwd_fl(
114109
input=input, weight=weight, eps=eps, ln_out=ln_out,
115110
quantizer=quantizer, odtype=otype,
116111
sm_margin=sm_margin, zero_centered_gamma=zero_centered_gamma,
117112
)
118-
119113
def rmsnorm_bwd(
120114
self,
121-
dy: torch.Tensor,
115+
dz: torch.Tensor,
122116
x: torch.Tensor,
123117
rsigma: torch.Tensor,
124118
gamma: torch.Tensor,
125-
sm_margin: int = 0,
126-
zero_centered_gamma: bool = False,
127-
eps: float = 1e-5,
128-
) -> Tuple[torch.Tensor, torch.Tensor]:
119+
sm_margin: int,
120+
zero_centered_gamma: bool,
121+
) -> List[Any]:
129122
return rmsnorm_bwd_fl(
130-
dy=dy, x=x, rsigma=rsigma, gamma=gamma,
131-
sm_margin=sm_margin, zero_centered_gamma=zero_centered_gamma, eps=eps,
123+
dy=dz, x=x, rsigma=rsigma, gamma=gamma,
124+
sm_margin=sm_margin, zero_centered_gamma=zero_centered_gamma
132125
)
126+
def get_fused_attn_backend(self, *args, **kwargs) -> int:
127+
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
133128

129+
# multi-tensor functions
134130
def multi_tensor_scale(
135131
self,
136132
chunk_size: int,
@@ -139,73 +135,61 @@ def multi_tensor_scale(
139135
scale: float,
140136
) -> None:
141137
return multi_tensor_scale_fl(chunk_size, noop_flag, tensor_lists, scale)
142-
143138
def multi_tensor_l2norm(
144139
self,
145140
chunk_size: int,
146141
noop_flag: torch.Tensor,
147142
tensor_lists: List[List[torch.Tensor]],
148-
per_tensor: bool = False,
149-
) -> Union[torch.Tensor, List[torch.Tensor]]:
150-
result, _ = multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor)
151-
return result
152-
143+
per_tensor: Optional[bool] = False,
144+
) -> Tuple[torch.Tensor, torch.Tensor]:
145+
return multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor)
153146
def multi_tensor_adam(
154147
self,
155-
chunk_size: int = None,
156-
noop_flag: torch.Tensor = None,
157-
tensor_lists: List[List[torch.Tensor]] = None,
158-
lr: float = None,
159-
beta1: float = None,
160-
beta2: float = None,
161-
eps: float = None,
162-
step: int = None,
163-
mode: int = None,
164-
bias_correction: int = None,
165-
weight_decay: float = None,
166-
):
167-
if chunk_size is None:
168-
return multi_tensor_adam_fl
148+
chunk_size: int,
149+
noop_flag: torch.Tensor,
150+
tensor_lists: List[List[torch.Tensor]],
151+
lr: float,
152+
beta1: float,
153+
beta2: float,
154+
epsilon: float,
155+
step: int,
156+
mode: int,
157+
bias_correction: int,
158+
weight_decay: float,
159+
) -> None:
169160
return multi_tensor_adam_fl(
170-
chunk_size=chunk_size, noop_flag=noop_flag, tensor_lists=tensor_lists,
171-
lr=lr, beta1=beta1, beta2=beta2, eps=eps,
172-
step=step, mode=mode, bias_correction=bias_correction, weight_decay=weight_decay,
161+
chunk_size, noop_flag, tensor_lists, lr, beta1, beta2, epsilon,
162+
step, mode, bias_correction, weight_decay,
173163
)
174-
175164
def multi_tensor_adam_param_remainder(
176165
self,
177-
chunk_size: int = None,
178-
noop_flag: torch.Tensor = None,
179-
tensor_lists: List[List[torch.Tensor]] = None,
180-
lr: float = None,
181-
beta1: float = None,
182-
beta2: float = None,
183-
eps: float = None,
184-
step: int = None,
185-
mode: int = None,
186-
bias_correction: int = None,
187-
weight_decay: float = None,
188-
):
189-
if chunk_size is None:
190-
return multi_tensor_adam_param_remainder_fl
166+
chunk_size: int,
167+
noop_flag: torch.Tensor,
168+
tensor_lists: List[List[torch.Tensor]],
169+
lr: float,
170+
beta1: float,
171+
beta2: float,
172+
epsilon: float,
173+
step: int,
174+
mode: int,
175+
bias_correction: int,
176+
weight_decay: float,
177+
) -> None:
191178
return multi_tensor_adam_param_remainder_fl(
192-
chunk_size=chunk_size, noop_flag=noop_flag, tensor_lists=tensor_lists,
193-
lr=lr, beta1=beta1, beta2=beta2, eps=eps,
194-
step=step, mode=mode, bias_correction=bias_correction, weight_decay=weight_decay,
179+
chunk_size, noop_flag, tensor_lists,
180+
lr, beta1, beta2, epsilon,
181+
step, mode, bias_correction, weight_decay,
195182
)
196183

184+
# Misc
197185
def get_cublasLt_version(self) -> int:
198186
return 110000
199-
200187
def get_cudnn_version(self) -> int:
201188
return 90000
202-
203189
def get_num_cublas_streams(self) -> int:
204190
return 0
205191

206-
def get_fused_attn_backend(self, *args, **kwargs) -> int:
207-
return NVTE_Fused_Attn_Backend.NVTE_No_Backend
208-
209-
def create_fp8_tensor_meta(self) -> FP8TensorMeta:
210-
return FP8TensorMeta()
211-
192+
############## class func #################################
193+
def get_flash_attention_class(self):
194+
from .attention.dot_product_attention.backends import FlashAttentionFL
195+
return FlashAttentionFL

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,4 @@ def multi_tensor_adam_param_remainder_fl(
187187

188188
# Write back
189189
flag_gems.copy_(p, param_bf16)
190-
flag_gems.copy_(p_remainder, remainder_int16)
190+
flag_gems.copy_(p_remainder, remainder_int16)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def multi_tensor_l2_norm_fl(chunk_size, noop_flag, tensor_lists, per_tensor, *ar
2323
def multi_tensor_scale_fl(chunk_size, noop_flag, tensor_lists, scale):
2424

2525
for src, dst in zip(tensor_lists[0], tensor_lists[1]):
26-
flag_gems.copy_(dst, src * scale)
26+
flag_gems.copy_(dst, src * scale)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def rmsnorm_bwd_fl(
4242
gamma,
4343
sm_margin,
4444
zero_centered_gamma,
45-
eps,
45+
eps=1e-5,
4646
):
4747
# When zero_centered_gamma is True, forward uses (1 + gamma) as weight
4848
# So backward needs to use (1 + gamma) for computing dx

transformer_engine/plugin/core/backends/reference/impl/normalization.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,37 @@
55
from typing import Any, Optional, Tuple
66
import torch
77
import torch.nn.functional as F
8+
from ....ops import DType
89

910
__all__ = [
1011
"layernorm_fwd_torch",
1112
"layernorm_bwd_torch",
1213
]
1314

15+
# Mapping from DType enum to torch.dtype
16+
_DTYPE_TO_TORCH_DTYPE = {
17+
DType.kByte: torch.uint8,
18+
DType.kInt16: torch.int16,
19+
DType.kInt32: torch.int32,
20+
DType.kInt64: torch.int64,
21+
DType.kFloat32: torch.float32,
22+
DType.kFloat16: torch.float16,
23+
DType.kBFloat16: torch.bfloat16,
24+
DType.kFloat8E4M3: torch.float8_e4m3fn,
25+
DType.kFloat8E5M2: torch.float8_e5m2,
26+
}
27+
28+
def _to_torch_dtype(dtype):
29+
"""Convert DType enum to torch.dtype."""
30+
if dtype is None:
31+
return None
32+
if isinstance(dtype, torch.dtype):
33+
return dtype
34+
if isinstance(dtype, (int, DType)):
35+
dtype_enum = DType(dtype)
36+
if dtype_enum in _DTYPE_TO_TORCH_DTYPE:
37+
return _DTYPE_TO_TORCH_DTYPE[dtype_enum]
38+
raise ValueError(f"Unsupported dtype: {dtype}")
1439

1540
def layernorm_fwd_torch(
1641
input: torch.Tensor,
@@ -19,10 +44,11 @@ def layernorm_fwd_torch(
1944
eps: float,
2045
ln_out: Optional[torch.Tensor],
2146
quantizer: Any,
22-
odtype: torch.dtype,
47+
odtype: DType,
2348
sm_margin: int,
2449
zero_centered_gamma: bool,
2550
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
51+
odtype = _to_torch_dtype(odtype)
2652
mean = input.mean(dim=-1, keepdim=True)
2753
var = input.var(dim=-1, keepdim=True, unbiased=False)
2854
rsigma = torch.rsqrt(var + eps)
@@ -45,7 +71,6 @@ def layernorm_fwd_torch(
4571

4672
return output, mean, rsigma
4773

48-
4974
def layernorm_bwd_torch(
5075
dy: torch.Tensor,
5176
x: torch.Tensor,

transformer_engine/plugin/core/backends/reference/impl/optimizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,4 @@ def multi_tensor_compute_scale_and_scale_inv_torch(
310310

311311
# Update scale and scale_inv
312312
scale.copy_(computed_scale)
313-
scale_inv.copy_(1.0 / computed_scale)
313+
scale_inv.copy_(1.0 / computed_scale)

transformer_engine/plugin/core/backends/reference/impl/rmsnorm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def rmsnorm_bwd_torch(
4343
gamma,
4444
sm_margin,
4545
zero_centered_gamma,
46-
eps,
4746
):
4847
inv_rms = rsigma.unsqueeze(-1)
4948

0 commit comments

Comments
 (0)