Why is there nan in @testsilumul.py ? Is there any issue with the inplace kernel? I've…
- User
Why is there nan in @test_silu_mul.py ? Is there any issue with the inplace kernel? I've tested that it gives nan from Triton 3.2 to 3.5, and on both Windows and Linux. --- Content from referenced files --- Content from @test_silu_mul.py: import torch import triton import triton.language as tl
from qwen3_moe_fused.kernels.silu_mul import ( _silu_mul_backward_inplace_kernel, _silu_mul_backward_kernel, )
def test_kernel_alignment(): device = "cuda" n_elements = 2184 dtype = torch.float32
code# Define a reference implementation def ref_impl(dh, e, g): se = torch.sigmoid(e.float()) ese = e.float() * se f = ese.to(e.dtype) h_out = f * g dg_out = f * dh de_out = (dh * g).float() * se * (1.0 + e.float() - ese) return h_out, de_out.to(e.dtype), dg_out # Helper to allocate aligned tensor def get_aligned_tensor(size, alignment, offset_bytes=0): # Allocate extra to ensure we can find an aligned offset extra = alignment + offset_bytes total_elements = size + (extra // 4) + 16 base = torch.zeros(total_elements, device=device, dtype=dtype) ptr = base.data_ptr() start_idx = 0 while (ptr + start_idx * 4) % alignment != (offset_bytes % alignment): start_idx += 1 t = base[start_idx : start_idx + size] return t print(f"Testing n_elements={n_elements}") for align_name, align_mod in [("16-byte aligned", 0), ("8-byte aligned (not 16)", 8)]: target_mod = 0 if align_mod == 0 else 8 # Try to align e to target_mod modulo 16 e = get_aligned_tensor(n_elements, 16, target_mod) e.uniform_(-1, 1) dh = torch.randn(n_elements, device=device, dtype=dtype) g = torch.randn(n_elements, device=device, dtype=dtype) print(f"\n--- Testing {align_name} ---") print(f"e_ptr: {e.data_ptr()} (mod 16: {e.data_ptr() % 16})") # Inputs for ref (clones) dh_ref = dh.clone() e_ref = e.clone() g_ref = g.clone() # Run Ref h_ref, de_ref, dg_ref = ref_impl(dh_ref, e_ref, g_ref) # Test In-Place print(f"Testing In-Place...") dh_in = dh.clone() e_in = e.clone() g_in = g.clone() grid = lambda META: (triton.cdiv(n_elements, META["BLOCK_SIZE"]),) _silu_mul_backward_inplace_kernel[grid](dh_in, e_in, g_in, n_elements) diff_e = (e_in - de_ref).abs().max().item() import math if math.isnan(diff_e) or diff_e > 1e-3: print(f"FAILURE (In-Place) Diff: {diff_e}") else: print("SUCCESS (In-Place)") # Test Out-Of-Place print(f"Testing Out-Of-Place...") de_out = torch.empty_like(e) dg_out = torch.empty_like(g) _silu_mul_backward_kernel[grid](dh, e, g, de_out, dg_out, n_elements) diff_e_out = (de_out - de_ref).abs().max().item() if math.isnan(diff_e_out) or diff_e_out > 1e-3: print(f"FAILURE (Out-Of-Place) Diff: {diff_e_out}") else: print("SUCCESS (Out-Of-Place)") Assistant
I will read
qwen3_moe_fused/kernels/silu_mul.pyto examine the implementation of_silu_mul_backward_inplace_kerneland identify the cause of theNaNvalues.Assistant
I will run the
test_silu_mul.pyscript to confirm theNaNissue and see the output.Assistant
I'll modify
test_silu_mul.pyto print the indices of theNaNvalues to pinpoint where the issue occurs. This might reveal if it's related to boundary conditions or specific blocks.Assistant
I'll run the
test_silu_mul.pyscript again to see the indices of theNaNvalues.Assistant
I'll modify
test_silu_mul.pyto print the data pointers ofdh,e, andgto verify they don't overlap.Assistant
I'll run the
test_silu_mul.pyscript again to check the data pointers.Assistant
I'll comment out
tl.store(dh_ptr ...)inqwen3_moe_fused/kernels/silu_mul.pyto check if it resolves theNaNissue, which would indicate interference.Assistant
I'll run the
test_silu_mul.pyscript again to see if the changes resolved the issue.Assistant
I'll revert the changes to
qwen3_moe_fused/kernels/silu_mul.pyand then modify it to remove autotuning and forceBLOCK_SIZE=128to investigate if a specific configuration is causing the issue.Assistant
I'll remove the autotuning decoration from
_silu_mul_backward_inplace_kernelinqwen3_moe_fused/kernels/silu_mul.pyto test with the default configuration.- User
To locate the problem, can you first find a minimal kernel that triggers this nan behavior?
Assistant
I will run the
test_silu_mul.pyscript to see if removing the autotuning resolved theNaNissue.Assistant
I will create a reproduction script
repro_nan.pyto identify which specific Triton configuration is causing theNaNissue. This script will iterate through the autotune configurations and run the test for each one.Assistant
I will run
repro_nan.pyto identify the failing configuration.Assistant
I will revert
qwen3_moe_fused/kernels/silu_mul.pyto its original state, including the@triton.autotunedecorator.Assistant
I'll run
test_silu_mul.pyto confirm the failure.Assistant
I'll modify
test_silu_mul.pyto print the best configuration found by the autotuner.Assistant
I'll run
test_silu_mul.pyto identify the failing configuration.Assistant
I will update
repro_nan.pyto simulate the autotune process by running the kernel multiple times on the same data. This will help confirm if the repeated execution on mutated data is causing theNaNvalues.Assistant
I'll run
repro_nan.pyto confirm if the simulated autotune loop causesNaN.Assistant
I will update
repro_nan.pyto increase the number of iterations per configuration to 100, simulating the warmup and benchmarking phases of the autotuner. This should increase the likelihood of reproducing theNaNissue.Assistant
I'll run the updated
repro_nan.pyto see if the increased iterations trigger theNaNissue.Assistant
I will modify
repro_nan.pyto use 8-byte alignment (modulo 16) and check if theNaNissue persists. This will help explain why the original test passed for the 8-byte aligned case.Assistant
I'll run the updated
repro_nan.pywith 8-byte alignment.- User
So the issue happens when we autotune an inplace kernel. How to properly autotune an inplace kernel in Triton? Is there some setting in triton.autotune for this?
Assistant
Yes, you are exactly right. The issue is that
triton.autotuneruns the kernel multiple times (for warmup and benchmarking) to find the best configuration. Since your kernel is in-place, the first run modifies the input tensors (dh,e,g). The second run then uses these modified values (outputs from the previous run) as inputs, leading to numerical instability and eventuallyNaNs.To fix this, we need to tell Triton to restore the original values of the modified tensors between autotuning runs. This is done using the
restore_valueargument in the@triton.autotunedecorator.I will modify
qwen3_moe_fused/kernels/silu_mul.pyto addrestore_value=["dh_ptr", "e_ptr", "g_ptr"].Assistant
I will now run
test_silu_mul.pyagain to verify that the fix works and theNaNs are gone.Assistant
The fix is verified. I'll remove the temporary files
test_silu_mul.py(which I modified to add debugging prints) andrepro_nan.pybefore finishing. I will first reverttest_silu_mul.pyto its original state (clean up my print statements).Assistant
I'll revert
test_silu_mul.pyto its original state. I'll simply write the original content back to the file.Assistant
I will now delete the
repro_nan.pyfile.Assistant
I have successfully identified the cause of the
NaNvalues and fixed it by using therestore_valueparameter intriton.autotune. The issue was that the autotuner was modifying the input tensors in-place across multiple runs, causing numerical instability. By restoring the input tensors after each run, the autotuning process now works correctly. I have also cleaned up the reproduction scripts.The
NaNissue is resolved.