This sample shows you how to run the Llama model with PyTorch, HuggingFace and ONNX Runtime.
python run_llama_pt.py --name <model> --prompt <prompt>
where <model> can be:
PY007/TinyLlama-1.1B-intermediate-step-480k-1TPY007/TinyLlama-1.1B-Chat-v0.3TinyLlama/TinyLlama-1.1B-Chat-v0.6meta-llama/Llama-2-7b-hf if you have access to the gated meta modelpython run_llama_opt_onnx.py
Same options.
Export the model with ONNX Runtime
Note: this step requires 54GB of memory
Install the latest version of torch
pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu118
Install onnxruntime
pip install onnxruntime-gpu
Export the model
Note: you cannot have a local folder that is same as the string passed to the -m argument, as HuggingFace will look for the model in this folder rather than downloading it and will error if it is not there.
huggingface-cli login --token <token>
python -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-chat-hf --output models/meta-llama/Llama-2-7b-chat-hf --execution_provider cuda --precision fp16 --use_gqa
Download the config.json and the generate_config.json files from HuggingFace and add them to the above folder
curl -H "Authorization: Bearer <token>" https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/raw/main/config.json > config.json
curl -H "Authorization: Bearer <token>" https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/raw/main/generation_config.json > generate_config.json
Run the model
python run_llama_opt_ort.py
usage: run_llama_opt_ort.py [-h] [--name NAME] [--prompt PROMPT] [--precision PRECISION] [--device DEVICE]
optional arguments:
-h, --help show this help message and exit
--name NAME Llama model name to export and run
--prompt PROMPT Prompt to run Llama with
--precision PRECISION The precision of the model to load
--device DEVICE Where to run the model
Generate the optimized ONNX model with model builder
python -m onnxruntime.models.builder [-m <HF model name> | -i <path to local PyTorch model] -e cpu -p int4 -o <output path>
Run the script to generate text with Llama
cd to the directory with your script and models
python run_llama_genai_ort.py
Install docker
Install other dependencies as well as azureml-inference-server-http
Run azmlinfsrv --entry_script score.py
In another terminal, send a prompt to the endpoint
curl --header "Content-Type: application/json" \
--request POST --data @data.json \
http://localhost:5001/score
az extension add --name ml --alow-preview
Setup the endpoint name, resource group etc
Linux/Mac
export ENDPOINT_NAME=llama
export AZURE_RESOURCE_GROUP=...
export AZURE_SUBSCRIPTION=...
export AZURE_MACHINE_LEARNING_WORKSPACE=...
export HUGGINGFACE_TOKEN=...
az login --use-device-code
az account set --subscription ${AZURE_SUBSCRIPTION}
az configure --defaults workspace=${AZURE_MACHINE_LEARNING_WORKSPACE} group=${AZURE_RESOURCE_GROUP}
Windows
set ENDPOINT_NAME=llama
set AZURE_RESOURCE_GROUP=...
set AZURE_SUBSCRIPTION=...
set AZURE_MACHINE_LEARNING_WORKSPACE=...
set HUGGINGFACE_TOKEN=...
az login --use-device-code
az account set --subscription %AZURE_SUBSCRIPTION%
az configure --defaults workspace=%AZURE_MACHINE_LEARNING_WORKSPACE% group=%AZURE_RESOURCE_GROUP%
Create a local endpoint
az ml online-endpoint create --local -n $ENDPOINT_NAME -f endpoint.yml
Create a local deployment
Note: a slightly different deployment spec is required for local deployments, as the model is specified as a local path rather than model deployment identifier.
az ml online-deployment create --local -n blue --endpoint $ENDPOINT_NAME -f deploy-local.yml --set environment_variables.HUGGINGFACE_TOKEN=${HUGGINGFACE_TOKEN}
(Optional) Update the deployment
az ml online-deployment update --local -n blue --endpoint $ENDPOINT_NAME -f deploy.yml
Upload the model to Azure
Model variants and names
cd models/meta-llama
az ml model create --name Llama-2-7b-chat-hf-fp16 --path Llama-2-7b-chat-hf
Create the endpoint in Azure
az ml online-endpoint create -n $ENDPOINT_NAME -f endpoint.yml
Create the deployment in Azure
az ml online-deployment create -n blue --endpoint $ENDPOINT_NAME -f deploy.yml --set environment_variables.HUGGINGFACE_TOKEN=${HUGGINGFACE_TOKEN}
Allocate traffic to the endpoint
az ml online-endpoint update --name $ENDPOINT_NAME --traffic "green=100"
Consume the online endpoint
curl -H "Authorization: Bearer ${ENDPOINT_TOKEN}" --data @data.json https://llama.australiaeast.inference.ml.azure.com/score
cd onnxruntime
build.sh --config RelWithDebInfo --build_shared_lib --build_wheel --skip_tests --parallel --skip_submodule_sync --use_cuda
az login --use-device-code
az account set --subscription <subscription>
az storage blob upload -f models/meta-llama/Llama-2-7b-chat-hf/rank_0_Llama-2-7b-chat-hf_decoder_merged_model_fp16.onnx --container-name models --account-name nakershadevstorage
az storage blob upload -f models/meta-llama/Llama-2-7b-chat-hf/Llama-2-7b-chat-hf_decoder_merged_model_fp16.onnx.data --container-name models --account-name nakershadevstorage
output_buffer = torch.empty(np.prod(output_shape), dtype=torch_type, device=self.device).contiguous()
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacty of 15.77 GiB of which 12.88 MiB is free. Process 13953 has 15.76 GiB memory in use. Of the allocated memory 480.73 MiB is allocated by PyTorch, and 63.27 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
PYTORCH_NO_CUDA_MEMORY_CACHING=1
32 commits
Shell
86.7%
Python
13.3%
This sample shows you how to run the Llama model with PyTorch, HuggingFace and ONNX Runtime.
python run_llama_pt.py --name <model> --prompt <prompt>
where <model> can be:
PY007/TinyLlama-1.1B-intermediate-step-480k-1TPY007/TinyLlama-1.1B-Chat-v0.3TinyLlama/TinyLlama-1.1B-Chat-v0.6meta-llama/Llama-2-7b-hf if you have access to the gated meta modelpython run_llama_opt_onnx.py
Same options.
Export the model with ONNX Runtime
Note: this step requires 54GB of memory
Install the latest version of torch
pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu118
Install onnxruntime
pip install onnxruntime-gpu
Export the model
Note: you cannot have a local folder that is same as the string passed to the -m argument, as HuggingFace will look for the model in this folder rather than downloading it and will error if it is not there.
huggingface-cli login --token <token>
python -m onnxruntime.transformers.models.llama.convert_to_onnx -m meta-llama/Llama-2-7b-chat-hf --output models/meta-llama/Llama-2-7b-chat-hf --execution_provider cuda --precision fp16 --use_gqa
Download the config.json and the generate_config.json files from HuggingFace and add them to the above folder
curl -H "Authorization: Bearer <token>" https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/raw/main/config.json > config.json
curl -H "Authorization: Bearer <token>" https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/raw/main/generation_config.json > generate_config.json
Run the model
python run_llama_opt_ort.py
usage: run_llama_opt_ort.py [-h] [--name NAME] [--prompt PROMPT] [--precision PRECISION] [--device DEVICE]
optional arguments:
-h, --help show this help message and exit
--name NAME Llama model name to export and run
--prompt PROMPT Prompt to run Llama with
--precision PRECISION The precision of the model to load
--device DEVICE Where to run the model
Generate the optimized ONNX model with model builder
python -m onnxruntime.models.builder [-m <HF model name> | -i <path to local PyTorch model] -e cpu -p int4 -o <output path>
Run the script to generate text with Llama
cd to the directory with your script and models
python run_llama_genai_ort.py
Install docker
Install other dependencies as well as azureml-inference-server-http
Run azmlinfsrv --entry_script score.py
In another terminal, send a prompt to the endpoint
curl --header "Content-Type: application/json" \
--request POST --data @data.json \
http://localhost:5001/score
az extension add --name ml --alow-preview
Setup the endpoint name, resource group etc
Linux/Mac
export ENDPOINT_NAME=llama
export AZURE_RESOURCE_GROUP=...
export AZURE_SUBSCRIPTION=...
export AZURE_MACHINE_LEARNING_WORKSPACE=...
export HUGGINGFACE_TOKEN=...
az login --use-device-code
az account set --subscription ${AZURE_SUBSCRIPTION}
az configure --defaults workspace=${AZURE_MACHINE_LEARNING_WORKSPACE} group=${AZURE_RESOURCE_GROUP}
Windows
set ENDPOINT_NAME=llama
set AZURE_RESOURCE_GROUP=...
set AZURE_SUBSCRIPTION=...
set AZURE_MACHINE_LEARNING_WORKSPACE=...
set HUGGINGFACE_TOKEN=...
az login --use-device-code
az account set --subscription %AZURE_SUBSCRIPTION%
az configure --defaults workspace=%AZURE_MACHINE_LEARNING_WORKSPACE% group=%AZURE_RESOURCE_GROUP%
Create a local endpoint
az ml online-endpoint create --local -n $ENDPOINT_NAME -f endpoint.yml
Create a local deployment
Note: a slightly different deployment spec is required for local deployments, as the model is specified as a local path rather than model deployment identifier.
az ml online-deployment create --local -n blue --endpoint $ENDPOINT_NAME -f deploy-local.yml --set environment_variables.HUGGINGFACE_TOKEN=${HUGGINGFACE_TOKEN}
(Optional) Update the deployment
az ml online-deployment update --local -n blue --endpoint $ENDPOINT_NAME -f deploy.yml
Upload the model to Azure
Model variants and names
cd models/meta-llama
az ml model create --name Llama-2-7b-chat-hf-fp16 --path Llama-2-7b-chat-hf
Create the endpoint in Azure
az ml online-endpoint create -n $ENDPOINT_NAME -f endpoint.yml
Create the deployment in Azure
az ml online-deployment create -n blue --endpoint $ENDPOINT_NAME -f deploy.yml --set environment_variables.HUGGINGFACE_TOKEN=${HUGGINGFACE_TOKEN}
Allocate traffic to the endpoint
az ml online-endpoint update --name $ENDPOINT_NAME --traffic "green=100"
Consume the online endpoint
curl -H "Authorization: Bearer ${ENDPOINT_TOKEN}" --data @data.json https://llama.australiaeast.inference.ml.azure.com/score
cd onnxruntime
build.sh --config RelWithDebInfo --build_shared_lib --build_wheel --skip_tests --parallel --skip_submodule_sync --use_cuda
az login --use-device-code
az account set --subscription <subscription>
az storage blob upload -f models/meta-llama/Llama-2-7b-chat-hf/rank_0_Llama-2-7b-chat-hf_decoder_merged_model_fp16.onnx --container-name models --account-name nakershadevstorage
az storage blob upload -f models/meta-llama/Llama-2-7b-chat-hf/Llama-2-7b-chat-hf_decoder_merged_model_fp16.onnx.data --container-name models --account-name nakershadevstorage
output_buffer = torch.empty(np.prod(output_shape), dtype=torch_type, device=self.device).contiguous()
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacty of 15.77 GiB of which 12.88 MiB is free. Process 13953 has 15.76 GiB memory in use. Of the allocated memory 480.73 MiB is allocated by PyTorch, and 63.27 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
PYTORCH_NO_CUDA_MEMORY_CACHING=1
32 commits
Shell
86.7%
Python
13.3%