End to End sample workflow for Token Classification starting with training in TAO Toolkit and deployment using Riva.
Deploying a Named Entity Recognition Model in Riva
Train Adapt Optimize (TAO) Toolkit provides the capability to export your model in a format that can deployed using NVIDIA Riva, a highly performant application framework for multi-modal conversational AI services using GPUs.
This tutorial explores taking a .riva model, the result of tao token_classification command, and leveraging the Riva ServiceMaker framework to aggregate all the necessary artifacts for Riva deployment to a target environment. Once the model is deployed in Riva, you can issue inference requests to the server. We will demonstrate how quick and straightforward this whole process is.
Learning Objectives
In this notebook, you will learn how to:
- Use Riva ServiceMaker to take a TAO exported .riva and convert it to .rmir
- Deploy the model(s) locally on the Riva Server
- Send inference requests from a demo client using Riva API bindings.
Pre-requisites
To follow along, please make sure:
- You have access to NVIDIA NGC, and are able to download the Riva Quickstart resources
- Have a .riva model file that you wish to deploy. You can obtain this from
tao <task> export(withexport_format=RIVA). Please refer the tutorial on Named entity recognition using Train Adapt Optimize (TAO) Toolkit for more details on training and exporting a .riva model.
Riva ServiceMaker
Servicemaker is the set of tools that aggregates all the necessary artifacts (models, files, configurations, and user settings) for Riva deployment to a target environment. It has two main components as shown below:
1. Riva-build
This step helps build a Riva-ready version of the model. It’s only output is an intermediate format (called a RMIR) of an end to end pipeline for the supported services within Riva. We are taking a NER BERT Model in consideration
riva-build is responsible for the combination of one or more exported models (.riva files) into a single file containing an intermediate format called Riva Model Intermediate Representation (.rmir). This file contains a deployment-agnostic specification of the whole end-to-end pipeline along with all the assets required for the final deployment and inference. Please checkout the documentation to find out more.
2. Riva-deploy
The deployment tool takes as input one or more Riva Model Intermediate Representation (RMIR) files and a target model repository directory. It creates an ensemble configuration specifying the pipeline for the execution and finally writes all those assets to the output model repository directory.
Start Riva Server
Once the model repository is generated, we are ready to start the Riva server. From this step onwards you need to download the Riva QuickStart Resource from NGC. Set the path to the directory here:
Next, we modify config.sh to enable relevant Riva services (nlp for token classification), provide the encryption key, and path to the model repository (riva_model_loc) generated in the previous step among other configurations.
For instance, if above the model repository is generated at $MODEL_LOC/models, then you can specify riva_model_loc as the same directory as MODEL_LOC
Pretrained versions of models specified in models_asr/nlp/tts are fetched from NGC. Since we are using our custom model, we can comment it in models_asr (and any others that are not relevant to your use case).
NOTE: Please perform the step of editing config.sh outside this notebook.
config.sh snipet
# Enable or Disable Riva Services
service_enabled_asr=false ## MAKE CHANGES HERE
service_enabled_nlp=true ## MAKE CHANGES HERE
service_enabled_tts=false ## MAKE CHANGES HERE
# Specify one or more GPUs to use
# specifying more than one GPU is currently an experimental feature, and may result in undefined behaviours.
gpus_to_use="device=0"
# Specify the encryption key to use to deploy models
MODEL_DEPLOY_KEY="tlt_encode" ## Set the model encryption key
# Locations to use for storing models artifacts
...
riva_model_loc="<add path>" ## Replace with MODEL_LOC
# The default RMIRs are downloaded from NGC by default in the above $riva_rmir_loc directory
# If you'd like to skip the download from NGC and use the existing RMIRs in the $riva_rmir_loc
# then set the below $use_existing_rmirs flag to true.
...
use_existing_rmirs=true ## Set to True
Run Inference
Once the Riva server is up and running with your models, you can send inference requests querying the server.
To send GRPC requests, you can install Riva Python API bindings for client. This is available as a pip .whl with the QuickStart.
Otherwise, you can use the riva-client docker container which comes pre-installed with all the inference dependancies.
Connect to Riva server and run inference
Now we actually query the Riva server, let's get started. The following cell queries the riva server(using grpc) to yield a result.
If you've installed the Riva Python API bindings for client using the pip whl, you can execute the above saved script with the following command:
Otherwise, to use the Riva client docker container, you can execute the below docker run command with the right RIVA_CLIENT_CONTAINER name.
Finally, executing the client python script in the above client container
Make sure to close the riva-client docker container before shutting down the jupyter kernel. In addition, do close the riva services container with the riva_stop.sh script in the quickstart