LMConfig.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 = 16,
  10. n_kv_heads: int = 8,
  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 = 512,
  16. dropout: float = 0.0,
  17. flash_attn: bool = False,
  18. ####################################################
  19. # Here are the specific configurations of MOE
  20. # When use_moe is false, the following is invalid
  21. ####################################################
  22. use_moe: bool = False,
  23. num_experts_per_tok=2,
  24. n_routed_experts=4,
  25. n_shared_experts: bool = True,
  26. scoring_func='softmax',
  27. aux_loss_alpha=0.01,
  28. seq_aux=True,
  29. norm_topk_prob=True,
  30. **kwargs,
  31. ):
  32. self.dim = dim
  33. self.n_layers = n_layers
  34. self.n_heads = n_heads
  35. self.n_kv_heads = n_kv_heads
  36. self.vocab_size = vocab_size
  37. self.hidden_dim = hidden_dim
  38. self.multiple_of = multiple_of
  39. self.norm_eps = norm_eps
  40. self.max_seq_len = max_seq_len
  41. self.dropout = dropout
  42. self.flash_attn = flash_attn
  43. ####################################################
  44. # Here are the specific configurations of MOE
  45. # When use_moe is false, the following is invalid
  46. ####################################################
  47. self.use_moe = use_moe
  48. self.num_experts_per_tok = num_experts_per_tok # 每个token选择的专家数量
  49. self.n_routed_experts = n_routed_experts # 总的专家数量
  50. self.n_shared_experts = n_shared_experts # 共享专家
  51. self.scoring_func = scoring_func # 评分函数,默认为'softmax'
  52. self.aux_loss_alpha = aux_loss_alpha # 辅助损失的alpha参数
  53. self.seq_aux = seq_aux # 是否在序列级别上计算辅助损失
  54. self.norm_topk_prob = norm_topk_prob # 是否标准化top-k概率
  55. super().__init__(**kwargs)