[Refactor] Clean up JIT kernel utilites (#16884)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
This commit is contained in:
DarkSharpness
2026-01-13 17:54:16 +08:00
committed by GitHub
parent 740d3c0b39
commit ba9f6d8f26
30 changed files with 928 additions and 513 deletions

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
import torch
@@ -27,6 +27,17 @@ def _jit_qknorm_module(head_dim: int, dtype: torch.dtype) -> Module:
)
@cache_once
def _jit_rmsnorm_module(hidden_size: int, dtype: torch.dtype) -> Module:
args = make_cpp_args(hidden_size, is_arch_support_pdl(), dtype)
return load_jit(
"rmsnorm",
*args,
cuda_files=["elementwise/rmsnorm.cuh"],
cuda_wrappers=[("rmsnorm", f"RMSNormKernel<{args}>::run")],
)
@cache_once
def can_use_fused_inplace_qknorm(head_dim: int, dtype: torch.dtype) -> bool:
logger = logging.getLogger(__name__)
@@ -53,3 +64,15 @@ def fused_inplace_qknorm(
head_dim = head_dim or q.size(-1)
module = _jit_qknorm_module(head_dim, q.dtype)
module.qknorm(q, k, q_weight, k_weight, eps)
def rmsnorm(
input: torch.Tensor,
weight: torch.Tensor,
output: Optional[torch.Tensor] = None,
eps: float = 1e-6,
) -> None:
output = output if output is not None else input
hidden_size = input.size(-1)
module = _jit_rmsnorm_module(hidden_size, input.dtype)
module.rmsnorm(input, weight, output, eps)