|
| 1 | +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from ..configuration_utils import PretrainedConfig |
| 16 | +from ..modeling_rope_utils import rope_config_validation, standardize_rope_params |
| 17 | + |
| 18 | + |
| 19 | +class GraniteConfig(PretrainedConfig): |
| 20 | + model_type = "granitemoehybrid" |
| 21 | + tokenizer_class = "GPT2Tokenizer" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + vocab_size=100352, |
| 26 | + hidden_size=1024, |
| 27 | + intermediate_size=2048, |
| 28 | + shared_intermediate_size=2048, |
| 29 | + max_position_embeddings=32768, |
| 30 | + num_hidden_layers=28, |
| 31 | + num_attention_heads=16, |
| 32 | + num_key_value_heads=4, |
| 33 | + initializer_range=0.1, |
| 34 | + rms_norm_eps=1e-5, |
| 35 | + use_cache=True, |
| 36 | + pad_token_id=100256, |
| 37 | + bos_token_id=100257, |
| 38 | + eos_token_id=100257, |
| 39 | + hidden_act="silu", |
| 40 | + attention_bias=False, |
| 41 | + attention_dropout=0.0, |
| 42 | + mlp_bias=False, |
| 43 | + head_dim=None, |
| 44 | + tie_word_embeddings=True, |
| 45 | + # MuP scaling factors |
| 46 | + embedding_multiplier=1.0, |
| 47 | + attention_multiplier=1.0, |
| 48 | + residual_multiplier=1.0, |
| 49 | + logits_scaling=1.0, |
| 50 | + # MoE (unused for 350m-base, all zero) |
| 51 | + num_local_experts=0, |
| 52 | + num_experts_per_tok=0, |
| 53 | + output_router_logits=False, |
| 54 | + router_aux_loss_coef=0.01, |
| 55 | + # Mamba (unused for 350m-base, all attention layers) |
| 56 | + mamba_n_heads=128, |
| 57 | + mamba_n_groups=1, |
| 58 | + mamba_d_state=256, |
| 59 | + mamba_d_head="auto", |
| 60 | + mamba_d_conv=4, |
| 61 | + mamba_expand=2, |
| 62 | + mamba_chunk_size=256, |
| 63 | + mamba_conv_bias=True, |
| 64 | + mamba_proj_bias=False, |
| 65 | + layer_types=None, |
| 66 | + position_embedding_type="rope", |
| 67 | + rope_theta=10000000.0, |
| 68 | + rope_scaling=None, |
| 69 | + **kwargs, |
| 70 | + ): |
| 71 | + self.vocab_size = vocab_size |
| 72 | + self.max_position_embeddings = max_position_embeddings |
| 73 | + self.hidden_size = hidden_size |
| 74 | + self.intermediate_size = intermediate_size |
| 75 | + self.shared_intermediate_size = shared_intermediate_size |
| 76 | + self.num_hidden_layers = num_hidden_layers |
| 77 | + self.num_attention_heads = num_attention_heads |
| 78 | + |
| 79 | + if num_key_value_heads is None: |
| 80 | + num_key_value_heads = num_attention_heads |
| 81 | + self.num_key_value_heads = num_key_value_heads |
| 82 | + |
| 83 | + self.hidden_act = hidden_act |
| 84 | + self.initializer_range = initializer_range |
| 85 | + self.rms_norm_eps = rms_norm_eps |
| 86 | + self.use_cache = use_cache |
| 87 | + self.attention_bias = attention_bias |
| 88 | + self.attention_dropout = attention_dropout |
| 89 | + self.mlp_bias = mlp_bias |
| 90 | + self.head_dim = head_dim if head_dim is not None else self.hidden_size // self.num_attention_heads |
| 91 | + |
| 92 | + # MuP scaling factors |
| 93 | + self.embedding_multiplier = embedding_multiplier |
| 94 | + self.attention_multiplier = attention_multiplier |
| 95 | + self.residual_multiplier = residual_multiplier |
| 96 | + self.logits_scaling = logits_scaling |
| 97 | + |
| 98 | + # MoE params |
| 99 | + self.num_local_experts = num_local_experts |
| 100 | + self.num_experts_per_tok = num_experts_per_tok |
| 101 | + self.output_router_logits = output_router_logits |
| 102 | + self.router_aux_loss_coef = router_aux_loss_coef |
| 103 | + |
| 104 | + # Mamba params (for architecture compatibility, unused in 350m-base) |
| 105 | + self.mamba_n_heads = mamba_n_heads |
| 106 | + self.mamba_n_groups = mamba_n_groups |
| 107 | + self.mamba_d_state = mamba_d_state |
| 108 | + self.mamba_d_head = mamba_d_head |
| 109 | + self.mamba_d_conv = mamba_d_conv |
| 110 | + self.mamba_expand = mamba_expand |
| 111 | + self.mamba_chunk_size = mamba_chunk_size |
| 112 | + self.mamba_conv_bias = mamba_conv_bias |
| 113 | + self.mamba_proj_bias = mamba_proj_bias |
| 114 | + |
| 115 | + # Layer types |
| 116 | + if layer_types is None: |
| 117 | + layer_types = ["attention"] * num_hidden_layers |
| 118 | + if any(layer_type != "attention" for layer_type in layer_types): |
| 119 | + raise ValueError("GraniteForCausalLM supports attention-only Granite configurations.") |
| 120 | + if num_local_experts or num_experts_per_tok: |
| 121 | + raise ValueError("GraniteForCausalLM does not support MoE configurations.") |
| 122 | + self.layer_types = layer_types |
| 123 | + self.position_embedding_type = position_embedding_type |
| 124 | + |
| 125 | + # RoPE |
| 126 | + self.rope_theta = rope_theta |
| 127 | + self.rope_scaling = rope_scaling |
| 128 | + if self.rope_scaling is not None and "type" in self.rope_scaling: |
| 129 | + self.rope_scaling["rope_type"] = self.rope_scaling["type"] |
| 130 | + self.rope_parameters = self.rope_scaling if self.rope_scaling is not None else {"rope_type": "default", "rope_theta": rope_theta} |
| 131 | + standardize_rope_params(self, rope_theta=self.rope_theta) |
| 132 | + rope_config_validation(self) |
| 133 | + |
| 134 | + super().__init__( |
| 135 | + pad_token_id=pad_token_id, |
| 136 | + bos_token_id=bos_token_id, |
| 137 | + eos_token_id=eos_token_id, |
| 138 | + tie_word_embeddings=tie_word_embeddings, |
| 139 | + **kwargs, |
| 140 | + ) |
0 commit comments