Speed up batch inference in huggingface transformers
Copy the git URL of this repo and
pip install git+URL
If you'd like to use a language model MODEL_NAME from HuggingFace Hub with a collection of prompts as conversations
sys_msg = "You are a comic. Respond with a joke."
prompts = [[{"role": "system", "content": sys_msg}, {"role": "user", "content": "Hi!"}],
[{"role": "system", "content": sys_msg}, {"role": "user", "content": "What's up?"}]]or strings
prompts = ["Hi!", "What's up"]you would normally use their pipeline
from transformers import pipeline
pipe = pipeline('text-generation', model=MODEL_NAME, max_length=MAX_LENGTH)
responses = [r[0]['generated_text'][-1]['content'] for r in pipe(prompts)]If you replace it with
from speedformers import fast_pipeline
pipe = fast_pipeline(model_name=MODEL_NAME)
responses = pipe(prompts, max_length=MAX_LENGTH)you will get the result much faster. In our benchmark (see test_all.py), ~20x faster.
The efficiency gains of fast_pipeline are achieved with smart batching (batching together prompts of similar length and re-batching once their length goes out of sync).
This will be mostly useless if:
- you only need to do inference on a couple of prompts
- you do the batching yourself and feed
fast_pipelineyour batches or single prompts - you don't have a device that supports data parallelism (GPU/TPU/NeuralEngine) available
It is thus recommended that you call fast_pipeline on your entire dataset.
The only exception would be when it does not fit into RAM: if that's the case you need to load the prompts in mega-batches and let fast_pipeline split them into mini-batches.
Note that fast_pipeline will cache all input prompts even if the prompts argument is a lazy loader.