Real-time machine learning models in real life

Ana Martinazzo23 Sep 2022

Problem

A team has models that run in batches and is asked to serve some of them in real time. The extra work is large: the model needs inputs at request time, its answer must come back in milliseconds, and the serving code must stay consistent with training. A naive endpoint around a trained model fails on all three. The team must decide when real-time inference is worth the cost.

Example from the article: a user opens the Nubank app chat and writes a message. The system must identify which product the message is about (credit card, savings account, investments) and, for example, redirect the user to a human specialist.

Solution in brief

Choose real-time inference only when the use case needs it: a synchronous answer, fresh data, an unknown set of inputs, or on-demand prediction to save compute. Then build two things from the start: a real-time input pipeline that mirrors the training preprocessing, and a light model made fast by compression and caching.

Questions this case answers
  • How to decide between precomputed batch predictions and on-request inference when inputs are unknown in advance and answers are needed synchronously
  • How to keep the feature preprocessing identical between a batch training pipeline and a request-time serving pipeline
  • How to bring the prediction latency of a heavy model down to milliseconds without specialized hardware
  • How to decide whether a model needs real-time inference or whether batch predictions are enough?

Tools and methods

  • Feature store — holds history features precomputed in batch and fetched at request time
  • Knowledge distillation — trains a small model to mimic a large one
  • Post-training quantization — lowers the precision of weights after training to shrink the model
  • DistilBERT — a ready-made distilled BERT, 60% of the original size
  • TensorFlow, PyTorch — both ship pruning and quantization utilities
Continue reading — it's free

Google or a one-time email code. No password.