Fuse adds in TorchScript

Is there a method in a graph that allows me to specify where a constant will be placed in the code?
Here’s an example:

original graph
%3 : int = prim::Constant value=1()
%2 : int = prim::Constant value=2()
%5 : int = prim::Constant value=4()
%9 : int = prim::Constant value=3()
%4 : Tensor = aten::add(%x.1, %2, %3)
%a.1 : Tensor = aten::add(%4, %5, %3)
%b.1 : Tensor = aten::add(%a.1, %9, %3)
%c.1 : Tensor = aten::relu(%b.1)
return (%c.1)


fuse_ops function
value1 = current_input2.toIValue()
value2 = next_input2.toIValue()
--------------------insertConstant------------------------
new_int = graph.insertConstant(value1 + value2)

--------------------------create-----------------------------
new_int_2 = graph.create(‘prim::Constant’)
new_int_2.f_( “value”, value1 + value2)
new_int_2.insertBefore(nodes[0])
new_add = graph.create(“aten::add”, [current_input1, new_int, current_alpha])
new_add.insertBefore(current)
next_node.output().replaceAllUsesWith(new_add.output())
---------------------------------------------------------------


fuse_ops graph
graph(%x.1 : Tensor):
%25 : Tensor = prim::Constant value=7. () # graph.create
%3 : int = prim::Constant value=1 ()
%2 : int = prim::Constant value=2 ()
%5 : int = prim::Constant value=4 ()
%9 : int = prim::Constant value=3 ()
%4 : Tensor = aten::add(%x.1, %2, %3)
%26 : Tensor = aten::add(%4, %24, %3)
%c.1 : Tensor = aten::relu(%26)
%24 : int = prim::Constant value=7 () # insertConstant
return (%c.1)


I tried two approaches: using graph.insertConstant and graph.create. Neither was successful.

  • graph.insertConstant only inserts the constant at the bottom of the code (as seen in the fuse_ops graph).
  • graph.create creates a node, which causes an issue when generating an aten::add.