Tensor and Packedaccessor for int tensors

username: el_samou_samou
cooked: <p>Hi,</p>
<p>I am very new to the c++/cuda pytorch API and I would like to manipulate tensors containing integers (int64). I was wondering how to do so. Also the PackedTensorAccessor don’t seems to accepts int as first argument.</p>
<p>So just two examples, first instantiate a 0-valued tensor of type int:</p>
<pre><code class="lang-auto">torch::Tensor test = torch::zeros({5}, int);
</code></pre>
<p>or trying to get an accessor from an existing tensor:</p>
<pre><code class="lang-auto">torch::Tensor test = torch::zeros({5});
torch::PackedTensorAccessor&lt;int, 1, torch::RestrictPtrTraits, size_t&gt; acc = test.packed_accessor&lt;int, 1, torch::RestrictPtrTraits, size_t&gt;();
</code></pre>
<p>I also tried:</p>
<pre><code class="lang-auto">torch::Tensor test = torch::zeros({5});
torch::PackedTensorAccessor&lt;torch:kInt, 1, torch::RestrictPtrTraits, size_t&gt; acc = test.packed_accessor&lt;torch:kInt, 1, torch::RestrictPtrTraits, size_t&gt;();
</code></pre>
<p>I get error for such lines. I guess I must be missing something very obvious, can you help me?</p>
<p>Thank you in advance,</p>
<p>Samuel</p>

username: tom
cooked: <p>You need to match the scalar type to the tensor’s scalar_type, so:<br>
If you instantiated a <code>torch::kLong</code> tensor (that’s 64 bit integers), use int64_t. For kInt (32 bit) use int32_t, for nothing (aka kFloat), float.</p>
<p>Note that you usually do not need packed_accessor for CPU tensors, they are intended for passing to CUDA kernels. accessor will do for CPU (pointing to the Tensor’s stride and size arrays) - unless you want 32 indexing (on ARM?, probably not on x86).<br>
(And you can use <code>auto</code> for less repetitive code.)</p>
<p>Best regards</p>
<p>Thomas</p>

username: el_samou_samou
cooked: <p>Thanks a lot for your answer Thomas it helped a lot.</p>
<p>I am actually implementing a Cuda extension this is why I am using packed_accessor.</p>
<p>Best regards,</p>
<p>Samuel</p>

username: oli4100
cooked: <p>Hi Tom, how can I implement bfloat16 in ATen with a packed_accessor? For floats the ATen definition and packed_accessor is (e.g.):</p>
<pre><code class="lang-auto">auto X = torch::zeros({a, b}, torch::dtype(torch::kF32))
X.packed_accessor32&lt;float,2,torch::RestrictPtrTraits&gt;()
</code></pre>
<p>but what should it read for bfloat16? I.e. what should come at the question marks below:</p>
<pre><code class="lang-auto">auto X = torch::zeros({a, b}, torch::dtype(torch::[?]))
X.packed_accessor32&lt;[?],2,torch::RestrictPtrTraits&gt;()
</code></pre>
<p>I can’t find the ATen reference for bfloat16 <a href="https://github.com/pytorch/pytorch/blob/master/torch/csrc/api/include/torch/types.h" rel="noopener nofollow ugc">here</a>, nor do I know what the packed_accessor reference should be…</p>

username: oli4100
cooked: <aside class="quote no-group" data-username="oli4100" data-post="4" data-topic="57263">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/o/d6d6ee/48.png" class="avatar"> oli4100:</div>
<blockquote>
<pre><code class="lang-auto">auto X = torch::zeros({a, b}, torch::dtype(torch::[?]))
X.packed_accessor32&lt;[?],2,torch::RestrictPtrTraits&gt;()
</code></pre>
</blockquote>
</aside>
<p>Sometimes you search for something for hours only to find the answer shortly after asking the question <img src="https://discuss.pytorch.org/images/emoji/apple/expressionless.png?v=12" title=":expressionless:" class="emoji" alt=":expressionless:" loading="lazy" width="20" height="20"> The following is the answer:</p>
<pre><code class="lang-auto">auto X = torch::zeros({a, b}, torch::dtype(at::ScalarType::BFloat16))
X.packed_accessor32&lt;at::BFloat16,2,torch::RestrictPtrTraits&gt;()
</code></pre>