# way to make inference Zero Shot pipeline faster?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%3E%5C
**Category:** Beginners\
**Created:** [October 6, 2020, 1:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384 "2020-10-06T13:27:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![munggok](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@munggok](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmunggok)%5C
**Post date:** [October 6, 2020, 1:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F1 "2020-10-06T13:27:55Z")

</div>

Hi  
Can you guys give me tips how to make Zero Shot pipeline inference faster?

My current approach right now is reducing the model size/parameter  
(trying to train “base model” instead of "large model)

Is there another approach?

---

<div class="post-metadata">

**Author:** ![joeddav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/joeddav/32/53881_2.png) [@joeddav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjoeddav)%5C
**Post date:** [October 6, 2020, 1:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F2 "2020-10-06T13:39:50Z")

</div>

There’s some discussion in [this topic](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fnew-pipeline-for-zero-shot-text-classification%2F681) that you could check out.

Here are a few things you can do:

- Try out one of the community-uploaded [distilled models](https://huggingface.co/models?filter=distilbart-mnli) on the hub (thx @valhalla) . I’ve found them to get pretty similar performance on zero shot classification and some of them are much smaller and faster. I’d start with `valhalla/distilbart-mnli-12-3` (models can be specified by passing e.g. `pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")` when you construct a model.
- If you’re on GPU, make sure you’re passing `device=0` to the pipeline factory in to utilize cuda.
- If you’re on CPU, try running the pipeline with ONNX Runtime. You should get a boost. [Here’s a project](https://github.com/patil-suraj/onnx_transformers) (thx again @valhalla) that lets you use HF pipelines with ORT automatically.
- If you have a lot of candidate labels, try to get clever about passing just the most likely ones to the pipeline. Passing a large # of labels for each sentence is really going to slow you down since each sentence/label pair has to be passed to the model together. If you have 100 possible labels but you can use some kind of heuristic or simpler model to narrow it down, that will help a lot.
- Use mixed precision. This is [pretty easy](https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/) if using PyTorch 1.6.

---

<div class="post-metadata">

**Author:** ![munggok](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@munggok](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmunggok)%5C
**Post date:** [October 6, 2020, 3:30pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F3 "2020-10-06T15:30:52Z")

</div>

thanks for the advice @joeddav and also thanks to @valhalla for amazing project about distiled model

distiled model seems interesting…will try to look for it,  
and it would be great if its support more model,such as xlm-roberta

anyway  
i’m trying to train/reproduce your [xlm-model](https://huggingface.co/joeddav/xlm-roberta-large-xnli)  
but using **base-model** instead of **large** one to improve inference speed.

will try use [this step](https://colab.research.google.com/github/huggingface/blog/blob/master/notebooks/trainer/01_text_classification.ipynb) maybe

using xlm-r because it support more languages

any tips that i should aware of ?  
thanks again

---

<div class="post-metadata">

**Author:** ![Buckeyes2019](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@Buckeyes2019](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBuckeyes2019)%5C
**Post date:** [December 23, 2020, 3:45am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F4 "2020-12-23T03:45:42Z")

</div>

> [@joeddav](#):
>
> mixed precision

I noticed using the zero-shot-classification pipeline that loading the model (i.e. this line: classifier = pipeline(“zero-shot-classification”, device=0)) takes about 60 seconds, but that inference afterward is quite fast. Is there a way to speed up the model/tokenizer loading process? Thanks!

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [December 23, 2020, 6:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F5 "2020-12-23T06:05:44Z")

</div>

the pipeline actually loads the model twice, once in the [get\_framework](https://github.com/huggingface/transformers/blob/master/src/transformers/pipelines.py#L90) function and then again on line 3296

> <https://github.com/huggingface/transformers/blob/master/src/transformers/pipelines.py#L3296>

For now, if you want, you could just hardcode the framework as `pt` and remove the call to `get_framework` to load the model once.

---

<div class="post-metadata">

**Author:** ![joeddav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/joeddav/32/53881_2.png) [@joeddav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjoeddav)%5C
**Post date:** [December 23, 2020, 5:17pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F6 "2020-12-23T17:17:42Z")

</div>

Seems like we should add a utility function to `file_utils.py` to check whether a tf/pt model file exists at a path without having to download it so that we don’t have to do this. Thoughts from @sgugger or @lysandre maybe?

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [December 23, 2020, 5:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F7 "2020-12-23T17:48:27Z")

</div>

I’m confused about what the question is. You can pass a local path to `XyzModel.from_pretrained` and it won’t download anything.

---

<div class="post-metadata">

**Author:** ![Buckeyes2019](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@Buckeyes2019](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBuckeyes2019)%5C
**Post date:** [December 23, 2020, 6:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F8 "2020-12-23T18:07:34Z")

</div>

Awesome thanks! – I made the change below and load time dropped from 61 second to 32 seconds:

classifier = pipeline(“zero-shot-classification”, device=0) ----\> classifier = pipeline(“zero-shot-classification”, framework=“pt”, device=0)

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [December 23, 2020, 6:26pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F9 "2020-12-23T18:26:29Z")

</div>

@sgugger the issue is whether the model is local or not the pipeline loads it twice, which adds up significant time for big models like `bart-large`

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [December 23, 2020, 6:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F10 "2020-12-23T18:38:47Z")

</div>

Yes, so this has nothing to do with `files_utils`.

---

<div class="post-metadata">

**Author:** ![joeddav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/joeddav/32/53881_2.png) [@joeddav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjoeddav)%5C
**Post date:** [January 7, 2021, 2:37pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F11 "2021-01-07T14:37:08Z")

</div>

I should have been clearer. The problem is that the [`get_framework`](https://github.com/huggingface/transformers/blob/master/src/transformers/pipelines/base.py#L49) function in the pipelines implementation determines the framework with a try/catch, attempting to load the model in pytorch and if it fails, loading it with tensorflow. But then it just throws the model away, so the pipeline constructor has to load it again later.

What I was trying to say is: would it make sense to have a utility in `file_utils.py` that can tell you whether a file exists without having to download the whole thing? In this case, it would allow us to check whether a model file exists for a particular framework (e.g. `pytorch_model.bin`) without having to wait for the large file to download if it does. I imagine that could be useful in other places too, but I’m not the expert, so I thought I’d see what you thought 🙂 If it wouldn’t be useful elsewhere, there’s probably an easier workaround without leaving `pipelines.py`.

(sorry for the belated response)

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [January 7, 2021, 3:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F12 "2021-01-07T15:36:42Z")

</div>

Understood now! This function could certainly be useful, yes.

---

<div class="post-metadata">

**Author:** ![sc3051](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@sc3051](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsc3051)%5C
**Post date:** [May 31, 2024, 11:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F13 "2024-05-31T23:20:29Z")

</div>

@joeddav @valhalla  
Thanks for the amazing work on the original zero-shot classification model/pipeline and the distilled versions. I am a bit late to the party but I want to know if there are new techniques that I can apply to speed up inference on a single GPU if I am using valhalla/distilbart-mnli-12-3 or a fine-tuned version of that. I read through this guide: [GPU inference](https://huggingface.co/docs/transformers/en/perf_infer_gpu_one) but to be honest I am not sure if these can be applied to the valhalla/distilbart-mnli-12-3 model given that some layers have been removed during distillation. I would really appreciate some pointers from you guys. Thank you!

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [June 3, 2024, 2:14pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F14 "2024-06-03T14:14:43Z")

</div>

Techniques which are often used to speed up models include quantization. I’d recommend taking a look at the Optimum library which implements a lot of optimizations for models available in the Transformers library, tailored towards various hardware providers: [🤗 Optimum](https://huggingface.co/docs/optimum/en/index). ONNX is a framework often used by companies in production to speed up inference.

---

<div class="post-metadata">

**Author:** ![hgbchatfp](https://avatars.discourse-cdn.com/v4/letter/h/848f3c/32.png) [@hgbchatfp](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhgbchatfp)%5C
**Post date:** [November 24, 2024, 6:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fway-to-make-inference-zero-shot-pipeline-faster%2F1384%2F15 "2024-11-24T18:19:10Z")

</div>

set torch\_dtype=torch.float16 likely faster, reference [pipelines doc](https://huggingface.co/docs/transformers/en/main_classes/pipelines), pipe = pipeline(“text-generation”, model=model\_id, torch\_dtype=torch.float16, batch\_size=2, device=0)
