Package: medusa-llm
Medusa: Simple Framework for Accelerating LLM Generation with Multiple Decoding Heads|Blog|Roadmap|News🔥[2023/09] Medusa v0.1 is released! 🎉IntroductionMedusa is a simple framework that democratizes the acceleration techniques for LLM generation with multiple decoding heads.Medusa on Vicuna-7b.We aim to tackle the three pain points of popular acceleration techniques like speculative decoding:Requirement of a good draft model.System complexity.Inefficiency when using sampling-based genenration.Medusa adds extra "heads" to LLMs to predict multiple future tokens simultaneously. When augmenting a model with Medusa, the original model stays untouched, and only the new heads are fine-tuned during. During generation, these heads each produce multiple likely words for the corresponding position. These options are then combined and processed using a tree-based attention mechanism. Finally, a typical acceptance scheme is employed to pick the longest plausible prefix from the candidates for further decoding.In a nutshell, we solve the challenges of speculative decoding with the following ideas:Instead of introducing a new model, we train multiple decoding heads on thesamemodel.The training is parameter-efficient so that even GPU poor can do it. And since there is no additional model, there is no need to adjust the distributed computing setup.Relaxing the requirement of matching the distribution of the original model makes the non-greedy generation even faster than greedy decoding.In this initial release, our primary focus is on optimizing Medusa for a batch size of 1—a setting commonly utilized for local model hosting. In this configuration, Medusa delivers approximately a 2x speed increase across a range of Vicuna models. We are actively working to extend Medusa's capabilities by integrating it into additional inference frameworks, with the aim of achieving even greater performance gains and extending Medusa to broader settings.ContentsIntroductionContentsInstallationMethod 1: With pipMethod 2: From sourceModel WeightsInferenceTrainingPrepare the dataTrain the modelPush to Hugging Face HubCitationCodebase GuideContributingAcknowledgementsInstallationMethod 1: With pippipinstallmedusa-llmMethod 2: From sourcegitclonehttps://github.com/FasterDecoding/Medusa.gitcdMedusa
pipinstall-e.Model WeightsSizeChat CommandHugging Face Repo7Bpython -m medusa.inference.cli --model FasterDecoding/medusa-vicuna-7b-v1.3FasterDecoding/medusa-vicuna-7b-v1.313Bpython -m medusa.inference.cli --model FasterDecoding/medusa-vicuna-13b-v1.3FasterDecoding/medusa-vicuna-13b-v1.333Bpython -m medusa.inference.cli --model FasterDecoding/medusa-vicuna-33b-v1.3FasterDecoding/medusa-vicuna-33b-v1.3InferenceWe currently support inference in the single GPU and batch size 1 setting, which is the most common setup for local model hosting. We are actively working to extend Medusa's capabilities by integrating it into other inference frameworks, please don't hesitate to reach out if you are interested in contributing to this effort.You can use the following command for lauching a CLI interface:python-mmedusa.inference.cli--model[pathofmedusamodel]You can also pass--load-in-8bitor--load-in-4bitto load the base model in quantized format.TrainingFor training, please install:pipinstall-e".[train]"Prepare the dataWe take a public version of the ShareGPT dataset, which is a subset of the Vicuna training data. For other models, you can use the corresponding training dataset.gitclonehttps://huggingface.co/datasets/Aeala/ShareGPT_Vicuna_unfilteredTrain the modelWe follow the training setup fromFastChat, but with a much larger learning rate because we freeze the original model and only train the new heads. Here is the training command for the Vicuna-7b model on 4 GPUs. Since we are only training the new heads, the training does not require a lot of memory, and only data parallelism is needed. You can modify the script to fit your own setup. For larger models, we use the same setup. You can also use--load_in_8bitor--load_in_4bitto load the base model in quantized format.torchrun--nproc_per_node=4medusa/train/train.py--model_name_or_pathlmsys/vicuna-7b-v1.3\--data_pathShareGPT_Vicuna_unfiltered/ShareGPT_V4.3_unfiltered_cleaned_split.json\--bf16True\--output_dirtest\--num_train_epochs1\--per_device_train_batch_size8\--per_device_eval_batch_size8\--gradient_accumulation_steps4\--evaluation_strategy"no"\--save_strategy"no"\--learning_rate1e-3\--weight_decay0.0\--warmup_ratio0.1\--lr_scheduler_type"cosine"\--logging_steps1\--tf32True\--model_max_length2048\--lazy_preprocessTrue\--medusa_num_heads3\--medusa_num_layers1Push to Hugging Face HubYou can use the following command to push your model to the Hugging Face Hub:python-mmedusa.hf_utils--folder[pathofthemodelfolder]--repo[nameoftherepo]Citation@misc{medusa,author={Tianle Cai and Yuhong Li and Zhengyang Geng and Hongwu Peng and Tri Dao},title={Medusa: Simple Framework for Accelerating LLM Generation with Multiple Decoding Heads},year={2023},publisher={GitHub},journal={GitHub repository},howpublished={\url{https://github.com/FasterDecoding/Medusa}},}Codebase Guidemedusa/model/medusa_model.pyis the key file for Medusa. It contains theMedusaModelclass, which is a wrapper of the original model and the new heads. This class also has implementation of a streaming generation method. If you want to dive into the details of Medusa, this is the place to start.We also provide some illustrative notebooks innotebooks/to help you understand the codebase.ContributingWe welcome community contributions to Medusa. If you have an idea for how to improve it, please open an issue to discuss it with us. When submitting a pull request, please ensure that your changes are well-tested. Please split each major change into a separate pull request. We also have aRoadmapsummarizing our future plans for Medusa. Don't hesitate to reach out if you are interested in contributing to any of the items on the roadmap.AcknowledgementsThis codebase is influenced by amazing works from the community, includingFastChat,TinyChat,vllmand many others.