Write a script to benchmark the fused softmax-topk kernel and its naive impl. See…
- User
Write a script to benchmark the fused softmax-topk kernel and its naive impl. See @test_softmax_topk.py for how to call the kernel, and @benchmark_silu_mul.py for an example of benchmarking. --- Content from referenced files --- Content from @benchmark_silu_mul.py: #!/usr/bin/env python3
import gc import os
import torch import triton from liger_kernel.ops.swiglu import LigerSiLUMulFunction
from qwen3_moe_fused.kernels.silu_mul import silu_mul as silu_mul_triton
os.environ["TRITON_PRINT_AUTOTUNING"] = "1"
def silu_mul(x, y): return torch.nn.functional.silu(x) * y
silu_mul_compiled = torch.compile(silu_mul, fullgraph=True, mode="max-autotune")
def silu_mul_liger(x, y): return LigerSiLUMulFunction.apply(x, y)
providers = { "torch": silu_mul, "compile": silu_mul_compiled, "liger": silu_mul_liger, "triton": silu_mul_triton, } provider_names = list(providers)
@triton.testing.perf_report( [ triton.testing.Benchmark( x_names=["N"], x_vals=range(1024, 16384 + 1, 1024), line_arg="provider", line_vals=provider_names, line_names=provider_names, ylabel="GB/s", plot_name="silu_mul", args={}, ) ] ) def benchmark(N, provider): print("N", N, "provider", provider, "begin") gc.collect() torch.cuda.empty_cache()
codedevice = "cuda" dtype = torch.bfloat16 x = torch.randn(N, N, device=device, dtype=dtype) y = torch.randn(N, N, device=device, dtype=dtype) numel = x.numel() elsize = x.element_size() quantiles = [0.5, 0.2, 0.8] ms, min_ms, max_ms = triton.testing.do_bench(lambda: providers[provider](x, y), quantiles=quantiles) gbps = lambda ms: 2 * numel * elsize / ms * 1e-6 print("N", N, "provider", provider, "end", gbps(ms)) return gbps(ms), gbps(max_ms), gbps(min_ms)if name == "main": with torch.inference_mode(): benchmark.run(print_data=True, save_path="./") Content from @test_softmax_topk.py: #!/usr/bin/env python3
import os
import torch
from qwen3_moe_fused.kernels.softmax_topk import softmax_topk_forward, softmax_topk_naive
os.environ["TRITON_PRINT_AUTOTUNING"] = "1"
def main(): device = "cuda"
code Assistant
I will read
qwen3_moe_fused/kernels/softmax_topk.pyto confirm the return types of the functions for accurate GB/s calculation.Assistant
I will write the
benchmark_softmax_topk.pyscript.Assistant
I will run the
benchmark_softmax_topk.pyscript to verify it works correctly and catch any runtime errors.- User
I've updated @benchmark_softmax_topk.py . Read C:\Python312\Lib\site-packages\liger_kernel to see if there is a fused softmax-topk kernel in Liger Kernels. If so, add it to the benchmark. --- Content from referenced files --- Content from @benchmark_softmax_topk.py: #!/usr/bin/env python3
import gc import os
import torch import triton
from qwen3_moe_fused.kernels.softmax_topk import softmax_topk_forward, softmax_topk_naive
os.environ["TRITON_PRINT_AUTOTUNING"] = "1"
def softmax_topk_compile(logits, k, norm): return softmax_topk_naive(logits, k, norm)
softmax_topk_compiled = torch.compile(softmax_topk_naive, fullgraph=True, mode="max-autotune")
Assistant
I will start by listing the contents of the
liger_kerneldirectory to check for any relevant kernels.Assistant
I will list the contents of the
liger_kernel/opsdirectory to find any softmax or topk operations.Assistant
I will read the
softmax.pyfile to verify if it includes topk functionality.Assistant
I will list the contents of the
liger_kernel/ops/experimentaldirectory to ensure I haven't missed any relevant kernels.Assistant
I will read
liger_kernel/__init__.pyto confirm that no fused softmax-topk kernel is exposed there.Assistant
I will read
liger_kernel/ops/__init__.pyto check for any exposed kernels that I might have missed.Assistant
I will read
liger_kernel/transformers/__init__.pyto check for any model-specific fused kernels.Assistant
I will read
monkey_patch.pyto check ifapply_liger_kernel_to_qwen3_moereplaces a softmax-topk router with a custom kernel.Assistant
I will run the
benchmark_softmax_topk.pyscript to generate baseline performance numbers, which I will report to the user along with the finding thatliger_kerneldoes not include a fused softmax-topk kernel.- User
Assistant
I will create a reproduction script
reproduce_backward.pyto verify the mathematical derivation of the backward pass for the softmax top-k operation, comparing a manual implementation against PyTorch's autograd.Assistant
I will run the
reproduce_backward.pyscript to confirm that my manual backward pass implementation matches the autograd results.Assistant
I will update
reproduce_backward.pyto implement the corrected logic fornorm=True, where gradients are calculated only for the top-k elements using the standard softmax gradient formula, and gradients for non-top-k elements are set to zero.Assistant
I will run the updated
reproduce_backward.pyto confirm that the corrected logic fornorm=True(treating it as local softmax on top-k logits) matches the autograd results.Assistant
I will update
qwen3_moe_fused/kernels/softmax_topk.pyto include the_softmax_topk_backward_kerneland theSoftmaxTopKautograd function, ensuring correct handling ofnorm_topk_proband efficient memory usage.Assistant
I will run
test_softmax_topk.pyto verify the backward pass. But first I need to updatetest_softmax_topk.pyto actually test the backward pass, as it currently only tests the forward pass. I will add a backward test case totest_softmax_topk.pythat compares the kernel gradient with the naive implementation gradient.Assistant
I will run the updated
test_softmax_topk.pyto verify that both the forward and backward passes of the kernel are correct.Assistant
I will update
qwen3_moe_fused/kernels/softmax_topk.pyto add the@triton.autotunedecorator to the_softmax_topk_backward_kernel, reusing the configuration from the forward kernel to ensureBLOCK_SIZE_Mis properly handled.Assistant
I will run
test_softmax_topk.pyagain to verify the fix.Assistant
I will remove the
reproduce_backward.pyscript as it is no longer needed.