Skip to content

Multiprocessing CUDA issues when importing transformers #9338

@maxjeblick

Description

@maxjeblick

Environment info

  • transformers version: 4.2.0dev0
  • Platform: Linux-4.15.0-128-generic-x86_64-with-Ubuntu-18.04-bionic
  • Python version: 3.6.9
  • PyTorch version (GPU?): 1.6.0 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Using GPU in script?: Yes
  • Using distributed or parallel set-up in script?: Partly

Who can help

@stas00

Information

When using multiprocessing, importing the transformers package causes
RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method

The problem arises when using:

  • import transformers is used in the main process.
    This is due to the following line in modeling_fsmt.py, removing torch.cuda.is_available() call resolves the issue.

To reproduce

import multiprocessing
import transformers  # NOQA
# You can also call torch.cuda instead of import transformers to get the same error
import torch.nn as nn


class Net(nn.Module):

    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 10)

    def forward(self, x):
        return self.linear(x)


def to_cuda(i):
    net = Net().cuda()
    print(f'Called {i} process')


try:
    cpus = multiprocessing.cpu_count()
except NotImplementedError:
    cpus = 2  # arbitrary default

pool = multiprocessing.Pool(processes=cpus)
pool.map(to_cuda, range(10))

Expected behavior

The code snippet above runs without issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions