How to generate all possible combinations of 0-1 matrix in Cuda?

Majid_Shirazi: <p>Hi, I have search for it and found below solution to generate all KxN possible combination of tensors with different arrangement of zeros and ones:</p>
<pre><code class="lang-auto">import itertools, torch
K, N = 2, 2
combinations = [torch.reshape(torch.tensor(i), (K, N)) for i in itertools.product([0, 1], repeat = K*N)]

</code></pre>
<p>then I can get this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/3/23d2424cbaf251e41d3d83c0f6745ee9cde68088.png" data-download-href="https://discuss.pytorch.org/uploads/default/23d2424cbaf251e41d3d83c0f6745ee9cde68088" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/3/23d2424cbaf251e41d3d83c0f6745ee9cde68088.png" alt="image" data-base62-sha1="56TbJvVyZe3QG2AYqpmMPvjmIyY" width="178" height="500" data-dominant-color="F2F2F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">212×593 2.92 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><strong>However,</strong> if I want to have bigger tensor combinations (e.g., with K, N = 5, 5), this process takes so much time. Can I please ask for a faster solution that it  use GPU or Cuda for finding?</p>

KFrank: <p>Hi Majid!</p>
<aside class="quote no-group" data-username="Majid_Shirazi" data-post="1" data-topic="163016" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/majid_shirazi/48/13913_2.png" class="avatar"> Majid_Shirazi:</div>
<blockquote>
<p>generate all KxN possible combination of tensors with different arrangement of zeros and ones:</p>
</blockquote>
</aside>
<p>Here is a loop-free approach that uses pytorch tensor operations:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.12.0'
&gt;&gt;&gt; K = 2
&gt;&gt;&gt; N = 2
&gt;&gt;&gt; (torch.bitwise_and (torch.arange (2**(K*N), device = 'cuda').unsqueeze (-1), 2**torch.arange (K*N, device = 'cuda')) &gt; 0).long().reshape (2**(K*N), K, N)
tensor([[[0, 0],
         [0, 0]],

        [[1, 0],
         [0, 0]],

        [[0, 1],
         [0, 0]],

        [[1, 1],
         [0, 0]],

        [[0, 0],
         [1, 0]],

        [[1, 0],
         [1, 0]],

        [[0, 1],
         [1, 0]],

        [[1, 1],
         [1, 0]],

        [[0, 0],
         [0, 1]],

        [[1, 0],
         [0, 1]],

        [[0, 1],
         [0, 1]],

        [[1, 1],
         [0, 1]],

        [[0, 0],
         [1, 1]],

        [[1, 0],
         [1, 1]],

        [[0, 1],
         [1, 1]],

        [[1, 1],
         [1, 1]]], device='cuda:0')
</code></pre>
<p>Note that for even small values of <code>K</code> and <code>N</code> there will be <em>a lot</em> of such<br>
tensors.  For your example of <code>K = N = 5</code> your final result will consist<br>
of about 800 million elements.</p>
<p>I don’t know of a pytorch function that will give you “all possible combinations”<br>
of some arbitrary sets of values.  For your “0-1” example, we may obtain the<br>
desired result from the binary representations of the integers up to <code>2**(K*N)</code>.</p>
<p>Best.</p>
<p>K. Frank</p>