Repository navigation
[XPU] Use SYCL kernels for DeepSeek V4 MHC on XPU #32166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4e8c95f
Use XPU mhc_fused_post_pre
cyxlily 63fda09
Use XPU fused_hc_head
cyxlily 54fde04
Avoid using CUDA when XPU
cyxlily 87204c3
Update mhc import
cyxlily 8d570c2
Not use deep_gemm when XPU
cyxlily 9ded9f0
Merge remote-tracking branch 'origin/main' into mhc
cyxlily a941127
Merge branch 'main' into mhc
cyxlily 9aa9e7f
Update ut
cyxlily 9f8158a
Merge remote-tracking branch 'origin/main' into mhc
cyxlily 961d36b
[XPU] Skip TileLang MHC prewarm on XPU
cyxlily 411968f
Use get_device_core_count
cyxlily 79013ef
Remove XPU-specific FP8 fallback and simplify MHC core count
cyxlily d6664d4
Remove bench from ut
cyxlily 2319fbd
Remove xpu check from tilelang file
cyxlily 132e0cd
Refactor MHC backend dispatch
cyxlily d27885a
Fix lint
cyxlily 2059c91
Merge remote-tracking branch 'upstream/main' into mhc
cyxlily 4356e1d
Move fused_hc_head into _get_mhc_ops
cyxlily 2fd197d
Modify mhc kernel dispatch
cyxlily 9fabd98
Keep CUDA multi-processor count untoched
cyxlily 8d4ebd0
Update use_fused_mhc_post_pre check
cyxlily File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import statistics | ||
| import time | ||
| import types | ||
| import unittest | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from sglang.kernels.ops.layernorm.mhc_head import fused_hc_head as triton_fused_hc_head | ||
| from sglang.srt.models.deepseek_v4 import DeepseekV4Model | ||
| from sglang.test.ci.ci_register import register_xpu_ci | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
| register_xpu_ci(est_time=30, suite="stage-b-test-1-gpu-xpu") | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def skip_if_no_xpu(): | ||
| if not hasattr(torch, "xpu") or not torch.xpu.is_available(): | ||
| pytest.skip("XPU not available") | ||
|
cyxlily marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _sync_xpu() -> None: | ||
| if hasattr(torch, "xpu") and torch.xpu.is_available(): | ||
| torch.xpu.synchronize() | ||
|
cyxlily marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _bench_ms(fn, warmup: int = 10, iters: int = 50): | ||
| for _ in range(warmup): | ||
| fn() | ||
| _sync_xpu() | ||
|
|
||
| samples = [] | ||
| for _ in range(iters): | ||
| start = time.perf_counter() | ||
| fn() | ||
| _sync_xpu() | ||
| samples.append((time.perf_counter() - start) * 1000.0) | ||
|
|
||
| return { | ||
| "mean_ms": statistics.mean(samples), | ||
| "median_ms": statistics.median(samples), | ||
| "min_ms": min(samples), | ||
| "max_ms": max(samples), | ||
| } | ||
|
cyxlily marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| class TestHCHead(CustomTestCase): | ||
| HC_MULT = 4 | ||
| HIDDEN_SIZE = 7168 | ||
| NUM_TOKENS = [1, 16, 128, 1024] | ||
| DTYPE = torch.bfloat16 | ||
| NORM_EPS = 1e-6 | ||
| HC_EPS = 1e-6 | ||
| SEED = 2026 | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| if not hasattr(torch, "xpu") or not torch.xpu.is_available(): | ||
| raise unittest.SkipTest("XPU is not available") | ||
| try: | ||
| from sgl_kernel.mhc import fused_hc_head as _ # noqa: F401 | ||
| except ImportError: | ||
| raise unittest.SkipTest("sgl-kernel fused_hc_head is not available") | ||
| torch.set_default_device("xpu") | ||
|
|
||
| def _make_inputs(self, num_tokens: int): | ||
| torch.manual_seed(self.SEED + num_tokens) | ||
| x = torch.randn( | ||
| num_tokens, | ||
| self.HC_MULT, | ||
| self.HIDDEN_SIZE, | ||
| dtype=self.DTYPE, | ||
| device="xpu", | ||
| ).contiguous() | ||
| hc_fn = torch.randn( | ||
| self.HC_MULT, | ||
| self.HC_MULT * self.HIDDEN_SIZE, | ||
| dtype=torch.float32, | ||
| device="xpu", | ||
| ).contiguous() | ||
| hc_base = torch.randn( | ||
| self.HC_MULT, | ||
| dtype=torch.float32, | ||
| device="xpu", | ||
| ).contiguous() | ||
| hc_scale = torch.randn(1, dtype=torch.float32, device="xpu").contiguous() | ||
| return x, hc_fn, hc_scale, hc_base | ||
|
|
||
| def test_hc_head(self): | ||
| for num_tokens in self.NUM_TOKENS: | ||
| with self.subTest(num_tokens=num_tokens): | ||
| x, hc_fn, hc_scale, hc_base = self._make_inputs(num_tokens) | ||
|
|
||
| _ctx = types.SimpleNamespace(norm_eps=self.NORM_EPS, hc_eps=self.HC_EPS) | ||
|
|
||
| with torch.inference_mode(): | ||
| triton_out = triton_fused_hc_head( | ||
| x, | ||
| hc_fn, | ||
| hc_scale, | ||
| hc_base, | ||
| self.NORM_EPS, | ||
| self.HC_EPS, | ||
| ) | ||
| # Go through the actual DeepseekV4Model.hc_head dispatch path | ||
| # (on XPU this calls sgl_kernel.mhc.fused_hc_head). | ||
| sycl_out = DeepseekV4Model.hc_head( | ||
| _ctx, x, hc_fn, hc_scale, hc_base | ||
| ) | ||
|
|
||
| torch.testing.assert_close( | ||
| triton_out, | ||
| sycl_out, | ||
| atol=2e-2, | ||
| rtol=2e-2, | ||
| ) | ||
|
|
||
| def test_hc_head_perf(self): | ||
| for num_tokens in self.NUM_TOKENS: | ||
| with self.subTest(num_tokens=num_tokens): | ||
| x, hc_fn, hc_scale, hc_base = self._make_inputs(num_tokens) | ||
|
|
||
| _ctx = types.SimpleNamespace(norm_eps=self.NORM_EPS, hc_eps=self.HC_EPS) | ||
|
|
||
| triton_stats = _bench_ms( | ||
| lambda: triton_fused_hc_head( | ||
| x, | ||
| hc_fn, | ||
| hc_scale, | ||
| hc_base, | ||
| self.NORM_EPS, | ||
| self.HC_EPS, | ||
| ) | ||
| ) | ||
| sycl_stats = _bench_ms( | ||
| lambda: DeepseekV4Model.hc_head(_ctx, x, hc_fn, hc_scale, hc_base) | ||
| ) | ||
|
|
||
| self.assertLess( | ||
| sycl_stats["median_ms"], | ||
| triton_stats["median_ms"], | ||
| f"sycl ({sycl_stats['median_ms']:.3f} ms) not faster than triton " | ||
| f"({triton_stats['median_ms']:.3f} ms) at num_tokens={num_tokens}", | ||
| ) | ||
|
cyxlily marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.