[PyTorch 2.7.0] Custom privateuse1 FakeTensor add + float scalar triggers AttributeError: 'float' object has no attribute 'item_memo'

hersungjin23: <p>I’m working with PyTorch 2.7.0 and have registered a custom device named <code>privateuse1</code>, implementing both the real-kernel <code>add.Tensor</code> and its fake/meta counterpart. I’ve also implemented both real and fake versions of <code>add.Scalar</code>, but in practice <code>add.Scalar</code> is never invoked when adding a float scalar. When I try to add a Python float (e.g. <code>1e-6</code>) to a tensor on this device under Dynamo/FX FakeTensor mode, I hit an <code>AttributeError</code> about <code>item_memo</code>. Adding via another tensor works fine, and CPU + float also behaves as expected.</p>
<hr>
<h2><a name="p-470768-environment-1" class="anchor" href="#p-470768-environment-1"></a>Environment</h2>
<ul>
<li><strong>PyTorch</strong>: 2.7.0</li>
<li><strong>Python</strong>: 3.10</li>
<li><strong>OS</strong>: Ubuntu 20.04</li>
</ul>
<hr>
<h2><a name="p-470768-implementation-reproduction-code-2" class="anchor" href="#p-470768-implementation-reproduction-code-2"></a>Implementation &amp; Reproduction Code</h2>
<pre><code class="lang-auto">import torch

# 1) Registered the custom device 'privateuse1'
#    – Implemented and registered real-kernel add.Tensor
#    – Implemented and registered fake/meta add.Tensor
#    – Implemented and registered real-kernel add.Scalar
#    – Implemented and registered fake/meta add.Scalar
#
#    Despite this, when performing x + float, only add.Tensor fake runs
#    (add.Scalar is not called at all)

x = torch.Tensor([1, 2, 3], device='privateuse1')
y = x + 1e-6
</code></pre>
<hr>
<h2><a name="p-470768-error-message-3" class="anchor" href="#p-470768-error-message-3"></a>Error Message</h2>
<pre><code class="lang-auto">torch._dynamo.exc.TorchRuntimeError: Dynamo failed to run FX node with fake tensors:
  call_function &lt;built-in function add&gt;(
    *(FakeTensor(..., device='privateuse1:0', size=(3,)), 1e-06), **{}
  ): got AttributeError("'float' object has no attribute 'item_memo'")
</code></pre>
<ul>
<li>Adding a tensor scalar works:</li>
</ul>
<pre><code class="lang-auto">x + torch.tensor(1e-6, device='privateuse1')
</code></pre>
<p>succeeds without error.</p>
<ul>
<li>On the <code>cpu</code> device, <code>x_cpu + 1e-6</code> also works normally.</li>
</ul>
<hr>
<h2><a name="p-470768-troubleshooting-attempts-4" class="anchor" href="#p-470768-troubleshooting-attempts-4"></a>Troubleshooting Attempts</h2>
<ol>
<li><strong>Auto-wrapping floats</strong> into a tensor inside the fake <code>add.Tensor</code> implementation</li>
<li><strong>Adding an <code>item_memo</code> attribute</strong> to the <code>FakeTensor</code> class so floats would carry a memo entry</li>
<li><strong>Disabling Dynamo/FX</strong> (pure Python fall-back) to isolate FakeTensor issues</li>
<li><strong>Comparing with other community reports</strong> (e.g., FakeTensor errors when prototyping backends)</li>
<li><strong>Reviewing custom-device extension PRs</strong> for PrivateUse1</li>
</ol>
<hr>
<h2><a name="p-470768-questions-5" class="anchor" href="#p-470768-questions-5"></a>Questions</h2>
<ol>
<li>Why is <code>add.Scalar</code> never invoked when adding a Python float scalar under FakeTensor/Dynamo mode?</li>
<li>What specifically triggers the <code>item_memo</code> AttributeError in this scenario?</li>
<li>Which methods or attributes must a <code>FakeTensor</code> (or its mode) implement to support scalar floats?</li>
<li>Are there any official guides, examples, or reference implementations for handling scalar inputs in custom FakeTensor kernels?</li>
</ol>
<p>Thank you for your insights!</p>