Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aten/src/ATen/native/Dropout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Tensor make_feature_noise(const Tensor& input) {
}

bool is_fused_kernel_acceptable(const Tensor& input, double p) {
return input.is_cuda() && p > 0 && p < 1;
return input.is_cuda() && p > 0 && p < 1 && input.numel() > 0;
}

// NB: sure, we could have used different overloads here, but I would feel insecure
Expand All @@ -41,7 +41,7 @@ Tensor multiply(const Tensor& input, const Tensor& noise) {
template<bool feature_dropout, bool alpha_dropout, bool inplace, typename T>
Ctype<inplace> _dropout_impl(T& input, double p, bool train) {
TORCH_CHECK(p >= 0 && p <= 1, "dropout probability has to be between 0 and 1, but got ", p);
if (p == 0 || !train) {
if (p == 0 || !train || input.numel() == 0) {
return input;
}

Expand Down
2 changes: 2 additions & 0 deletions aten/src/ATen/native/cuda/Dropout.cu
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ fused_dropout_cuda(const Tensor& self, double p, Generator * gen){
Tensor ret = at::empty_like(self);
Tensor mask = at::empty(self.sizes(), self.options().dtype(kByte));
const int64_t nelem = self.numel();
//empty tensors should not get here, but just in case, avoid FPE
if (nelem==0) return std::tuple<Tensor,Tensor>(self, mask);
const int64_t block_size = 256;
unsigned int blocks_per_sm = at::cuda::getCurrentDeviceProperties()->maxThreadsPerMultiProcessor/block_size;
dim3 dim_block(block_size);
Expand Down
11 changes: 11 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5027,6 +5027,17 @@ def test_invalid_dropout_p(self):
self.assertRaises(ValueError, lambda: F.dropout(v, -0.1))
self.assertRaises(ValueError, lambda: F.dropout(v, 1.1))

def test_empty_dropout(self):
x = torch.Tensor([])
out = torch.nn.functional.dropout(x)
self.assertEqual(out.size(), x.size())

@unittest.skipIf(not TEST_CUDA, 'CUDA not available')
def test_empty_dropout_cuda(self):
x = torch.Tensor([]).to('cuda')
out = torch.nn.functional.dropout(x)
self.assertEqual(out.size(), x.size())

def test_pad_sequence(self):
def pad(tensor, length):
return torch.cat(
Expand Down