config.py 4.3 KB

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