Package: deepspeed-mii
Latest News[2024/01]DeepSpeed-FastGen: Introducting Mixtral, Phi-2, and Falcon support with major performance and feature enhancements.[2023/11]DeepSpeed-FastGen: High-throughput Text Generation for LLMs via MII and DeepSpeed-Inference[2022/11]Stable Diffusion Image Generation under 1 second w. DeepSpeed MII[2022/10]Announcing DeepSpeed Model Implementations for Inference (MII)ContentsDeepSpeed-MIIKey TechnologiesHow does MII work?Supported ModelsGetting StartedDeepSpeed Model Implementations for Inference (MII)Introducing MII, an open-source Python library designed by DeepSpeed to democratize powerful model inference with a focus on high-throughput, low latency, and cost-effectiveness.MII features include blocked KV-caching, continuous batching, Dynamic SplitFuse, tensor parallelism, and high-performance CUDA kernels to support fast high throughput text-generation for LLMs such as Llama-2-70B, Mixtral (MoE) 8x7B, and Phi-2. The latest updates in v0.2 add new model families, performance optimizations, and feature enhancements. MII now delivers up to 2.5 times higher effective throughput compared to leading systems such as vLLM. For detailed performance results please see ourlatest DeepSpeed-FastGen blogandDeepSpeed-FastGen release blog.We firstannounced MIIin 2022, which covers all prior releases up to v0.0.9. In addition to language models, we also support acceleratingtext2image models like Stable Diffusion. For more details on our previous releases please see ourlegacy APIs.Key TechnologiesMII for High-Throughput Text GenerationMII provides accelerated text-generation inference through the use of four key technologies:Blocked KV CachingContinuous BatchingDynamic SplitFuseHigh Performance CUDA KernelsFor a deeper dive into understanding these features pleaserefer to our blogwhich also includes a detailed performance analysis.MII LegacyIn the past, MII introduced severalkey performance optimizationsfor low-latency serving scenarios:DeepFusion for TransformersMulti-GPU Inference with Tensor-SlicingZeRO-Inference for Resource Constrained SystemsCompiler OptimizationsHow does MII work?Figure 1: MII architecture, showing how MII automatically optimizes OSS models using DS-Inference before deploying them. DeepSpeed-FastGen optimizations in the figure have been published inour blog post.Under-the-hood MII is powered byDeepSpeed-Inference. Based on the model architecture, model size, batch size, and available hardware resources, MII automatically applies the appropriate set of system optimizations to minimize latency and maximize throughput.Supported ModelsMII currently supports over 20,000 models across eight popular model architectures. We plan to add additional models in the near term, if there are specific model architectures you would like supported pleasefile an issueand let us know. All current models leverage Hugging Face in our backend to provide both the model weights and the model's corresponding tokenizer. For our current release we support the following model architectures:model familysize range~model countfalcon7B - 180B300llama7B - 65B19,000llama-27B - 70B900mistral7B6,000mixtral (MoE)8x7B1,100opt0.1B - 66B1,300phi-22.7B200qwen7B - 72B200MII Legacy Model SupportMII Legacy APIs support over 50,000 different models including BERT, RoBERTa, Stable Diffusion, and other text-generation models like Bloom, GPT-J, etc. For a full list please see ourlegacy supported models table.Getting Started with MIIDeepSpeed-MII allows users to create non-persistent and persistent deployments for supported models in just a few lines of code.InstallationNon-Persistent PipelinePersistent DeploymentInstallationThe fasest way to get started is with ourPyPI release of DeepSpeed-MIIwhich means you can get started within minutes via:pipinstalldeepspeed-miiFor ease of use and significant reduction in lengthy compile times that many projects require in this space we distribute a pre-compiled python wheel covering the majority of our custom kernels through a new library calledDeepSpeed-Kernels. We have found this library to be very portable across environments with NVIDIA GPUs with compute capabilities 8.0+ (Ampere+), CUDA 11.6+, and Ubuntu 20+. In most cases you shouldn't even need to know this library exists as it is a dependency of DeepSpeed-MII and will be installed with it. However, if for whatever reason you need to compile our kernels manually please see ouradvanced installation docs.Non-Persistent PipelineA non-persistent pipeline is a great way to try DeepSpeed-MII. Non-persistent pipelines are only around for the duration of the python script you are running. The full example for running a non-persistent pipeline deployment is only 4 lines. Give it a try!importmiipipe=mii.pipeline("mistralai/Mistral-7B-v0.1")response=pipe(["DeepSpeed is","Seattle is"],max_new_tokens=128)print(response)The returnedresponseis a list ofResponseobjects. We can access several details about the generation (e.g.,response[0].prompt_length):generated_text: strText generated by the model.prompt_length: intNumber of tokens in the original prompt.generated_length: intNumber of tokens generated.finish_reason: strReason for stopping generation.stopindicates the EOS token was generated andlengthindicates the generation reachedmax_new_tokensormax_length.If you want to free device memory and destroy the pipeline, use thedestroymethod:pipe.destroy()Tensor parallelismTaking advantage of multi-GPU systems for greater performance is easy with MII. When run with thedeepspeedlauncher, tensor parallelism is automatically controlled by the--num_gpusflag:# Run on a single GPUdeepspeed--num_gpus1mii-example.py# Run on multiple GPUsdeepspeed--num_gpus2mii-example.pyPipeline OptionsWhile only the model name or path is required to stand up a non-persistent pipeline deployment, we offer customization options to our users:mii.pipeline()Options:model_name_or_path: strName or local path to aHuggingFacemodel.max_length: intSets the default maximum token length for the prompt + response.all_rank_output: boolWhen enabled, all ranks return the generated text. By default, only rank 0 will return text.Users can also control the generation characteristics for individual prompts (i.e., when callingpipe()) with the following options:max_length: intSets the per-prompt maximum token length for prompt + response.min_new_tokens: intSets the minimum number of tokens generated in the response.max_lengthwill take precedence over this setting.max_new_tokens: intSets the maximum number of tokens generated in the response.ignore_eos: bool(Defaults toFalse) Setting toTrueprevents generation from ending when the EOS token is encountered.top_p: float(Defaults to0.9) When set below1.0, filter tokens and keep only the most probable, where token probabilities sum to ≥top_p.top_k: int(Defaults toNone) WhenNone, top-k filtering is disabled. When set, the number of highest probability tokens to keep.temperature: float(Defaults toNone) WhenNone, temperature is disabled. When set, modulates token probabilities.do_sample: bool(Defaults toTrue) WhenTrue, sample output logits. WhenFalse, use greedy sampling.return_full_text: bool(Defaults toFalse) WhenTrue, prepends the input prompt to the returned textPersistent DeploymentA persistent deployment is ideal for use with long-running and production applications. The persistent model uses a lightweight GRPC server that can be queried by multiple clients at once. The full example for running a persistent model is only 5 lines. Give it a try!importmiiclient=mii.serve("mistralai/Mistral-7B-v0.1")response=client.generate(["Deepspeed is","Seattle is"],max_new_tokens=128)print(response)The returnedresponseis a list ofResponseobjects. We can access several details about the generation (e.g.,response[0].prompt_length):generated_text: strText generated by the model.prompt_length: intNumber of tokens in the original prompt.generated_length: intNumber of tokens generated.finish_reason: strReason for stopping generation.stopindicates the EOS token was generated andlengthindicates the generation reachedmax_new_tokensormax_length.If we want to generate text from other processes, we can do that too:client=mii.client("mistralai/Mistral-7B-v0.1")response=client.generate("Deepspeed is",max_new_tokens=128)When we no longer need a persistent deployment, we can shutdown the server from any client:client.terminate_server()Model ParallelismTaking advantage of multi-GPU systems for better latency and throughput is also easy with the persistent deployments. Model parallelism is controlled by thetensor_parallelinput tomii.serve:client=mii.serve("mistralai/Mistral-7B-v0.1",tensor_parallel=2)The resulting deployment will split the model across 2 GPUs to deliver faster inference and higher throughput than a single GPU.Model ReplicasWe can also take advantage of multi-GPU (and multi-node) systems by setting up multiple model replicas and taking advantage of the load-balancing that DeepSpeed-MII provides:client=mii.serve("mistralai/Mistral-7B-v0.1",replica_num=2)The resulting deployment will load 2 model replicas (one per GPU) and load-balance incoming requests between the 2 model instances.Model parallelism and replicas can also be combined to take advantage of systems with many more GPUs. In the example below, we run 2 model replicas, each split across 2 GPUs on a system with 4 GPUs:client=mii.serve("mistralai/Mistral-7B-v0.1",tensor_parallel=2,replica_num=2)The choice between model parallelism and model replicas for maximum performance will depend on the nature of the hardware, model, and workload. For example, with small models users may find that model replicas provide the lowest average latency for requests. Meanwhile, large models may achieve greater overall throughput when using only model parallelism.RESTful APIMII makes it easy to setup and run model inference via RESTful APIs by settingenable_restful_api=Truewhen creating a persistent MII deployment. The RESTful API can receive requests athttp://{HOST}:{RESTFUL_API_PORT}/mii/{DEPLOYMENT_NAME}. A full example is provided below:client=mii.serve("mistralai/Mistral-7B-v0.1",deployment_name="mistral-deployment",enable_restful_api=True,restful_api_port=28080,)📌Note:While providing adeployment_nameis not necessary (MII will autogenerate one for you), it is good practice to provide adeployment_nameso that you can ensure you are interfacing with the correct RESTful API.You can then send prompts to the RESTful gateway with any HTTP client, such ascurl:curl--header"Content-Type: application/json"--requestPOST-d'{"prompts": ["DeepSpeed is", "Seattle is"], "max_length": 128}'http://localhost:28080/mii/mistral-deploymentorpython:importjsonimportrequestsurl=f"http://localhost:28080/mii/mistral-deployment"params={"prompts":["DeepSpeed is","Seattle is"],"max_length":128}json_params=json.dumps(params)output=requests.post(url,data=json_params,headers={"Content-Type":"application/json"})Persistent Deployment OptionsWhile only the model name or path is required to stand up a persistent deployment, we offer customization options to our users.mii.serve()Options:model_name_or_path: str(Required) Name or local path to aHuggingFacemodel.max_length: int(Defaults to maximum sequence length in model config) Sets the default maximum token length for the prompt + response.deployment_name: str(Defaults tof"{model_name_or_path}-mii-deployment") A unique identifying string for the persistent model. If provided, client objects should be retrieved withclient = mii.client(deployment_name).tensor_parallel: int(Defaults to1) Number of GPUs to split the model across.replica_num: int(Defaults to1) The number of model replicas to stand up.enable_restful_api: bool(Defaults toFalse) When enabled, a RESTful API gateway process is launched that can be queried athttp://{host}:{restful_api_port}/mii/{deployment_name}. See thesection on RESTful APIsfor more details.restful_api_port: int(Defaults to28080) The port number used to interface with the RESTful API whenenable_restful_apiis set toTrue.mii.client()Options:model_or_deployment_name: strName of the model ordeployment_namepassed tomii.serve()Users can also control the generation characteristics for individual prompts (i.e., when callingclient.generate()) with the following options:max_length: intSets the per-prompt maximum token length for prompt + response.min_new_tokens: intSets the minimum number of tokens generated in the response.max_lengthwill take precedence over this setting.max_new_tokens: intSets the maximum number of tokens generated in the response.ignore_eos: bool(Defaults toFalse) Setting toTrueprevents generation from ending when the EOS token is encountered.top_p: float(Defaults to0.9) When set below1.0, filter tokens and keep only the most probable, where token probabilities sum to ≥top_p.top_k: int(Defaults toNone) WhenNone, top-k filtering is disabled. When set, the number of highest probability tokens to keep.temperature: float(Defaults toNone) WhenNone, temperature is disabled. When set, modulates token probabilities.do_sample: bool(Defaults toTrue) WhenTrue, sample output logits. WhenFalse, use greedy sampling.return_full_text: bool(Defaults toFalse) WhenTrue, prepends the input prompt to the returned textContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor
contactopencode@microsoft.comwith any additional questions or comments.TrademarksThis project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
trademarks or logos is subject to and must followMicrosoft's Trademark & Brand Guidelines.
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.