DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Navigating the Complexities of Text Summarization With NLP
  • Prompt Engineering Is Not a Thing
  • Understanding the Basics of Neural Networks and Deep Learning
  • Transforming Translation: The Power of Context in NLP

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Start Coding With Google Cloud Workstations
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Accelerating ML Model Inferencing

Accelerating ML Model Inferencing

This article explains how to Balance speed and accuracy for the execution of machine learning models in production.

By 
Kumar Abhishek user avatar
Kumar Abhishek
·
Jan. 12, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

Expedia Group™️ acts as a booking agent for all sorts of travel. An issue we’ve wrestled with is when a hotel (or vacation rental, activity, etc.) shares public contact information through our platform that allows for direct booking. This act violates our service agreements, and we must detect if it happens. Furthermore, this mechanism needs to scale to review an ongoing stream of data generated by bookings, website updates, client updates, etc.

This is not a unique problem in the travel industry. It can be generalized to verify a whole class of financial and legal agreements on an ongoing basis. Speed is of the essence; quickly detecting violations protects the platform’s viability and stops possible fraud.

We require real-time decisions from unstructured data but would a large language model be up to the task? We started with BERT, the open-source transformer-based machine learning model for Natural Language Processing(NLP). After running sample data, we discovered it could give us inferences reliably — but was too slow.

In fact, there was no way to get results within the constraints of our service-level agreement (SLA). Simulating each call with listing descriptions of 500 words or more, the model took up to two seconds. The speed was too slow for upstream services and didn’t meet the 250 milliseconds SLA we promised.

This was when we embarked on a journey to improve speed while maintaining inference reliability. We went straight to standard model compression techniques which are readily available and with which we were already familiar:

  1. Knowledge Distillation (KD) Teacher-to-student transfer reduces model size.
  2. Graph compression using Open Neural Network Exchange(ONNX) allows for data transfer to many different environments. It also has training capabilities.
  3. Using specialized hardware like GPU/Xeon processors through AWS exchange

We used these techniques to improve speed without significantly impacting the model’s accuracy.

A Bit About Knowledge Distillation (KD)

In machine learning, Knowledge Distillation refers to transferring knowledge from a large model to a smaller one. The end goal is to develop accurate models that are more computationally efficient.

In a nutshell, we want to transfer knowledge from larger models (e.g., deep neural networks) to smaller models (e.g., shallow neural networks).

One solution is to use a technique called Knowledge Distillation. This involves “distilling” the knowledge from your large model into a new one that’s smaller, more manageable, and easier to work with.

Both Teacher and Student consume training data, although Student has fewer layers of learning. Teacher prediction feeds into calibration which feeds into Student prediction, hence Distilled Knowledge moves from Teacher to Student. Finally true labels are applied to Student prediction.
Source: Lehan Yang and Jincen Song

The general idea of performing KD is:

  • Use a pre-trained teacher model.
  • Then, take some training data on which you want to train the Student model.
  • Pass data through the Teacher model to generate soft labels (actual labels are “hard” labels).
  • Train Student model on data + soft labels.

There are several ways to distill the knowledge of the Teacher model into a simpler Student model. The main techniques are Multi-teacher, Offline, and Online.

Multi-teacher approaches benefit from more diverse data sources and, in some cases, less biased data. For example, for offline KD, the Teacher is trained, and then the model is transferred directly to the Student. Conversely, online KD uses the ensemble result from multiple soft target Student models to generate a Student model.

Technique 1: Use DistilBERT Instead of BERT

  • DistilBERT has the same general architecture as BERT but only half the layers of BERT.
  • DistilBERT was trained on eight 16GB V100 GPUs for around 90 hours.
  • KD is done during the pre-training phase, reducing the size of BERT by 40% while retaining 97% accuracy and becoming 60% faster.

Technique 2: Convert the Model to ONNX

  • Convert the DistilBERT model to ONNX format.
  • It is effectively a serialized format to represent the model and additionally functions as a model compression technique.

Technique 3: Use a Suitable AWS Instance Type

  • Use inf1.6xlarge instance type to bring down latency further (published improvement of 2.3 X)
  • 24 vCPU, 48 GiB memory
  • 4 AWS Inferentia chips — dedicated to inference with a large onboard memory cache and speeds of 128 TOPS.
  • Much better than g4dn.2xlarge(GPU) and c5–9xlarge(compute intensive) instance types.

Our Request/Response

Request

 
{
  "transactionContext": {
    "transactionId": "123456"
  },
  "attributes": {
    "listingDescription": "Near Xel Casino and the Cosmo Casino, the Tenebian Resort Las Vegas provides a casino, a nightclub, and a grocery/convenience store. For those looking to try their luck, this resort boasts a sportsbook, 137 casino gaming tables, and 7 casino VIP rooms. Indulge in a hot stone massage, a manicure/pedicure, and hydrotherapy at City Ranch Spa + Fitness, the onsite spa. Be sure to enjoy a meal at any of the 21 onsite restaurants, which feature Asian cuisine and happy hour. Yoga classes and aerobics classes are offered at the gym."
  }
}


Response

 
{
  "modelCode": "DistilBertNLPSupplyListingDescHA",
  "modelVersion": "1.0",
  "refreshVersion": "v20211220",
  "probabilityScore": "1.02e-10",
  "modelSegment": "SUPPLY",
  "topContributor": "listingDescription",
  "scoreDetails": {
    "label": "NonFraud"
  }
}


Numbers

 
|     Label    | Num requests |  Avg  | tp90 (ms) |  tp99  |  Min  |   Max  | throughput |
|--------------|--------------|-------|-----------|--------|-------|--------|------------|
| HTTP Request |      60      | 77.25 |   96.75   | 110.67 | 68.25 | 126.75 |   12.71    |

Latency for DistilBERT+ONNX model

Notes:

  • Averaged over 12 different runs using Apache Jmeter, free Java open-source software for load testing.
  • Predictor service running on AWS ECS with 1 Fargate instance.

More Numbers

 
| Model           | Size   | Latency tp99 (top 99 %-ile) | Accuracy           |
|-----------------|--------|-----------------------------|--------------------|
| BERT            | 712 MB | >500 ms                     | 91.14              |
| DistilBERT      | 528 MB | 164 ms                      | 90.48              |
| DistilBERT+ONNX | 516 MB | 116 ms                      | same as DistilBERT |


  • Instance type used: inf1.6xlarge
  • Each input request: 512 tokens

The bottom line was that our work was successful. We met both our accuracy and our latency goals. We were able to improve latency by 95%(two sec to 110 milliseconds) and reduce the model size by 27% while impacting accuracy by only 1%.

Following this general procedure should work for a lot of common NLP challenges. As for us, we plan to use the three-step technique outlined above for various use cases we have today. Then it’s off to the next challenge.

AWS Entity component system Language model Load testing Machine learning NLP Data (computing) Label neural network Neural Networks (journal)

Published at DZone with permission of Kumar Abhishek. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Navigating the Complexities of Text Summarization With NLP
  • Prompt Engineering Is Not a Thing
  • Understanding the Basics of Neural Networks and Deep Learning
  • Transforming Translation: The Power of Context in NLP

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!