Picture by Editor | ChatGPTÂ
#Â Introduction
Â
Hugging Face has develop into the usual for a lot of AI builders and information scientists as a result of it drastically lowers the barrier to working with superior AI. Somewhat than working with AI fashions from scratch, builders can entry a variety of pretrained fashions with out problem. Customers can even adapt these fashions with customized datasets and deploy them shortly.
One of many Hugging Face framework API wrappers is the Transformers Pipelines, a sequence of packages that consists of the pretrained mannequin, its tokenizer, pre- and post-processing, and associated elements to make an AI use case work. These pipelines summary advanced code and supply a easy, seamless API.
Nevertheless, working with Transformers Pipelines can get messy and will not yield an optimum pipeline. That’s the reason we’ll discover 5 other ways you may optimize your Transformers Pipelines.
Let’s get into it.
Â
#Â 1. Batch Inference Requests
Â
Usually, when utilizing Transformers Pipelines, we don’t absolutely make the most of the graphics processing unit (GPU). Batch processing of a number of inputs can considerably enhance GPU utilization and improve inference effectivity.
As an alternative of processing one pattern at a time, you should utilize the pipeline’s batch_size parameter or go a listing of inputs so the mannequin processes a number of inputs in a single ahead go. Here’s a code instance:
from transformers import pipeline
pipe = pipeline(
activity="text-classification",
mannequin="distilbert-base-uncased-finetuned-sst-2-english",
device_map="auto"
)
texts = [
"Great product and fast delivery!",
"The UI is confusing and slow.",
"Support resolved my issue quickly.",
"Not worth the price."
]
outcomes = pipe(texts, batch_size=16, truncation=True, padding=True)
for r in outcomes:
print(r)
Â
By batching requests, you may obtain increased throughput with solely a minimal impression on latency.
Â
#Â 2. Use Decrease Precision And Quantization
Â
Many pretrained fashions fail at inference as a result of improvement and manufacturing environments don’t have sufficient reminiscence. Decrease numerical precision helps cut back reminiscence utilization and hurries up inference with out sacrificing a lot accuracy.
For instance, right here is find out how to use half precision on the GPU in a Transformers Pipeline:
import torch
from transformers import AutoModelForSequenceClassification
mannequin = AutoModelForSequenceClassification.from_pretrained(
model_id,
torch_dtype=torch.float16
)
Â
Equally, quantization strategies can compress mannequin weights with out noticeably degrading efficiency:
# Requires bitsandbytes for 8-bit quantization
from transformers import AutoModelForCausalLM
mannequin = AutoModelForCausalLM.from_pretrained(
model_id,
load_in_8bit=True,
device_map="auto"
)
Â
Utilizing decrease precision and quantization in manufacturing often hurries up pipelines and reduces reminiscence use with out considerably impacting mannequin accuracy.
Â
#Â 3. Choose Environment friendly Mannequin Architectures
Â
In lots of functions, you do not want the biggest mannequin to resolve the duty. Deciding on a lighter transformer structure, resembling a distilled mannequin, usually yields higher latency and throughput with an appropriate accuracy trade-off.
Compact fashions or distilled variations, resembling DistilBERT, retain a lot of the unique mannequin’s accuracy however with far fewer parameters, leading to sooner inference.
Select a mannequin whose structure is optimized for inference and fits your activity’s accuracy necessities.
Â
#Â 4. Leverage Caching
Â
Many methods waste compute by repeating costly work. Caching can considerably improve efficiency by reusing the outcomes of expensive computations.
with torch.inference_mode():
output_ids = mannequin.generate(
**inputs,
max_new_tokens=120,
do_sample=False,
use_cache=True
)
Â
Environment friendly caching reduces computation time and improves response occasions, reducing latency in manufacturing methods.
Â
#Â 5. Use An Accelerated Runtime By way of Optimum (ONNX Runtime)
Â
Many pipelines run in a PyTorch not-so-optimal mode, which provides Python overhead and further reminiscence copies. Utilizing Optimum with Open Neural Community Change (ONNX) Runtime — through ONNX Runtime — converts the mannequin to a static graph and fuses operations, so the runtime can use sooner kernels on a central processing unit (CPU) or GPU with much less overhead. The result’s often sooner inference, particularly on CPU or combined {hardware}, with out altering the way you name the pipeline.
Set up the required packages with:
pip set up -U transformers optimum[onnxruntime] onnxruntime
Â
Then, convert the mannequin with code like this:
from optimum.onnxruntime import ORTModelForSequenceClassification
ort_model = ORTModelForSequenceClassification.from_pretrained(
model_id,
from_transformers=True
)
Â
By changing the pipeline to ONNX Runtime by way of Optimum, you may hold your present pipeline code whereas getting decrease latency and extra environment friendly inference.
Â
#Â Wrapping Up
Â
Transformers Pipelines is an API wrapper within the Hugging Face framework that facilitates AI software improvement by condensing advanced code into easier interfaces. On this article, we explored 5 tricks to optimize Hugging Face Transformers Pipelines, from batch inference requests, to choosing environment friendly mannequin architectures, to leveraging caching and past.
I hope this has helped!
Â
Â
Cornellius Yudha Wijaya is an information science assistant supervisor and information author. Whereas working full-time at Allianz Indonesia, he likes to share Python and information ideas through social media and writing media. Cornellius writes on a wide range of AI and machine studying subjects.
