FirePFN 1.0.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of FirePFN might be problematic. Click here for more details.
- firepfn/__init__.py +7 -0
- firepfn/class_tree.py +77 -0
- firepfn/classifier.py +301 -0
- firepfn/encoding.py +347 -0
- firepfn/ensemble.py +44 -0
- firepfn/eval/__init__.py +14 -0
- firepfn/eval/analysis.py +294 -0
- firepfn/eval/plots.py +330 -0
- firepfn/eval/runtime.py +205 -0
- firepfn/eval/tabarena.py +388 -0
- firepfn/loading.py +97 -0
- firepfn/many_class.py +157 -0
- firepfn/model/__init__.py +5 -0
- firepfn/model/_mamba_block.py +49 -0
- firepfn/model/backbone.py +110 -0
- firepfn/model/cross_attention.py +123 -0
- firepfn/model/deltanet.py +134 -0
- firepfn/model/fire_pfn.py +396 -0
- firepfn/model/fla_autotune.py +73 -0
- firepfn/model/hydra2.py +537 -0
- firepfn/model/int32_safe.py +122 -0
- firepfn/model/kumo.py +704 -0
- firepfn/model/linear_attention.py +167 -0
- firepfn/model/stream_infer.py +568 -0
- firepfn/model/triton_rmsnorm.py +119 -0
- firepfn/model/triton_rope.py +152 -0
- firepfn/preprocessing.py +301 -0
- firepfn/train/__init__.py +3 -0
- firepfn/train/__main__.py +57 -0
- firepfn/train/_tabicl_dataset.py +1762 -0
- firepfn/train/configs.py +113 -0
- firepfn/train/loop.py +224 -0
- firepfn/train/optim.py +109 -0
- firepfn/train/prior.py +74 -0
- firepfn/train/schedule.py +125 -0
- firepfn-1.0.0.dev0.dist-info/METADATA +270 -0
- firepfn-1.0.0.dev0.dist-info/RECORD +46 -0
- firepfn-1.0.0.dev0.dist-info/WHEEL +5 -0
- firepfn-1.0.0.dev0.dist-info/licenses/AUTHORS +14 -0
- firepfn-1.0.0.dev0.dist-info/licenses/LICENSE +33 -0
- firepfn-1.0.0.dev0.dist-info/licenses/LICENSES/Apache-2.0.txt +176 -0
- firepfn-1.0.0.dev0.dist-info/licenses/LICENSES/BSD-3-Clause-TabICL.txt +236 -0
- firepfn-1.0.0.dev0.dist-info/licenses/LICENSES/MIT-Hydra.txt +21 -0
- firepfn-1.0.0.dev0.dist-info/licenses/LICENSES/OpenMDW-1.1-Kumo-Tabular-weights.txt +52 -0
- firepfn-1.0.0.dev0.dist-info/licenses/NOTICE +39 -0
- firepfn-1.0.0.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
# Copyright (c) 2025-2026, the FirePFN authors.
|
|
3
|
+
"""Non-causal gated linear attention over the row axis (backbone mixer "attn")."""
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn as nn
|
|
6
|
+
import torch.nn.functional as F
|
|
7
|
+
|
|
8
|
+
from firepfn.model.stream_infer import infer_segment_rows, use_segmented_inference
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _RMSNorm(nn.Module):
|
|
12
|
+
"""Plain RMSNorm over the last axis (fp32 math, output in the input dtype)."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, dim, eps=1e-5, device=None, dtype=None):
|
|
15
|
+
super().__init__()
|
|
16
|
+
self.eps = eps
|
|
17
|
+
self.weight = nn.Parameter(torch.ones(dim, device=device, dtype=dtype))
|
|
18
|
+
|
|
19
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
20
|
+
norm = x.float().pow(2).mean(dim=-1, keepdim=True).add(self.eps).rsqrt()
|
|
21
|
+
return (x.float() * norm).to(x.dtype) * self.weight
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RowLinearAttentionBranch(nn.Module):
|
|
25
|
+
"""ELU+1 kernel linear attention over the (exchangeable) rows, with
|
|
26
|
+
|
|
27
|
+
* a sigmoid gate on V (g_proj) and a content-dependent decay gate on K (decay_proj): each
|
|
28
|
+
row's contribution to the shared context matrix S = K^T V is down-weighted by a learned
|
|
29
|
+
factor in (0, 1);
|
|
30
|
+
* stabilize_norm: Q and K are L2-normalised along head_dim after the feature map, so only
|
|
31
|
+
their direction (not q_proj/k_proj's weight norm) enters S and the normaliser Z;
|
|
32
|
+
* a floor on Z = Q . sum(K): eps_z_rel * median(Z) of the call (or the fixed eps_z);
|
|
33
|
+
* a per-head RMSNorm on the output before o_proj.
|
|
34
|
+
|
|
35
|
+
No prenorm / residual inside: HydraBlock supplies both.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, d_model, n_heads=8, stabilize_norm=False, eps_z=1e-6, eps_z_rel=None,
|
|
39
|
+
device=None, dtype=None):
|
|
40
|
+
factory_kwargs = {"device": device, "dtype": dtype}
|
|
41
|
+
super().__init__()
|
|
42
|
+
assert d_model % n_heads == 0, "d_model must be divisible by n_heads"
|
|
43
|
+
self.n_heads = n_heads
|
|
44
|
+
self.head_dim = d_model // n_heads
|
|
45
|
+
self.stabilize_norm = stabilize_norm
|
|
46
|
+
self.eps_z = eps_z
|
|
47
|
+
self.eps_z_rel = eps_z_rel
|
|
48
|
+
|
|
49
|
+
self.q_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
50
|
+
self.k_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
51
|
+
self.v_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
52
|
+
self.g_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
53
|
+
self.o_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
54
|
+
# Optimizer groups (training): q/k get the linattn_qk_wd weight decay (their norm acts as
|
|
55
|
+
# an inverse temperature of the ELU+1 feature map), v the extra cross-attention decay.
|
|
56
|
+
self.q_proj.weight._qk_weight_decay = True
|
|
57
|
+
self.k_proj.weight._qk_weight_decay = True
|
|
58
|
+
self.v_proj.weight._extra_weight_decay = True
|
|
59
|
+
self.decay_proj = nn.Linear(d_model, d_model, bias=False, **factory_kwargs)
|
|
60
|
+
self.out_norm = _RMSNorm(self.head_dim, eps=1e-5, **factory_kwargs)
|
|
61
|
+
# Norm of S from the last training forward (with gradient), for the s_norm penalty loss.
|
|
62
|
+
self._last_s_norm = None
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def _phi(x: torch.Tensor) -> torch.Tensor:
|
|
66
|
+
return F.elu(x) + 1.0
|
|
67
|
+
|
|
68
|
+
def _qkvg(self, x, need_q=True, need_kv=True):
|
|
69
|
+
"""Per-row feature maps of forward() for rows x: (B, n, D) -> tensors (B, H, n, Hd)."""
|
|
70
|
+
B, n, _ = x.shape
|
|
71
|
+
H, Hd = self.n_heads, self.head_dim
|
|
72
|
+
Q = K = V = None
|
|
73
|
+
if need_q:
|
|
74
|
+
Q = self._phi(self.q_proj(x).view(B, n, H, Hd).transpose(1, 2))
|
|
75
|
+
if self.stabilize_norm:
|
|
76
|
+
Q = Q / Q.norm(dim=-1, keepdim=True).clamp(min=1e-6)
|
|
77
|
+
if need_kv:
|
|
78
|
+
K = self._phi(self.k_proj(x).view(B, n, H, Hd).transpose(1, 2))
|
|
79
|
+
V = self.v_proj(x).view(B, n, H, Hd).transpose(1, 2)
|
|
80
|
+
if self.stabilize_norm:
|
|
81
|
+
K = K / K.norm(dim=-1, keepdim=True).clamp(min=1e-6)
|
|
82
|
+
V = torch.sigmoid(self.g_proj(x).view(B, n, H, Hd).transpose(1, 2)) * V
|
|
83
|
+
K = K * torch.sigmoid(self.decay_proj(x).view(B, n, H, Hd).transpose(1, 2))
|
|
84
|
+
return Q, K, V
|
|
85
|
+
|
|
86
|
+
@torch.no_grad()
|
|
87
|
+
def _forward_segmented(self, x, seg):
|
|
88
|
+
"""forward(x) without gradients, over row chunks of `seg` rows.
|
|
89
|
+
|
|
90
|
+
The only cross-row quantities are S = K^T V, K_sum and (with eps_z_rel) the median of
|
|
91
|
+
Z_raw over all rows, so: pass 1 accumulates S and K_sum (fp32, cast to the dtype the
|
|
92
|
+
unsegmented matmul/sum would return); pass 2 computes Z_raw for the floor's median; pass 3
|
|
93
|
+
produces the outputs chunk by chunk. Same result as forward() up to float rounding.
|
|
94
|
+
"""
|
|
95
|
+
B, L, D = x.shape
|
|
96
|
+
S = K_sum = None
|
|
97
|
+
dt = None
|
|
98
|
+
for s in range(0, L, seg):
|
|
99
|
+
_, K, V = self._qkvg(x[:, s:s + seg], need_q=False)
|
|
100
|
+
dS = torch.matmul(K.float().transpose(-2, -1), V.float())
|
|
101
|
+
dK = K.float().sum(dim=2, keepdim=True)
|
|
102
|
+
S = dS if S is None else S + dS
|
|
103
|
+
K_sum = dK if K_sum is None else K_sum + dK
|
|
104
|
+
dt = torch.matmul(K[..., :1, :].transpose(-2, -1), V[..., :1, :]).dtype
|
|
105
|
+
kdt = K.dtype
|
|
106
|
+
del K, V, dS, dK
|
|
107
|
+
S, K_sum = S.to(dt), K_sum.to(kdt)
|
|
108
|
+
if self.eps_z_rel is not None:
|
|
109
|
+
z_all = torch.cat([(self._qkvg(x[:, s:s + seg], need_kv=False)[0] * K_sum).sum(-1, keepdim=True)
|
|
110
|
+
for s in range(0, L, seg)], dim=2)
|
|
111
|
+
if B > 1: # one floor per table (see forward)
|
|
112
|
+
z_floor = self.eps_z_rel * z_all.float().flatten(1).median(dim=1).values.view(B, 1, 1, 1)
|
|
113
|
+
else:
|
|
114
|
+
z_floor = self.eps_z_rel * z_all.float().median()
|
|
115
|
+
del z_all
|
|
116
|
+
else:
|
|
117
|
+
z_floor = self.eps_z
|
|
118
|
+
out = None
|
|
119
|
+
for s in range(0, L, seg):
|
|
120
|
+
Q = self._qkvg(x[:, s:s + seg], need_kv=False)[0]
|
|
121
|
+
Z = (Q * K_sum).sum(dim=-1, keepdim=True).clamp(min=z_floor)
|
|
122
|
+
O = torch.matmul(Q, S) / Z
|
|
123
|
+
O = self.out_norm(O)
|
|
124
|
+
n = O.shape[2]
|
|
125
|
+
o = self.o_proj(O.transpose(1, 2).contiguous().view(B, n, D))
|
|
126
|
+
if out is None:
|
|
127
|
+
out = torch.empty(B, L, o.shape[-1], device=x.device, dtype=o.dtype)
|
|
128
|
+
out[:, s:s + n] = o
|
|
129
|
+
del Q, Z, O, o
|
|
130
|
+
return out
|
|
131
|
+
|
|
132
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
133
|
+
"""x: (B, L, D) -> (B, L, D)"""
|
|
134
|
+
B, L, D = x.shape
|
|
135
|
+
H, Hd = self.n_heads, self.head_dim
|
|
136
|
+
if use_segmented_inference(L):
|
|
137
|
+
return self._forward_segmented(x, infer_segment_rows())
|
|
138
|
+
|
|
139
|
+
Q = self._phi(self.q_proj(x).view(B, L, H, Hd).transpose(1, 2)) # (B, H, L, Hd)
|
|
140
|
+
K = self._phi(self.k_proj(x).view(B, L, H, Hd).transpose(1, 2))
|
|
141
|
+
V = self.v_proj(x).view(B, L, H, Hd).transpose(1, 2)
|
|
142
|
+
if self.stabilize_norm:
|
|
143
|
+
Q = Q / Q.norm(dim=-1, keepdim=True).clamp(min=1e-6)
|
|
144
|
+
K = K / K.norm(dim=-1, keepdim=True).clamp(min=1e-6)
|
|
145
|
+
V = torch.sigmoid(self.g_proj(x).view(B, L, H, Hd).transpose(1, 2)) * V
|
|
146
|
+
K = K * torch.sigmoid(self.decay_proj(x).view(B, L, H, Hd).transpose(1, 2))
|
|
147
|
+
|
|
148
|
+
S = torch.matmul(K.transpose(-2, -1), V) # (B, H, Hd, Hd)
|
|
149
|
+
O = torch.matmul(Q, S) # (B, H, L, Hd)
|
|
150
|
+
K_sum = K.sum(dim=2, keepdim=True) # (B, H, 1, Hd)
|
|
151
|
+
Z_raw = (Q * K_sum).sum(dim=-1, keepdim=True)
|
|
152
|
+
if self.eps_z_rel is not None:
|
|
153
|
+
# Median (detached) of this call's Z. Inference on several tables at once (batched
|
|
154
|
+
# ensemble members): one floor per table, so each equals running it alone.
|
|
155
|
+
if not self.training and B > 1:
|
|
156
|
+
z_floor = self.eps_z_rel * Z_raw.detach().float().flatten(1).median(dim=1).values.view(B, 1, 1, 1)
|
|
157
|
+
else:
|
|
158
|
+
z_floor = self.eps_z_rel * Z_raw.detach().float().median()
|
|
159
|
+
else:
|
|
160
|
+
z_floor = self.eps_z
|
|
161
|
+
O = O / Z_raw.clamp(min=z_floor)
|
|
162
|
+
if self.training:
|
|
163
|
+
self._last_s_norm = S.norm()
|
|
164
|
+
|
|
165
|
+
O = self.out_norm(O)
|
|
166
|
+
O = O.transpose(1, 2).contiguous().view(B, L, D)
|
|
167
|
+
return self.o_proj(O)
|