Python functions to create nodes / modify graph

I’d like to modify the graph by inserting new inputs (hopefully by using addInput in the graph). Is there a way to do this from Python after getting a torch._C.Graph object? Couldn’t seem to find an constructor/create function when doing dir(torch._C.Node).

This is purposefully not exposed, the only way to get a graph is by compiling code with torch.jit.script or tracing with torch.jit.trace. Potentially you could extract the code you are compiling initially with the Python inspect module and edit it that way, so it would be changed in the way you want before TorchScript sees it.

You could also drop down to C++ (and potentially use PyBind to make it callable from Python) and write a custom pass that operates on a Graph object, see this tutorial for details.

1 Like