# Deploy high performance question-answer model on AzureML with ONNX Runtime
{: .no_toc }
This tutorial takes a BERT model from HuggingFace, converts it to ONNX, and deploys the ONNX model with ONNX Runtime through AzureML.
In the following sections, we use the HuggingFace BERT model trained with Stanford Question Answering Dataset (SQuAD) dataset as an example. You can also train or fine-tune your own question answer model.
The question answer scenario takes a question and a piece of text called a context, and produces answer, which is a string of text taken from the context. This scenario tokenizes and encodes the question and the context, feeds the inputs into the transformer model and generates the answer by producing the most likely start and end tokens in the context, which are then mapped back into words.

The model and scoring code are then deployed on AzureML using an online endpoint.
## Contents
{: .no_toc }
* TOC placeholder
{:toc}
## Pre-requisites
The [source code for this tutorial](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/python/azureml) is published on GitHub.
To run on AzureML, you need:
* an Azure subscription
* an Azure Machine Learning Workspace (see [the AzureML configuration notebook](https://github.com/Azure/MachineLearningNotebooks/blob/56e0ebc5acb9614fac51d8b98ede5acee8003820/configuration.ipynb) for creation of the workspace if you do not already have one)
* the Azure Machine Learning SDK
* the Azure CLI and the Azure Machine learning CLI extension (> version 2.2.2)
You might also find the following resources useful:
* Understand the [architecture and terms](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture) introduced by Azure Machine Learning
* The [Azure Portal](https://portal.azure.com/) allows you to track the status of your deployments.
If you do not have access to an AzureML subscription, you can run this tutorial locally.
## Environment
To install dependencies directly run the following
```bash
pip install torch
pip install transformers
pip install azureml azureml.core
pip install onnxruntime
pip install matplotlib
```
To create a a Jupter kernel from your conda environment, run the following. Replace <kernelname> with the name of your kernel.
Install the AzureML CLI extension, which is used in the deployment steps below
```bash
az login
az extension add --name ml
# Remove the azure-cli-ml extension if it is installed, as it is not compatible with the az ml extension
az extension remove azure-cli-ml
```
## Obtain and convert PyTorch model to ONNX format
In the code below, we obtain a BERT model fine-tuned for question answering with the SQUAD dataset from HuggingFace.
If you'd like to pre-train a BERT model from scratch, follow the instructions in [Pre-train BERT model](https://github.com/microsoft/AzureML-BERT/blob/master/pretrain/PyTorch/notebooks/BERT_Pretrain.ipynb). And if you would like to fine-tune the model with your own dataset, refer to [AzureML BERT Eval Squad](https://github.com/microsoft/AzureML-BERT/blob/master/finetune/PyTorch/notebooks/BERT_Eval_SQUAD.ipynb) or [AzureML BERT Eval GLUE](https://github.com/microsoft/AzureML-BERT/blob/master/finetune/PyTorch/notebooks/BERT_Eval_GLUE.ipynb).
### Export the model
Use the PyTorch ONNX exporter to create a model in ONNX format, to be run with ONNX Runtime.
We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook.
Note that, the following code assumes you have a config.json file containing the subscription information in the same directory as the notebook, or in a sub-directory called .azureml. You can also supply the workspace name, subscription name, and resource group explicity using the Workspace.get() method.
# # If you'd like to delete the models from workspace
# model_to_delete = Model(ws, name)
# model_to_delete.delete()
```
### Deploy the model and scoring code as an AzureML endpoint
Note: the endpoint interface of the Python SDK has not been publicly released yet, so for this section, we will use the Azure ML CLI.
There are three YML files in the yml folder:
*`env.yml`: A conda environment specification, from which the execution environment of the endpoint will be generated
*`endpoint.yml`: The endpoint specification, which simply contains the name of the endpoint and the authorization method
*`deployment.yml`: The deployment specification, which contains specifications of the scoring code, model, and environment. You can create multiple deployments per endpoint, and route different amounts of traffic to the deployments. For this example, we will create only one deployment.
The deployment can take up to 15 minutes. Note also that all of the files in the directory with the notebook will be uploaded into the docker container that forms the basis of your endpoint, including any local copies of the ONNX model (which has already been deployed to AzureML in the previous step). To reduce the deployment time remove any local copies of large files, before creating the endpoint.
```bash
az ml online-endpoint create --name question-answer-ort --file yml/endpoint.yml --subscription {ws.subscription_id} --resource-group {ws.resource_group} --workspace-name {ws.name}
az ml online-deployment create --endpoint-name question-answer-ort --name blue --file yml/deployment.yml --all-traffic --subscription {ws.subscription_id} --resource-group {ws.resource_group} --workspace-name {ws.name}
```
### Test the deployed endpoint
The following command runs the deployed question answer model. There is a test question in the test-data.json file. You can edit this file with your own question and context.
```bash
az ml online-endpoint invoke --name question-answer-ort --request-file test-data.json --subscription {ws.subscription_id} --resource-group {ws.resource_group} --workspace-name {ws.name}
```
If you've made it this far, you've deployed a working endpoint that answers a question using an ONNX model.
You can supply your own questions and context to answer a question!
### Clean up Azure resources
The following command deletes the AzureML endpoint that you have deployed. You may also want to clean up your AzureML workspace, compute, and registered models.
```bash
az ml online-endpoint delete --name question-answer-ort --yes --subscription {ws.subscription_id} --resource-group {ws.resource_group} --workspace-name {ws.name}