PGL icon indicating copy to clipboard operation
PGL copied to clipboard

input type Graph 时的静态图转换

Open QShiX opened this issue 3 years ago • 6 comments

1、比如在PGL/examples/gin/model.py中加入 @paddle.jit.to_static def forward(self, graph): 会报seg fault

2、在paddle.jit.save的api文档中也未发现input type为Graph时的input_spec说明。

麻烦请问如何将PGL的model存为静态图model,谢谢。如果已经支持此功能,我可以提供详细fault信息。谢谢。

QShiX avatar Oct 08 '22 10:10 QShiX

https://github.com/PaddlePaddle/PGL/blob/main/examples/deploy_gnn/convert_to_static.py

建议你可以用这种方法来,动静转换,不建议直接用 graph作为forward对象。最好把边和特征显式列出来再转

Yelrose avatar Oct 09 '22 08:10 Yelrose

好的。麻烦问下,就是说目前@paddle.jit.to_static用graph input还不支持是吧。如果已经支持,seg fault只是个例,我会先尝试其他model。谢谢。

QShiX avatar Oct 09 '22 09:10 QShiX

因为paddle里面的jit只支持tensor输入,不支持复杂自定义数据结构。所以如果你要直接用jit.to_static应该还是要改一下的,我估计应该像下面这种改法,仿照我上面说的,估计是可以的。

@paddle.jit.to_static
def forward(self, num_nodes, edges, x):
    graph = pgl.Graph(num_nodes, edges, node_feat={"x": x}
    blablabla

Yelrose avatar Oct 09 '22 09:10 Yelrose

好的,非常感谢。

QShiX avatar Oct 09 '22 09:10 QShiX

Hi,我试了下发现会有如下问题:

AssertionError: 'numpy' only can be called by paddle.Tensor in dynamic graph mode. Suggestions:

  1. If you are in static graph mode, you can switch to dynamic graph mode by turning off paddle.enable_static() or calling paddle.disable_static().
  2. If you are using @paddle.jit.to_static, you can turn off ProgramTranslator by calling paddle.jit.ProgramTranslator().enable(False). If you have to translate dynamic graph to static graph, please use other API to replace 'numpy'.

如果是numpy在static graph下用不了,那岂不是目前所有PGL model都用不了静态图了?因为https://github.com/PaddlePaddle/PGL/blob/main/pgl/graph.py 使用了numpy。谢谢。

QShiX avatar Oct 16 '22 06:10 QShiX

如果你要搞静态图,你可以看看这里的写法 https://github.com/PaddlePaddle/PGL/blob/main/examples/deploy_gnn/convert_to_static.py

num_nodes = static.data(name='num_nodes', shape=[-1], dtype='int32')
edges = static.data(name='edges', shape=[-1, 2], dtype='int32')
feature = static.data(
    name="feature",
    shape=[-1, graph_obj["node_feat"].shape[-1]],
    dtype="float32")

先定义一些placehodler,然后输入到网络里,在用静态图组网

class GNNModel(nn.Layer):
    def __init__(self, input_size, output_size, num_layers=3):
        super(GNNModel, self).__init__()
        self.conv_fn = nn.LayerList()
        self.conv_fn.append(gnn.GCNConv(input_size, output_size))
        for i in range(num_layers - 1):
            self.conv_fn.append(gnn.GCNConv(output_size, output_size))
        self.pool_fn = gnn.GraphPool("sum")

    def forward(self, num_nodes, edges, feature):
        graph = pgl.Graph(num_nodes=num_nodes, edges=edges)
        for fn in self.conv_fn:
            feature = fn(graph, feature)
        output = self.pool_fn(graph, feature)
        return output

然后数据加载的地方全部用numpy的图

Yelrose avatar Nov 29 '22 12:11 Yelrose