I would like to use sentence-transformer (https://www.sbert.net/) to encode some English sentences. In order to improve the efficiency, I am trying to run it on 2 T4 GPUs from Jupyter notebook on GCP (Linux Debian python 3.8). (The original question was posted on https://github.com/UKPLab/sentence-transformers/issues/2235 but no response).
from sentence_transformers import SentenceTransformer, LoggingHandler
import logging
logging.basicConfig(format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO,
handlers=[LoggingHandler()])
sentences = ["This is sentence {}".format(i) for i in range(10)]
#Define the model
model = SentenceTransformer('all-MiniLM-L6-v2', device='cuda')
#Start the multi-process pool on all available CUDA devices
pool = model.start_multi_process_pool(target_devices=['cuda:0', 'cuda:1'])
#Compute the embeddings using the multi-process pool
emb = model.encode_multi_process(sentences, pool). # error - Jupyter kernel restarting
print("Embeddings computed. Shape:", emb.shape, "type: ", type(emb))
print("Embeddings computed:", emb)
Output:
- Load pretrained SentenceTransformer: all-MiniLM-L6-v2
- Start multi-process pool on devices: cuda:0, cuda:1
Then, I got error:
Kernel RestartingThe kernel for my_notebook.ipynb appears to have died. It will restart automatically.
Could anybody let me know if I missed anything ?
============== UPDATE ===========
TypeError Traceback (most recent call last)
Cell In[13], line 15
12 model = SentenceTransformer('all-MiniLM-L6-v2')
14 # Move the model to the first device
---> 15 model = model.to(devices[0])
17 # Wrap the model with DataParallel to utilize multiple GPUs
18 model = torch.nn.DataParallel(model, device_ids=[device.index for device in devices])
File /usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py:1126, in Module.to(self, *args, **kwargs)
1039 def to(self, *args, **kwargs):
1040 r"""Moves and/or casts the parameters and buffers.
1041
1042 This can be called as
(...)
1123
1124 """
-> 1126 device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to(*args, **kwargs)
1128 if dtype is not None:
1129 if not (dtype.is_floating_point or dtype.is_complex):
TypeError: to() received an invalid combination of arguments - got (device), but expected one of:
* (torch.device device, torch.dtype dtype, bool non_blocking, bool copy, *, torch.memory_format memory_format)
* (torch.dtype dtype, bool non_blocking, bool copy, *, torch.memory_format memory_format)
* (Tensor tensor, bool non_blocking, bool copy, *, torch.memory_format memory_format)