Feature Extraction
Transformers
Safetensors
spera
eeg
biosignal
time-series
foundation-model
self-supervised-learning
representation-learning
transformer
jepa
neuroscience
brain-computer-interface
neurips-2026
custom_code
Eval Results (legacy)
Instructions to use sees4321/SPERA_EEG with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use sees4321/SPERA_EEG with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="sees4321/SPERA_EEG", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("sees4321/SPERA_EEG", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 10,304 Bytes
99c5ca3 | 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | from __future__ import annotations
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Tuple
from .config import EEGModelConfig
def build_rope_cache(
max_pos: int,
rotary_dim: int,
theta: float,
device: torch.device,
dtype: torch.dtype,
) -> Tuple[torch.Tensor, torch.Tensor]:
assert rotary_dim % 2 == 0
half = rotary_dim // 2
inv_freq = 1.0 / (theta ** (torch.arange(0, half, device=device, dtype=torch.float32) / half))
t = torch.arange(max_pos, device=device, dtype=torch.float32)
freqs = torch.einsum("i,j->ij", t, inv_freq)
return torch.cos(freqs).to(dtype), torch.sin(freqs).to(dtype)
def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
assert x.shape[-1] % 2 == 0
if cos.dim() == 2:
cos = cos[None, None, :, :]
sin = sin[None, None, :, :]
elif cos.dim() == 3:
cos = cos[:, None, :, :]
sin = sin[:, None, :, :]
x1 = x[..., 0::2]
x2 = x[..., 1::2]
out = torch.empty_like(x)
out[..., 0::2] = x1 * cos - x2 * sin
out[..., 1::2] = x1 * sin + x2 * cos
return out
class TimePatchEmbed(nn.Module):
def __init__(self, cfg: EEGModelConfig):
super().__init__()
self.patch_samples = int(round(cfg.sample_rate * cfg.patch_seconds))
self.proj = nn.Linear(self.patch_samples, cfg.d_model)
def forward_packed(self, patches: torch.Tensor) -> torch.Tensor:
return self.proj(patches)
class SwiGLU(nn.Module):
def __init__(self, d_model: int, mlp_ratio: float, dropout: float):
super().__init__()
hidden = int(d_model * mlp_ratio * (2.0 / 3.0))
self.fc = nn.Linear(d_model, 2 * hidden)
self.proj = nn.Linear(hidden, d_model)
self.drop = nn.Dropout(dropout)
def forward(self, x: torch.Tensor) -> torch.Tensor:
a, b = self.fc(x).chunk(2, dim=-1)
x = F.silu(a) * b
x = self.drop(x)
x = self.proj(x)
x = self.drop(x)
return x
class MultiheadSelfAttentionRoPE(nn.Module):
def __init__(
self,
d_model: int,
n_heads: int,
attn_dropout: float,
rope_theta: float,
rotary_pct: float,
spatial_qk_dim: int = 0,
spatial_qk_scale: float = 1.0,
use_spatial_qk: bool = False,
max_seq_len: int = 4096,
):
super().__init__()
assert d_model % n_heads == 0
self.d_model = d_model
self.n_heads = n_heads
self.head_dim = d_model // n_heads
self.attn_dropout = float(attn_dropout)
self.spatial_qk_scale = float(spatial_qk_scale)
rotary_dim = int(self.head_dim * rotary_pct)
rotary_dim = rotary_dim - (rotary_dim % 2)
self.rotary_dim = max(0, rotary_dim)
if self.rotary_dim > 0:
cos, sin = build_rope_cache(
max_pos=max_seq_len,
rotary_dim=self.rotary_dim,
theta=float(rope_theta),
device=torch.device("cpu"),
dtype=torch.float32,
)
self.register_buffer("_rope_cos", cos, persistent=False)
self.register_buffer("_rope_sin", sin, persistent=False)
self.qkv = nn.Linear(d_model, 3 * d_model, bias=True)
self.out = nn.Linear(d_model, d_model, bias=True)
if use_spatial_qk:
self.spatial_q_proj = nn.Linear(spatial_qk_dim, d_model, bias=False)
self.spatial_k_proj = nn.Linear(spatial_qk_dim, d_model, bias=False)
else:
self.spatial_q_proj = None
self.spatial_k_proj = None
def _get_rope(self, rope_pos: torch.Tensor, dtype: torch.dtype) -> Tuple[torch.Tensor, torch.Tensor]:
cos = self._rope_cos.to(dtype=dtype)
sin = self._rope_sin.to(dtype=dtype)
if rope_pos.dim() == 1:
return cos[rope_pos], sin[rope_pos]
if rope_pos.dim() == 2:
return cos[rope_pos], sin[rope_pos]
raise ValueError(f"rope_pos must be 1D or 2D, got {rope_pos.shape}")
def forward(
self,
x: torch.Tensor,
padding_mask: Optional[torch.Tensor],
rope_pos: Optional[torch.Tensor] = None,
attn_bias: Optional[torch.Tensor] = None,
spatial_q_add: Optional[torch.Tensor] = None,
spatial_k_add: Optional[torch.Tensor] = None,
) -> torch.Tensor:
B, N, D = x.shape
qkv = self.qkv(x)
q, k, v = qkv.chunk(3, dim=-1)
q = q.view(B, N, self.n_heads, self.head_dim).transpose(1, 2)
k = k.view(B, N, self.n_heads, self.head_dim).transpose(1, 2)
v = v.view(B, N, self.n_heads, self.head_dim).transpose(1, 2)
if self.rotary_dim > 0:
# RoPE is applied along the temporal patch index.
# Channel geometry is handled separately by coordinate embeddings and Legendre priors.
if rope_pos is None:
raise ValueError("rope_pos must be provided when rotary_dim > 0")
cos, sin = self._get_rope(rope_pos, dtype=q.dtype)
if self.rotary_dim == self.head_dim:
q = apply_rope(q, cos, sin)
k = apply_rope(k, cos, sin)
else:
q_rot, q_pass = q[..., : self.rotary_dim], q[..., self.rotary_dim :]
k_rot, k_pass = k[..., : self.rotary_dim], k[..., self.rotary_dim :]
q = torch.cat([apply_rope(q_rot, cos, sin), q_pass], dim=-1)
k = torch.cat([apply_rope(k_rot, cos, sin), k_pass], dim=-1)
if (spatial_q_add is not None) or (spatial_k_add is not None):
# Legendre anchor geometry features enter Q/K after RoPE (Eq. 4).
# The resulting scores include content–geometry cross terms (F.3).
if spatial_q_add is None or spatial_k_add is None:
raise ValueError("spatial_q_add and spatial_k_add must be provided together")
q_sp = spatial_q_add.view(B, N, self.n_heads, self.head_dim).transpose(1, 2)
k_sp = spatial_k_add.view(B, N, self.n_heads, self.head_dim).transpose(1, 2)
q = q + self.spatial_qk_scale * q_sp
k = k + self.spatial_qk_scale * k_sp
attn_mask = None
if attn_bias is None:
if padding_mask is not None:
attn_mask = (~padding_mask)[:, None, None, :]
else:
# Optional pairwise spatial bias, used mainly in factorized spatial attention where attention is only C x C per time step.
attn_mask = attn_bias[:, None, :, :] if attn_bias.dim() == 3 else attn_bias
if attn_mask.dtype != q.dtype:
attn_mask = attn_mask.to(dtype=q.dtype)
if padding_mask is not None:
attn_mask = attn_mask.masked_fill(padding_mask[:, None, None, :], float("-inf"))
out = F.scaled_dot_product_attention(
q, k, v,
attn_mask=attn_mask,
dropout_p=self.attn_dropout if self.training else 0.0,
is_causal=False,
)
out = out.transpose(1, 2).contiguous().view(B, N, D)
return self.out(out)
class CrossAttentionRoPE(nn.Module):
def __init__(self, d_model: int, n_heads: int, attn_dropout: float, rope_theta: float, rotary_pct: float, max_seq_len: int = 4096):
super().__init__()
assert d_model % n_heads == 0
self.d_model = d_model
self.n_heads = n_heads
self.head_dim = d_model // n_heads
self.attn_dropout = float(attn_dropout)
rotary_dim = int(self.head_dim * rotary_pct)
rotary_dim = rotary_dim - (rotary_dim % 2)
self.rotary_dim = max(0, rotary_dim)
if self.rotary_dim > 0:
cos, sin = build_rope_cache(
max_pos=max_seq_len,
rotary_dim=self.rotary_dim,
theta=float(rope_theta),
device=torch.device("cpu"),
dtype=torch.float32,
)
self.register_buffer("_rope_cos", cos, persistent=False)
self.register_buffer("_rope_sin", sin, persistent=False)
self.q = nn.Linear(d_model, d_model, bias=True)
self.kv = nn.Linear(d_model, 2 * d_model, bias=True)
self.out = nn.Linear(d_model, d_model, bias=True)
def _get_rope(self, rope_pos: torch.Tensor, dtype: torch.dtype):
cos = self._rope_cos.to(dtype=dtype)
sin = self._rope_sin.to(dtype=dtype)
if rope_pos.dim() == 1:
return cos[rope_pos], sin[rope_pos]
if rope_pos.dim() == 2:
return cos[rope_pos], sin[rope_pos]
raise ValueError(f"rope_pos must be 1D or 2D, got {rope_pos.shape}")
def forward(
self,
q_in: torch.Tensor,
kv_in: torch.Tensor,
kv_padding_mask: Optional[torch.Tensor],
rope_pos_q: torch.Tensor,
rope_pos_k: torch.Tensor,
) -> torch.Tensor:
B, Lq, D = q_in.shape
_, Lk, _ = kv_in.shape
q = self.q(q_in).view(B, Lq, self.n_heads, self.head_dim).transpose(1, 2)
kv = self.kv(kv_in)
k, v = kv.chunk(2, dim=-1)
k = k.view(B, Lk, self.n_heads, self.head_dim).transpose(1, 2)
v = v.view(B, Lk, self.n_heads, self.head_dim).transpose(1, 2)
if self.rotary_dim > 0:
cos_q, sin_q = self._get_rope(rope_pos_q, dtype=q.dtype)
cos_k, sin_k = self._get_rope(rope_pos_k, dtype=q.dtype)
if self.rotary_dim == self.head_dim:
q = apply_rope(q, cos_q, sin_q)
k = apply_rope(k, cos_k, sin_k)
else:
q = torch.cat([apply_rope(q[..., : self.rotary_dim], cos_q, sin_q), q[..., self.rotary_dim :]], dim=-1)
k = torch.cat([apply_rope(k[..., : self.rotary_dim], cos_k, sin_k), k[..., self.rotary_dim :]], dim=-1)
attn_mask = None
if kv_padding_mask is not None:
attn_mask = (~kv_padding_mask)[:, None, None, :]
out = F.scaled_dot_product_attention(
q, k, v,
attn_mask=attn_mask,
dropout_p=self.attn_dropout if self.training else 0.0,
is_causal=False,
)
out = out.transpose(1, 2).contiguous().view(B, Lq, D)
return self.out(out)
|