Skip to content

Commit

Permalink
add dtype checking for gather and scatter (pytorch#38025)
Browse files Browse the repository at this point in the history
Summary:
Fixed pytorch#37996

in the `cpu_scatter_gather_base_kernel`, it interpret a pointer as `int64_t` regardless the actual dtype.
https://github.com/pytorch/pytorch/blob/2b41b9bceb01851df83d40c1280b3d3b09e1395b/aten/src/ATen/native/cpu/ScatterGatherKernel.cpp#L106
add a index dtype checking will avoid the nasty index out of bound error. As using `int64_t` is convention in ATen code (a.k.a, a limitation), no further fix is needed at the moment.
Pull Request resolved: pytorch#38025

Differential Revision: D21498146

Pulled By: ezyang

fbshipit-source-id: b1f96f394a460c4bc63d21ec8d4a2cfbf3e97b03
  • Loading branch information
cloudhan authored and facebook-github-bot committed May 11, 2020
1 parent 503be4e commit 324dc16
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions aten/src/ATen/native/TensorAdvancedIndexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,40 +518,48 @@ Tensor index_fill(const Tensor & self, int64_t dim, const Tensor & index, const
}

Tensor & gather_out_cpu(Tensor & result, const Tensor & self, int64_t dim, const Tensor & index, bool sparse_grad) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "gather_out(): Expected dtype int64 for index");
result.resize_(index.sizes());
gather_stub(result.device().type(), result, self, dim, index);
return result;
}

Tensor gather_cpu(const Tensor & self, int64_t dim, const Tensor & index, bool sparse_grad) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "gather(): Expected dtype int64 for index");
Tensor result = at::empty({0}, self.options());
return gather_out_cpu(result, self, dim, index, sparse_grad);
}

Tensor & scatter_(Tensor & self, int64_t dim, const Tensor & index, const Tensor & src) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter_(): Expected dtype int64 for index");
scatter_stub(self.device().type(), self, dim, index, src);
return self;
}

Tensor & scatter_fill_(Tensor & self, int64_t dim, const Tensor & index, Scalar src) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter_fill_(): Expected dtype int64 for index");
scatter_fill_stub(self.device().type(), self, dim, index, src);
return self;
}

Tensor scatter(const Tensor & self, int64_t dim, const Tensor & index, const Tensor & source) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter(): Expected dtype int64 for index");
return self.clone(at::MemoryFormat::Preserve).scatter_(dim, index, source);
}

Tensor scatter(const Tensor & self, int64_t dim, const Tensor & index, Scalar source) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter(): Expected dtype int64 for index");
return self.clone(at::MemoryFormat::Preserve).scatter_(dim, index, source);
}

Tensor & scatter_add_cpu_(Tensor & self, int64_t dim, const Tensor & index, const Tensor & src) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter_add_(): Expected dtype int64 for index");
scatter_add_stub(self.device().type(), self, dim, index, src);
return self;
}

Tensor scatter_add(const Tensor & self, int64_t dim, const Tensor & index, const Tensor & source) {
TORCH_CHECK_INDEX(index.scalar_type() == ScalarType::Long, "scatter_add(): Expected dtype int64 for index");
return self.clone(at::MemoryFormat::Preserve).scatter_add_(dim, index, source);
}

Expand Down

0 comments on commit 324dc16

Please sign in to comment.