|
| 1 | +# Copyright (c) 2025 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, layer_type_validation |
| 16 | +from ..configuration_utils import PretrainedConfig |
| 17 | + |
| 18 | +# from ...modeling_rope_utils import rope_config_validation |
| 19 | + |
| 20 | + |
| 21 | +class GptOssConfig(PretrainedConfig): |
| 22 | + r""" |
| 23 | + This will yield a configuration to that of the BERT |
| 24 | + [google-bert/bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased) architecture. |
| 25 | + """ |
| 26 | + |
| 27 | + model_type = "gpt_oss" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + num_hidden_layers: int = 24, |
| 32 | + num_local_experts: int = 128, |
| 33 | + vocab_size: int = 201088, |
| 34 | + hidden_size: int = 2880, |
| 35 | + intermediate_size: int = 2880, |
| 36 | + head_dim: int = 64, |
| 37 | + num_attention_heads: int = 64, |
| 38 | + num_key_value_heads: int = 8, |
| 39 | + sliding_window: int = 128, |
| 40 | + rope_theta: float = 150000.0, |
| 41 | + tie_word_embeddings=False, |
| 42 | + hidden_act: str = "silu", |
| 43 | + initializer_range: float = 0.02, |
| 44 | + max_position_embeddings=131072, |
| 45 | + rms_norm_eps: float = 1e-5, |
| 46 | + rope_scaling={"rope_type": "yarn", "factor": 32.0, "beta_fast": 32.0, "beta_slow": 1.0, "truncate": False}, |
| 47 | + attention_dropout: float = 0.0, |
| 48 | + num_experts_per_tok=4, |
| 49 | + router_aux_loss_coef: float = 0.9, |
| 50 | + output_router_logits=False, |
| 51 | + use_cache=True, |
| 52 | + layer_types=None, |
| 53 | + **kwargs, |
| 54 | + ): |
| 55 | + self.vocab_size = vocab_size |
| 56 | + self.hidden_size = hidden_size |
| 57 | + self.intermediate_size = intermediate_size |
| 58 | + self.num_hidden_layers = num_hidden_layers |
| 59 | + self.num_attention_heads = num_attention_heads |
| 60 | + self.num_experts = num_local_experts |
| 61 | + self.sliding_window = sliding_window |
| 62 | + self.num_experts_per_tok = num_experts_per_tok |
| 63 | + # for backward compatibility |
| 64 | + if num_key_value_heads is None: |
| 65 | + num_key_value_heads = num_attention_heads |
| 66 | + |
| 67 | + self.num_key_value_heads = num_key_value_heads |
| 68 | + self.hidden_act = hidden_act |
| 69 | + self.initializer_range = initializer_range |
| 70 | + self.rms_norm_eps = rms_norm_eps |
| 71 | + self.rope_theta = rope_theta |
| 72 | + self.rope_scaling = rope_scaling |
| 73 | + self.attention_dropout = attention_dropout |
| 74 | + self.head_dim = head_dim if head_dim is not None else self.hidden_size // self.num_attention_heads |
| 75 | + self.layer_types = layer_types |
| 76 | + if self.layer_types is None: |
| 77 | + self.layer_types = [ |
| 78 | + "sliding_attention" if bool((i + 1) % 2) else "full_attention" for i in range(self.num_hidden_layers) |
| 79 | + ] |
| 80 | + # layer_type_validation(self.layer_types) |
| 81 | + |
| 82 | + # Validate the correctness of rotary position embeddings parameters |
| 83 | + # BC: if there is a 'type' field, copy it it to 'rope_type'. |
| 84 | + if self.rope_scaling is not None and "type" in self.rope_scaling: |
| 85 | + self.rope_scaling["rope_type"] = self.rope_scaling["type"] |
| 86 | + # rope_config_validation(self) |
| 87 | + |
| 88 | + self.attention_bias = True |
| 89 | + self.max_position_embeddings = max_position_embeddings |
| 90 | + self.router_aux_loss_coef = router_aux_loss_coef |
| 91 | + self.output_router_logits = output_router_logits |
| 92 | + self.use_cache = use_cache |
| 93 | + self.use_bias = False |
| 94 | + |
| 95 | + super().__init__( |
| 96 | + tie_word_embeddings=tie_word_embeddings, |
| 97 | + **kwargs, |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +__all__ = ["GptOssConfig"] |
0 commit comments