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

Created on 29 Nov 2018  Â·  15Comments  Â·  Source: facebookresearch/InferSent

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                    |
|-------------------------------+----------------------+----------------------+

Most helpful comment

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)          

All 15 comments

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

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

any resolution to stride error?

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)          

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.

Merged PR. Thanks!

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

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

@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)        

@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.

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

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

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 :))

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

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)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hitzkrieg picture hitzkrieg  Â·  3Comments

Tiriar picture Tiriar  Â·  4Comments

dami23 picture dami23  Â·  9Comments

gkutiel picture gkutiel  Â·  12Comments

dougc333 picture dougc333  Â·  5Comments