-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathkernels.py
More file actions
134 lines (105 loc) · 3.48 KB
/
Copy pathkernels.py
File metadata and controls
134 lines (105 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Attention / RMS / RoPE / CE helpers for the dense 1B reference.
submission_nonce: dense-1b-b200-20260819T1952Z
Hot-path train (seq=512) is one Flash/SDPA call — no Python token loop.
"""
from __future__ import annotations
import os
import torch
import torch.nn.functional as F
ATTN_KERNEL = "sdpa"
RMS_KERNEL = "torch"
CE_KERNEL = "torch"
SWIGLU_KERNEL = "eager"
ROPE_KERNEL = "torch"
def kernel_map():
return {
"attn_kernel": ATTN_KERNEL,
"rmsnorm_kernel": RMS_KERNEL,
"ce_kernel": CE_KERNEL,
"swiglu_kernel": SWIGLU_KERNEL,
"rope_kernel": ROPE_KERNEL,
}
def enable_attn_backends():
global ATTN_KERNEL
if not torch.cuda.is_available():
ATTN_KERNEL = "math"
return
try:
torch.backends.cuda.enable_flash_sdp(True)
torch.backends.cuda.enable_mem_efficient_sdp(True)
torch.backends.cuda.enable_math_sdp(True)
except Exception: # noqa: BLE001
pass
attn = os.environ.get("DENSE1B_ATTN_KERNEL", "").strip().lower()
if attn:
ATTN_KERNEL = attn
return
try:
import flash_attn # noqa: F401
ATTN_KERNEL = "fa2"
return
except Exception: # noqa: BLE001
pass
try:
import transformer_engine.pytorch as te # noqa: F401
if hasattr(te, "DotProductAttention"):
ATTN_KERNEL = "te_avail"
except Exception: # noqa: BLE001
pass
ATTN_KERNEL = "sdpa"
def sdpa(q, k, v, *, is_causal=False, attn_mask=None):
"""q/k/v: (b, h, t, d). Uses the fastest enabled SDPA backend."""
return F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask, is_causal=is_causal)
def rms_norm(x, weight, eps=1e-6):
global RMS_KERNEL
RMS_KERNEL = "torch"
return F.rms_norm(x, (x.shape[-1],), weight=weight, eps=eps)
def apply_rope(x, cos, sin):
global ROPE_KERNEL
ROPE_KERNEL = "torch"
half = x.shape[-1] // 2
x1, x2 = x[..., :half], x[..., half:]
c = cos.unsqueeze(0).unsqueeze(0)
s = sin.unsqueeze(0).unsqueeze(0)
return torch.cat([x1 * c - x2 * s, x1 * s + x2 * c], dim=-1)
def rope_tables(t, head_dim, theta, device, dtype):
inv_freq = 1.0 / (
theta ** (torch.arange(0, head_dim, 2, device=device, dtype=torch.float32) / head_dim)
)
pos = torch.arange(t, device=device, dtype=torch.float32)
freqs = torch.outer(pos, inv_freq)
return freqs.cos().to(dtype), freqs.sin().to(dtype)
_CE_FN = None
_CE_PROBED = False
def _probe_ce():
global _CE_FN, _CE_PROBED, CE_KERNEL
if _CE_PROBED:
return _CE_FN
_CE_PROBED = True
try:
from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss # type: ignore
_CE_FN = LigerCrossEntropyLoss(reduction="mean")
CE_KERNEL = "liger"
except Exception: # noqa: BLE001
_CE_FN = None
CE_KERNEL = "torch"
return _CE_FN
def cross_entropy(logits, labels):
"""logits (N, V) float, labels (N,)."""
fn = _probe_ce()
if fn is not None:
try:
return fn(logits, labels)
except Exception as exc: # noqa: BLE001
print(f"[dense1b] liger CE failed ({exc}); torch", flush=True)
global CE_KERNEL
CE_KERNEL = "torch"
return F.cross_entropy(logits, labels)
def log_kernel_banner():
enable_attn_backends()
_probe_ce()
print(
f"[dense1b] kernel_map attn={ATTN_KERNEL} rmsnorm={RMS_KERNEL} "
f"ce={CE_KERNEL} swiglu={SWIGLU_KERNEL} rope={ROPE_KERNEL}",
flush=True,
)