Update modeling_neollm.py
Browse files- modeling_neollm.py +30 -285
modeling_neollm.py
CHANGED
|
@@ -1878,77 +1878,16 @@ class SeeDNorm(nn.Module):
|
|
| 1878 |
return f"dim={self.dim}, eps={self.eps}"
|
| 1879 |
|
| 1880 |
|
| 1881 |
-
def
|
| 1882 |
-
"""Canonicalise NeoLLM normalisation names."""
|
| 1883 |
-
raw = str(value).lower().replace("_", "").replace("-", "")
|
| 1884 |
-
aliases = {
|
| 1885 |
-
"rms": "rmsnorm",
|
| 1886 |
-
"rmsnorm": "rmsnorm",
|
| 1887 |
-
"seed": "seednorm",
|
| 1888 |
-
"seednorm": "seednorm",
|
| 1889 |
-
"seeednorm": "seednorm",
|
| 1890 |
-
"siamese": "siamesenorm",
|
| 1891 |
-
"siamesenorm": "siamesenorm",
|
| 1892 |
-
}
|
| 1893 |
-
out = aliases.get(raw)
|
| 1894 |
-
valid = {"seednorm", "rmsnorm"} | ({"siamesenorm"} if allow_siamese else set())
|
| 1895 |
-
if out not in valid:
|
| 1896 |
-
raise ValueError(
|
| 1897 |
-
f"Invalid normalization type {value!r}. Valid values are {sorted(valid)}."
|
| 1898 |
-
)
|
| 1899 |
-
return out
|
| 1900 |
-
|
| 1901 |
-
|
| 1902 |
-
def _uses_siamesenorm(config: NeoLLMConfig) -> bool:
|
| 1903 |
-
norm_type = getattr(config, "norm_type", None)
|
| 1904 |
-
if norm_type is None:
|
| 1905 |
-
return bool(getattr(config, "use_siamesenorm", False))
|
| 1906 |
-
return _canonical_norm_type(norm_type, allow_siamese=True) == "siamesenorm"
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
-
def _active_base_norm_type(config: NeoLLMConfig) -> str:
|
| 1910 |
-
"""
|
| 1911 |
-
Return the local normalizer used inside modules.
|
| 1912 |
-
|
| 1913 |
-
``norm_type="siamesenorm"`` is a two-stream topology, not a scalar
|
| 1914 |
-
normalization layer. Its internal local normalizers are selected by
|
| 1915 |
-
``siamese_norm_base_type`` and default to RMSNorm, matching the released
|
| 1916 |
-
SiameseNorm/OLMo configuration style.
|
| 1917 |
"""
|
| 1918 |
-
|
| 1919 |
-
if norm_type is None:
|
| 1920 |
-
norm_type = (
|
| 1921 |
-
"siamesenorm"
|
| 1922 |
-
if bool(getattr(config, "use_siamesenorm", False))
|
| 1923 |
-
else ("seednorm" if bool(getattr(config, "use_seednorm", True)) else "rmsnorm")
|
| 1924 |
-
)
|
| 1925 |
-
norm_type = _canonical_norm_type(norm_type, allow_siamese=True)
|
| 1926 |
-
if norm_type == "siamesenorm":
|
| 1927 |
-
return _canonical_norm_type(
|
| 1928 |
-
getattr(config, "siamese_norm_base_type", "rmsnorm"),
|
| 1929 |
-
allow_siamese=False,
|
| 1930 |
-
)
|
| 1931 |
-
return norm_type
|
| 1932 |
-
|
| 1933 |
|
| 1934 |
-
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
-
norm_type: Optional[str] = None,
|
| 1939 |
-
) -> nn.Module:
|
| 1940 |
"""
|
| 1941 |
-
|
| 1942 |
-
|
| 1943 |
-
``norm_type="seednorm"`` instantiates SeeDNorm.
|
| 1944 |
-
``norm_type="rmsnorm"`` instantiates ``nn.RMSNorm``.
|
| 1945 |
-
SiameseNorm is handled at the decoder-layer topology level and must not be
|
| 1946 |
-
requested as a single local layer.
|
| 1947 |
-
"""
|
| 1948 |
-
if norm_type is None:
|
| 1949 |
-
norm_type = "seednorm" if use_seednorm else "rmsnorm"
|
| 1950 |
-
norm_type = _canonical_norm_type(norm_type, allow_siamese=False)
|
| 1951 |
-
if norm_type == "seednorm":
|
| 1952 |
return SeeDNorm(dim, eps=eps)
|
| 1953 |
return nn.RMSNorm(dim, eps=eps)
|
| 1954 |
|
|
@@ -3457,11 +3396,10 @@ class NeoLLMAttention(nn.Module):
|
|
| 3457 |
enable_multipliers=self.use_learnable_multipliers,
|
| 3458 |
)
|
| 3459 |
|
| 3460 |
-
self.
|
| 3461 |
-
self.use_seednorm = self.norm_type == "seednorm"
|
| 3462 |
self.mea_norm_type = str(getattr(config, "mea_norm_type", "seednorm")).lower()
|
| 3463 |
-
self.q_norm = _make_norm(self.head_dim, eps=config.rms_norm_eps,
|
| 3464 |
-
self.k_norm = _make_norm(self.head_dim, eps=config.rms_norm_eps,
|
| 3465 |
|
| 3466 |
if self.use_mea_attention:
|
| 3467 |
self.mea_key_mix = nn.Parameter(
|
|
@@ -4993,7 +4931,7 @@ class VersatileFFN(nn.Module):
|
|
| 4993 |
self.ff_norm = _make_norm(
|
| 4994 |
config.hidden_size,
|
| 4995 |
eps=config.rms_norm_eps,
|
| 4996 |
-
|
| 4997 |
)
|
| 4998 |
|
| 4999 |
# ── Width path: expert router + contiguous segment indices ──────────
|
|
@@ -5520,10 +5458,7 @@ class NeoLLMDecoderLayer(GradientCheckpointingLayer):
|
|
| 5520 |
self.hidden_size = config.hidden_size
|
| 5521 |
self.layer_idx = layer_idx
|
| 5522 |
self.use_jtokm = config.use_jtokm
|
| 5523 |
-
self.
|
| 5524 |
-
self.norm_type = _active_base_norm_type(config)
|
| 5525 |
-
self.use_seednorm = self.norm_type == "seednorm"
|
| 5526 |
-
self.siamese_depth_scaling = bool(getattr(config, "siamese_depth_scaling", True))
|
| 5527 |
# Controls only the first pre-attention normalisation applied directly
|
| 5528 |
# to the embedding stream. Defaults to True for checkpoint/config
|
| 5529 |
# backward compatibility. When False, layer 0 does not instantiate
|
|
@@ -5547,7 +5482,7 @@ class NeoLLMDecoderLayer(GradientCheckpointingLayer):
|
|
| 5547 |
_make_norm(
|
| 5548 |
config.hidden_size,
|
| 5549 |
eps=config.rms_norm_eps,
|
| 5550 |
-
|
| 5551 |
)
|
| 5552 |
if self.has_input_layernorm
|
| 5553 |
else None
|
|
@@ -5555,27 +5490,8 @@ class NeoLLMDecoderLayer(GradientCheckpointingLayer):
|
|
| 5555 |
self.post_attention_layernorm = _make_norm(
|
| 5556 |
config.hidden_size,
|
| 5557 |
eps=config.rms_norm_eps,
|
| 5558 |
-
|
| 5559 |
)
|
| 5560 |
-
if self.use_siamesenorm:
|
| 5561 |
-
# SiameseNorm best variant from the released source: the X stream
|
| 5562 |
-
# uses the ordinary Hybrid/Post-like normalizers, while the Y
|
| 5563 |
-
# stream has its own Pre-Norm normalizers. The fused MLP input is
|
| 5564 |
-
# normalized explicitly because the paper's ablation finds that
|
| 5565 |
-
# normalized fused inputs are necessary.
|
| 5566 |
-
self.siamese_attn_y_norm = _make_norm(
|
| 5567 |
-
config.hidden_size, eps=config.rms_norm_eps, norm_type=self.norm_type
|
| 5568 |
-
)
|
| 5569 |
-
self.siamese_mlp_y_norm = _make_norm(
|
| 5570 |
-
config.hidden_size, eps=config.rms_norm_eps, norm_type=self.norm_type
|
| 5571 |
-
)
|
| 5572 |
-
self.siamese_mlp_input_norm = _make_norm(
|
| 5573 |
-
config.hidden_size, eps=config.rms_norm_eps, norm_type=self.norm_type
|
| 5574 |
-
)
|
| 5575 |
-
else:
|
| 5576 |
-
self.siamese_attn_y_norm = None
|
| 5577 |
-
self.siamese_mlp_y_norm = None
|
| 5578 |
-
self.siamese_mlp_input_norm = None
|
| 5579 |
self.lns_attn = LNS(layer_idx)
|
| 5580 |
self.lns_mlp = LNS(layer_idx)
|
| 5581 |
self.gpas_attn = GPAS(config.hidden_size)
|
|
@@ -5834,136 +5750,10 @@ class NeoLLMDecoderLayer(GradientCheckpointingLayer):
|
|
| 5834 |
# LAUREL-LR (paper eq. 3): f(x) + BAx + x
|
| 5835 |
return delta + lr_delta + residual
|
| 5836 |
|
| 5837 |
-
def _siamese_scale(self) -> float:
|
| 5838 |
-
"""Depth-wise residual scale for the SiameseNorm X stream."""
|
| 5839 |
-
if not self.siamese_depth_scaling:
|
| 5840 |
-
return 1.0
|
| 5841 |
-
return math.sqrt(2.0 * float(self.layer_idx + 1))
|
| 5842 |
-
|
| 5843 |
-
def _forward_siamesenorm(
|
| 5844 |
-
self,
|
| 5845 |
-
hidden_states: torch.Tensor,
|
| 5846 |
-
siamese_states: Optional[torch.Tensor],
|
| 5847 |
-
position_embeddings: tuple[torch.Tensor, torch.Tensor],
|
| 5848 |
-
attention_mask: Optional[torch.Tensor] = None,
|
| 5849 |
-
first_layer_fan: Optional[torch.Tensor] = None,
|
| 5850 |
-
z_tilde: Optional[torch.Tensor] = None,
|
| 5851 |
-
B_vals: Optional[torch.Tensor] = None,
|
| 5852 |
-
layer_analysis: Optional[LayerAnalysis] = None,
|
| 5853 |
-
output_attentions: Optional[bool] = False,
|
| 5854 |
-
repo_rope_args: Optional[Tuple[torch.Tensor, float]] = None,
|
| 5855 |
-
position_ids: Optional[torch.LongTensor] = None,
|
| 5856 |
-
**kwargs: Unpack[FlashAttentionKwargs],
|
| 5857 |
-
) -> Tuple:
|
| 5858 |
-
"""
|
| 5859 |
-
SiameseNorm two-stream decoder block.
|
| 5860 |
-
|
| 5861 |
-
X stream receives the shared residual updates divided by
|
| 5862 |
-
c_l = sqrt(2(l+1)) when depth scaling is enabled. Y stream receives
|
| 5863 |
-
the same shared updates without depth scaling, preserving the Pre-Norm
|
| 5864 |
-
identity-gradient path. Attention and MLP parameters are shared.
|
| 5865 |
-
"""
|
| 5866 |
-
if siamese_states is None:
|
| 5867 |
-
siamese_states = hidden_states
|
| 5868 |
-
|
| 5869 |
-
if layer_analysis is not None:
|
| 5870 |
-
layer_analysis.hidden_states_input = hidden_states.detach()
|
| 5871 |
-
|
| 5872 |
-
# ── Attention fused input: Norm_X(x) + Norm_Y(y) ─────────────────
|
| 5873 |
-
x_residual_attn = hidden_states
|
| 5874 |
-
y_residual_attn = siamese_states
|
| 5875 |
-
sn_pre = layer_analysis.seednorm_pre_attn if layer_analysis is not None else None
|
| 5876 |
-
|
| 5877 |
-
if self.input_layernorm is None:
|
| 5878 |
-
x_normed = x_residual_attn
|
| 5879 |
-
if sn_pre is not None:
|
| 5880 |
-
sn_pre.output = x_normed.detach()
|
| 5881 |
-
else:
|
| 5882 |
-
x_normed = _apply_norm(self.input_layernorm, x_residual_attn, analysis=sn_pre)
|
| 5883 |
-
y_normed = _apply_norm(self.siamese_attn_y_norm, y_residual_attn, analysis=None)
|
| 5884 |
-
h_lns = self.lns_attn(x_normed + y_normed)
|
| 5885 |
-
if layer_analysis is not None:
|
| 5886 |
-
layer_analysis.lns_attn_output = h_lns.detach()
|
| 5887 |
-
|
| 5888 |
-
attn_delta, attn_weights, self.current_layer_fan = self.self_attn(
|
| 5889 |
-
hidden_states=h_lns,
|
| 5890 |
-
key_value_states=None,
|
| 5891 |
-
attention_mask=attention_mask,
|
| 5892 |
-
position_embeddings=position_embeddings,
|
| 5893 |
-
first_layer_fan=first_layer_fan,
|
| 5894 |
-
attn_analysis=layer_analysis.attention if layer_analysis is not None else None,
|
| 5895 |
-
repo_rope_args=repo_rope_args,
|
| 5896 |
-
position_ids=position_ids,
|
| 5897 |
-
**kwargs,
|
| 5898 |
-
)
|
| 5899 |
-
if layer_analysis is not None:
|
| 5900 |
-
layer_analysis.attn_contribution = attn_delta.detach()
|
| 5901 |
-
|
| 5902 |
-
scale = self._siamese_scale()
|
| 5903 |
-
gpas_attn_a = layer_analysis.gpas_attn if layer_analysis is not None else None
|
| 5904 |
-
x_after_attn = self.gpas_attn(x_residual_attn + attn_delta / scale, analysis=gpas_attn_a)
|
| 5905 |
-
y_after_attn = y_residual_attn + attn_delta
|
| 5906 |
-
|
| 5907 |
-
if layer_analysis is not None:
|
| 5908 |
-
layer_analysis.h_tilde = x_after_attn.detach()
|
| 5909 |
-
|
| 5910 |
-
# ── MLP fused input: NormInput(Norm_X(x) + Norm_Y(y)) ─────────────
|
| 5911 |
-
x_residual_mlp = x_after_attn
|
| 5912 |
-
sn_post = layer_analysis.seednorm_post_attn if layer_analysis is not None else None
|
| 5913 |
-
x_mlp_normed = _apply_norm(self.post_attention_layernorm, x_after_attn, analysis=sn_post)
|
| 5914 |
-
y_mlp_normed = _apply_norm(self.siamese_mlp_y_norm, y_after_attn, analysis=None)
|
| 5915 |
-
mlp_fused = _apply_norm(self.siamese_mlp_input_norm, x_mlp_normed + y_mlp_normed, analysis=None)
|
| 5916 |
-
h_lns2 = self.lns_mlp(mlp_fused)
|
| 5917 |
-
if layer_analysis is not None:
|
| 5918 |
-
layer_analysis.lns_mlp_output = h_lns2.detach()
|
| 5919 |
-
|
| 5920 |
-
mlp_a = layer_analysis.mlp if layer_analysis is not None else None
|
| 5921 |
-
if self.use_versatile_ffn:
|
| 5922 |
-
delta_m, versatile_aux = self.mlp(h_lns2, analysis=mlp_a)
|
| 5923 |
-
else:
|
| 5924 |
-
delta_m = self.mlp(h_lns2, analysis=mlp_a)
|
| 5925 |
-
versatile_aux = None
|
| 5926 |
-
|
| 5927 |
-
if layer_analysis is not None:
|
| 5928 |
-
layer_analysis.mlp_contribution = delta_m.detach()
|
| 5929 |
-
|
| 5930 |
-
aux_stats = None
|
| 5931 |
-
shared_mlp_update = delta_m
|
| 5932 |
-
if self.use_jtokm and z_tilde is not None and B_vals is not None:
|
| 5933 |
-
orig_shape = x_after_attn.shape
|
| 5934 |
-
h_flat = x_after_attn.reshape(-1, self.hidden_size)
|
| 5935 |
-
z_flat = z_tilde.reshape(-1, z_tilde.shape[-1])
|
| 5936 |
-
B_flat = B_vals.reshape(-1, B_vals.shape[-2], B_vals.shape[-1])
|
| 5937 |
-
|
| 5938 |
-
jtokm_a = layer_analysis.jtokm if layer_analysis is not None else None
|
| 5939 |
-
delta_r, aux_stats = self.jtokm(h_flat, z_flat, B_flat, analysis=jtokm_a)
|
| 5940 |
-
delta_r = delta_r.reshape(orig_shape)
|
| 5941 |
-
shared_mlp_update = shared_mlp_update + delta_r
|
| 5942 |
-
|
| 5943 |
-
gpas_mlp_a = layer_analysis.gpas_mlp if layer_analysis is not None else None
|
| 5944 |
-
hidden_states = self.gpas_mlp(
|
| 5945 |
-
x_residual_mlp + shared_mlp_update / scale,
|
| 5946 |
-
analysis=gpas_mlp_a,
|
| 5947 |
-
)
|
| 5948 |
-
siamese_states = y_after_attn + shared_mlp_update
|
| 5949 |
-
|
| 5950 |
-
if layer_analysis is not None:
|
| 5951 |
-
layer_analysis.hidden_states_output = hidden_states.detach()
|
| 5952 |
-
|
| 5953 |
-
outputs = (hidden_states, siamese_states)
|
| 5954 |
-
if output_attentions:
|
| 5955 |
-
outputs += (attn_weights,)
|
| 5956 |
-
if aux_stats is not None:
|
| 5957 |
-
outputs += (aux_stats,)
|
| 5958 |
-
if versatile_aux is not None:
|
| 5959 |
-
outputs += (versatile_aux,)
|
| 5960 |
-
return outputs
|
| 5961 |
-
|
| 5962 |
def forward(
|
| 5963 |
self,
|
| 5964 |
hidden_states: torch.Tensor,
|
| 5965 |
-
|
| 5966 |
-
position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None,
|
| 5967 |
attention_mask: Optional[torch.Tensor] = None,
|
| 5968 |
first_layer_fan: Optional[torch.Tensor] = None,
|
| 5969 |
z_tilde: Optional[torch.Tensor] = None,
|
|
@@ -5977,22 +5767,6 @@ class NeoLLMDecoderLayer(GradientCheckpointingLayer):
|
|
| 5977 |
position_ids: Optional[torch.LongTensor] = None,
|
| 5978 |
**kwargs: Unpack[FlashAttentionKwargs],
|
| 5979 |
) -> Tuple:
|
| 5980 |
-
if self.use_siamesenorm:
|
| 5981 |
-
return self._forward_siamesenorm(
|
| 5982 |
-
hidden_states=hidden_states,
|
| 5983 |
-
siamese_states=siamese_states,
|
| 5984 |
-
position_embeddings=position_embeddings,
|
| 5985 |
-
attention_mask=attention_mask,
|
| 5986 |
-
first_layer_fan=first_layer_fan,
|
| 5987 |
-
z_tilde=z_tilde,
|
| 5988 |
-
B_vals=B_vals,
|
| 5989 |
-
layer_analysis=layer_analysis,
|
| 5990 |
-
output_attentions=output_attentions,
|
| 5991 |
-
repo_rope_args=repo_rope_args,
|
| 5992 |
-
position_ids=position_ids,
|
| 5993 |
-
**kwargs,
|
| 5994 |
-
)
|
| 5995 |
-
|
| 5996 |
# ── Snapshot input ────────────────────────────────────────────────
|
| 5997 |
if layer_analysis is not None:
|
| 5998 |
layer_analysis.hidden_states_input = hidden_states.detach()
|
|
@@ -6694,27 +6468,11 @@ class NeoLLMModel(NeoLLMPreTrainedModel):
|
|
| 6694 |
[NeoLLMDecoderLayer(config, layer_idx)
|
| 6695 |
for layer_idx in range(config.num_hidden_layers)]
|
| 6696 |
)
|
| 6697 |
-
self.use_siamesenorm = _uses_siamesenorm(config)
|
| 6698 |
-
self.norm_type = _active_base_norm_type(config)
|
| 6699 |
self.norm = _make_norm(
|
| 6700 |
config.hidden_size,
|
| 6701 |
eps=config.rms_norm_eps,
|
| 6702 |
-
|
| 6703 |
)
|
| 6704 |
-
if self.use_siamesenorm:
|
| 6705 |
-
self.siamese_final_y_norm = _make_norm(
|
| 6706 |
-
config.hidden_size,
|
| 6707 |
-
eps=config.rms_norm_eps,
|
| 6708 |
-
norm_type=self.norm_type,
|
| 6709 |
-
)
|
| 6710 |
-
self.siamese_final_norm = _make_norm(
|
| 6711 |
-
config.hidden_size,
|
| 6712 |
-
eps=config.rms_norm_eps,
|
| 6713 |
-
norm_type=self.norm_type,
|
| 6714 |
-
)
|
| 6715 |
-
else:
|
| 6716 |
-
self.siamese_final_y_norm = None
|
| 6717 |
-
self.siamese_final_norm = None
|
| 6718 |
self.rotary_emb = NeoLLMRotaryEmbedding(config=config)
|
| 6719 |
self.gradient_checkpointing = False
|
| 6720 |
self.first_layer_fan = None if getattr(config, "use_fan_residual", True) else False
|
|
@@ -6900,7 +6658,6 @@ class NeoLLMModel(NeoLLMPreTrainedModel):
|
|
| 6900 |
)
|
| 6901 |
|
| 6902 |
hidden_states = inputs_embeds
|
| 6903 |
-
siamese_states = hidden_states if self.use_siamesenorm else None
|
| 6904 |
all_hidden_states = () if output_hidden_states else None
|
| 6905 |
all_attentions = () if output_attentions else None
|
| 6906 |
all_aux_stats = []
|
|
@@ -7015,7 +6772,6 @@ class NeoLLMModel(NeoLLMPreTrainedModel):
|
|
| 7015 |
|
| 7016 |
layer_outputs = decoder_layer(
|
| 7017 |
hidden_states,
|
| 7018 |
-
siamese_states=siamese_states,
|
| 7019 |
position_embeddings=position_embeddings,
|
| 7020 |
attention_mask=causal_mask,
|
| 7021 |
first_layer_fan=self.first_layer_fan,
|
|
@@ -7031,10 +6787,6 @@ class NeoLLMModel(NeoLLMPreTrainedModel):
|
|
| 7031 |
**kwargs,
|
| 7032 |
)
|
| 7033 |
hidden_states = layer_outputs[0]
|
| 7034 |
-
output_offset = 1
|
| 7035 |
-
if self.use_siamesenorm:
|
| 7036 |
-
siamese_states = layer_outputs[1]
|
| 7037 |
-
output_offset = 2
|
| 7038 |
|
| 7039 |
# Update AttnRes partial sum — the new partial is the layer output
|
| 7040 |
if use_attn_res:
|
|
@@ -7045,34 +6797,27 @@ class NeoLLMModel(NeoLLMPreTrainedModel):
|
|
| 7045 |
dca_sources = dca_sources + [hidden_states]
|
| 7046 |
|
| 7047 |
if output_attentions:
|
| 7048 |
-
all_attentions = all_attentions + (layer_outputs[
|
| 7049 |
-
|
| 7050 |
-
|
| 7051 |
-
|
| 7052 |
-
|
| 7053 |
-
|
| 7054 |
-
#
|
| 7055 |
-
#
|
| 7056 |
-
|
| 7057 |
-
|
| 7058 |
-
|
| 7059 |
-
|
| 7060 |
-
all_aux_stats.append(item)
|
| 7061 |
-
jtokm_aux_taken = True
|
| 7062 |
-
elif getattr(self.config, "use_versatile_ffn", False):
|
| 7063 |
all_aux_stats.append(("versatile", item))
|
|
|
|
| 7064 |
|
| 7065 |
if (getattr(self.config, "use_fan_residual", True)
|
| 7066 |
and self.first_layer_fan is None
|
| 7067 |
and hasattr(decoder_layer, "current_layer_fan")):
|
| 7068 |
self.first_layer_fan = decoder_layer.current_layer_fan
|
| 7069 |
|
| 7070 |
-
|
| 7071 |
-
hidden_states = self.norm(hidden_states)
|
| 7072 |
-
siamese_states = self.siamese_final_y_norm(siamese_states)
|
| 7073 |
-
hidden_states = self.siamese_final_norm(hidden_states + siamese_states)
|
| 7074 |
-
else:
|
| 7075 |
-
hidden_states = self.norm(hidden_states)
|
| 7076 |
|
| 7077 |
if output_hidden_states:
|
| 7078 |
all_hidden_states = all_hidden_states + (hidden_states,)
|
|
|
|
| 1878 |
return f"dim={self.dim}, eps={self.eps}"
|
| 1879 |
|
| 1880 |
|
| 1881 |
+
def _make_norm(dim: int, eps: float, use_seednorm: bool = True) -> nn.Module:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1882 |
"""
|
| 1883 |
+
Build the active Transformer normalisation module.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1884 |
|
| 1885 |
+
``use_seednorm=True`` preserves the previous SeeDNorm path.
|
| 1886 |
+
``use_seednorm=False`` uses standard RMSNorm at the same sites, so the
|
| 1887 |
+
module keeps the same pre-norm topology without SeeDNorm's dynamic
|
| 1888 |
+
alpha/beta/gamma re-scaling parameters.
|
|
|
|
|
|
|
| 1889 |
"""
|
| 1890 |
+
if use_seednorm:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1891 |
return SeeDNorm(dim, eps=eps)
|
| 1892 |
return nn.RMSNorm(dim, eps=eps)
|
| 1893 |
|
|
|
|
| 3396 |
enable_multipliers=self.use_learnable_multipliers,
|
| 3397 |
)
|
| 3398 |
|
| 3399 |
+
self.use_seednorm = bool(getattr(config, "use_seednorm", True))
|
|
|
|
| 3400 |
self.mea_norm_type = str(getattr(config, "mea_norm_type", "seednorm")).lower()
|
| 3401 |
+
self.q_norm = _make_norm(self.head_dim, eps=config.rms_norm_eps, use_seednorm=self.use_seednorm)
|
| 3402 |
+
self.k_norm = _make_norm(self.head_dim, eps=config.rms_norm_eps, use_seednorm=self.use_seednorm)
|
| 3403 |
|
| 3404 |
if self.use_mea_attention:
|
| 3405 |
self.mea_key_mix = nn.Parameter(
|
|
|
|
| 4931 |
self.ff_norm = _make_norm(
|
| 4932 |
config.hidden_size,
|
| 4933 |
eps=config.rms_norm_eps,
|
| 4934 |
+
use_seednorm=bool(getattr(config, "use_seednorm", True)),
|
| 4935 |
)
|
| 4936 |
|
| 4937 |
# ── Width path: expert router + contiguous segment indices ──────────
|
|
|
|
| 5458 |
self.hidden_size = config.hidden_size
|
| 5459 |
self.layer_idx = layer_idx
|
| 5460 |
self.use_jtokm = config.use_jtokm
|
| 5461 |
+
self.use_seednorm = bool(getattr(config, "use_seednorm", True))
|
|
|
|
|
|
|
|
|
|
| 5462 |
# Controls only the first pre-attention normalisation applied directly
|
| 5463 |
# to the embedding stream. Defaults to True for checkpoint/config
|
| 5464 |
# backward compatibility. When False, layer 0 does not instantiate
|
|
|
|
| 5482 |
_make_norm(
|
| 5483 |
config.hidden_size,
|
| 5484 |
eps=config.rms_norm_eps,
|
| 5485 |
+
use_seednorm=self.use_seednorm,
|
| 5486 |
)
|
| 5487 |
if self.has_input_layernorm
|
| 5488 |
else None
|
|
|
|
| 5490 |
self.post_attention_layernorm = _make_norm(
|
| 5491 |
config.hidden_size,
|
| 5492 |
eps=config.rms_norm_eps,
|
| 5493 |
+
use_seednorm=self.use_seednorm,
|
| 5494 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5495 |
self.lns_attn = LNS(layer_idx)
|
| 5496 |
self.lns_mlp = LNS(layer_idx)
|
| 5497 |
self.gpas_attn = GPAS(config.hidden_size)
|
|
|
|
| 5750 |
# LAUREL-LR (paper eq. 3): f(x) + BAx + x
|
| 5751 |
return delta + lr_delta + residual
|
| 5752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5753 |
def forward(
|
| 5754 |
self,
|
| 5755 |
hidden_states: torch.Tensor,
|
| 5756 |
+
position_embeddings: tuple[torch.Tensor, torch.Tensor],
|
|
|
|
| 5757 |
attention_mask: Optional[torch.Tensor] = None,
|
| 5758 |
first_layer_fan: Optional[torch.Tensor] = None,
|
| 5759 |
z_tilde: Optional[torch.Tensor] = None,
|
|
|
|
| 5767 |
position_ids: Optional[torch.LongTensor] = None,
|
| 5768 |
**kwargs: Unpack[FlashAttentionKwargs],
|
| 5769 |
) -> Tuple:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5770 |
# ── Snapshot input ────────────────────────────────────────────────
|
| 5771 |
if layer_analysis is not None:
|
| 5772 |
layer_analysis.hidden_states_input = hidden_states.detach()
|
|
|
|
| 6468 |
[NeoLLMDecoderLayer(config, layer_idx)
|
| 6469 |
for layer_idx in range(config.num_hidden_layers)]
|
| 6470 |
)
|
|
|
|
|
|
|
| 6471 |
self.norm = _make_norm(
|
| 6472 |
config.hidden_size,
|
| 6473 |
eps=config.rms_norm_eps,
|
| 6474 |
+
use_seednorm=bool(getattr(config, "use_seednorm", True)),
|
| 6475 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6476 |
self.rotary_emb = NeoLLMRotaryEmbedding(config=config)
|
| 6477 |
self.gradient_checkpointing = False
|
| 6478 |
self.first_layer_fan = None if getattr(config, "use_fan_residual", True) else False
|
|
|
|
| 6658 |
)
|
| 6659 |
|
| 6660 |
hidden_states = inputs_embeds
|
|
|
|
| 6661 |
all_hidden_states = () if output_hidden_states else None
|
| 6662 |
all_attentions = () if output_attentions else None
|
| 6663 |
all_aux_stats = []
|
|
|
|
| 6772 |
|
| 6773 |
layer_outputs = decoder_layer(
|
| 6774 |
hidden_states,
|
|
|
|
| 6775 |
position_embeddings=position_embeddings,
|
| 6776 |
attention_mask=causal_mask,
|
| 6777 |
first_layer_fan=self.first_layer_fan,
|
|
|
|
| 6787 |
**kwargs,
|
| 6788 |
)
|
| 6789 |
hidden_states = layer_outputs[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6790 |
|
| 6791 |
# Update AttnRes partial sum — the new partial is the layer output
|
| 6792 |
if use_attn_res:
|
|
|
|
| 6797 |
dca_sources = dca_sources + [hidden_states]
|
| 6798 |
|
| 6799 |
if output_attentions:
|
| 6800 |
+
all_attentions = all_attentions + (layer_outputs[1],)
|
| 6801 |
+
|
| 6802 |
+
# Collect JTok-M aux stats (last element if present)
|
| 6803 |
+
if self.config.use_jtokm and len(layer_outputs) > (2 if output_attentions else 1):
|
| 6804 |
+
all_aux_stats.append(layer_outputs[-1])
|
| 6805 |
+
|
| 6806 |
+
# Collect VersatileFFN aux stats (second-to-last if jtokm also present,
|
| 6807 |
+
# or last if jtokm is absent). Only non-None during training.
|
| 6808 |
+
if getattr(self.config, "use_versatile_ffn", False):
|
| 6809 |
+
for item in layer_outputs[1:]:
|
| 6810 |
+
if isinstance(item, tuple) and len(item) == 3:
|
| 6811 |
+
# (p_sum, f_sum, N_tokens) signature
|
|
|
|
|
|
|
|
|
|
| 6812 |
all_aux_stats.append(("versatile", item))
|
| 6813 |
+
break
|
| 6814 |
|
| 6815 |
if (getattr(self.config, "use_fan_residual", True)
|
| 6816 |
and self.first_layer_fan is None
|
| 6817 |
and hasattr(decoder_layer, "current_layer_fan")):
|
| 6818 |
self.first_layer_fan = decoder_layer.current_layer_fan
|
| 6819 |
|
| 6820 |
+
hidden_states = self.norm(hidden_states)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6821 |
|
| 6822 |
if output_hidden_states:
|
| 6823 |
all_hidden_states = all_hidden_states + (hidden_states,)
|