configuration_nanochat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2025 The HuggingFace Inc. team. 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. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PretrainedConfig
  16. from ...modeling_rope_utils import RopeParameters
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="karpathy/nanochat-d32")
  19. @strict
  20. class NanoChatConfig(PretrainedConfig):
  21. r"""
  22. Example:
  23. ```python
  24. >>> from transformers import NanoChatModel, NanoChatConfig
  25. >>> # Initializing a NanoChat style configuration
  26. >>> configuration = NanoChatConfig()
  27. >>> # Initializing a model from the NanoChat style configuration
  28. >>> model = NanoChatModel(configuration)
  29. >>> # Accessing the model configuration
  30. >>> configuration = model.config
  31. ```"""
  32. model_type = "nanochat"
  33. keys_to_ignore_at_inference = ["past_key_values"]
  34. base_model_tp_plan = {
  35. "layers.*.self_attn.q_proj": "colwise",
  36. "layers.*.self_attn.k_proj": "colwise",
  37. "layers.*.self_attn.v_proj": "colwise",
  38. "layers.*.self_attn.o_proj": "rowwise",
  39. "layers.*.mlp.fc1": "colwise",
  40. "layers.*.mlp.fc2": "rowwise",
  41. }
  42. vocab_size: int = 50304
  43. hidden_size: int = 768
  44. intermediate_size: int = 8192
  45. num_hidden_layers: int = 12
  46. num_attention_heads: int = 6
  47. num_key_value_heads: int | None = None
  48. max_position_embeddings: int = 2048
  49. hidden_act: str = "relu2"
  50. attention_dropout: float | int = 0.0
  51. rms_norm_eps: float = 1e-6
  52. initializer_range: float = 0.02
  53. rope_parameters: RopeParameters | dict | None = None
  54. use_cache: bool = True
  55. final_logit_softcapping: float | None = 15.0
  56. attention_bias: bool = False
  57. bos_token_id: int | None = 0
  58. eos_token_id: int | list[int] | None = 1
  59. pad_token_id: int | None = 1
  60. tie_word_embeddings: bool = False
  61. def __post_init__(self, **kwargs):
  62. if self.num_key_value_heads is None:
  63. self.num_key_value_heads = self.num_attention_heads
  64. super().__post_init__(**kwargs)
  65. __all__ = ["NanoChatConfig"]