Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input type Graph 时的静态图转换 #475

Open
QShiX opened this issue Oct 8, 2022 · 6 comments
Open

input type Graph 时的静态图转换 #475

QShiX opened this issue Oct 8, 2022 · 6 comments

Comments

@QShiX
Copy link

QShiX commented Oct 8, 2022

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信息。谢谢。

@Yelrose
Copy link
Collaborator

Yelrose commented Oct 9, 2022

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

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

@QShiX
Copy link
Author

QShiX commented Oct 9, 2022

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

@Yelrose
Copy link
Collaborator

Yelrose commented Oct 9, 2022

因为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

@QShiX
Copy link
Author

QShiX commented Oct 9, 2022

好的,非常感谢。

@QShiX QShiX closed this as completed Oct 9, 2022
@QShiX
Copy link
Author

QShiX commented Oct 16, 2022

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 QShiX reopened this Oct 16, 2022
@Yelrose
Copy link
Collaborator

Yelrose commented Nov 29, 2022

如果你要搞静态图,你可以看看这里的写法 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的图

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants