|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +<p align="center"><img width="50%" src="images/ONNX_Runtime_logo_dark.png" /></p> |
| 4 | + |
| 5 | +**A library for developing and deploying PyTorch models using ONNX Runtime**. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +[Key Features](#key-features) • |
| 10 | +[Installation](#Installation) • |
| 11 | +[Training](#Training) • |
| 12 | +[Inference](#Inference) • |
| 13 | +[Docs](https://www.onnxruntime.ai/) • |
| 14 | +[License](https://github.com/pytorch/ort/blob/main/LICENSE) |
| 15 | + |
| 16 | +[](https://pypi.org/project/torch-ort/) |
| 17 | +[]() |
| 18 | +[]() |
| 19 | +[](https://github.com/pytorch/ort/actions/workflows/api-ci.yml) |
| 20 | +[](https://github.com/pytorch/ort/actions/workflows/doc-automation.yml) |
| 21 | + |
| 22 | +</div> |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +# Introduction |
| 27 | + |
| 28 | +A library for accelerating PyTorch models using ONNX Runtime: |
| 29 | + |
| 30 | +- torch-ort to train PyTorch models faster with ONNX Runtime |
| 31 | +- moe to scale large models and improve their quality |
| 32 | +- torch-ort-infer to perform inference on PyTorch models with ONNX Runtime and Intel OpenVINO |
| 33 | + |
| 34 | +# 🚀 Installation |
| 35 | + |
| 36 | +## Training |
| 37 | + |
| 38 | +### Pre-requisites |
| 39 | + |
| 40 | +You need a machine with at least one NVIDIA or AMD GPU to run ONNX Runtime for PyTorch. |
| 41 | + |
| 42 | +You can install and run torch-ort in your local environment, or with [Docker](torch_ort/docker/README.md). |
| 43 | + |
| 44 | +### Install in a local Python environment |
| 45 | + |
| 46 | +1. Install CUDA |
| 47 | + |
| 48 | +2. Install CuDNN |
| 49 | + |
| 50 | +3. Install torch-ort |
| 51 | + |
| 52 | + - `pip install torch-ort` |
| 53 | + |
| 54 | +4. Run post-installation script for ORTModule |
| 55 | + |
| 56 | + - `python -m torch_ort.configure` |
| 57 | + |
| 58 | +Get install instructions for other combinations in the `Get Started Easily` section at <https://www.onnxruntime.ai/> under the `Optimize Training` tab. |
| 59 | + |
| 60 | +### Verify your installation |
| 61 | + |
| 62 | +1. Clone this repo |
| 63 | + |
| 64 | + - `git clone [email protected]:pytorch/ort.git` |
| 65 | + |
| 66 | +2. Install extra dependencies |
| 67 | + |
| 68 | + - `pip install wget pandas sklearn transformers` |
| 69 | + |
| 70 | +3. Run a test training script |
| 71 | + |
| 72 | + - `python ./ort/tests/bert_for_sequence_classification.py` |
| 73 | + |
| 74 | +## Mixture Of Experts |
| 75 | + |
| 76 | +Mixture of Experts layer implementation is available in the ort_moe folder. |
| 77 | + |
| 78 | + |
| 79 | +Clone this repo |
| 80 | + |
| 81 | +```bash |
| 82 | +git clone https://github.com/pytorch/ort.git |
| 83 | +``` |
| 84 | + |
| 85 | +Build MoE |
| 86 | + |
| 87 | +```bash |
| 88 | +cd ort_moe |
| 89 | +pip install build # Install PyPA build |
| 90 | +python -m build |
| 91 | +``` |
| 92 | + |
| 93 | +## Inference |
| 94 | + |
| 95 | +### Prerequisites |
| 96 | + |
| 97 | +- Ubuntu 18.04, 20.04 |
| 98 | +- Python* 3.7, 3.8 or 3.9 |
| 99 | + |
| 100 | +### Install in a local Python environment |
| 101 | + |
| 102 | +- `pip install torch-ort-infer[openvino]` |
| 103 | +- Run post installation configuration script `python -m torch_ort.configure` |
| 104 | + |
| 105 | +### Verify your installation |
| 106 | + |
| 107 | +1. Clone this repo |
| 108 | + |
| 109 | + - `git clone [email protected]:pytorch/ort.git` |
| 110 | + |
| 111 | +2. Install extra dependencies |
| 112 | + |
| 113 | + - `pip install wget pandas sklearn transformers` |
| 114 | + |
| 115 | +3. Run a test script |
| 116 | + |
| 117 | + - `python ./torch_ort_inference/tests/bert_for_sequence_classification.py` |
| 118 | + |
| 119 | +# 📈 Training |
| 120 | + |
| 121 | +The torch-ort library accelerates training of large transformer PyTorch models to reduce the training time and GPU cost with a few lines of code change. It is built on top of highly successful and proven technologies of ONNX Runtime and ONNX format and includes the ONNX Runtime Optimizer and Data Sampler. |
| 122 | + |
| 123 | +## Samples |
| 124 | + |
| 125 | +To see torch-ort in action, see https://github.com/microsoft/onnxruntime-training-examples, which shows you how to train the most popular HuggingFace models. |
| 126 | + |
| 127 | +# 🤓 Mixture of Experts |
| 128 | + |
| 129 | +To run MoE, add the layer to your model as described in the tutorial: [ort_moe/docs/tutorials/moe_tutorial.py](tutorial) |
| 130 | + |
| 131 | +For more details, see [ort_moe/docs/moe.md](moe.md) |
| 132 | + |
| 133 | +Note: ONNX Runtime is not required to run the MoE layer. It is integrated in standalone PyTorch. |
| 134 | + |
| 135 | +# 🎯 Inference |
| 136 | + |
| 137 | +<div align="center"> |
| 138 | + |
| 139 | +<p align="center"><img width="30%" src="images/ONNX_Runtime_logo_dark.png" /></p> |
| 140 | +<p align="center">➕</p> |
| 141 | +<p align="center"><img width="30%" src="images/openvino-logo-purple-black.png" /></p> |
| 142 | + |
| 143 | +</div> |
| 144 | + |
| 145 | +ONNX Runtime for PyTorch supports PyTorch model inference using ONNX Runtime. |
| 146 | + |
| 147 | +It is available via the torch-ort-infer python package. This preview package enables OpenVINO™ Execution Provider for ONNX Runtime by default for accelerating inference on various Intel® CPUs, Intel® integrated GPUs, and Intel® Movidius™ Vision Processing Units - referred to as VPU. |
| 148 | + |
| 149 | +## Supported Execution Providers |
| 150 | + |
| 151 | +|Execution Providers| |
| 152 | +|---| |
| 153 | +|OpenVINO []() | |
| 154 | + |
| 155 | +## Provider Options |
| 156 | + |
| 157 | +Users can configure different options for a given Execution Provider to run inference. As an example, OpenVINO™ Execution Provider options can be configured as shown below: |
| 158 | + |
| 159 | +```python |
| 160 | +from torch_ort import ORTInferenceModule, OpenVINOProviderOptions |
| 161 | +provider_options = OpenVINOProviderOptions(backend = "GPU", precision = "FP16") |
| 162 | +model = ORTInferenceModule(model, provider_options = provider_options) |
| 163 | + |
| 164 | +# PyTorch inference script follows |
| 165 | +``` |
| 166 | +### List of Provider Options |
| 167 | + |
| 168 | +Supported backend-precision combinations: |
| 169 | +| Backend | Precision | |
| 170 | +| --------| --------- | |
| 171 | +| CPU | FP32 | |
| 172 | +| GPU | FP32 | |
| 173 | +| GPU | FP16 | |
| 174 | +| MYRIAD | FP16 | |
| 175 | + |
| 176 | +If no provider options are specified by user, OpenVINO™ Execution Provider is enabled with following options by default: |
| 177 | + |
| 178 | +```python |
| 179 | +backend = "CPU" |
| 180 | +precision = "FP32" |
| 181 | +``` |
| 182 | + |
| 183 | +For more details on APIs, see [usage.md](/torch_ort_inference/docs/usage.md). |
| 184 | + |
| 185 | +## Code Sample |
| 186 | + |
| 187 | +Below is an example of how you can leverage OpenVINO™ integration with Torch-ORT in a simple NLP usecase. |
| 188 | + |
| 189 | +A pretrained [BERT model](https://huggingface.co/textattack/bert-base-uncased-CoLA) fine-tuned on the CoLA dataset from HuggingFace model hub is used to predict grammar correctness on a given input text. |
| 190 | + |
| 191 | +```python |
| 192 | +from transformers |
| 193 | +import AutoTokenizer, AutoModelForSequenceClassification |
| 194 | +import numpy as np |
| 195 | +from torch_ort import ORTInferenceModule |
| 196 | +tokenizer = AutoTokenizer.from_pretrained( |
| 197 | + "textattack/bert-base-uncased-CoLA") |
| 198 | +model = AutoModelForSequenceClassification.from_pretrained( |
| 199 | + "textattack/bert-base-uncased-CoLA") |
| 200 | +# Wrap model in ORTInferenceModule to prepare the model for inference using OpenVINO Execution Provider on CPU |
| 201 | +model = ORTInferenceModule(model) |
| 202 | +text = "Replace me any text by you'd like ." |
| 203 | +encoded_input = tokenizer(text, return_tensors='pt') |
| 204 | +output = model(**encoded_input) |
| 205 | +# Post processing |
| 206 | +logits = output.logits |
| 207 | +logits = logits.detach().cpu().numpy() |
| 208 | +# predictions |
| 209 | +pred = np.argmax(logits, axis=1).flatten() |
| 210 | +print("Grammar correctness label (0=unacceptable, 1=acceptable)") |
| 211 | +print(pred) |
| 212 | +``` |
| 213 | + |
| 214 | +## Samples |
| 215 | + |
| 216 | +To see OpenVINO™ integration with Torch-ORT in action, see [demos](/torch_ort_inference/demos), which shows you how to run inference on some of the most popular Deep Learning models. |
| 217 | + |
| 218 | +# 🤝 Contribute |
| 219 | + |
| 220 | +Please refer to our [contributing guide](CONTRIBUTING.md) for more information on how to contribute! |
| 221 | + |
| 222 | +## License |
| 223 | + |
| 224 | +This project has an MIT license, as found in the [LICENSE](LICENSE) file. |
0 commit comments