LMConfig.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from transformers import PretrainedConfig
  2. from typing import List
  3. # 定义 LMConfig 类,继承自 PretrainedConfig
  4. class LMConfig(PretrainedConfig):
  5. model_type = "minimind" # 设置模型类型为 "minimind"
  6. def __init__(
  7. self,
  8. dim: int = 512, # 模型维度,默认为 512
  9. n_layers: int = 8, # Transformer 层数,默认为 8
  10. n_heads: int = 16, # 注意力头数,默认为 16
  11. n_kv_heads: int = 8, # KV 头数,默认为 8
  12. vocab_size: int = 6400, # 词汇表大小,默认为 6400
  13. hidden_dim: int = None, # 隐藏层维度,默认为 None
  14. multiple_of: int = 64, # 隐藏层维度的倍数,默认为 64
  15. norm_eps: float = 1e-5, # 归一化层的 epsilon 值,默认为 1e-5
  16. max_seq_len: int = 512, # 最大序列长度,默认为 512
  17. dropout: float = 0.0, # Dropout 概率,默认为 0.0
  18. flash_attn: bool = True, # 是否使用 Flash Attention,默认为 True
  19. ####################################################
  20. # 以下是 MOE(Mixture of Experts)的特定配置
  21. # 当 use_moe 为 False 时,以下配置无效
  22. ####################################################
  23. use_moe: bool = False, # 是否使用 MOE,默认为 False
  24. num_experts_per_tok=2, # 每个 token 选择的专家数量,默认为 2
  25. n_routed_experts=4, # 总的专家数量,默认为 4
  26. n_shared_experts: bool = True, # 是否使用共享专家,默认为 True
  27. scoring_func='softmax', # 评分函数,默认为 'softmax'
  28. aux_loss_alpha=0.01, # 辅助损失的 alpha 参数,默认为 0.01
  29. seq_aux=True, # 是否在序列级别上计算辅助损失,默认为 True
  30. norm_topk_prob=True, # 是否标准化 top-k 概率,默认为 True
  31. **kwargs,
  32. ):
  33. self.dim = dim # 设置模型维度
  34. self.n_layers = n_layers # 设置 Transformer 层数
  35. self.n_heads = n_heads # 设置注意力头数
  36. self.n_kv_heads = n_kv_heads # 设置 KV 头数
  37. self.vocab_size = vocab_size # 设置词汇表大小
  38. self.hidden_dim = hidden_dim # 设置隐藏层维度
  39. self.multiple_of = multiple_of # 设置隐藏层维度的倍数
  40. self.norm_eps = norm_eps # 设置归一化层的 epsilon 值
  41. self.max_seq_len = max_seq_len # 设置最大序列长度
  42. self.dropout = dropout # 设置 Dropout 概率
  43. self.flash_attn = flash_attn # 设置是否使用 Flash Attention
  44. ####################################################
  45. # 以下是 MOE(Mixture of Experts)的特定配置
  46. # 当 use_moe 为 False 时,以下配置无效
  47. ####################################################
  48. self.use_moe = use_moe # 设置是否使用 MOE
  49. self.num_experts_per_tok = num_experts_per_tok # 设置每个 token 选择的专家数量
  50. self.n_routed_experts = n_routed_experts # 设置总的专家数量
  51. self.n_shared_experts = n_shared_experts # 设置是否使用共享专家
  52. self.scoring_func = scoring_func # 设置评分函数
  53. self.aux_loss_alpha = aux_loss_alpha # 设置辅助损失的 alpha 参数
  54. self.seq_aux = seq_aux # 设置是否在序列级别上计算辅助损失
  55. self.norm_topk_prob = norm_topk_prob # 设置是否标准化 top-k 概率
  56. super().__init__(**kwargs) # 调用父类 PretrainedConfig 的初始化方法