configuration_phi.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2023 Microsoft and 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. """Phi model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...modeling_rope_utils import RopeParameters
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="microsoft/phi-1")
  20. @strict
  21. class PhiConfig(PreTrainedConfig):
  22. r"""
  23. qk_layernorm (`bool`, *optional*, defaults to `False`):
  24. Whether or not to normalize the Queries and Keys after projecting the hidden states.
  25. Example:
  26. ```python
  27. >>> from transformers import PhiModel, PhiConfig
  28. >>> # Initializing a Phi-1 style configuration
  29. >>> configuration = PhiConfig.from_pretrained("microsoft/phi-1")
  30. >>> # Initializing a model from the configuration
  31. >>> model = PhiModel(configuration)
  32. >>> # Accessing the model configuration
  33. >>> configuration = model.config
  34. ```"""
  35. model_type = "phi"
  36. keys_to_ignore_at_inference = ["past_key_values"]
  37. base_model_tp_plan = {
  38. "layers.*.self_attn.q_proj": "colwise",
  39. "layers.*.self_attn.k_proj": "colwise",
  40. "layers.*.self_attn.v_proj": "colwise",
  41. "layers.*.self_attn.dense": "rowwise",
  42. "layers.*.mlp.fc1": "colwise",
  43. "layers.*.mlp.fc2": "rowwise",
  44. }
  45. base_model_pp_plan = {
  46. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  47. "embed_dropout": (["inputs_embeds"], ["inputs_embeds"]),
  48. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  49. "final_layernorm": (["hidden_states"], ["hidden_states"]),
  50. }
  51. vocab_size: int = 51200
  52. hidden_size: int = 2048
  53. intermediate_size: int = 8192
  54. num_hidden_layers: int = 24
  55. num_attention_heads: int = 32
  56. num_key_value_heads: int | None = None
  57. resid_pdrop: float | int = 0.0
  58. embd_pdrop: float | int = 0.0
  59. attention_dropout: float | int | None = 0.0
  60. hidden_act: str = "gelu_new"
  61. max_position_embeddings: int = 2048
  62. initializer_range: float = 0.02
  63. layer_norm_eps: float = 1e-5
  64. use_cache: bool = True
  65. tie_word_embeddings: bool = False
  66. rope_parameters: RopeParameters | dict | None = None
  67. qk_layernorm: bool = False
  68. bos_token_id: int | None = 1
  69. eos_token_id: int | list[int] | None = 2
  70. pad_token_id: int | None = None
  71. def __post_init__(self, **kwargs):
  72. if self.num_key_value_heads is None:
  73. self.num_key_value_heads = self.num_attention_heads
  74. kwargs.setdefault("partial_rotary_factor", 0.5) # assign default for BC
  75. super().__post_init__(**kwargs)
  76. __all__ = ["PhiConfig"]