Update configuration_neollm.py
Browse files- configuration_neollm.py +132 -2
configuration_neollm.py
CHANGED
|
@@ -12,8 +12,9 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 12 |
|
| 13 |
Instantiates a NeoLLM model according to the specified arguments, defining the
|
| 14 |
full architecture including attention mechanisms, normalization, periodicity
|
| 15 |
-
modeling, an optional Leviathan continuous token embedding generator,
|
| 16 |
-
optional Leviathan-JTok-M token-indexed modulation module
|
|
|
|
| 17 |
|
| 18 |
Configuration objects inherit from :class:`~transformers.PretrainedConfig` and
|
| 19 |
can be used to control the model outputs. Read the documentation from
|
|
@@ -247,6 +248,36 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 247 |
Coefficient ``λ`` for the load-balancing auxiliary loss.
|
| 248 |
jtokm_norm_eps (:obj:`float`, *optional*, defaults to 1e-6):
|
| 249 |
Epsilon for L2 normalisation of modulation vectors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
use_hadamard_o_proj (:obj:`bool`, *optional*, defaults to ``False``):
|
| 251 |
Replace the dense ``W_O ∈ R^{d×d}`` output projection in every
|
| 252 |
multi-head attention block with a fixed Walsh–Hadamard Transform
|
|
@@ -269,10 +300,64 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 269 |
Projection: Structured Hadamard Transforms for Efficient
|
| 270 |
Transformers.* arXiv:2603.08343.
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
Constraints:
|
| 274 |
- ``use_jtokm=True`` requires ``use_token_generator=True``.
|
| 275 |
- ``1 ≤ jtokm_top_k < jtokm_num_experts`` when ``use_jtokm=True``.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
Examples::
|
| 278 |
|
|
@@ -298,6 +383,13 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 298 |
... use_jtokm=True,
|
| 299 |
... )
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
References:
|
| 302 |
Bae, J. et al. (2026). *Affine-Scaled Attention: Towards Flexible and
|
| 303 |
Stable Transformer Attention.* arXiv:2602.23057.
|
|
@@ -315,6 +407,12 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 315 |
|
| 316 |
Robinson, M. et al. (2025). *Token Embeddings Violate the Manifold
|
| 317 |
Hypothesis.* arXiv:2504.01002.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
"""
|
| 319 |
|
| 320 |
model_type = "neollm"
|
|
@@ -377,6 +475,12 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 377 |
jtokm_norm_eps=1e-6,
|
| 378 |
# ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
|
| 379 |
use_hadamard_o_proj=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
**kwargs,
|
| 381 |
):
|
| 382 |
# ── Generator / tying consistency ─────────────────────────────────
|
|
@@ -407,6 +511,24 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 407 |
f"got jtokm_top_k={jtokm_top_k}, jtokm_num_experts={jtokm_num_experts}."
|
| 408 |
)
|
| 409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
|
| 411 |
|
| 412 |
# ── Core Transformer ──────────────────────────────────────────────
|
|
@@ -490,6 +612,14 @@ class NeoLLMConfig(PretrainedConfig):
|
|
| 490 |
# ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
|
| 491 |
self.use_hadamard_o_proj = use_hadamard_o_proj
|
| 492 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
self.auto_map = {
|
| 494 |
"AutoConfig": "configuration_neollm.NeoLLMConfig",
|
| 495 |
"AutoModel": "modeling_neollm.NeoLLMModel",
|
|
|
|
| 12 |
|
| 13 |
Instantiates a NeoLLM model according to the specified arguments, defining the
|
| 14 |
full architecture including attention mechanisms, normalization, periodicity
|
| 15 |
+
modeling, an optional Leviathan continuous token embedding generator, an
|
| 16 |
+
optional Leviathan-JTok-M token-indexed modulation module, and optional
|
| 17 |
+
Spelling Bee character-level embedding augmentation.
|
| 18 |
|
| 19 |
Configuration objects inherit from :class:`~transformers.PretrainedConfig` and
|
| 20 |
can be used to control the model outputs. Read the documentation from
|
|
|
|
| 248 |
Coefficient ``λ`` for the load-balancing auxiliary loss.
|
| 249 |
jtokm_norm_eps (:obj:`float`, *optional*, defaults to 1e-6):
|
| 250 |
Epsilon for L2 normalisation of modulation vectors.
|
| 251 |
+
use_spelling_bee_embeddings (:obj:`bool`, *optional*, defaults to ``False``):
|
| 252 |
+
Augment token embeddings with character-level byte information
|
| 253 |
+
(Rabe, Clymo & Dong, 2026).
|
| 254 |
+
|
| 255 |
+
Each token's UTF-8 encoding (up to 16 bytes) is embedded through a
|
| 256 |
+
shared ``nn.Embedding(256, d)`` table. Byte embeddings are
|
| 257 |
+
position-encoded with RoPE using intra-token byte positions (not
|
| 258 |
+
sequence positions), summed and normalised by ``√byte_len``, then
|
| 259 |
+
averaged with the standard token embedding:
|
| 260 |
+
|
| 261 |
+
.. math::
|
| 262 |
+
e_{\\text{bee}}(t) = \\frac{1}{2}\\left(e_{\\text{tok}}(t) +
|
| 263 |
+
\\frac{1}{\\sqrt{|t|}}\\sum_{i=1}^{16}
|
| 264 |
+
\\text{RoPE}(e_{\\text{byte}}[b_i], i)\\right)
|
| 265 |
+
|
| 266 |
+
Adds ``256 × hidden_size`` parameters (≈0.13M for d=512).
|
| 267 |
+
Zero inference overhead when ``bake_inference_table()`` is called
|
| 268 |
+
after training.
|
| 269 |
+
|
| 270 |
+
Compatible with all four combinations of ``use_token_generator``
|
| 271 |
+
and ``use_spelling_bee_embeddings``.
|
| 272 |
+
|
| 273 |
+
**Setup required**: call
|
| 274 |
+
``model.model.spelling_bee.set_byte_table(tokenizer)`` once after
|
| 275 |
+
model instantiation (handled automatically by ``setup_model`` in
|
| 276 |
+
``train.py``).
|
| 277 |
+
|
| 278 |
+
Reference: Rabe, Clymo & Dong (2026). *Spelling Bee Embeddings for
|
| 279 |
+
Language Modeling.* arXiv:2601.18030.
|
| 280 |
+
|
| 281 |
use_hadamard_o_proj (:obj:`bool`, *optional*, defaults to ``False``):
|
| 282 |
Replace the dense ``W_O ∈ R^{d×d}`` output projection in every
|
| 283 |
multi-head attention block with a fixed Walsh–Hadamard Transform
|
|
|
|
| 300 |
Projection: Structured Hadamard Transforms for Efficient
|
| 301 |
Transformers.* arXiv:2603.08343.
|
| 302 |
|
| 303 |
+
use_repo (:obj:`bool`, *optional*, defaults to ``False``):
|
| 304 |
+
Enable Context Re-Positioning (REPO) in attention layers at or
|
| 305 |
+
above ``repo_start_layer`` (Li et al., 2026).
|
| 306 |
+
|
| 307 |
+
REPO replaces the fixed linear position indices ``0…L-1`` fed to
|
| 308 |
+
RoPE with continuous, data-dependent positions ``z_i = f_ϕ(h_i)``
|
| 309 |
+
learned end-to-end. The attention score between tokens ``i`` and
|
| 310 |
+
``j`` becomes:
|
| 311 |
+
|
| 312 |
+
.. math::
|
| 313 |
+
A^{\\text{REPO}}_{i,j} = q_i^\\top\\, g_\\theta(z_j - z_i)\\, k_j
|
| 314 |
+
|
| 315 |
+
where ``g_θ`` is the standard RoPE rotation and ``z_i`` is
|
| 316 |
+
predicted from the hidden state ``h_i`` by a lightweight SwiGLU
|
| 317 |
+
sub-layer ``f_ϕ``:
|
| 318 |
+
|
| 319 |
+
.. math::
|
| 320 |
+
r_i = \\text{Swish}(h_i W_g) \\odot (h_i W_c), \\quad
|
| 321 |
+
z_i^{(h)} = r_i w_z^{(h)}
|
| 322 |
+
|
| 323 |
+
``W_g, W_c \\in \\mathbb{R}^{d \\times d_p}`` are shared across
|
| 324 |
+
all query heads within a layer; ``w_z^{(h)} \\in \\mathbb{R}^{d_p}``
|
| 325 |
+
is learned independently per head. The assigned positions are
|
| 326 |
+
real-valued and unconstrained — the model may learn constant
|
| 327 |
+
(NoPE-like), monotonic (RoPE-like), or hybrid patterns as needed.
|
| 328 |
+
|
| 329 |
+
Lower layers (``layer_idx < repo_start_layer``) retain the
|
| 330 |
+
standard integer RoPE positions because they primarily capture
|
| 331 |
+
surface-level, locally-dependent features that benefit less from
|
| 332 |
+
re-positioning (Li et al., 2026, §3).
|
| 333 |
+
|
| 334 |
+
Overhead: +0.9% parameters; inference latency negligible.
|
| 335 |
+
|
| 336 |
+
repo_start_layer (:obj:`int`, *optional*, defaults to
|
| 337 |
+
``num_hidden_layers // 3``):
|
| 338 |
+
Index of the first decoder layer to which REPO is applied.
|
| 339 |
+
Layers ``[0, repo_start_layer)`` continue to use standard
|
| 340 |
+
integer RoPE positions. Must satisfy
|
| 341 |
+
``0 <= repo_start_layer < num_hidden_layers``.
|
| 342 |
+
Ignored when ``use_repo=False``.
|
| 343 |
+
|
| 344 |
+
repo_d_p (:obj:`int`, *optional*, defaults to
|
| 345 |
+
``hidden_size // 8``):
|
| 346 |
+
Dimensionality of the intermediate position representation
|
| 347 |
+
``r_i \\in \\mathbb{R}^{d_p}`` inside ``f_ϕ``. The paper sets
|
| 348 |
+
``d_p = d/8`` on the assumption that positional information
|
| 349 |
+
is less rich than the full hidden representation. Ignored
|
| 350 |
+
when ``use_repo=False``.
|
| 351 |
|
| 352 |
Constraints:
|
| 353 |
- ``use_jtokm=True`` requires ``use_token_generator=True``.
|
| 354 |
- ``1 ≤ jtokm_top_k < jtokm_num_experts`` when ``use_jtokm=True``.
|
| 355 |
+
- ``use_spelling_bee_embeddings=True`` requires calling
|
| 356 |
+
``model.model.spelling_bee.set_byte_table(tokenizer)`` before
|
| 357 |
+
training (handled automatically by ``setup_model``).
|
| 358 |
+
- ``repo_start_layer`` must satisfy
|
| 359 |
+
``0 <= repo_start_layer < num_hidden_layers`` when
|
| 360 |
+
``use_repo=True``.
|
| 361 |
|
| 362 |
Examples::
|
| 363 |
|
|
|
|
| 383 |
... use_jtokm=True,
|
| 384 |
... )
|
| 385 |
|
| 386 |
+
>>> # REPO: context re-positioning from layer 4 onward (default for 12 layers)
|
| 387 |
+
>>> config_repo = NeoLLMConfig(
|
| 388 |
+
... use_repo=True,
|
| 389 |
+
... # repo_start_layer defaults to num_hidden_layers // 3 = 4
|
| 390 |
+
... # repo_d_p defaults to hidden_size // 8 = 64
|
| 391 |
+
... )
|
| 392 |
+
|
| 393 |
References:
|
| 394 |
Bae, J. et al. (2026). *Affine-Scaled Attention: Towards Flexible and
|
| 395 |
Stable Transformer Attention.* arXiv:2602.23057.
|
|
|
|
| 407 |
|
| 408 |
Robinson, M. et al. (2025). *Token Embeddings Violate the Manifold
|
| 409 |
Hypothesis.* arXiv:2504.01002.
|
| 410 |
+
|
| 411 |
+
Rabe, M. N., Clymo, J. & Dong, Z. (2026). *Spelling Bee Embeddings for
|
| 412 |
+
Language Modeling.* arXiv:2601.18030.
|
| 413 |
+
|
| 414 |
+
Li, H., Zhao, T., Cai, D. & Sproat, R. (2026). *REPO: Language Models
|
| 415 |
+
with Context Re-Positioning.* arXiv:2512.14391.
|
| 416 |
"""
|
| 417 |
|
| 418 |
model_type = "neollm"
|
|
|
|
| 475 |
jtokm_norm_eps=1e-6,
|
| 476 |
# ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
|
| 477 |
use_hadamard_o_proj=True,
|
| 478 |
+
# ── Spelling Bee Embeddings (Rabe et al., 2026) ───────────────────
|
| 479 |
+
use_spelling_bee_embeddings=False,
|
| 480 |
+
# ── Context Re-Positioning (Li et al., 2026) ──────────────────────
|
| 481 |
+
use_repo=True,
|
| 482 |
+
repo_start_layer=None,
|
| 483 |
+
repo_d_p=None,
|
| 484 |
**kwargs,
|
| 485 |
):
|
| 486 |
# ── Generator / tying consistency ─────────────────────────────────
|
|
|
|
| 511 |
f"got jtokm_top_k={jtokm_top_k}, jtokm_num_experts={jtokm_num_experts}."
|
| 512 |
)
|
| 513 |
|
| 514 |
+
# ── REPO: resolve defaults and validate ───────────────────────────
|
| 515 |
+
# repo_start_layer defaults to num_hidden_layers // 3, matching the
|
| 516 |
+
# paper's 1/3-of-depth heuristic (Li et al., 2026, §3).
|
| 517 |
+
# repo_d_p defaults to hidden_size // 8, matching the paper's
|
| 518 |
+
# assumption that positional information is less rich than the full
|
| 519 |
+
# hidden representation (Li et al., 2026, §3.2).
|
| 520 |
+
if repo_start_layer is None:
|
| 521 |
+
repo_start_layer = num_hidden_layers // 3
|
| 522 |
+
if repo_d_p is None:
|
| 523 |
+
repo_d_p = hidden_size // 8
|
| 524 |
+
if use_repo and not (0 <= repo_start_layer < num_hidden_layers):
|
| 525 |
+
raise ValueError(
|
| 526 |
+
f"`repo_start_layer` must satisfy "
|
| 527 |
+
f"0 <= repo_start_layer < num_hidden_layers, "
|
| 528 |
+
f"got repo_start_layer={repo_start_layer}, "
|
| 529 |
+
f"num_hidden_layers={num_hidden_layers}."
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
|
| 533 |
|
| 534 |
# ── Core Transformer ──────────────────────────────────────────────
|
|
|
|
| 612 |
# ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
|
| 613 |
self.use_hadamard_o_proj = use_hadamard_o_proj
|
| 614 |
|
| 615 |
+
# ── Spelling Bee Embeddings (Rabe et al., 2026) ───────────────────
|
| 616 |
+
self.use_spelling_bee_embeddings = use_spelling_bee_embeddings
|
| 617 |
+
|
| 618 |
+
# ── Context Re-Positioning (Li et al., 2026) ──────────────────────
|
| 619 |
+
self.use_repo = use_repo
|
| 620 |
+
self.repo_start_layer = repo_start_layer
|
| 621 |
+
self.repo_d_p = repo_d_p
|
| 622 |
+
|
| 623 |
self.auto_map = {
|
| 624 |
"AutoConfig": "configuration_neollm.NeoLLMConfig",
|
| 625 |
"AutoModel": "modeling_neollm.NeoLLMModel",
|