-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add LLaMA scripts #17020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kunal-vaishnavi
merged 26 commits into
microsoft:main
from
kunal-vaishnavi:kvaishnavi/add-llama-and-whisper-scripts
Aug 23, 2023
Merged
Add LLaMA scripts #17020
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d0495f7
Add LLaMA and Whisper scripts
kunal-vaishnavi 5bfde7a
Undo some linter changes
kunal-vaishnavi 2701a52
Move noqa after linter changes
kunal-vaishnavi 24ef7a0
Remove initial comments to satisfy linter
kunal-vaishnavi 9d56243
Update whisper readme
kunal-vaishnavi 78e7074
Use export and convert methods directly
kunal-vaishnavi 7210fe9
Remove Whisper scripts
kunal-vaishnavi cce84c9
Update markdown style
kunal-vaishnavi 7c18d4f
Address PR feedback
kunal-vaishnavi fc0e372
Add period at end of sentence
kunal-vaishnavi 97f8c59
Add blank end of line
kunal-vaishnavi 4f66dc9
Remove masked fill and add SmoothQuant
kunal-vaishnavi e75e807
Update all scripts
kunal-vaishnavi 7755c29
Remove masked fill
kunal-vaishnavi db1d4dc
Fix CodeQL errors
kunal-vaishnavi 6466b78
Remove unused noqa directives
kunal-vaishnavi 8cc4903
Update log verbosity level
kunal-vaishnavi bfa145e
Update README and CUDA requirements
kunal-vaishnavi 83a5af4
Address PR feedback
kunal-vaishnavi 0630df8
Update init and remove path appends
kunal-vaishnavi 1422309
Merge branch 'main' into kvaishnavi/add-llama-and-whisper-scripts
kunal-vaishnavi fae7cae
Update script invocations in README
kunal-vaishnavi 73d0576
Update benchmarking docs
kunal-vaishnavi f1e869e
Update README formatting
kunal-vaishnavi 86661d5
Update path navigation
kunal-vaishnavi eeb2a07
Update version number in README
kunal-vaishnavi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
onnxruntime/python/tools/transformers/models/llama/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| # LLaMA | ||
|
|
||
| ## Exporting LLaMA | ||
|
|
||
| There are several ways to export LLaMA models such as LLaMA and LLaMA-2 (using LLaMA-2 7B as an example). | ||
|
|
||
| Option 1: from convert_to_onnx | ||
| ``` | ||
| # From source: | ||
| $ git clone https://github.com/microsoft/onnxruntime | ||
| $ cd onnxruntime/onnxruntime/python/tools/transformers/models/llama | ||
| $ python3 convert_to_onnx.py -m meta-llama/Llama-2-7b-hf --output llama2-7b | ||
|
|
||
| # From wheel: | ||
| $ python3 -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-hf --output llama2-7b | ||
| ``` | ||
|
|
||
| To make this option compatible with [Hugging Face's Optimum](https://github.com/huggingface/optimum), you will need to create `config.json` and `generation_config.json` for your model and store them in the same directory as your ONNX models. For example, you can find those JSON files for LLaMA-2 7B on Hugging Face [here](https://huggingface.co/meta-llama/Llama-2-7b-hf). | ||
|
|
||
| Option 2: from [Microsoft's custom export](https://github.com/microsoft/Llama-2-Onnx) | ||
|
|
||
| Option 3: from [Hugging Face's Optimum](https://github.com/huggingface/optimum) | ||
|
|
||
| ## Examples of Exporting LLaMA | ||
|
|
||
| Here are some additional examples for exporting LLaMA. | ||
|
|
||
| ### Export Saved Model on Disk | ||
| ``` | ||
| # From source: | ||
| $ python3 convert_to_onnx.py -m meta-llama/Llama-2-7b-hf --input ./Llama-2-7b-hf --output ./llama2-7b | ||
|
|
||
| # From wheel: | ||
| $ python3 -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-hf --input ./Llama-2-7b-hf --output ./llama2-7b | ||
| ``` | ||
|
|
||
| ### Export with Different Precision | ||
|
|
||
| FP16: | ||
| ``` | ||
| # From source: | ||
| $ python3 convert_to_onnx.py -m meta-llama/Llama-2-7b-hf --output llama2-7b --precision fp16 | ||
|
|
||
| # From wheel: | ||
| $ python3 -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-hf --output llama2-7b --precision fp16 | ||
| ``` | ||
|
|
||
| INT8: | ||
| ``` | ||
| # From source: | ||
| $ python3 convert_to_onnx.py -m meta-llama/Llama-2-7b-hf --output llama2-7b --precision int8 --smooth_quant | ||
|
kunal-vaishnavi marked this conversation as resolved.
Outdated
|
||
|
|
||
| # From wheel: | ||
| $ python3 -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-hf --output llama2-7b --precision int8 --smooth_quant | ||
| ``` | ||
|
|
||
| Note: [Intel's Neural Compressor](https://github.com/intel/neural-compressor) takes time to run the SmoothQuant quantization algorithm on LLMs. On an [Azure Standard_NC24s_v3 VM](https://learn.microsoft.com/en-us/azure/virtual-machines/ncv3-series), it takes about ~30-45 min for each of the exported ONNX models. | ||
|
|
||
| ## Benchmark LLaMA | ||
|
|
||
| Here are some examples of how you can benchmark LLaMA. | ||
|
|
||
| Note: In the below examples, `PyTorch` refers to running in PyTorch without `torch.compile` and `PyTorch 2.0` refers to running in PyTorch with `torch.compile`. | ||
|
|
||
| ### Variants | ||
|
|
||
| 1. PyTorch (without `torch.compile`), FP32 | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type hf-pt \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp32 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cpu \ | ||
| --auth | ||
| ``` | ||
|
|
||
| 2. PyTorch 2.0 (with `torch.compile`), FP16 | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type hf-pt2 \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp16 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cuda \ | ||
| --auth | ||
| ``` | ||
|
|
||
| 3. Optimum + ONNX Runtime, FP32, export via Optimum or convert_to_onnx | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type hf-ort \ | ||
| --hf-ort-model-path ./Llama-2-7b-hf-onnx/ \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp32 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cpu \ | ||
| --auth | ||
| ``` | ||
|
|
||
| 4. Optimum + ONNX Runtime, FP16, export via convert_to_onnx | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type hf-ort \ | ||
| --hf-ort-model-path ./llama2-7b/ \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp16 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cuda \ | ||
| --auth | ||
| ``` | ||
|
|
||
| 5. Optimum + ONNX Runtime, INT8, export via convert_to_onnx | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type hf-ort \ | ||
| --hf-ort-model-path ./llama2-7b/ \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision int8 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cpu \ | ||
| --auth | ||
| ``` | ||
|
|
||
| 6. ONNX Runtime, FP32, Microsoft custom export | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type ort \ | ||
| --ort-model-path llama-2-onnx/7B_float32/ONNX/LlamaV2_7B_float32.onnx \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp32 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cpu | ||
| ``` | ||
|
|
||
| 7. ONNX Runtime, FP16, Microsoft custom export | ||
| ``` | ||
| python3 benchmark.py \ | ||
| --benchmark-type ort \ | ||
| --ort-model-path ./llama-2-onnx/7B_float16/ONNX/LlamaV2_7B_float16.onnx \ | ||
| --model-name meta-llama/Llama-2-7b-hf \ | ||
| --model-size 7b \ | ||
| --precision fp16 \ | ||
| --batch-sizes "1 2" \ | ||
| --sequence-lengths "8 16" \ | ||
| --device cuda | ||
| ``` | ||
|
|
||
| You can also profile each of these variants by adding the `--profile` flag and providing one batch size and sequence length combination. | ||
8 changes: 8 additions & 0 deletions
8
onnxruntime/python/tools/transformers/models/llama/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| import os | ||
| import sys | ||
|
|
||
| sys.path.append(os.path.dirname(__file__)) | ||
|
kunal-vaishnavi marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.