Support for slicing with a step != 1?

username: Ajoo
cooked: <p>Currently when attempting to slice a tensor or Variable object with a step different from 1 I get a Runtime Error:</p>
<blockquote>
<p>RuntimeError: Trying to slice with a step of -1, but only a step of 1 is supported</p>
</blockquote>
<p>Is support for this opperation coming anytime soon and what is the best way to reverse a tensor along an axis currently? Best I can come up with is doing it via index_select which is clunky and copies the tensor… Doing it via numpy is also not possible as from_numpy() doesn’t seem to work with negative strides.</p>

username: apaszke
cooked: <p>Support for positive strides is added in <a href="https://github.com/pytorch/pytorch/pull/794">this PR</a>. Adding negative strides will need some additional work in our C backends, as they’re not ready for that yet.</p>

username: acgtyrant
cooked: <p>I try to do <code>image = image[:, :, ::-1]</code>, but it complains that <code>ValueError: slice step has to be greater than 0</code>. I try <code>image = torch.from_numpy(image.numpy()[:, :, ::-1])</code>, it still complains: <code>RuntimeError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.</code></p>
<p>So how do I flip the image?</p>

username: acgtyrant
cooked: <p>I decide to use <code>image = torch.from_numpy(image.numpy()[:, :, ::-1].copy())</code>…</p>

username: bobiblazeski
cooked: <p>Strange it sometimes work, and sometimes doesn’t:</p>
<blockquote>
<p>torch.reshape(weights, weights.shape[::-1])</p>
</blockquote>
<pre><code class="lang-auto">tensor([[-0.8948],
            [-0.3556],
            [ 1.2324],
            [ 0.1382],
            [-1.6822]])
</code></pre>
<blockquote>
<p>torch.mm(features, torch.reshape(weights, weights[::-1]))</p>
</blockquote>
<pre><code class="lang-auto">_ ValueError                                Traceback (most recent call last)_
_ &lt;ipython-input-42-67cae86c60db&gt; in &lt;module&gt;_
_torch.mm(features, torch.reshape(weights, weights[::-1]))_
_ ValueError: negative step not yet supported_
</code></pre>
<blockquote>
<p>torch.mm(features, weights.view(weights.shape[::-1]))</p>
</blockquote>
<pre><code class="lang-auto">tensor([[-0.8948],
            [-0.3556],
            [ 1.2324],
            [ 0.1382],
            [-1.6822]])
</code></pre>

username: Shriya_Kak
cooked: <p>Hi guys,</p>
<p>I am also facing the same issue. Could anyone please confirm if the issue is resolved?</p>

username: albanD
cooked: <p>Which issue? negative strides? I’m afraid we do not support it.</p>

username: Shriya_Kak
cooked: <pre><code>detected_faces = face_detector.detect_from_image(data[..., ::-1].copy())
</code></pre>
<p>ValueError: negative step not yet supported</p>
<p>This is what I am getting.</p>

username: albanD
cooked: <p>Hi,</p>
<p>I’m afraid we still do not.<br>
But if you want a clone of the Tensor, you can use <a href="https://pytorch.org/docs/stable/torch.html#torch.flip"><code>torch.flip()</code></a>. Note that this will return new memory, but will have the exact same behavior as <code>data[..., ::-1].copy()</code>.</p>

username: clockzhong
cooked: <p>I confirm that it isn’t supported by pytorch. But supported by numpy, and according to those LLMs (Gemini/ChatGPT/Grok3), they also believe tensorflow supports it. But my tests show it really doesn’t support in pytorch.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/7/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b.png" data-download-href="https://discuss.pytorch.org/uploads/default/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b" title="Screenshot from 2025-03-21 11-49-30"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/7/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b_2_487x500.png" alt="Screenshot from 2025-03-21 11-49-30" data-base62-sha1="11ZfaUugrJXL9mh4bCgiiHMJXo7" width="487" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/7/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b_2_487x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/7/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b_2_730x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/7/073bb2b35c6fd173da2ce77fd8c19ba87ed8d67b.png 2x" data-dominant-color="F3F2F0"><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">Screenshot from 2025-03-21 11-49-30</span><span class="informations">929×952 88 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/7/471c268a30a05e2bda40627d462796f827864580.png" data-download-href="https://discuss.pytorch.org/uploads/default/471c268a30a05e2bda40627d462796f827864580" title="Screenshot from 2025-03-21 11-49-45"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/471c268a30a05e2bda40627d462796f827864580_2_487x500.png" alt="Screenshot from 2025-03-21 11-49-45" data-base62-sha1="a94dbuVby4pLC0kgCoi9lppMM4o" width="487" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/471c268a30a05e2bda40627d462796f827864580_2_487x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/471c268a30a05e2bda40627d462796f827864580_2_730x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/7/471c268a30a05e2bda40627d462796f827864580.png 2x" data-dominant-color="F7F8FA"><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">Screenshot from 2025-03-21 11-49-45</span><span class="informations">929×952 94.5 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><br>
It’s not only confused by human, also confused by AI.</p>