configuration_cohere2.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/cohere2/modular_cohere2.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_cohere2.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2024 Cohere Inc. HuggingFace Inc. team. All rights reserved.
  8. #
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License");
  11. # you may not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS,
  18. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. from huggingface_hub.dataclasses import strict
  22. from ...configuration_utils import PreTrainedConfig
  23. from ...modeling_rope_utils import RopeParameters
  24. from ...utils import auto_docstring
  25. @auto_docstring(checkpoint="CohereForAI/c4ai-command-r-v01")
  26. @strict
  27. class Cohere2Config(PreTrainedConfig):
  28. r"""
  29. logit_scale (`float`, *optional*, defaults to 0.0625):
  30. The scaling factor for the output logits.
  31. ```python
  32. >>> from transformers import Cohere2Model, Cohere2Config
  33. >>> # Initializing a Cohere Nextmodel configuration
  34. >>> configuration = Cohere2Config()
  35. >>> # Initializing a model from the Cohere2 configuration
  36. >>> model = Cohere2Model(configuration) # doctest: +SKIP
  37. >>> # Accessing the model configuration
  38. >>> configuration = model.config # doctest: +SKIP
  39. ```
  40. """
  41. model_type = "cohere2"
  42. keys_to_ignore_at_inference = ["past_key_values"]
  43. base_model_tp_plan = {
  44. "layers.*.self_attn.q_proj": "colwise",
  45. "layers.*.self_attn.k_proj": "colwise",
  46. "layers.*.self_attn.v_proj": "colwise",
  47. "layers.*.self_attn.o_proj": "rowwise",
  48. "layers.*.mlp.gate_proj": "colwise",
  49. "layers.*.mlp.up_proj": "colwise",
  50. "layers.*.mlp.down_proj": "rowwise",
  51. }
  52. base_model_pp_plan = {
  53. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  54. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  55. "norm": (["hidden_states"], ["hidden_states"]),
  56. }
  57. vocab_size: int = 256000
  58. hidden_size: int = 8192
  59. intermediate_size: int = 22528
  60. logit_scale: float = 0.0625
  61. num_hidden_layers: int = 40
  62. num_attention_heads: int = 64
  63. num_key_value_heads: int | None = None
  64. hidden_act: str = "silu"
  65. max_position_embeddings: int = 8192
  66. initializer_range: float = 0.02
  67. layer_norm_eps: float = 1e-5
  68. use_cache: bool = True
  69. pad_token_id: int | None = 0
  70. bos_token_id: int | None = 5
  71. eos_token_id: int | list[int] | None = 255001
  72. tie_word_embeddings: bool = True
  73. rope_parameters: RopeParameters | dict | None = None
  74. attention_bias: bool = False
  75. attention_dropout: float | int = 0.0
  76. sliding_window: int | None = 4096
  77. layer_types: list[str] | None = None
  78. def __post_init__(self, **kwargs):
  79. if self.num_key_value_heads is None:
  80. self.num_key_value_heads = self.num_attention_heads
  81. # Need to specify head_dim in the config so it can be used in the attention forward functions
  82. self.head_dim = self.hidden_size // self.num_attention_heads
  83. # BC -> the pattern used to be a simple int, and it's still present in configs on the Hub
  84. if self.layer_types is None:
  85. # BC -> the pattern used to be a simple int, and it's still present in configs on the Hub
  86. _sliding_window_pattern = kwargs.pop("sliding_window_pattern", 4)
  87. self.layer_types = [
  88. "sliding_attention" if bool((i + 1) % _sliding_window_pattern) else "full_attention"
  89. for i in range(self.num_hidden_layers)
  90. ]
  91. super().__post_init__(**kwargs)
  92. __all__ = ["Cohere2Config"]