I am using Bert sentence transformers to encode strings into sentence embeddings in a scalable way and I’ve run into a blocking problem: I can’t get the code to run on a GPU.
Without using a GPU I can pass individual strings to SentenceTransformer.encode() (a Model) and get vector representations. When I use a GPU I get a GPU error that arguments are located on different machines. How do I get string data to the GPU? The docs I find indicate this can’t be done and I need to find a way to batch a bunch of strings to get encoded at once on the GPU. When I pass a numpy array of strings to Model.encode(), I get an exception: arguments are located on different GPUs
. How do you get string data on a GPU?
Please help!
I looked up how to send a numpy array of strings to a GPU using PyTorch and there seems to be no way to do that. I looked at DataParallel
and it doesn’t seem to encode data, and I can’t get it to work.
I start out creating a SentenceTransformer using a pooling model and I can run it on one string at a time. The encode()
method takes a string or list of strings as an argument. Without sending it to the GPU it works fine. I can’t figure out how to get a list or numpy array of strings to the GPU! It is maddening, there is nothing out there on this. It must be possible because sentence transformers operate on strings. But how?
device = torch.device("cuda:0")
...
sentence_model = SentenceTransformer(
modules=[bert_model, pooling_model]
)
sentence_model = sentence_model.to(device)
RuntimeError: arguments are located on different GPUs at /opt/conda/conda-bld/pytorch_1579022034529/work/aten/src/THC/generic/THCTensorIndex.cu:400
When I look at the series, it is a numpy array of .
In [21]: series.dtype
Out[21]: dtype('O')
In [22]: series.values
Out[22]:
array(['I like cats but also dogs', 'I like cars full of cats',
'I like apples but not oranges', ..., 'I like cars full of cats',
'I like apples but not oranges', 'I like tutus on ballerinas.'],
dtype=object)
When I try to create a tensor to get the data on a GPU, I run into this problem:
In [20]: torch.from_numpy(series.values)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-d0a262ca0b02> in <module>
----> 1 torch.from_numpy(series.values)
TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool.
So I am very much stuck. How do you encode multiple strings on a GPU?