Repository navigation
[ops] fix: honor sigmoid activation in npu gated rmsnorm #1223
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
Closed
YLlllllllllll
wants to merge
6
commits into
ByteDance-Seed:main
from
YLlllllllllll:qwen38-base-gated-norm
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98cf4a2
[ops] fix: honor sigmoid activation in npu gated rmsnorm
YLlllllllllll 17a9a2d
[ops, tests] fix: gate native RMSNorm forward and backward parity
YLlllllllllll 69a0183
[ops] fix: align gated norm regression fixtures with elementwise cont…
YLlllllllllll e911972
[ci] fix: require native Ascend for gated norm parity
YLlllllllllll 0df9418
[ops] fix: preserve validated sigmoid precision and upstream activati…
YLlllllllllll 1256dc0
ci: require a live NPU inside gated RMSNorm parity tests
YLlllllllllll 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
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,133 @@ | ||
| """CPU contract checks and native Ascend forward/backward parity.""" | ||
|
|
||
| import importlib.util | ||
| import os | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
|
|
||
| def load_wrapper(): | ||
| def rms(x, weight, eps): | ||
| normalized = x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim=True) + eps) | ||
| return (normalized.to(x.dtype) * weight,) | ||
|
|
||
| def swiglu(x, dim): | ||
| gate, value = x.chunk(2, dim=dim) | ||
| return F.silu(gate) * value | ||
|
|
||
| fake = types.SimpleNamespace(npu_rms_norm=rms, npu_swiglu=swiglu) | ||
| path = Path(__file__).parents[2] / "veomni/ops/kernels/gated_delta_rule/npu_rms_norm_gated.py" | ||
| spec = importlib.util.spec_from_file_location("_gated_norm_test", path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| with patch.dict(sys.modules, {"torch_npu": fake}): | ||
| spec.loader.exec_module(module) | ||
| return module.NPUFusedRMSNormGated | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32]) | ||
| def test_sigmoid_reference_and_gradients(dtype): | ||
| torch.manual_seed(73) | ||
| module = load_wrapper()(128, dtype=dtype, activation="sigmoid") | ||
| with torch.no_grad(): | ||
| module.weight.copy_(torch.linspace(0.25, 1.75, 128).to(dtype)) | ||
| x = torch.randn(7, 128, dtype=dtype).requires_grad_() | ||
| z = torch.linspace(-20, 20, 896).reshape(7, 128).to(dtype).requires_grad_() | ||
| r = x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim=True) + module.variance_epsilon) | ||
| expected = (module.weight * r.to(dtype) * z.float().sigmoid()).to(dtype) | ||
| actual = module(x, z) | ||
| torch.testing.assert_close(actual, expected, atol=0, rtol=0) | ||
| dy = torch.randn_like(actual) | ||
| got = torch.autograd.grad(actual, (x, z, module.weight), dy) | ||
| ref = torch.autograd.grad(expected, (x, z, module.weight), dy) | ||
| for a, b in zip(got, ref, strict=True): | ||
| torch.testing.assert_close(a, b, atol=0, rtol=0) | ||
|
|
||
|
|
||
| def test_zero_gate_is_half_normalized_and_silu_unchanged(): | ||
| cls = load_wrapper() | ||
| x = torch.ones(2, 128) | ||
| z = torch.zeros_like(x) | ||
| torch.testing.assert_close(cls(128, activation="sigmoid")(x, z), x * 0.5 / (1 + 1e-6) ** 0.5) | ||
| torch.testing.assert_close(cls(128)(x, z), torch.zeros_like(x)) | ||
| with pytest.raises(ValueError, match="activation"): | ||
| cls(128, activation="unsupported_gate") | ||
| with pytest.raises(ValueError, match="gate"): | ||
| cls(128, activation="sigmoid")(x) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32]) | ||
| @pytest.mark.parametrize("activation", ["sigmoid", "silu"]) | ||
| @pytest.mark.parametrize("autocast_enabled", [False, True]) | ||
| def test_native_npu_reference_and_gradients(dtype, activation, autocast_enabled): | ||
| # Load the real wrapper without the CPU tests' torch_npu substitution. | ||
| if os.environ.get("VEOMNI_REQUIRE_NPU") == "1": | ||
| import torch_npu # noqa: F401 | ||
|
|
||
| assert torch.npu.is_available(), "Ascend parity gate requires a live NPU" | ||
| else: | ||
| pytest.importorskip("torch_npu") | ||
| if not torch.npu.is_available(): | ||
| pytest.skip("Native Ascend device is required") | ||
| path = Path(__file__).parents[2] / "veomni/ops/kernels/gated_delta_rule/npu_rms_norm_gated.py" | ||
| spec = importlib.util.spec_from_file_location("_native_gated_norm_test", path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| torch.manual_seed(73) | ||
| norm = module.NPUFusedRMSNormGated(128, device="npu", dtype=dtype, activation=activation) | ||
| with torch.no_grad(): | ||
| norm.weight.copy_(torch.linspace(0.25, 1.75, 128).to(device="npu", dtype=dtype)) | ||
| x = torch.randn(2, 7, 128, device="npu", dtype=dtype).requires_grad_() | ||
| z = torch.linspace(-20, 20, x.numel()).reshape(x.shape).to(device="npu", dtype=dtype).requires_grad_() | ||
| rx, rz, rw = (tensor.detach().clone().requires_grad_() for tensor in (x, z, norm.weight)) | ||
| # An independent torch reference preserves the architecture's cast boundary. | ||
| with torch.autocast("npu", enabled=False): | ||
| normalized = rx.float() * torch.rsqrt(rx.float().square().mean(-1, keepdim=True) + norm.variance_epsilon) | ||
| if activation == "sigmoid": | ||
| expected = (rw * normalized.to(dtype) * rz.float().sigmoid()).to(dtype) | ||
| else: | ||
| expected = F.silu(rz) * (normalized * rw.float()).to(dtype) | ||
| with torch.autocast("npu", dtype=torch.bfloat16, enabled=autocast_enabled): | ||
| actual = norm(x, z) | ||
| assert actual.dtype == dtype | ||
| dy = torch.randn_like(actual) | ||
| got = torch.autograd.grad(actual, (x, z, norm.weight), dy) | ||
| ref = torch.autograd.grad(expected, (rx, rz, rw), dy) | ||
| # Fused CANN reductions need tolerance; BF16 roundoff is not bitwise FP32. | ||
| rtol, atol = (3e-2, 2e-2) if dtype == torch.bfloat16 else (3e-5, 3e-5) | ||
| for a, b in zip((actual, *got), (expected, *ref), strict=True): | ||
| assert torch.isfinite(a).all() | ||
| torch.testing.assert_close(a, b, atol=atol, rtol=rtol) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("activation", ["sigmoid", "silu"]) | ||
| def test_gate_must_match_input_shape(activation): | ||
| norm = load_wrapper()(32, activation=activation) | ||
| x = torch.ones(1, 4, 32) | ||
| for gate in [None, torch.zeros(1, 4, 64), torch.zeros(1, 1, 32)]: | ||
| with pytest.raises(ValueError, match="gate with the input shape"): | ||
| norm(x, gate) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("activation", ["relu", "gelu", "tanh", "swish"]) | ||
| def test_upstream_activation_fallback_is_preserved(activation): | ||
| from transformers.activations import ACT2FN | ||
|
|
||
| torch.manual_seed(94) | ||
| norm = load_wrapper()(32, activation=activation) | ||
| x = torch.randn(2, 32, requires_grad=True) | ||
| z = torch.randn_like(x, requires_grad=True) | ||
| normalized = x * torch.rsqrt(x.square().mean(-1, keepdim=True) + norm.variance_epsilon) | ||
| expected = normalized * norm.weight * ACT2FN[activation](z) | ||
| actual = norm(x, z) | ||
| torch.testing.assert_close(actual, expected) | ||
| for got, ref in zip( | ||
| torch.autograd.grad(actual.sum(), (x, z, norm.weight)), | ||
| torch.autograd.grad(expected.sum(), (x, z, norm.weight)), | ||
| ): | ||
| torch.testing.assert_close(got, ref) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.