0

I have a problem I cannot manage to solve... I read the Pytorch documentation to get rid of this but my code looks fine to me, but an error occurs.

        indices = ...
        X = ...
        Y = ...

        print(torch.min(indices)) # tensor(0, device='cuda:0')
        print(torch.max(indices)) # tensor(30, device='cuda:0')
        print(indices.dtype) # torch.int64
        print(indices.shape) # torch.Size([498])

        print(X.shape) # torch.Size([498, 2048])
        print(X.dtype) # torch.float32

        print(Y.shape) # torch.Size([31, 2048])
        print(Y.dtype) # torch.float32

        Y = Y.index_add(0, indices, X)

The call of index_add results in:

RuntimeError: number of dims don't match in permute

What am I doing wrong? Thank you in advance.

EDIT: From the documentation:

The dim-th dimension of tensor must have the same size as the length of index (which must be a vector), and all other dimensions must match self, or an error will be raised.

In this case dim=0 tensor=X self=Y and it follows that X.shape[0] should be equal to len(indices) and that X.shape[1] should be equal Y.shape[1], that is the case... might it be a pytorch bug?

EDIT2: The error occurs just for specific values of tensors and using:

torch.use_deterministic_algorithms(True)
torch.manual_seed(1234)

So it should be a pytorch bug.

1
  • Can you provide a fully reproducible code for the bug ? With seeds and specific tensors. Commented Nov 8, 2021 at 13:15

1 Answer 1

0

You are probably trying to do

X = X.index_add(0, indices, Y)

you swapped X and Y.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer, but no, I want to add X to Y using the indices, I refer to the example in pytorch.org/docs/stable/generated/torch.Tensor.index_add_.html
From my understanding I need to have one index for each row of X that will be used to add that row to the row of Y specified by the index. X has 498 rows and I have 498 indices between 0 and 30 (in fact Y has 31 rows). Am I wrong? In the example I referred seem it works in this way
Strange, I just simulated your stuff with a minimal example, and it works. pastebin.com/4Unt5mka (this code works without any error). See if I reproduced correctly.
Yes you did it correctly and substituting in my code my tensors with random ones it works aswell... with the values of my particular tensors it raises the exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.