ScalarType notations in PyTorch

rohithkrn: <p>Could someone please help me in understanding different notations of scalar types?</p>
<p>For example, I see at::ScalarType::Half, kHalf, half for fp16 type in the code. Are they all identical? Couldn’t find definitions of all notations.</p>

tom: <p>Yeah, that’s a bit of a mess. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>So these stem from c10 and the c10 namespace, but are also available in the at (and torch) namespace.</p>
<p>kHalf is an alias for <code>ScalarType::Half</code>; historically, it was common to use the <code>kFloat</code>, <code>kLong</code>,… types and there have been some refactorings until we arrived at the ScalarType we have today (or at least <code>t.scalar_type()</code>  is newer than some PyTorch code). <code>ScalarType::Half</code> it is a type identifier (ScalarType is the C++ side fo dtype).</p>
<p><code>Half</code> is a type like <code>int64_t</code>, it matches <code>half</code> or <code>__half</code> when the platform has one and provides the usual ops. This is an abstraction that allows templating to include <code>Half</code> where one would have to do crazy dances around not defined things otherwise).</p>
<p>Best regards</p>
<p>Thomas</p>

rohithkrn: <p>Thanks Thomas for the reply. Could you please point me to the code where kHalf alias is defined?</p>

tom: <p>It is right there in <a href="https://github.com/pytorch/pytorch/blob/764e0ee88245c435be6934a5a06316c64ea171cc/c10/core/ScalarType.h#L220-L224" rel="nofollow noopener"><code>c10/core/ScalarType.h</code></a>, but it is not easy to find because it uses some macro magic.</p>
<p>Best regards</p>
<p>Thomas</p>

guanbaoy: <p>hi <a class="mention" href="/u/tom">@tom</a> , just curious about one thing: ScalarType is defined in namespace <strong>c10</strong> (<a href="https://github.com/pytorch/pytorch/blob/13e806a591739453665c8389d0ae5550f2a4af92/c10/core/ScalarType.h#L139" class="inline-onebox" rel="noopener nofollow ugc">pytorch/c10/core/ScalarType.h at 13e806a591739453665c8389d0ae5550f2a4af92 · pytorch/pytorch · GitHub</a>), how does torch make it available in namespace <strong>at</strong>? and in most cases, I see <strong>at::ScalarType</strong> is used, instead of <strong>c10::ScalarType</strong>.</p>

tom: <p>As far as I know, the <code>at</code> namespace has all of the <code>c10</code> namespace and the <code>torch</code> namespace all of the <code>at</code> namespace. I think in C++ <code>using namespace foo;</code> will add everything from the namespace foo in the current namespace.</p>

guanbaoy: <p>Thanks tom, I was actually trying to find such line: using namespace c10, in the namespace at <img src="https://discuss.pytorch.org/images/emoji/apple/rofl.png?v=12" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"> Thanks again for your reply.</p>

tom: <p>You mean this?</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/6c1f1563e1b2fa54673033a5aed8fabd9fb28bc2/c10/macros/Macros.h#L186-L188">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/6c1f1563e1b2fa54673033a5aed8fabd9fb28bc2/c10/macros/Macros.h#L186-L188" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/6c1f1563e1b2fa54673033a5aed8fabd9fb28bc2/c10/macros/Macros.h#L186-L188" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/6c1f1563e1b2fa54673033a5aed8fabd9fb28bc2/c10/macros/Macros.h#L186-L188</a></h4>



    <pre class="onebox"><code class="lang-h">
      <ol class="start lines" start="186" style="counter-reset: li-counter 185 ;">
          <li>namespace at {</li>
          <li>using namespace c10;</li>
          <li>}</li>
      </ol>
    </code></pre>



  </article>

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

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


guanbaoy: <p>yes, exactly. This is how torch export c10 namespace to at.</p>