config.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from typing import Optional
  2. import gguf
  3. from transformers import AutoConfig, PretrainedConfig
  4. from transformers.models.auto.configuration_auto import CONFIG_MAPPING
  5. from aphrodite.transformers_utils.configs import (BaiChuanConfig,
  6. ChatGLMConfig, MPTConfig,
  7. QWenConfig, RWConfig)
  8. _CONFIG_REGISTRY = {
  9. "baichuan": BaiChuanConfig,
  10. "chatglm": ChatGLMConfig,
  11. "mpt": MPTConfig,
  12. "qwen": QWenConfig,
  13. "RefinedWeb": RWConfig, # For tiiuae/falcon-40b(-instruct)
  14. "RefinedWebModel": RWConfig, # For tiiuae/falcon-7b(-instruct)
  15. }
  16. def extract_gguf_config(checkpoint):
  17. result = gguf.GGUFReader(checkpoint)
  18. architecture = result.fields['general.architecture']
  19. architecture = str(bytes(architecture.parts[architecture.data[0]]),
  20. encoding='utf-8')
  21. # Only support llama so far
  22. if architecture != "llama":
  23. raise RuntimeError(f"Unsupported architecture {architecture}")
  24. # write config
  25. vocab_size = len(result.fields['tokenizer.ggml.token_type'].data)
  26. context_length = int(result.fields['llama.context_length'].parts[-1])
  27. n_layer = int(result.fields['llama.block_count'].parts[-1])
  28. n_head = int(result.fields['llama.attention.head_count'].parts[-1])
  29. n_local_heads = int(
  30. result.fields['llama.attention.head_count_kv'].parts[-1])
  31. intermediate_size = int(
  32. result.fields['llama.feed_forward_length'].parts[-1])
  33. norm_eps = float(
  34. result.fields['llama.attention.layer_norm_rms_epsilon'].parts[-1])
  35. dim = int(result.fields['llama.embedding_length'].parts[-1])
  36. arch = "MixtralForCausalLM"
  37. if 'llama.expert_count' in result.fields:
  38. arch = "MixtralForCausalLM"
  39. name = "mixtral"
  40. else:
  41. arch = "LlamaForCausalLM"
  42. name = "llama"
  43. model_config = {
  44. "architectures": [arch],
  45. "bos_token_id": 1,
  46. "eos_token_id": 2,
  47. "hidden_act": "silu",
  48. "hidden_size": dim,
  49. "intermediate_size": intermediate_size,
  50. "max_position_embeddings": context_length,
  51. "model_type": name,
  52. "num_attention_heads": n_head,
  53. "num_hidden_layers": n_layer,
  54. "num_key_value_heads": n_local_heads,
  55. "rms_norm_eps": norm_eps,
  56. "torch_dtype": "float16",
  57. "vocab_size": vocab_size
  58. }
  59. if 'llama.rope.freq_base' in result.fields:
  60. model_config['rope_theta'] = float(
  61. result.fields['llama.rope.freq_base'].parts[-1])
  62. if 'llama.expert_count' in result.fields:
  63. model_config['num_local_experts'] = int(
  64. result.fields['llama.expert_count'].parts[-1])
  65. model_config['num_experts_per_tok'] = int(
  66. result.fields['llama.expert_used_count'].parts[-1])
  67. if name in _CONFIG_REGISTRY:
  68. config_class = _CONFIG_REGISTRY[name]
  69. else:
  70. config_class = CONFIG_MAPPING[name]
  71. hf_config = config_class.from_dict(model_config)
  72. return hf_config
  73. def get_config(model: str,
  74. trust_remote_code: bool,
  75. revision: Optional[str] = None,
  76. code_revision: Optional[str] = None) -> PretrainedConfig:
  77. if model.endswith("gguf"):
  78. return extract_gguf_config(model)
  79. try:
  80. config = AutoConfig.from_pretrained(
  81. model,
  82. trust_remote_code=trust_remote_code,
  83. revision=revision,
  84. code_revision=code_revision)
  85. except ValueError as e:
  86. if (not trust_remote_code and
  87. "requires you to execute the configuration file" in str(e)):
  88. err_msg = (
  89. "Failed to load the model config. If the model is a custom "
  90. "model not yet available in the HuggingFace transformers "
  91. "library, consider setting `trust_remote_code=True` in LLM "
  92. "or using the `--trust-remote-code` flag in the CLI.")
  93. raise RuntimeError(err_msg) from e
  94. else:
  95. raise e
  96. if config.model_type in _CONFIG_REGISTRY:
  97. config_class = _CONFIG_REGISTRY[config.model_type]
  98. config = config_class.from_pretrained(model,
  99. revision=revision,
  100. code_revision=code_revision)
  101. return config