facebookresearch / InferSent

InferSent sentence embeddings
Other
2.28k stars 470 forks source link

ValueError: some of the strides of a given numpy array are negative. This is currently not supported, #99

Closed thammegowda closed 5 years ago

thammegowda commented 5 years ago

Trying to use pretrained InferSent2 Model ( i.e. fastext) to encode sentences. The pretrained model works perfectly on CPU (where I have torch=0.4.1) However, it crashes with cuda backend (where I have torch 1.0.0.dev20181017)

 File ".../InferSent/models.py", line 224, in encode
    batch = self.forward((batch, lengths[stidx:stidx + bsize])).data.cpu().numpy()
  File ".../InferSent/models.py", line 66, in forward
    sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len_sorted)
  File ".../python3.7/site-packages/torch/nn/utils/rnn.py", line 147, in pack_padded_sequence
    lengths = torch.as_tensor(lengths, dtype=torch.int64)
ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.
 $ python --version
Python 3.7.0
$ pip list | grep torch
torch           1.0.0.dev20181017
$ nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.48                 Driver Version: 390.48                    |
|-------------------------------+----------------------+----------------------+
thammegowda commented 5 years ago

Looks like this is related to https://discuss.pytorch.org/t/torch-from-numpy-not-support-negative-strides/3663

mgalbato commented 5 years ago

I also get the exact same error, even on the CPU. I'm using Python 2.7 and Torch 1.0 without CUDA.

sashaostr commented 5 years ago

+1 on CPU, Python 3.6, torch==1.0.0

badri-thinker commented 5 years ago

any resolution to stride error?

sashaostr commented 5 years ago

wait for this PR to be merged: https://github.com/facebookresearch/InferSent/pull/100 or alter models.py manually - add this line sent_len_sorted = sent_len_sorted.copy() here:

       # Sort by length (keep idx)
        sent_len_sorted, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)                 
        sent_len_sorted = sent_len_sorted.copy()
        idx_unsort = np.argsort(idx_sort)          
badri-thinker commented 5 years ago

It worked thanks

Sent from my iPhone

On Dec 11, 2018, at 6:49 AM, alexander ostrikov notifications@github.com wrote:

wait for this PR to be merged: #100 or alter models.py manually - add this line sent_len_sorted = sent_len_sorted.copy() here:

   # Sort by length (keep idx)
    sent_len_sorted, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)                   
    sent_len_sorted = sent_len_sorted.copy()
    idx_unsort = np.argsort(idx_sort)        

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

aconneau commented 5 years ago

Merged PR. Thanks!

kaumilturabit commented 5 years ago

This error is still occuring despite being merged. Can anyone help me finding solution to this problem?

fooSynaptic commented 5 years ago

@sashaostr still not work with the latest version of this rep

sashaostr commented 5 years ago

@fooSynaptic I didn't tried the latest version, I've added the fix manually and it worked for me. The PR made the same, so it's strange, but anyway manual fix worked:

wait for this PR to be merged: #100 or alter models.py manually - add this line sent_len_sorted = sent_len_sorted.copy() here:

       # Sort by length (keep idx)
        sent_len_sorted, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)                   
        sent_len_sorted = sent_len_sorted.copy()
        idx_unsort = np.argsort(idx_sort)        
fooSynaptic commented 5 years ago

@sashaostr thanks, i modified a lot of the source code and it works for me. I believe its the data process issue for i use different dataset.

datduong commented 5 years ago

@kaumilturabit, I added copy() like this and it works

sent_len,idx_sort = np.sort(sent_len)[::-1].copy(), np.argsort(-sent_len)

MahmudRana commented 5 years ago

wait for this PR to be merged: #100 or alter models.py manually - add this line sent_len_sorted = sent_len_sorted.copy() here:

       # Sort by length (keep idx)
        sent_len_sorted, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)                   
        sent_len_sorted = sent_len_sorted.copy()
        idx_unsort = np.argsort(idx_sort)        

Thanks for the answer and also please change sent_len to sent_len_sorted in the later part of the code. (In case, someone missed that, like me :))

amitlohan commented 4 years ago

Convert the numpy array to np.int16 or np.int32 before converting it to tensor.

ynuwm commented 4 years ago

This also works for me, thanks very much.

sent_len_sorted, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)                 
sent_len_sorted = sent_len_sorted.copy()
idx_unsort = np.argsort(idx_sort)