| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import os |
| 5 | |
| 6 | from transformers import PreTrainedConfig |
| 7 | |
| 8 | from vllm.transformers_utils.utils import without_trust_remote_code |
| 9 | |
| 10 | |
| 11 | class MedusaConfig(PreTrainedConfig): |
| 12 | model_type = "medusa" |
| 13 | |
| 14 | def __init__( |
| 15 | self, |
| 16 | hidden_size: int = 4096, |
| 17 | vocab_size: int = 32001, |
| 18 | num_heads: int = 5, |
| 19 | num_hidden_layers: int = 1, |
| 20 | max_paths: int = 64, |
| 21 | topk: int = 10, |
| 22 | truncated_vocab_size: int | None = None, |
| 23 | **kwargs, |
| 24 | ): |
| 25 | self.hidden_size = hidden_size |
| 26 | self.vocab_size = vocab_size |
| 27 | self.num_heads = num_heads |
| 28 | self.num_hidden_layers = num_hidden_layers |
| 29 | self.max_paths = max_paths |
| 30 | self.topk = topk |
| 31 | self.max_seq_len = int(2**20) |
| 32 | self.truncated_vocab_size = ( |
| 33 | vocab_size if truncated_vocab_size is None else truncated_vocab_size |
| 34 | ) |
| 35 | if "architectures" not in kwargs: |
| 36 | kwargs["architectures"] = ["MedusaModel"] |
| 37 | |
| 38 | super().__init__(**kwargs) |
| 39 | |
| 40 | @classmethod |
| 41 | def from_pretrained( |
| 42 | cls, |
| 43 | pretrained_model_name_or_path: str | os.PathLike, |
| 44 | **kwargs, |
| 45 | ) -> "MedusaConfig": |
| 46 | config_dict, kwargs = cls.get_config_dict( |
| 47 | pretrained_model_name_or_path, **without_trust_remote_code(kwargs) |
| 48 | ) |
| 49 | for k in list(config_dict.keys()): |
| 50 | if "num" in k: |
| 51 | if "heads" in k: |
| 52 | config_dict["num_heads"] = config_dict.pop(k) |
| 53 | elif "layers" in k: |
| 54 | config_dict["num_hidden_layers"] = config_dict.pop(k) |
| 55 | return cls.from_dict(config_dict, **kwargs) |
| 56 | |
| 57 | @property |
| 58 | def num_attention_heads(self): |
| 59 | return 0 |
| 60 | |
| 61 | @property |
| 62 | def num_lookahead_tokens(self): |
| 63 | return self.num_heads |
| 64 | |
| 65 | @num_lookahead_tokens.setter |
| 66 | def num_lookahead_tokens(self, num_lookahead_tokens: int): |
| 67 | self.num_heads = num_lookahead_tokens |