-
Notifications
You must be signed in to change notification settings - Fork 31.4k
Closed
Description
Environment info
transformersversion: 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
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 transformersis used in the main process.
This is due to the following line inmodeling_fsmt.py, removingtorch.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
Labels
No labels