Skip to content

About

Speed up batch inference in huggingface transformers

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Speedformers

Speed up batch inference in huggingface transformers

How to install

Copy the git URL of this repo and

pip install git+URL

How to use

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.

How to get the most of it

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_pipeline your 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.

About

Speed up batch inference in huggingface transformers

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages