KitsuVp commited on
Commit
4447da8
·
verified ·
1 Parent(s): a4f29f3

Update configuration_neollm.py

Browse files
Files changed (1) hide show
  1. configuration_neollm.py +104 -96
configuration_neollm.py CHANGED
@@ -1,3 +1,4 @@
 
1
  # ==================== configuration_neollm.py ====================
2
  from transformers.configuration_utils import PretrainedConfig
3
  from transformers.modeling_rope_utils import rope_config_validation
@@ -636,9 +637,9 @@ class NeoLLMConfig(PretrainedConfig):
636
  attn_res_num_blocks=4,
637
  # ── StackMemory / STACKTRANS (Zhang et al., NeurIPS 2025) ───────
638
  use_stack_memory=False,
639
- stack_d_model=32, # H * d_s = 4 * 16
640
- num_mem_heads=4, # H = 4
641
- stack_slots=16, # S = 24
642
  stack_memory_cache_size=2048,
643
  # ── ResFormer cross-layer FAN residual (He et al., 2023) ─────────
644
  use_fan_residual=False,
@@ -693,9 +694,9 @@ class NeoLLMConfig(PretrainedConfig):
693
  laurel_lr_rank=32,
694
  # ── Interleaved Head Attention (Duvvuri et al., 2026) ─────────────
695
  use_iha=True,
696
- iha_num_pseudo_heads=2, # P=2 → 2×2=4 patrones por head
697
- iha_local_global_pattern="LLLLG", # 4 locales + 1 global (paper §5.1)
698
- iha_sliding_window=None, # auto = N // (2*P^2) usando la longitud real del batch
699
  iha_global_layers_use_iha=False, # False replica el paper: la capa G es global estándar, sin IHA
700
  **kwargs,
701
  ):
@@ -758,7 +759,9 @@ class NeoLLMConfig(PretrainedConfig):
758
  repo_start_layer = num_hidden_layers // 3
759
  if repo_d_p is None:
760
  repo_d_p = hidden_size // 8
761
- if (use_repo or use_repo_grape or use_repo_goat_prior) and not (0 <= repo_start_layer < num_hidden_layers):
 
 
762
  raise ValueError(
763
  f"`repo_start_layer` must satisfy "
764
  f"0 <= repo_start_layer < num_hidden_layers, "
@@ -775,8 +778,7 @@ class NeoLLMConfig(PretrainedConfig):
775
  )
776
  if repo_goat_sink_decay <= 0.0:
777
  raise ValueError(
778
- f"`repo_goat_sink_decay` must be > 0, "
779
- f"got {repo_goat_sink_decay}."
780
  )
781
  _repo_goat_prior_dim = 2 * int(repo_goat_num_frequencies) + 2
782
  if (head_dim + _repo_goat_prior_dim) % 8 != 0:
@@ -785,7 +787,9 @@ class NeoLLMConfig(PretrainedConfig):
785
  "head_dim=%d, giving internal attention dim=%d. Some "
786
  "FlashAttention builds prefer dimensions divisible by 8; "
787
  "the default repo_goat_num_frequencies=3 gives +8.",
788
- _repo_goat_prior_dim, head_dim, head_dim + _repo_goat_prior_dim,
 
 
789
  )
790
  # ── SiameseNorm: RMS-only topology by construction ────────────────
791
 
@@ -803,7 +807,9 @@ class NeoLLMConfig(PretrainedConfig):
803
  if use_iha:
804
  _iha_pattern = str(iha_local_global_pattern).upper().strip()
805
  if not _iha_pattern:
806
- raise ValueError("`iha_local_global_pattern` must not be empty when `use_iha=True`.")
 
 
807
  _bad_iha_pattern_chars = sorted(set(_iha_pattern) - {"L", "G"})
808
  if _bad_iha_pattern_chars:
809
  raise ValueError(
@@ -821,7 +827,9 @@ class NeoLLMConfig(PretrainedConfig):
821
  "use_stack_memory": bool(use_stack_memory),
822
  "use_siamesenorm": bool(use_siamesenorm),
823
  }
824
- _active_residual_flow = [name for name, enabled in _residual_flow_flags.items() if enabled]
 
 
825
  if len(_active_residual_flow) > 1:
826
  raise ValueError(
827
  "Enable at most one residual-flow mechanism among "
@@ -854,45 +862,45 @@ class NeoLLMConfig(PretrainedConfig):
854
  super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
855
 
856
  # ── Core Transformer ──────────────────────────────────────────────
857
- self.vocab_size = vocab_size
858
  self.max_position_embeddings = max_position_embeddings
859
- self.hidden_size = hidden_size
860
- self.intermediate_size = intermediate_size
861
- self.num_hidden_layers = num_hidden_layers
862
- self.num_attention_heads = num_attention_heads
863
- self.num_key_value_heads = num_key_value_heads
864
- self.hidden_act = hidden_act
865
- self.initializer_range = initializer_range
866
- self.rms_norm_eps = rms_norm_eps
867
 
868
  # ── Positional encoding ───────────────────────────────────────────
869
- self.rope_theta = rope_theta
870
- self.rope_scaling = rope_scaling
871
- self.partial_rotary_factor = partial_rotary_factor
872
 
873
  # ── Attention ─────────────────────────────────────────────────────
874
- self.attention_bias = attention_bias
875
- self.attention_dropout = attention_dropout
876
- self.head_dim = head_dim
877
- self.use_momentum_attention = use_momentum_attention
878
- self.momentum_gamma = momentum_gamma
879
- self.use_mea_attention = use_mea_attention
880
  self.mea_component_key_value_heads = (
881
  num_key_value_heads
882
  if mea_component_key_value_heads is None
883
  else int(mea_component_key_value_heads)
884
  )
885
- self.mea_groupnorm_eps = mea_groupnorm_eps
886
- self.use_lucid_attention = use_lucid_attention
887
- self.lucid_attention_eps = lucid_attention_eps
888
- self.use_affine_scaled_attention = use_affine_scaled_attention
889
- self.affine_momentum = affine_momentum
890
- self.use_xsa = use_xsa
891
- self.xsa_eps = xsa_eps
892
- # ── Directional Routing ───────────────────────────────────────────
893
- self.use_directional_routing = use_directional_routing
894
- self.directional_routing_k = directional_routing_k
895
- self.directional_routing_temp = directional_routing_temp
896
 
897
  # ── Attention Residuals ───────────────────────────────────────────
898
  # use_attn_res=True: replace fixed residual accumulation with learned
@@ -903,36 +911,36 @@ class NeoLLMConfig(PretrainedConfig):
903
  # block_size = num_hidden_layers // 4 = 3 layers per block.
904
  # Memory cost: O(num_blocks × batch × seq × hidden) instead of
905
  # O(num_layers × batch × seq × hidden).
906
- self.use_attn_res = use_attn_res
907
- self.attn_res_num_blocks = attn_res_num_blocks
908
 
909
  # ── StackMemory / STACKTRANS ──────────────────────────────────────
910
- self.use_stack_memory = use_stack_memory
911
- self.stack_d_model = stack_d_model
912
- self.num_mem_heads = num_mem_heads
913
- self.stack_slots = stack_slots
914
- self.stack_memory_cache_size = stack_memory_cache_size
915
  rope_config_validation(self)
916
 
917
  # ── FANformer periodicity ─────────────────────────────────────────
918
- self.use_fan_residual = use_fan_residual
919
- self.fan_ratio = fan_ratio
920
- self.fan_ratio_ffn = fan_ratio_ffn
921
 
922
  # ── Regularization ────────────────────────────────────────────────
923
- self.dropout_rate = dropout_rate
924
 
925
  # ── Learnable Multipliers ─────────────────────────────────────────
926
- self.use_learnable_multipliers = use_learnable_multipliers
927
- self.use_embedding_multipliers = use_embedding_multipliers
928
 
929
  # ── Normalization controls ────────────────────────────────────────
930
- self.use_lns = bool(use_lns)
931
- self.use_gpas = bool(use_gpas)
932
- self.use_siamesenorm = bool(use_siamesenorm)
933
- self.siamese_normalized_input = bool(siamese_normalized_input)
934
- self.siamese_depth_scaling = bool(siamese_depth_scaling)
935
- self.siamese_attn_x_scale_init = float(siamese_attn_x_scale_init)
936
 
937
  # ── Embedding input normalization ─────────────────────────────────
938
  # True preserves the previous layer-0 pre-norm path, using the active
@@ -941,48 +949,48 @@ class NeoLLMConfig(PretrainedConfig):
941
  # False removes that first input-norm module entirely, so raw embeddings
942
  # enter the first attention block and the layer-0 input-norm parameters
943
  # are not counted; layers >= 1 keep their pre-norm unchanged.
944
- self.use_embedding_input_norm = use_embedding_input_norm
945
 
946
  # ── Leviathan generator ───────────────────────────────────────────
947
- self.use_token_generator = use_token_generator
948
- self.generator_d_seed = generator_d_seed
949
- self.generator_num_modes = generator_num_modes
950
- self.generator_num_knots = generator_num_knots
951
- self.generator_spline_degree = generator_spline_degree
952
- self.generator_k = generator_k
953
- self.generator_krank = generator_krank
954
 
955
  # ── Leviathan-JTok-M ─────────────────────────────────────────────
956
- self.use_jtokm = use_jtokm
957
- self.jtokm_num_experts = jtokm_num_experts
958
- self.jtokm_top_k = jtokm_top_k
959
- self.jtokm_num_modes = jtokm_num_modes
960
- self.jtokm_aux_loss_weight = jtokm_aux_loss_weight
961
- self.jtokm_norm_eps = jtokm_norm_eps
962
 
963
  # ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
964
- self.use_hadamard_o_proj = use_hadamard_o_proj
965
 
966
  # ── PolyNorm exclusivity ────────────���─────────────────────────────
967
- self.polynorm_exclusive = polynorm_exclusive
968
 
969
  # ── Spelling Bee Embeddings (Rabe et al., 2026) ───────────────────
970
- self.use_spelling_bee_embeddings = use_spelling_bee_embeddings
971
 
972
  # ── Context Re-Positioning (Li et al., 2026) ──────────────────────
973
- self.use_repo = use_repo
974
- self.repo_start_layer = repo_start_layer
975
- self.repo_d_p = repo_d_p
976
- self.use_repo_grape = use_repo_grape
977
- self.use_repo_goat_prior = use_repo_goat_prior
978
- self.repo_goat_num_frequencies = int(repo_goat_num_frequencies)
979
- self.repo_goat_sink_decay = float(repo_goat_sink_decay)
980
 
981
  # ── LAuReL: Learned Augmented Residual Layer (Menghani et al., 2025) ─
982
- self.use_laurel = use_laurel
983
- self.use_laurel_rw = use_laurel_rw
984
- self.use_laurel_lr = use_laurel_lr
985
- self.laurel_lr_rank = laurel_lr_rank
986
 
987
  # ── Interleaved Head Attention (Duvvuri et al., 2026) ─────────────
988
  # use_iha=True: enables learned cross-head mixing of Q, K, V.
@@ -1011,17 +1019,17 @@ class NeoLLMConfig(PretrainedConfig):
1011
  # True → G layers instantiate and run global IHA over the expanded
1012
  # sequence; useful only as an explicit high-cost ablation.
1013
  # Init: identity (IHA ≡ MHA at step 0, Theorem 2 inclusion proof).
1014
- self.use_iha = use_iha
1015
- self.iha_num_pseudo_heads = iha_num_pseudo_heads
1016
- self.iha_local_global_pattern = iha_local_global_pattern
1017
- self.iha_sliding_window = iha_sliding_window
1018
- self.iha_global_layers_use_iha = bool(iha_global_layers_use_iha)
1019
 
1020
  self.auto_map = {
1021
- "AutoConfig": "configuration_neollm.NeoLLMConfig",
1022
- "AutoModel": "modeling_neollm.NeoLLMModel",
1023
  "AutoModelForCausalLM": "modeling_neollm.NeoLLMForCausalLM",
1024
  }
1025
 
1026
 
1027
- __all__ = ["NeoLLMConfig"]
 
1
+ # mypy: ignore-errors
2
  # ==================== configuration_neollm.py ====================
3
  from transformers.configuration_utils import PretrainedConfig
4
  from transformers.modeling_rope_utils import rope_config_validation
 
637
  attn_res_num_blocks=4,
638
  # ── StackMemory / STACKTRANS (Zhang et al., NeurIPS 2025) ───────
639
  use_stack_memory=False,
640
+ stack_d_model=32, # H * d_s = 4 * 16
641
+ num_mem_heads=4, # H = 4
642
+ stack_slots=16, # S = 24
643
  stack_memory_cache_size=2048,
644
  # ── ResFormer cross-layer FAN residual (He et al., 2023) ─────────
645
  use_fan_residual=False,
 
694
  laurel_lr_rank=32,
695
  # ── Interleaved Head Attention (Duvvuri et al., 2026) ─────────────
696
  use_iha=True,
697
+ iha_num_pseudo_heads=2, # P=2 → 2×2=4 patrones por head
698
+ iha_local_global_pattern="LLLLG", # 4 locales + 1 global (paper §5.1)
699
+ iha_sliding_window=None, # auto = N // (2*P^2) usando la longitud real del batch
700
  iha_global_layers_use_iha=False, # False replica el paper: la capa G es global estándar, sin IHA
701
  **kwargs,
702
  ):
 
759
  repo_start_layer = num_hidden_layers // 3
760
  if repo_d_p is None:
761
  repo_d_p = hidden_size // 8
762
+ if (use_repo or use_repo_grape or use_repo_goat_prior) and not (
763
+ 0 <= repo_start_layer < num_hidden_layers
764
+ ):
765
  raise ValueError(
766
  f"`repo_start_layer` must satisfy "
767
  f"0 <= repo_start_layer < num_hidden_layers, "
 
778
  )
779
  if repo_goat_sink_decay <= 0.0:
780
  raise ValueError(
781
+ f"`repo_goat_sink_decay` must be > 0, got {repo_goat_sink_decay}."
 
782
  )
783
  _repo_goat_prior_dim = 2 * int(repo_goat_num_frequencies) + 2
784
  if (head_dim + _repo_goat_prior_dim) % 8 != 0:
 
787
  "head_dim=%d, giving internal attention dim=%d. Some "
788
  "FlashAttention builds prefer dimensions divisible by 8; "
789
  "the default repo_goat_num_frequencies=3 gives +8.",
790
+ _repo_goat_prior_dim,
791
+ head_dim,
792
+ head_dim + _repo_goat_prior_dim,
793
  )
794
  # ── SiameseNorm: RMS-only topology by construction ────────────────
795
 
 
807
  if use_iha:
808
  _iha_pattern = str(iha_local_global_pattern).upper().strip()
809
  if not _iha_pattern:
810
+ raise ValueError(
811
+ "`iha_local_global_pattern` must not be empty when `use_iha=True`."
812
+ )
813
  _bad_iha_pattern_chars = sorted(set(_iha_pattern) - {"L", "G"})
814
  if _bad_iha_pattern_chars:
815
  raise ValueError(
 
827
  "use_stack_memory": bool(use_stack_memory),
828
  "use_siamesenorm": bool(use_siamesenorm),
829
  }
830
+ _active_residual_flow = [
831
+ name for name, enabled in _residual_flow_flags.items() if enabled
832
+ ]
833
  if len(_active_residual_flow) > 1:
834
  raise ValueError(
835
  "Enable at most one residual-flow mechanism among "
 
862
  super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
863
 
864
  # ── Core Transformer ──────────────────────────────────────────────
865
+ self.vocab_size = vocab_size
866
  self.max_position_embeddings = max_position_embeddings
867
+ self.hidden_size = hidden_size
868
+ self.intermediate_size = intermediate_size
869
+ self.num_hidden_layers = num_hidden_layers
870
+ self.num_attention_heads = num_attention_heads
871
+ self.num_key_value_heads = num_key_value_heads
872
+ self.hidden_act = hidden_act
873
+ self.initializer_range = initializer_range
874
+ self.rms_norm_eps = rms_norm_eps
875
 
876
  # ── Positional encoding ───────────────────────────────────────────
877
+ self.rope_theta = rope_theta
878
+ self.rope_scaling = rope_scaling
879
+ self.partial_rotary_factor = partial_rotary_factor
880
 
881
  # ── Attention ─────────────────────────────────────────────────────
882
+ self.attention_bias = attention_bias
883
+ self.attention_dropout = attention_dropout
884
+ self.head_dim = head_dim
885
+ self.use_momentum_attention = use_momentum_attention
886
+ self.momentum_gamma = momentum_gamma
887
+ self.use_mea_attention = use_mea_attention
888
  self.mea_component_key_value_heads = (
889
  num_key_value_heads
890
  if mea_component_key_value_heads is None
891
  else int(mea_component_key_value_heads)
892
  )
893
+ self.mea_groupnorm_eps = mea_groupnorm_eps
894
+ self.use_lucid_attention = use_lucid_attention
895
+ self.lucid_attention_eps = lucid_attention_eps
896
+ self.use_affine_scaled_attention = use_affine_scaled_attention
897
+ self.affine_momentum = affine_momentum
898
+ self.use_xsa = use_xsa
899
+ self.xsa_eps = xsa_eps
900
+ # ── Directional Routing ───────────────────────────────────────────
901
+ self.use_directional_routing = use_directional_routing
902
+ self.directional_routing_k = directional_routing_k
903
+ self.directional_routing_temp = directional_routing_temp
904
 
905
  # ── Attention Residuals ───────────────────────────────────────────
906
  # use_attn_res=True: replace fixed residual accumulation with learned
 
911
  # block_size = num_hidden_layers // 4 = 3 layers per block.
912
  # Memory cost: O(num_blocks × batch × seq × hidden) instead of
913
  # O(num_layers × batch × seq × hidden).
914
+ self.use_attn_res = use_attn_res
915
+ self.attn_res_num_blocks = attn_res_num_blocks
916
 
917
  # ── StackMemory / STACKTRANS ──────────────────────────────────────
918
+ self.use_stack_memory = use_stack_memory
919
+ self.stack_d_model = stack_d_model
920
+ self.num_mem_heads = num_mem_heads
921
+ self.stack_slots = stack_slots
922
+ self.stack_memory_cache_size = stack_memory_cache_size
923
  rope_config_validation(self)
924
 
925
  # ── FANformer periodicity ─────────────────────────────────────────
926
+ self.use_fan_residual = use_fan_residual
927
+ self.fan_ratio = fan_ratio
928
+ self.fan_ratio_ffn = fan_ratio_ffn
929
 
930
  # ── Regularization ────────────────────────────────────────────────
931
+ self.dropout_rate = dropout_rate
932
 
933
  # ── Learnable Multipliers ─────────────────────────────────────────
934
+ self.use_learnable_multipliers = use_learnable_multipliers
935
+ self.use_embedding_multipliers = use_embedding_multipliers
936
 
937
  # ── Normalization controls ────────────────────────────────────────
938
+ self.use_lns = bool(use_lns)
939
+ self.use_gpas = bool(use_gpas)
940
+ self.use_siamesenorm = bool(use_siamesenorm)
941
+ self.siamese_normalized_input = bool(siamese_normalized_input)
942
+ self.siamese_depth_scaling = bool(siamese_depth_scaling)
943
+ self.siamese_attn_x_scale_init = float(siamese_attn_x_scale_init)
944
 
945
  # ── Embedding input normalization ─────────────────────────────────
946
  # True preserves the previous layer-0 pre-norm path, using the active
 
949
  # False removes that first input-norm module entirely, so raw embeddings
950
  # enter the first attention block and the layer-0 input-norm parameters
951
  # are not counted; layers >= 1 keep their pre-norm unchanged.
952
+ self.use_embedding_input_norm = use_embedding_input_norm
953
 
954
  # ── Leviathan generator ───────────────────────────────────────────
955
+ self.use_token_generator = use_token_generator
956
+ self.generator_d_seed = generator_d_seed
957
+ self.generator_num_modes = generator_num_modes
958
+ self.generator_num_knots = generator_num_knots
959
+ self.generator_spline_degree = generator_spline_degree
960
+ self.generator_k = generator_k
961
+ self.generator_krank = generator_krank
962
 
963
  # ── Leviathan-JTok-M ─────────────────────────────────────────────
964
+ self.use_jtokm = use_jtokm
965
+ self.jtokm_num_experts = jtokm_num_experts
966
+ self.jtokm_top_k = jtokm_top_k
967
+ self.jtokm_num_modes = jtokm_num_modes
968
+ self.jtokm_aux_loss_weight = jtokm_aux_loss_weight
969
+ self.jtokm_norm_eps = jtokm_norm_eps
970
 
971
  # ── Hadamard output projection (Aggarwal & Kumar, 2026) ───────────
972
+ self.use_hadamard_o_proj = use_hadamard_o_proj
973
 
974
  # ── PolyNorm exclusivity ────────────���─────────────────────────────
975
+ self.polynorm_exclusive = polynorm_exclusive
976
 
977
  # ── Spelling Bee Embeddings (Rabe et al., 2026) ───────────────────
978
+ self.use_spelling_bee_embeddings = use_spelling_bee_embeddings
979
 
980
  # ── Context Re-Positioning (Li et al., 2026) ──────────────────────
981
+ self.use_repo = use_repo
982
+ self.repo_start_layer = repo_start_layer
983
+ self.repo_d_p = repo_d_p
984
+ self.use_repo_grape = use_repo_grape
985
+ self.use_repo_goat_prior = use_repo_goat_prior
986
+ self.repo_goat_num_frequencies = int(repo_goat_num_frequencies)
987
+ self.repo_goat_sink_decay = float(repo_goat_sink_decay)
988
 
989
  # ── LAuReL: Learned Augmented Residual Layer (Menghani et al., 2025) ─
990
+ self.use_laurel = use_laurel
991
+ self.use_laurel_rw = use_laurel_rw
992
+ self.use_laurel_lr = use_laurel_lr
993
+ self.laurel_lr_rank = laurel_lr_rank
994
 
995
  # ── Interleaved Head Attention (Duvvuri et al., 2026) ─────────────
996
  # use_iha=True: enables learned cross-head mixing of Q, K, V.
 
1019
  # True → G layers instantiate and run global IHA over the expanded
1020
  # sequence; useful only as an explicit high-cost ablation.
1021
  # Init: identity (IHA ≡ MHA at step 0, Theorem 2 inclusion proof).
1022
+ self.use_iha = use_iha
1023
+ self.iha_num_pseudo_heads = iha_num_pseudo_heads
1024
+ self.iha_local_global_pattern = iha_local_global_pattern
1025
+ self.iha_sliding_window = iha_sliding_window
1026
+ self.iha_global_layers_use_iha = bool(iha_global_layers_use_iha)
1027
 
1028
  self.auto_map = {
1029
+ "AutoConfig": "configuration_neollm.NeoLLMConfig",
1030
+ "AutoModel": "modeling_neollm.NeoLLMModel",
1031
  "AutoModelForCausalLM": "modeling_neollm.NeoLLMForCausalLM",
1032
  }
1033
 
1034
 
1035
+ __all__ = ["NeoLLMConfig"]