LMConfig.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from transformers import PretrainedConfig
  2. from typing import List
  3. class LMConfig(PretrainedConfig):
  4. model_type = "minimind"
  5. def __init__(
  6. self,
  7. dim: int = 512,
  8. n_layers: int = 8,
  9. n_heads: int = 8,
  10. n_kv_heads: int = 2,
  11. vocab_size: int = 6400,
  12. hidden_dim: int = None,
  13. multiple_of: int = 64,
  14. norm_eps: float = 1e-5,
  15. max_seq_len: int = 8192,
  16. rope_theta: int = 1e6,
  17. dropout: float = 0.0,
  18. flash_attn: bool = True,
  19. ####################################################
  20. # Here are the specific configurations of MOE
  21. # When use_moe is false, the following is invalid
  22. ####################################################
  23. use_moe: bool = False,
  24. ####################################################
  25. num_experts_per_tok: int = 2,
  26. n_routed_experts: int = 4,
  27. n_shared_experts: bool = True,
  28. scoring_func: str = 'softmax',
  29. aux_loss_alpha: float = 0.1,
  30. seq_aux: bool = True,
  31. norm_topk_prob: bool = True,
  32. **kwargs,
  33. ):
  34. self.dim = dim
  35. self.n_layers = n_layers
  36. self.n_heads = n_heads
  37. self.n_kv_heads = n_kv_heads
  38. self.vocab_size = vocab_size
  39. self.hidden_dim = hidden_dim
  40. self.multiple_of = multiple_of
  41. self.norm_eps = norm_eps
  42. self.max_seq_len = max_seq_len
  43. self.rope_theta = rope_theta
  44. self.dropout = dropout
  45. self.flash_attn = flash_attn
  46. ####################################################
  47. # Here are the specific configurations of MOE
  48. # When use_moe is false, the following is invalid
  49. ####################################################
  50. self.use_moe = use_moe
  51. self.num_experts_per_tok = num_experts_per_tok # 每个token选择的专家数量
  52. self.n_routed_experts = n_routed_experts # 总的专家数量
  53. self.n_shared_experts = n_shared_experts # 共享专家
  54. self.scoring_func = scoring_func # 评分函数,默认为'softmax'
  55. self.aux_loss_alpha = aux_loss_alpha # 辅助损失的alpha参数
  56. self.seq_aux = seq_aux # 是否在序列级别上计算辅助损失
  57. self.norm_topk_prob = norm_topk_prob # 是否标准化top-k概率
  58. super().__init__(**kwargs)