RuntimeError: n cannot be greater than 2^24+1 for Float type

username: ProGamerGov
cooked: <p>So, this code results in an error message:</p>
<pre><code class="lang-auto">import torch
from torch.utils.cpp_extension import load
cpp = torch.utils.cpp_extension.load(name="histogram_cpp", sources=["histogram.cpp", "histogram.cu"])

dtype=torch.cuda.FloatTensor

a = torch.randn(128, 512, 720).type(dtype)
b = torch.randn(128, 256).type(dtype)

cpp.matchHistogram(a, b) # Causes error message
</code></pre>
<p>The <code>matchHistogram()</code> function comes from these files:</p>
<p><a href="https://gist.githubusercontent.com/ProGamerGov/30e95ac9ff42f3e09288ae07dc012a76/raw/63e935bf8315080e2a7b7b002459141cfc3bffe8/histogram.cpp" class="onebox" target="_blank" rel="nofollow noopener">https://gist.githubusercontent.com/ProGamerGov/30e95ac9ff42f3e09288ae07dc012a76/raw/63e935bf8315080e2a7b7b002459141cfc3bffe8/histogram.cpp</a><br>
<a href="https://gist.githubusercontent.com/ProGamerGov/30e95ac9ff42f3e09288ae07dc012a76/raw/63e935bf8315080e2a7b7b002459141cfc3bffe8/histogram.cu" class="onebox" target="_blank" rel="nofollow noopener">https://gist.githubusercontent.com/ProGamerGov/30e95ac9ff42f3e09288ae07dc012a76/raw/63e935bf8315080e2a7b7b002459141cfc3bffe8/histogram.cu</a></p>
<p>This is the full error message:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "t.py", line 14, in &lt;module&gt;
    cpp.matchHistogram(a, b)
RuntimeError: n cannot be greater than 2^24+1 for Float type. (check_supported_max_int_with_precision at /pytorch/aten/src/ATen/native/TensorFactories.h:78)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x33 (0x7f0ff5f36813 in /usr/local/lib/python3.5/dist-packages/torch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x1bb1638 (0x7f0ff8142638 in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #2: at::native::randperm_out_cpu(at::Tensor&amp;, long, at::Generator*) + 0x3c (0x7f0ff813ad0c in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #3: &lt;unknown function&gt; + 0x1d9e3e4 (0x7f0ff832f3e4 in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #4: at::native::randperm(long, at::Generator*, c10::TensorOptions const&amp;) + 0xab (0x7f0ff81375eb in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #5: at::native::randperm(long, c10::TensorOptions const&amp;) + 0xe (0x7f0ff81376ee in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #6: &lt;unknown function&gt; + 0x1ecce9b (0x7f0ff845de9b in /usr/local/lib/python3.5/dist-packages/torch/lib/libtorch.so)
frame #7: at::Tensor at::ATenOpTable::callUnboxed&lt;at::Tensor, long, c10::TensorOptions const&amp;&gt;(long, c10::TensorOptions const&amp;) const + 0xb6 (0x7f0ff330e1d4 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #8: &lt;unknown function&gt; + 0x82f69 (0x7f0ff3300f69 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #9: torch::randperm(long, c10::TensorOptions const&amp;)::{lambda()#1}::operator()() const + 0x97 (0x7f0ff3309b81 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #10: torch::randperm(long, c10::TensorOptions const&amp;) + 0x192 (0x7f0ff3309d5c in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #11: matchHistogram(at::Tensor&amp;, at::Tensor&amp;) + 0x10a (0x7f0ff3301696 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #12: &lt;unknown function&gt; + 0x7e653 (0x7f0ff32fc653 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #13: &lt;unknown function&gt; + 0x7b692 (0x7f0ff32f9692 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #14: &lt;unknown function&gt; + 0x77343 (0x7f0ff32f5343 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #15: &lt;unknown function&gt; + 0x77533 (0x7f0ff32f5533 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
frame #16: &lt;unknown function&gt; + 0x6a4a1 (0x7f0ff32e84a1 in /tmp/torch_extensions/histogram_cpp/histogram_cpp.so)
&lt;omitting python frames&gt;
frame #19: python3() [0x540199]
frame #21: python3() [0x60c272]
frame #26: __libc_start_main + 0xf0 (0x7f104d266830 in /lib/x86_64-linux-gnu/libc.so.6)
</code></pre>
<p>I am not sure if this is a limitation with PyTorch, C++, and if I can circumvent it in my loss function by resizing the tensors for every layer that the <code>matchHistogram()</code> function is on?</p>

username: albanD
cooked: <p>You seem to call randperm with a number &gt; 2^24. I think there is a problem with your code. Can you can which value you give to randperm to make sure it’s what you expect?</p>

username: ProGamerGov
cooked: <p><code>a.numel()</code> = 47185920 in the above example. If I use <code>a = torch.randn(128, 512, 256)</code>, then <code>a.numel()</code> is equal to 16777216 and that works.</p>

username: albanD
cooked: <p>Given that you generate these in float32 Tensor, this would overflow.</p>