Skip to content

Commit 1e11d80

Browse files
author
root
committed
Integrate KunLunXin TE-FL backend patches
Move the temporary XTE TE-FL patch behavior into TE-FL native backend implementations. Register KunLunXin layernorm and GEMM operators, route attention backend selection through transformer_engine_klx_torch, and add reference GLU/DGLU fallback implementations.
1 parent 4f732e2 commit 1e11d80

6 files changed

Lines changed: 172 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .activation import (
1111
gelu_torch,
1212
geglu_torch,
13+
glu_torch,
1314
qgelu_torch,
1415
qgeglu_torch,
1516
relu_torch,
@@ -21,6 +22,7 @@
2122
clamped_swiglu_torch,
2223
dgelu_torch,
2324
dgeglu_torch,
25+
dglu_torch,
2426
dqgelu_torch,
2527
dqgeglu_torch,
2628
drelu_torch,
@@ -71,6 +73,7 @@
7173
"layernorm_bwd_torch",
7274
"gelu_torch",
7375
"geglu_torch",
76+
"glu_torch",
7477
"qgelu_torch",
7578
"qgeglu_torch",
7679
"relu_torch",
@@ -82,6 +85,7 @@
8285
"clamped_swiglu_torch",
8386
"dgelu_torch",
8487
"dgeglu_torch",
88+
"dglu_torch",
8589
"dqgelu_torch",
8690
"dqgeglu_torch",
8791
"drelu_torch",

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def geglu_torch(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
4646
return F.gelu(a, approximate="tanh") * b
4747

4848

49+
def glu_torch(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
50+
a, b = input.chunk(2, dim=-1)
51+
return torch.sigmoid(a) * b
52+
53+
4954
def qgelu_torch(input: torch.Tensor, quantizer: Any) -> torch.Tensor:
5055
return input * torch.sigmoid(1.702 * input)
5156

@@ -123,6 +128,18 @@ def dgeglu_torch(grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) ->
123128
return torch.cat([a.grad, b.grad], dim=-1)
124129

125130

131+
def dglu_torch(grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> torch.Tensor:
132+
a, b = fwd_input.chunk(2, dim=-1)
133+
a = a.detach().requires_grad_(True)
134+
b = b.detach().requires_grad_(True)
135+
136+
with torch.enable_grad():
137+
y = torch.sigmoid(a) * b
138+
y.backward(grad)
139+
140+
return torch.cat([a.grad, b.grad], dim=-1)
141+
142+
126143
def dqgelu_torch(grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> torch.Tensor:
127144
x = fwd_input.detach().requires_grad_(True)
128145
with torch.enable_grad():

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
layernorm_bwd_torch,
1616
gelu_torch,
1717
geglu_torch,
18+
glu_torch,
1819
qgelu_torch,
1920
qgeglu_torch,
2021
relu_torch,
@@ -26,6 +27,7 @@
2627
clamped_swiglu_torch,
2728
dgelu_torch,
2829
dgeglu_torch,
30+
dglu_torch,
2931
dqgelu_torch,
3032
dqgeglu_torch,
3133
drelu_torch,
@@ -161,6 +163,9 @@ def gelu(self, input: torch.Tensor, quantizer: Any) -> Any:
161163
def geglu(self, input: torch.Tensor, quantizer: Any) -> Any:
162164
return geglu_torch(input, quantizer)
163165

166+
def glu(self, input: torch.Tensor, quantizer: Any) -> Any:
167+
return glu_torch(input, quantizer)
168+
164169
def qgelu(self, input: torch.Tensor, quantizer: Any) -> Any:
165170
return qgelu_torch(input, quantizer)
166171

@@ -203,6 +208,9 @@ def dgelu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) ->
203208
def dgeglu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> Any:
204209
return dgeglu_torch(grad, fwd_input, quantizer)
205210

211+
def dglu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> Any:
212+
return dglu_torch(grad, fwd_input, quantizer)
213+
206214
def dqgelu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> Any:
207215
return dqgelu_torch(grad, fwd_input, quantizer)
208216

transformer_engine/plugin/core/backends/reference/register_ops.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ def register_builtins(registry) -> None:
101101
vendor=None,
102102
priority=50,
103103
),
104+
OpImpl(
105+
op_name="glu",
106+
impl_id="reference.torch",
107+
kind=BackendImplKind.REFERENCE,
108+
fn=_bind_is_available(backend.glu, is_avail),
109+
vendor=None,
110+
priority=50,
111+
),
104112
OpImpl(
105113
op_name="qgelu",
106114
impl_id="reference.torch",
@@ -190,6 +198,14 @@ def register_builtins(registry) -> None:
190198
vendor=None,
191199
priority=50,
192200
),
201+
OpImpl(
202+
op_name="dglu",
203+
impl_id="reference.torch",
204+
kind=BackendImplKind.REFERENCE,
205+
fn=_bind_is_available(backend.dglu, is_avail),
206+
vendor=None,
207+
priority=50,
208+
),
193209
OpImpl(
194210
op_name="dqgelu",
195211
impl_id="reference.torch",

transformer_engine/plugin/core/backends/vendor/kunlunxin/kunlunxin.py

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ def rmsnorm_bwd(
101101
tex = self._get_tex()
102102
return tex.rmsnorm_bwd(dz, x, rsigma, gamma, sm_margin, zero_centered_gamma)
103103

104+
def layernorm_fwd(
105+
self,
106+
input: torch.Tensor,
107+
weight: torch.Tensor,
108+
bias: Optional[torch.Tensor],
109+
eps: float,
110+
ln_out: Any,
111+
quantizer: Any,
112+
otype: DType,
113+
sm_margin: int,
114+
zero_centered_gamma: bool,
115+
) -> List[Any]:
116+
tex = self._get_tex()
117+
otype = tex.DType(int(otype)) if otype is not None else None
118+
return tex.layernorm_fwd(
119+
input, weight, bias, eps, ln_out, quantizer, otype, sm_margin, zero_centered_gamma,
120+
)
121+
122+
def layernorm_bwd(
123+
self,
124+
dz: torch.Tensor,
125+
x: torch.Tensor,
126+
mu: torch.Tensor,
127+
rsigma: torch.Tensor,
128+
gamma: torch.Tensor,
129+
sm_margin: int,
130+
zero_centered_gamma: bool,
131+
) -> List[Any]:
132+
tex = self._get_tex()
133+
return tex.layernorm_bwd(dz, x, mu, rsigma, gamma, sm_margin, zero_centered_gamma)
134+
104135
def multi_tensor_adam(
105136
self,
106137
chunk_size: int,
@@ -298,17 +329,16 @@ def get_cudnn_version(self) -> int:
298329
return 0
299330

300331
def get_attention_backend(self, attention_params=None):
301-
from transformer_engine_klx.pytorch import attention
332+
tex = self._get_tex()
302333

303334
(
304335
use_flash_attention,
336+
flash_attention_backend,
305337
use_fused_attention,
306338
fused_attention_backend,
307339
use_unfused_attention,
308340
available_backends,
309-
) = attention.get_attention_backend(attention_params)
310-
311-
flash_attention_backend = None
341+
) = tex.get_attention_backend(attention_params)
312342

313343
return (
314344
use_flash_attention,
@@ -375,3 +405,63 @@ def multi_tensor_compute_scale_inv_e8m0(
375405
tensor_lists,
376406
block_len,
377407
)
408+
409+
def generic_gemm(
410+
self,
411+
A: Any,
412+
transA: bool,
413+
B: Any,
414+
transB: bool,
415+
D: Any,
416+
quantizer: Any,
417+
output_dtype: Optional[DType],
418+
bias: Optional[torch.Tensor],
419+
bias_type: DType,
420+
gelu: bool,
421+
gelu_in: Optional[torch.Tensor],
422+
grad: bool,
423+
workspace: torch.Tensor,
424+
workspace_size: int,
425+
accumulate: bool,
426+
use_split_accumulator: bool,
427+
comm_overlap: Optional[Any] = None,
428+
comm_type: Optional[CommOverlapType] = None,
429+
extra_output: Optional[torch.Tensor] = None,
430+
bulk_overlap: bool = False,
431+
alpha: float = 1.0,
432+
beta: Optional[float] = None,
433+
) -> List[Any]:
434+
tex = self._get_tex()
435+
return tex.generic_gemm(
436+
A, transA, B, transB, D, quantizer, output_dtype, bias, bias_type,
437+
gelu, gelu_in, grad, workspace, workspace_size, accumulate,
438+
use_split_accumulator, comm_overlap, comm_type, extra_output,
439+
bulk_overlap, alpha, beta,
440+
)
441+
442+
def te_general_grouped_gemm(
443+
self,
444+
A: List[torch.Tensor],
445+
transa: bool,
446+
B: List[torch.Tensor],
447+
transb: bool,
448+
D: Optional[List[torch.Tensor]],
449+
D_type,
450+
m_splits: List[int],
451+
bias: List[torch.Tensor],
452+
bias_type,
453+
single_output: bool,
454+
pre_gelu_out: List[torch.Tensor],
455+
grad: bool,
456+
workspace: List[torch.Tensor],
457+
workspaceSizes: int,
458+
accumulate: bool,
459+
use_split_accumulator: bool,
460+
math_sm_count: int,
461+
) -> Optional[List[torch.Tensor]]:
462+
tex = self._get_tex()
463+
return tex.te_general_grouped_gemm(
464+
A, transa, B, transb, D, D_type, m_splits, bias, bias_type,
465+
single_output, pre_gelu_out, grad, workspace, workspaceSizes,
466+
accumulate, use_split_accumulator, math_sm_count,
467+
)

transformer_engine/plugin/core/backends/vendor/kunlunxin/register_ops.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ def register_builtins(registry) -> None:
6161
vendor="KUNLUNXIN",
6262
priority=100,
6363
),
64+
OpImpl(
65+
op_name="layernorm_fwd",
66+
impl_id="vendor.kunlunxin",
67+
kind=BackendImplKind.VENDOR,
68+
fn=_bind_is_available(backend.layernorm_fwd, is_avail),
69+
vendor="KUNLUNXIN",
70+
priority=200,
71+
),
72+
OpImpl(
73+
op_name="layernorm_bwd",
74+
impl_id="vendor.kunlunxin",
75+
kind=BackendImplKind.VENDOR,
76+
fn=_bind_is_available(backend.layernorm_bwd, is_avail),
77+
vendor="KUNLUNXIN",
78+
priority=200,
79+
),
6480
OpImpl(
6581
op_name="multi_tensor_adam",
6682
impl_id="vendor.kunlunxin",
@@ -181,6 +197,23 @@ def register_builtins(registry) -> None:
181197
vendor="KUNLUNXIN",
182198
priority=100,
183199
),
200+
# GEMM (XPU via hydrax in transformer_engine_klx_torch.gemm)
201+
OpImpl(
202+
op_name="generic_gemm",
203+
impl_id="vendor.kunlunxin",
204+
kind=BackendImplKind.VENDOR,
205+
fn=_bind_is_available(backend.generic_gemm, is_avail),
206+
vendor="KUNLUNXIN",
207+
priority=200,
208+
),
209+
OpImpl(
210+
op_name="te_general_grouped_gemm",
211+
impl_id="vendor.kunlunxin",
212+
kind=BackendImplKind.VENDOR,
213+
fn=_bind_is_available(backend.te_general_grouped_gemm, is_avail),
214+
vendor="KUNLUNXIN",
215+
priority=200,
216+
),
184217
]
185218

186219
registry.register_many(impls)

0 commit comments

Comments
 (0)