Does PyTorch plan to support "Cpp-like" round?

Eta_C: <p><code>round</code> in PyTorch is a “Python-like” round (<code>std::nearbyint</code>) but not a “Cpp-like” round (<code>std::round</code>). It means, <code>round([-0.5, 0.5])</code> would return <code>[0, 0]</code> but not <code>[-1, 1]</code>. I need “Cpp-like” round, especially when deploying my model.</p>
<p>I check the source code and find</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/cpu/vec/vec_base.h#L469-L471">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/cpu/vec/vec_base.h#L469-L471" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/cpu/vec/vec_base.h#L469-L471" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/cpu/vec/vec_base.h#L469-L471</a></h4>



  <pre class="onebox">    <code class="lang-h">
      <ol class="start lines" start="469" style="counter-reset: li-counter 468 ;">
          <li>Vectorized&lt;T&gt; round() const {</li>
          <li>  // We do not use std::round because we would like to round midway numbers to the nearest even integer.</li>
          <li>  return map(at::native::round_impl);</li>
      </ol>
    </code>
  </pre>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/native/cpu/zmath.h#L175-L178">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/native/cpu/zmath.h#L175-L178" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/native/cpu/zmath.h#L175-L178" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/178010455d5cd477af5a98b0e03084ffeecaecf2/aten/src/ATen/native/cpu/zmath.h#L175-L178</a></h4>



  <pre class="onebox">    <code class="lang-h">
      <ol class="start lines" start="175" style="counter-reset: li-counter 174 ;">
          <li>template &lt;typename TYPE&gt;</li>
          <li>inline TYPE round_impl (TYPE z) {</li>
          <li>  return std::nearbyint(z);</li>
          <li>}</li>
      </ol>
    </code>
  </pre>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>One year ago, I posted <a href="https://discuss.pytorch.org/t/is-there-any-faster-way-to-rounding-a-tensor/62620" class="inline-onebox">Is there any faster way to rounding a tensor?</a> and found a temporary solution. But it is complex and inefficient.</p>
<p>Does PyTorch have a “Cpp-like” round function, or an option?<br>
Thank you.</p>

tom: <p>Probably not unless there is a very compelling reason?</p>
<p>Using where + “manually rounding” + fuser should work reasonably well. In the thread you link, you use boolean indexing which is probably not a good idea for efficiency.</p>
<p>Best regards</p>
<p>Thomas</p>

Eta_C: <p>Thanks for you reply, <a class="mention" href="/u/tom">@tom</a><br>
How could I <code>where</code> all midway numbers (0.5 and 1.5 and 2.5 and 3.5 and so on)?</p>

tom: <p>Remind me what</p>
<pre><code class="lang-auto">@torch.jit.script
def round_away_from_0(x):
    return torch.trunc(x + 0.5 * torch.sign(x))
</code></pre>
<p>does not do for you?</p>
<p>Then you could use <code>torch._C._jit_override_can_fuse_on_cpu(True)</code> to get fusion on CPU (but you need LLVM for this to be built-in).</p>

Eta_C: <p>It works, thank you again <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=10" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:">. <a class="mention" href="/u/tom">@tom</a></p>