# Beam\_search bottlenecks inference with only 1 used cpu

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbeam-search-bottlenecks-inference-with-only-1-used-cpu%2F24315%3E%5C
**Category:** 🤗Transformers\
**Created:** [October 12, 2022, 7:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbeam-search-bottlenecks-inference-with-only-1-used-cpu%2F24315 "2022-10-12T07:48:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adelplace](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/adelplace/32/12037_2.png) [@adelplace](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fadelplace)%5C
**Post date:** [October 12, 2022, 7:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbeam-search-bottlenecks-inference-with-only-1-used-cpu%2F24315%2F1 "2022-10-12T07:48:18Z")

</div>

Hello there, 🙂

I am having trouble optimizing my inference translation pipeline. The bottleneck seems to be beam\_search using only 1 cpu whereas 1 gpu and 16 cpus are available.

Here is an overview of cpu usage thanks to py-spy:

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/3/31b5eb86a438b06588d990665d40a7d3dc1b244b.png)

I have looked at the code to try to understand what is happening without success.  
([https://github.com/huggingface/transformers/blob/bd469c40659ce76c81f69c7726759d249b4aef49/src/transformers/generation\_beam\_search.py#L208](https://github.com/huggingface/transformers/blob/bd469c40659ce76c81f69c7726759d249b4aef49/src/transformers/generation_beam_search.py#L208))

If you need the lines of the problematic code :

```auto
Thread 188015 (active): "MainThread"
    process (transformers/generation_beam_search.py:273)
    beam_search (transformers/generation_utils.py:2285)
    generate (transformers/generation_utils.py:1385)
    decorate_context (torch/autograd/grad_mode.py:27)
    infer_dataset (multi_translator.py:43)
    _CallAndUpdateTrace (fire/core.py:681)
    _Fire (fire/core.py:466)
    Fire (fire/core.py:141)
    main (multi_translator.py:58)
    <module> (multi_translator.py:62)
    _run_code (runpy.py:87)
    _run_module_as_main (runpy.py:197)

```

What seems strange is that I thought beam\_search was using the gpu to be fast (we can see the device=device in the code). I don’t know why the cpu is used here and how to make it use either the gpu or all the available cpus.

Here is a small reproducible code :

```auto
device=0

tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en").to(device)
dataset_class = TranslationDataset("data.csv", tokenizer)

for input_ids, attention_mask in tqdm.tqdm(DataLoader(dataset_class, 
                                                      batch_size=64, 
                                                      num_workers=16, 
                                                      pin_memory=True)):
      tokenized_outputs = model.generate(input_ids=input_ids.to(device), 
                                         attention_mask=attention_mask.to(device), 
                                         max_length=512)

```

```auto
class TranslationDataset(Dataset):
    def __init__ (self, dataset_path, tokenizer):
        super(). __init__ ()
        self.tokenizer = tokenizer
        self.dataset = pd.read_csv(dataset_path)["text"].values
        
    def __len__ (self) -> int:
        return len(self.dataset)

    def __getitem__ (self, idx: int):
        x = self.tokenizer(self.dataset[idx], return_tensors="pt", max_length=512, padding='max_length')
        return x['input_ids'][0], x['attention_mask'][0]

```

Could you help me figure out why beam\_search is the bottleneck of the pipeline and how to make it work on multi cpus or gpu please ? 🙂

Relevant infos:

- num\_beams \> 1
- num\_beam\_groups = 1
- do\_sample = False
- is\_constraint\_gen\_mode = False

Thanks in advance 😃

---

<div class="post-metadata">

**Author:** ![adelplace](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/adelplace/32/12037_2.png) [@adelplace](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fadelplace)%5C
**Post date:** [October 13, 2022, 12:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fbeam-search-bottlenecks-inference-with-only-1-used-cpu%2F24315%2F2 "2022-10-13T12:06:01Z")

</div>

It seems like I am not the only one facing this problem :

> [@Model.generate() is extremely slow while using beam search](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmodel-generate-is-extremely-slow-while-using-beam-search%2F6915)%3A
>
> I was previously using trainer.predict() to run my inference but it does not offer that much flexibility and control over generation. For instance, I want to have more than one generation for each sample and it is not possible with trainer.predict(). So I changed to model.generate(). The problem is now I have to handle batching on my own and can not really use multiple GPUs. Still, I feel like it can not be that slow even under these circumstances. Below I have a small code snippet that does the…

Any ideas of solution ? 🙂
