-
Notifications
You must be signed in to change notification settings - Fork 29
Update DeepSeek v4 precision checks #237
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
Open
high-cloud
wants to merge
1
commit into
hw-native-sys:main
Choose a base branch
from
high-cloud:fix/deepseek-v4-precision-tol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -329,10 +329,63 @@ def rms_norm(x, gamma, eps=EPS): | |
| inv = torch.rsqrt(x.square().mean(-1, keepdim=True) + eps) | ||
| return x * inv * gamma | ||
|
|
||
| def matmul_bf16_input_fp32(a, b): | ||
| def matmul_wqa_tiled(a, b): | ||
| a_fp32 = a.to(torch.bfloat16).float() | ||
| b_fp32 = b.to(torch.bfloat16).float() | ||
| return torch.matmul(a_fp32, b_fp32).float() | ||
| out = torch.empty(T, Q_LORA, dtype=torch.float32) | ||
| for qb in range(Q_BLOCKS): | ||
| q0 = qb * Q_LORA_CHUNK | ||
| acc = torch.matmul(a_fp32[:, :D_CHUNK], b_fp32[:D_CHUNK, q0:q0 + Q_LORA_CHUNK]) | ||
| for db in range(1, D_BLOCKS): | ||
| d0 = db * D_CHUNK | ||
| acc = acc + torch.matmul( | ||
| a_fp32[:, d0:d0 + D_CHUNK], | ||
| b_fp32[d0:d0 + D_CHUNK, q0:q0 + Q_LORA_CHUNK], | ||
| ) | ||
| out[:, q0:q0 + Q_LORA_CHUNK] = acc | ||
| return out | ||
|
|
||
| def matmul_wkv_tiled(a, b): | ||
| a_fp32 = a.to(torch.bfloat16).float() | ||
| b_fp32 = b.to(torch.bfloat16).float() | ||
| out = torch.empty(T, HEAD_DIM, dtype=torch.float32) | ||
| for kb in range(KV_BLOCKS): | ||
| k0 = kb * KV_CHUNK | ||
| acc = torch.matmul(a_fp32[:, :D_CHUNK], b_fp32[:D_CHUNK, k0:k0 + KV_CHUNK]) | ||
| for db in range(1, D_BLOCKS): | ||
| d0 = db * D_CHUNK | ||
| acc = acc + torch.matmul( | ||
| a_fp32[:, d0:d0 + D_CHUNK], | ||
| b_fp32[d0:d0 + D_CHUNK, k0:k0 + KV_CHUNK], | ||
| ) | ||
| out[:, k0:k0 + KV_CHUNK] = acc | ||
| return out | ||
|
|
||
| def rms_norm_q_tiled(x, gamma): | ||
| sq_sum = torch.zeros(T, 1, dtype=torch.float32) | ||
| for qb in range(Q_BLOCKS): | ||
| q0 = qb * Q_LORA_CHUNK | ||
| chunk = x[:, q0:q0 + Q_LORA_CHUNK] | ||
| sq_sum = sq_sum + chunk.square().sum(dim=-1, keepdim=True) | ||
| inv = torch.rsqrt(sq_sum * (1.0 / Q_LORA) + EPS) | ||
| out = torch.empty_like(x) | ||
| for qb in range(Q_BLOCKS): | ||
| q0 = qb * Q_LORA_CHUNK | ||
| out[:, q0:q0 + Q_LORA_CHUNK] = x[:, q0:q0 + Q_LORA_CHUNK] * inv * gamma[q0:q0 + Q_LORA_CHUNK] | ||
| return out | ||
|
|
||
| def rms_norm_kv_tiled(x, gamma): | ||
| sq_sum = torch.zeros(T, 1, dtype=torch.float32) | ||
| for kb in range(KV_BLOCKS): | ||
| k0 = kb * KV_CHUNK | ||
| chunk = x[:, k0:k0 + KV_CHUNK] | ||
| sq_sum = sq_sum + chunk.square().sum(dim=-1, keepdim=True) | ||
| inv = torch.rsqrt(sq_sum * (1.0 / HEAD_DIM) + EPS) | ||
| out = torch.empty_like(x) | ||
| for kb in range(KV_BLOCKS): | ||
| k0 = kb * KV_CHUNK | ||
| out[:, k0:k0 + KV_CHUNK] = x[:, k0:k0 + KV_CHUNK] * inv * gamma[k0:k0 + KV_CHUNK] | ||
| return out | ||
|
|
||
| def apply_rope(x_rope, cos, sin): | ||
| # x_rope: [T, ..., ROPE_DIM] using lo/hi half split. | ||
|
|
@@ -356,7 +409,7 @@ def apply_rope(x_rope, cos, sin): | |
| token_x = rms_norm(x.view(T, D), norm_w) # [T, D] | ||
|
|
||
| # Q path | ||
| qr_out = rms_norm(matmul_bf16_input_fp32(token_x, wq_a), gamma_cq) # [T, Q_LORA] | ||
| qr_out = rms_norm_q_tiled(matmul_wqa_tiled(token_x, wq_a), gamma_cq) # [T, Q_LORA] | ||
| # W8A8C16: wq_b W8 per-output-channel int8; qr_out A8 per-token int8. | ||
| # flash: also quantizes wq_a/wkv to fp8 (default Linear dtype). | ||
| qr_out_bf16 = qr_out.to(torch.bfloat16) | ||
|
|
@@ -370,7 +423,7 @@ def apply_rope(x_rope, cos, sin): | |
| q_out = torch.cat([q_nope, q_rope], dim=-1) | ||
|
|
||
| # KV path | ||
| kv_full = rms_norm(matmul_bf16_input_fp32(token_x, wkv), gamma_ckv) # [T, HEAD_DIM] | ||
| kv_full = rms_norm_kv_tiled(matmul_wkv_tiled(token_x, wkv), gamma_ckv) # [T, HEAD_DIM] | ||
| kv_nope = kv_full[..., :NOPE_DIM] | ||
| kv_rope_in = kv_full[..., NOPE_DIM:].unsqueeze(1) # add a pseudo head dim | ||
| kv_rope = apply_rope(kv_rope_in, rope_cos, rope_sin).squeeze(1) | ||
|
|
@@ -441,6 +494,7 @@ def init_gamma_ckv(): | |
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
| import torch | ||
| from golden import RunConfig, run_jit | ||
|
|
||
| def int8_lsb_compare(actual, expected, actual_outputs, expected_outputs, inputs, rtol, atol): | ||
|
|
@@ -451,22 +505,44 @@ def int8_lsb_compare(actual, expected, actual_outputs, expected_outputs, inputs, | |
| return True, "" | ||
| return False, "max INT8 diff > 1" | ||
|
|
||
| def bf16_outlier_compare(actual, expected, actual_outputs, expected_outputs, inputs, rtol, atol): | ||
| import torch | ||
|
|
||
| close = torch.isclose(actual, expected, rtol=rtol, atol=atol) | ||
| mismatch = int((~close).sum().item()) | ||
| max_mismatch = int(actual.numel() * 0.005) | ||
| if mismatch <= max_mismatch: | ||
| return True, f"mismatch={mismatch}/{actual.numel()} <= {max_mismatch}" | ||
|
|
||
| diff = (actual.float() - expected.float()).abs() | ||
| max_idx = int(diff.flatten().argmax().item()) | ||
| return False, ( | ||
| f" BF16 outlier budget exceeded: mismatch={mismatch}/{actual.numel()} " | ||
| f"limit={max_mismatch} rtol={rtol} atol={atol}\n" | ||
| f" max_abs={diff.max().item():.8g} idx={max_idx} " | ||
| f"actual={actual.flatten()[max_idx].item()} expected={expected.flatten()[max_idx].item()}" | ||
| ) | ||
|
Comment on lines
+508
to
+524
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. |
||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-p", "--platform", type=str, default="a2a3", | ||
| choices=["a2a3", "a2a3sim", "a5", "a5sim"]) | ||
| parser.add_argument("-d", "--device", type=int, default=0) | ||
| parser.add_argument("--seed", type=int, default=20260508) | ||
| parser.add_argument("--runtime-profiling", action="store_true", default=False) | ||
| args = parser.parse_args() | ||
|
|
||
| torch.manual_seed(args.seed) | ||
|
|
||
| result = run_jit( | ||
| fn=deepseek_v4_decode_qkv_proj_rope_test, | ||
| specs=build_tensor_specs(), | ||
| golden_fn=golden_deepseek_v4_decode_qkv_proj_rope, | ||
| config=RunConfig( | ||
| # W8A8C16 q_proj adds INT8 quant/dequant round-off before per-head RMSNorm. | ||
| rtol=5e-3, | ||
| atol=5e-3, | ||
| compare_fn={"qr": int8_lsb_compare}, | ||
| # Allow a small BF16 tail: at most 0.5% elements may exceed the tolerance. | ||
| rtol=2e-3, | ||
| atol=2e-3, | ||
| compare_fn={"q": bf16_outlier_compare, "kv": bf16_outlier_compare, "qr": int8_lsb_compare}, | ||
| compile=dict(dump_passes=True), | ||
| runtime=dict( | ||
| platform=args.platform, | ||
|
|
||
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.
The
_to_device_bf16helper function is duplicated inmodels/deepseek/v4/deepseek_v4_decode_sparse_attn.py. Consider refactoring this into a shared utility module to adhere to DRY principles and ensure consistent rounding behavior across the project.