|
| 1 | +import lbann |
| 2 | +from lbann.modules import Module, ChannelwiseFullyConnectedModule, ConvolutionModule |
| 3 | +import lbann.modules |
| 4 | + |
| 5 | + |
| 6 | +class GCN(Module): |
| 7 | + """ |
| 8 | + Graph convolutional kernel |
| 9 | + """ |
| 10 | + |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + num_nodes, |
| 14 | + num_edges, |
| 15 | + input_features, |
| 16 | + output_features, |
| 17 | + activation=lbann.Relu, |
| 18 | + distconv_enabled=True, |
| 19 | + num_groups=4, |
| 20 | + ): |
| 21 | + super().__init__() |
| 22 | + self._input_dims = input_features |
| 23 | + self._output_dims = output_features |
| 24 | + self._num_nodes = num_nodes |
| 25 | + self._num_edges = num_edges |
| 26 | + |
| 27 | + def forward(self, node_features, source_indices, target_indices): |
| 28 | + x = lbann.Gather(node_features, target_indices, axis=0) |
| 29 | + x = lbann.ChannelwiseFullyConnected(x, output_channel_dims=self._output_dims) |
| 30 | + x = self._activation(x) |
| 31 | + x = lbann.Scatter(x, source_indices, dims=self._ft_dims) |
| 32 | + return x |
| 33 | + |
| 34 | + |
| 35 | +def create_model(num_nodes, num_edges, input_features, output_features, num_layers=3): |
| 36 | + """ |
| 37 | + Create a GCN model |
| 38 | + """ |
| 39 | + # Layer graph |
| 40 | + input_ = lbann.Input() |
| 41 | + split_indices = [0, num_nodes * input_features] |
| 42 | + split_indices += [split_indices[-1] + num_edges] |
| 43 | + split_indices += [split_indices[-1] + num_edges] |
| 44 | + split_indices += [split_indices[-1] + num_nodes] |
| 45 | + |
| 46 | + node_features = lbann.Reshape( |
| 47 | + lbann.Identity(input_), dims=[num_nodes, input_features] |
| 48 | + ) |
| 49 | + |
| 50 | + source_indices = lbann.Reshape(lbann.Identity(input_), dims=[num_edges]) |
| 51 | + target_indices = lbann.Reshape(lbann.Identity(input_), dims=[num_edges]) |
| 52 | + label = lbann.Reshape(lbann.Identity(input_), dims=[num_nodes]) |
| 53 | + |
| 54 | + x = GCN( |
| 55 | + num_nodes, |
| 56 | + num_edges, |
| 57 | + input_features, |
| 58 | + output_features, |
| 59 | + activation=lbann.Relu, |
| 60 | + distconv_enabled=False, |
| 61 | + num_groups=4, |
| 62 | + )(node_features, source_indices, target_indices) |
| 63 | + |
| 64 | + for _ in range(num_layers - 1): |
| 65 | + x = GCN( |
| 66 | + num_nodes, |
| 67 | + num_edges, |
| 68 | + input_features, |
| 69 | + output_features, |
| 70 | + activation=lbann.Relu, |
| 71 | + distconv_enabled=False, |
| 72 | + num_groups=4, |
| 73 | + )(x, source_indices, target_indices) |
| 74 | + |
| 75 | + # Loss function |
| 76 | + loss = lbann.CrossEntropy([x, label]) |
| 77 | + |
| 78 | + # Metrics |
| 79 | + acc = lbann.CategoricalAccuracy([x, label]) |
| 80 | + |
| 81 | + # Callbacks |
| 82 | + callbacks = [lbann.CallbackPrint(), lbann.CallbackTimer()] |
| 83 | + |
| 84 | + # Construct model |
| 85 | + return lbann.Model( |
| 86 | + num_epochs=1, |
| 87 | + layers=lbann.traverse_layer_graph(input_), |
| 88 | + objective_function=loss, |
| 89 | + metrics=[acc], |
| 90 | + callbacks=callbacks, |
| 91 | + ) |
0 commit comments