[XPU] Route activation CustomOps to SYCL kernels (#53734)
Signed-off-by: Marceli Fylcek <marceli.fylcek@intel.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com>
This commit is contained in:
co-authored by
Claude Opus 5
Kunshang Ji
parent
8600db5dff
commit
4c21d41733
@@ -87,7 +87,7 @@ class FatreluAndMul(CustomOp):
|
|||||||
def __init__(self, threshold: float = 0.0):
|
def __init__(self, threshold: float = 0.0):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.threshold = threshold
|
self.threshold = threshold
|
||||||
if current_platform.is_cuda_alike():
|
if current_platform.is_cuda_alike() or current_platform.is_xpu():
|
||||||
self.op = torch.ops._C.fatrelu_and_mul
|
self.op = torch.ops._C.fatrelu_and_mul
|
||||||
elif current_platform.is_cpu():
|
elif current_platform.is_cpu():
|
||||||
self._forward_method = self.forward_native
|
self._forward_method = self.forward_native
|
||||||
@@ -106,6 +106,9 @@ class FatreluAndMul(CustomOp):
|
|||||||
self.op(out, x, self.threshold)
|
self.op(out, x, self.threshold)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
return self.forward_cuda(x)
|
||||||
|
|
||||||
|
|
||||||
# --8<-- [start:silu_and_mul]
|
# --8<-- [start:silu_and_mul]
|
||||||
@CustomOp.register("silu_and_mul")
|
@CustomOp.register("silu_and_mul")
|
||||||
@@ -171,7 +174,7 @@ class SituAndMul(CustomOp):
|
|||||||
super().__init__(compile_native=compile_native)
|
super().__init__(compile_native=compile_native)
|
||||||
self.beta = float(beta)
|
self.beta = float(beta)
|
||||||
self.linear_beta = None if linear_beta is None else float(linear_beta)
|
self.linear_beta = None if linear_beta is None else float(linear_beta)
|
||||||
if current_platform.is_cuda_alike():
|
if current_platform.is_cuda_alike() or current_platform.is_xpu():
|
||||||
self.op = torch.ops._C.situ_and_mul
|
self.op = torch.ops._C.situ_and_mul
|
||||||
|
|
||||||
def forward_native(self, x: torch.Tensor) -> torch.Tensor:
|
def forward_native(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
@@ -184,7 +187,7 @@ class SituAndMul(CustomOp):
|
|||||||
return (gate * up).to(x.dtype)
|
return (gate * up).to(x.dtype)
|
||||||
|
|
||||||
def forward_cuda(self, x: torch.Tensor) -> torch.Tensor:
|
def forward_cuda(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
# Fused CUDA kernel: writes straight to `out`, no fp32 temporaries.
|
# Fused CUDA/XPU kernel: writes straight to `out`, no fp32 temporaries.
|
||||||
# linear_beta<=0 signals "unset" to the kernel (up passed through).
|
# linear_beta<=0 signals "unset" to the kernel (up passed through).
|
||||||
d = x.shape[-1] // 2
|
d = x.shape[-1] // 2
|
||||||
out = torch.empty(x.shape[:-1] + (d,), dtype=x.dtype, device=x.device)
|
out = torch.empty(x.shape[:-1] + (d,), dtype=x.dtype, device=x.device)
|
||||||
@@ -194,7 +197,7 @@ class SituAndMul(CustomOp):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
return self.forward_native(x)
|
return self.forward_cuda(x)
|
||||||
|
|
||||||
|
|
||||||
@CustomOp.register("silu_and_mul_with_clamp")
|
@CustomOp.register("silu_and_mul_with_clamp")
|
||||||
@@ -503,6 +506,9 @@ class SwigluOAIAndMul(CustomOp):
|
|||||||
torch.ops._C.swigluoai_and_mul(out, x, self.alpha, self.limit)
|
torch.ops._C.swigluoai_and_mul(out, x, self.alpha, self.limit)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
return self.forward_cuda(x)
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
def extra_repr(self) -> str:
|
||||||
return f"alpha={repr(self.alpha)}, limit={repr(self.limit)}"
|
return f"alpha={repr(self.alpha)}, limit={repr(self.limit)}"
|
||||||
|
|
||||||
@@ -541,6 +547,13 @@ class SwigluStepAndMul(CustomOp):
|
|||||||
swiglustep_and_mul_triton(out, x, self.limit)
|
swiglustep_and_mul_triton(out, x, self.limit)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
d = x.shape[-1] // 2
|
||||||
|
output_shape = x.shape[:-1] + (d,)
|
||||||
|
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
|
||||||
|
torch.ops._C.swiglustep_and_mul(out, x, self.limit)
|
||||||
|
return out
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
def extra_repr(self) -> str:
|
||||||
return f"limit={repr(self.limit)}"
|
return f"limit={repr(self.limit)}"
|
||||||
|
|
||||||
@@ -656,6 +669,8 @@ class ReLUSquaredActivation(CustomOp):
|
|||||||
super().__init__(compile_native=compile_native)
|
super().__init__(compile_native=compile_native)
|
||||||
if current_platform.is_cuda_alike():
|
if current_platform.is_cuda_alike():
|
||||||
self.op = torch.ops._C.relu_squared
|
self.op = torch.ops._C.relu_squared
|
||||||
|
elif current_platform.is_xpu():
|
||||||
|
self.op = torch.ops._C.relu2_no_mul
|
||||||
|
|
||||||
def forward_native(self, x: torch.Tensor) -> torch.Tensor:
|
def forward_native(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
"""PyTorch-native implementation equivalent to forward()."""
|
"""PyTorch-native implementation equivalent to forward()."""
|
||||||
@@ -666,6 +681,9 @@ class ReLUSquaredActivation(CustomOp):
|
|||||||
self.op(out, x)
|
self.op(out, x)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def forward_xpu(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
return self.forward_cuda(x)
|
||||||
|
|
||||||
|
|
||||||
# --8<-- [start:xielu]
|
# --8<-- [start:xielu]
|
||||||
@CustomOp.register("xielu")
|
@CustomOp.register("xielu")
|
||||||
|
|||||||
Reference in New Issue
Block a user