Skip to content

Conversation

@ssnl
Copy link
Collaborator

@ssnl ssnl commented Oct 3, 2017

Fixes #1653

This ensures the following condition on scatter for both cpu and cuda:

  1. index.size[d] <= output.size[d] for d != dim
  2. index.size[d] <= src.size[d] for all d

The drawback is introducing a weird macro argument. Let me know if I should think of other ways,

This comment was marked as off-topic.

This comment was marked as off-topic.

@ssnl ssnl force-pushed the scatter_consistency branch from 64084f6 to f0d6178 Compare October 3, 2017 22:05
@ssnl
Copy link
Collaborator Author

ssnl commented Oct 3, 2017

Updated cuda side error msg with size information.

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't finish reviewing. Just asking for comments

#define TH_TENSOR_DIM_APPLY_INC

#define TH_TENSOR_DIM_APPLY3(TYPE1, TENSOR1, TYPE2, TENSOR2, TYPE3, TENSOR3, DIMENSION, CODE) \
#define TH_TENSOR_DIM_APPLY3_SIZE_EQ_EXCEPT_DIM(TENSOR1, TENSOR2, TENSOR3, DIMENSION) \

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either I don't understand something, or it's not a complete fix for the issue linked. Some size checks seem to be unnecessarily strict, and the main issue mentioned there is that we accept 2 as index for the 0th dim, even if it's out of bound. This requires us to inspect the index data, which is not done in this PR.

"Input tensor must have same size as output tensor apart from the specified dimension");
THArgCheck(indexSizeD <= THCTensor_(size)(state, tensor, d), 3,
"Index tensor must not have larger size than output tensor apart from the specified dimension %d, but got index %s output %s",
dim, THCudaLongTensor_sizeDesc(state, index).str, THCTensor_(sizeDesc)(state, tensor).str);

This comment was marked as off-topic.

"Index tensor must not have larger size than output tensor apart from the specified dimension %d, but got index %s output %s",
dim, THCudaLongTensor_sizeDesc(state, index).str, THCTensor_(sizeDesc)(state, tensor).str);
}
THArgCheck(indexSizeD <= THCTensor_(size)(state, src, d), 3,

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@ssnl
Copy link
Collaborator Author

ssnl commented Oct 4, 2017

According to my understanding of scatter, what it does is:
for specified dim
for all idx = index[x_1, x_2, ..., x_dim, ..., x_n],
let output[x_1, x_2, ..., idx, ..., x_n] = src[x_1, x_2, ..., x_dim, ..., x_n]

The issue linked is talking about the size relation between index and src, i.e. index has larger size than src and thus scatters uninitialized memory data to output.

I wonder how scatter can be used to repeat elements. If I understand correctly, it can only copy each value to a certain position.

I think you might be talking about gather?

@soumith soumith merged commit f608208 into pytorch:master Oct 4, 2017
@ssnl ssnl deleted the scatter_consistency branch October 4, 2017 15:10
@apaszke
Copy link
Contributor

apaszke commented Oct 4, 2017

Yeah I might have confused some parts with gather. Still, do we check for out of bound indices in index tensor?

@ssnl
Copy link
Collaborator Author

ssnl commented Oct 4, 2017

I just tested. Both CPU and CUDA version will throw error in that case. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tensor.scatter_ accepts bogus input where index size > src size

4 participants