-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[CPU] add optimizations for INT8 and FP8 DeepSeek #6769
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
+179
−3
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ccae96b
support W8A8 for DeepSeek-R1 on CPU (#19)
chunyuan-w da85df4
Use shared_expert if use_intel_amx_backend and n_shared_experts is no…
chunyuan-w 05df591
Use int8_scaled_mm_with_quant (#28)
chunyuan-w ae7d006
Use sgl_kernel.cpu.fp8_scaled_mm in Fp8LinearMethod (#50)
chunyuan-w 01cb26f
Enable FP8 DeepSeek R1 (#52)
chunyuan-w 8c5dc85
Integrate FP8 shared_expert (#59)
chunyuan-w 4159a92
Integrate FP8 fused_moe (#62)
chunyuan-w 575bc3c
fix capability check
chunyuan-w 2a4513a
fix FP8 linear after rebase
chunyuan-w 9793c80
convert bmm weight from fp8 to bf16
blzheng b61007d
fix format
chunyuan-w 5ff3d9a
refine frontend change in deepseek_v2.py
chunyuan-w 1c5be08
simplify shared_experts_is_int8 and shared_experts_is_fp8
chunyuan-w 331ba0e
remove unnecessary view
chunyuan-w 8723032
fix if the obj does not have the use_intel_amx_backend attr
chunyuan-w 3ef5f18
use _is_cpu and _is_cpu_amx_available to check when converting bmm FP…
chunyuan-w b253b91
Merge branch 'main' into chunyuan/pr_int8_fp8
chunyuan-w 3b778ee
use _is_cpu for device check
chunyuan-w 4742008
Merge branch 'main' into chunyuan/pr_int8_fp8
chunyuan-w ac476cf
Merge branch 'main' into chunyuan/pr_int8_fp8
zhyncs 63a0197
Merge branch 'main' into chunyuan/pr_int8_fp8
zhyncs 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ def dummy_func(*args, **kwargs): | |
| ) | ||
| from sglang.srt.layers.utils import is_sm100_supported | ||
| from sglang.srt.utils import ( | ||
| _process_weight_after_loading, | ||
| cpu_has_amx_support, | ||
| get_bool_env_var, | ||
| is_cpu, | ||
|
|
@@ -330,6 +331,12 @@ def process_weights_after_loading(self, layer: Module) -> None: | |
| ) | ||
|
|
||
| layer.input_scale = None | ||
| elif layer.weight.device.type == "cpu": | ||
| assert ( | ||
| cpu_has_amx_support() | ||
| ), "Fp8LinearMethod on CPU requires that CPU has AMX support" | ||
| _process_weight_after_loading(layer, ["weight"]) | ||
| return | ||
| else: | ||
| weight, weight_scale = layer.weight.data, layer.weight_scale_inv.data | ||
| layer.weight = torch.nn.Parameter(weight, requires_grad=False) | ||
|
|
@@ -426,6 +433,17 @@ def apply( | |
| ) | ||
|
|
||
| if self.block_quant: | ||
| if getattr(layer, "use_intel_amx_backend", False): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refine this line after addressing #6641 (review)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in #7647 |
||
| return torch.ops.sgl_kernel.fp8_scaled_mm_cpu( | ||
| x, | ||
| layer.weight, | ||
| layer.weight_scale_inv, | ||
| self.quant_config.weight_block_size, | ||
| bias, | ||
| x.dtype, | ||
| True, # is_vnni | ||
| ) | ||
|
|
||
| return self.w8a8_block_fp8_linear( | ||
| input=x, | ||
| weight=layer.weight, | ||
|
|
@@ -746,6 +764,13 @@ def process_weights_after_loading(self, layer: Module) -> None: | |
| layer.w2_weight.data = shuffle_weight( | ||
| layer.w2_weight.contiguous(), (16, 16) | ||
| ) | ||
|
|
||
| if all(w.device.type == "cpu" for w in [layer.w13_weight, layer.w2_weight]): | ||
| assert ( | ||
| cpu_has_amx_support() | ||
| ), "Fp8MoEMethod on CPU requires that CPU has AMX support" | ||
| _process_weight_after_loading(layer, ["w13_weight", "w2_weight"]) | ||
|
|
||
| return | ||
|
|
||
| # If checkpoint is fp16 or bfloat16, quantize in place. | ||
|
|
@@ -971,6 +996,24 @@ def apply( | |
| routed_scaling_factor=routed_scaling_factor, | ||
| ) | ||
|
|
||
| if getattr(layer, "use_intel_amx_backend", False): | ||
| return torch.ops.sgl_kernel.fused_experts_cpu( | ||
| x, | ||
| layer.w13_weight, | ||
| layer.w2_weight, | ||
| topk_weights, | ||
| topk_ids, | ||
| False, # inplace See [Note] inplace should be False in fused_experts. | ||
| False, # use_int8_w8a8 | ||
| True, # use_fp8_w8a16 | ||
| layer.w13_weight_scale_inv, # w1_scale | ||
| layer.w2_weight_scale_inv, # w2_scale | ||
| self.quant_config.weight_block_size, # block_size | ||
| None, # a1_scale | ||
| None, # a2_scale | ||
| True, # is_vnni | ||
| ) | ||
|
|
||
| if _is_hip: | ||
| ret = self.maybe_apply_hip_fused_experts( | ||
| layer, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refine this method after addressing #6641 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #7647