Create a constant inside a graph

Omer_B: <p>Hey,<br>
I’m trying to edit a graph representation (<code>torch._C.Graph</code>) via python API.<br>
I want to add a node at the begging of the graph that returns a CUDA device.</p>
<p>this is what I did:</p>
<pre><code class="lang-python">cuda_node = graph.create('prim::Constant[value="cuda:0"]')
cuda_value = next(cuda_node.outputs())
cuda_value.setType(torch._C.DeviceObjType.get())
graph.prependNode(cuda_node)
</code></pre>
<p>This code has been successfully ran, but I get this error when I run the graph function by <code>torch._C._create_function_from_graph(...)</code> :</p>
<pre><code class="lang-auto">RuntimeError: 0 INTERNAL ASSERT FAILED at "/pytorch/torch/csrc/jit/ir/alias_analysis.cpp":465, please report a bug to PyTorch. We don't have an op for prim::Constant[value="cuda:0"] but it isn't a special case.  Argument types: 
</code></pre>
<p>How can I create this kind of node without experience this issue ?<br>
Thanks.</p>

ptrblck: <p>It seems you are using internal methods from the <code>_C</code> namespace, so I’m unsure if your use case is supported out of the box (without breaking stuff).<br>
What’s your use case you need to manipulate the graph and cannot recreate it?</p>

Omer_B: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> I’m not sure that recreate a graph replica will help in that case, since that I still don’t know how to create a <code>prim::Constant</code> with the attribute <code>value="cuda:0"</code>.</p>
<p>my mistake was that there is no <code>prim::Constant[value="cuda:0"]</code> node kind, I need to create the node by:</p>
<pre><code class="lang-python">cuda_node = graph.create('prim::Constant')
</code></pre>
<p>and then I need to set for <code>cuda_node</code> an attribute <code>value="cuda:0"</code> but I’m not sure how.</p>

ptrblck: <p>This code snippet would create a <code>prim::Constant</code> on the GPU:</p>
<pre><code class="lang-python">@torch.jit.script
def fun(x):
    y = x + torch.tensor(1, device='cuda')
    return y

print(fun.graph)
&gt; graph(%x.1 : Tensor):
  %7 : bool = prim::Constant[value=0]()
  %21 : Device = prim::Constant[value="cuda"]()
  %5 : None = prim::Constant()
  %2 : int = prim::Constant[value=1]() # &lt;ipython-input-36-11fdcf9e4060&gt;:9:25
  %8 : Tensor = aten::tensor(%2, %5, %21, %7) # &lt;ipython-input-36-11fdcf9e4060&gt;:9:12
  %y.1 : Tensor = aten::add(%x.1, %8, %2) # &lt;ipython-input-36-11fdcf9e4060&gt;:9:8
  return (%y.1)
</code></pre>

Omer_B: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks, seems like I can take this example and use <code>createClone</code> to create my desired node in my graph. I guess there is no exposed API to set attributes for nodes, right?</p>

ptrblck: <aside class="quote no-group" data-username="Omer_B" data-post="5" data-topic="96856">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/omer_b/48/23472_2.png" class="avatar"> Omer_B:</div>
<blockquote>
<p>I guess there is no exposed API to set attributes for nodes, right?</p>
</blockquote>
</aside>
<p>I don’t know, so lets wait for some experts here. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

Omer_B: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> actually, it takes too much time that I can’t afford to copy an entire graph just for 1 node modification. if there will be an option to set attributes for nodes it would be great.</p>