Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch.scatter_: support index.size(d) > src.size(d) #63265

Open
ekhahniii opened this issue Aug 14, 2021 · 9 comments · May be fixed by #63452
Open

torch.scatter_: support index.size(d) > src.size(d) #63265

ekhahniii opened this issue Aug 14, 2021 · 9 comments · May be fixed by #63452

Comments

@ekhahniii
Copy link

@ekhahniii ekhahniii commented Aug 14, 2021

🚀 Feature

Teach torch.Tensor.scatter_ to handle index.size(d) > src.size(d).

Motivation

Currently, torch.Tensor.scatter_ requires index.size(d) <= src.size(d) for all dimensions d, unless src is float-valued. This constraint seems artificial.

import math
import torch

device = 'cuda'  # fails if device == 'cpu' (cf. #61854)

dim = 1
shape_src = 3, 5
shape_idx = 3, 7

index = torch.empty(shape_idx, dtype=torch.long, device=device).random_(0, shape_src[dim])

# succeeds
output = torch.zeros(shape_src, device=device)
output.scatter_(dim, index, 1.0, reduce='add')
print(output)

# desired usage
# output = torch.zeros(shape_src, device=device)
# values = torch.rand(shape_src, device=device)
# output.scatter_(dim, index, values, reduce='add')   # <-- failure
# print(output)

Alternatives

An inefficient workaround is to chunk the index tensor:

output = torch.zeros(shape_src, device=device)
values = torch.ones(shape_src, device=device)
chunks = math.ceil(index.shape[dim] / output.shape[dim])
for index_chunk in torch.chunk(index, chunks, dim):
    output.scatter_(dim, index_chunk, values, reduce='add')
print(output)
@bfgray3
Copy link
Contributor

@bfgray3 bfgray3 commented Aug 15, 2021

I'd like to work on this issue.

bfgray3 added a commit to bfgray3/pytorch that referenced this issue Aug 16, 2021
@dhivyadharshin
Copy link

@dhivyadharshin dhivyadharshin commented Aug 17, 2021

Can I solve this issue?

@bfgray3 bfgray3 linked a pull request that will close this issue Aug 18, 2021
@adrian2504
Copy link

@adrian2504 adrian2504 commented Aug 18, 2021

Would like to work on this

@MohitAnand-Hub
Copy link

@MohitAnand-Hub MohitAnand-Hub commented Aug 19, 2021

You are looking to index on different dimensions at the same time. I had a look around in the documentation, torch.index_add will only receive a vector as index. My hopes were on torch.scatter but it doesn't to fit well to this problem. As it turns out you can achieve this pretty easily with a little work, the most difficult parts are the setup and teardown.

@dhivyadharshin
Copy link

@dhivyadharshin dhivyadharshin commented Aug 19, 2021

@NaYrA-IaR
Copy link

@NaYrA-IaR NaYrA-IaR commented Aug 19, 2021

Hello, I am a beginner and I would like to contribute to this project. Can you please help me with how to get started ?

@goelm08
Copy link

@goelm08 goelm08 commented Aug 19, 2021

Hello, I am a beginner and I want to contribute to this project. Can you please help me to get started?

@ankit332000
Copy link

@ankit332000 ankit332000 commented Aug 19, 2021

Hello , I wanna do my first pull request can you help me with that by allowing me to contribute?

@dhivyadharshin
Copy link

@dhivyadharshin dhivyadharshin commented Aug 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

9 participants