# Distributed inference on multiple files

**URL:** <https://discuss.huggingface.co/t/distributed-inference-on-multiple-files/15513>\
**Category:** 🤗Transformers\
**Created:** [March 8, 2022, 3:46pm UTC](https://discuss.huggingface.co/t/distributed-inference-on-multiple-files/15513 "2022-03-08T15:46:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikalex](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@vikalex](https://discuss.huggingface.co/u/vikalex)\
**Post date:** [March 8, 2022, 3:46pm UTC](https://discuss.huggingface.co/t/distributed-inference-on-multiple-files/15513/1 "2022-03-08T15:46:18Z")

</div>

Reproducing the issue from github [Deadlock when loading the model in multiprocessing context · Issue #15976 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/issues/15976)

I am using the following snippet

```python
import torch
from pathlib import Path
import multiprocessing as mp
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

queue = mp.Queue()

def load_model(filename):
    device = queue.get()
    print('Loading')
    model = AutoModelForSeq2SeqLM.from_pretrained('models/sqgen').to(device)
    print('Loaded')
    queue.put(device)

def parallel():
    num_gpus = torch.cuda.device_count()

    with mp.get_context('spawn').Pool(processes=num_gpus) as pool:
        for gpu_id in range(num_gpus):
            queue.put('cuda:{0}'.format(gpu_id))
        pool = mp.Pool(processes=num_gpus)
        flist = list(Path('data').glob('*.json'))
        pool.map(
            load_model,
            flist,
        )
        pool.close()
        pool.join()

if __name__ == ' __main__':
    parallel()

```

This just hangs when loading the model. This is minimal example I cooked up to demonstrate the issue.

What I am actually doing is that, I have 16 large files (possibly more) and 8 GPUs, so I am trying to assign each file to a GPU and do the inference in parallel 8 processes at a time to use all GPUs simultaneously.

Why is this issue happening? Why does model loading deadlock?  
What’s the right way to do what I want to achieve?

---

<div class="post-metadata">

**Author:** ![Gian-hf](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@Gian-hf](https://discuss.huggingface.co/u/Gian-hf)\
**Post date:** [January 22, 2023, 2:20pm UTC](https://discuss.huggingface.co/t/distributed-inference-on-multiple-files/15513/2 "2023-01-22T14:20:09Z")

</div>

Hi, it may be too late but this answer your question:

> **[Parallel Inference of HuggingFace 🤗 Transformers on CPUs](https://towardsdatascience.com/parallel-inference-of-huggingface-transformers-on-cpus-4487c28abe23)**
>
> An introduction to multiprocessing predictions of large machine learning and deep learning models
