Repository: dmlc/dgl

Issue:
GIN model cannot handle empty graphs
## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

## To Reproduce

Steps to reproduce the behavior:

1. Create an empty graph
1. Pass the empty graph into a GIN model
1. Run, and it would error.

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->
It may seem weird, but for compatibility, the GIN model should have the ability to handle empty graph. For an expected behavior, I hope the model would generate embeddings with all zeros, too.

## Environment

 - DGL Version (e.g., 1.0):  0.5.2
 - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): Pytorch 1.7.0 (cpu)
 - OS (e.g., Linux): Windows 10
 - How you installed DGL (`conda`, `pip`, source): pip

 - Python version: 3.8


## Additional context

<!-- Add any other context about the problem here. -->
In the **forward** function of the following code defining a GINConv module:
```python
 graph.update_all(fn.copy_u('h', 'm'), self._reducer('m', 'neigh'))
 rst = (1 + self.eps) * feat_dst + graph.dstdata['neigh']
```


For an empty graph, the **updata_all** function does not generate the **neigh** attribute, so the next line would error when trying to access it. So is there a way to generate all zeros attributes when the graph is empty?



```python
 class GINConv(nn.Module):
    """

    Description
    -----------
    Graph Isomorphism Network layer from paper `How Powerful are Graph
    Neural Networks? <https://arxiv.org/pdf/1810.00826.pdf>`__.

    .. math::
        h_i^{(l+1)} = f_\Theta \left((1 + \epsilon) h_i^{l} +
        \mathrm{aggregate}\left(\left\{h_j^{l}, j\in\mathcal{N}(i)
        \right\}\right)\right)

    Parameters
    ----------
    apply_func : callable activation function/layer or None
        If not None, apply this function to the updated node feature,
        the :math:`f_\Theta` in the formula.
    aggregator_type : str
        Aggregator type to use (``sum``, ``max`` or ``mean``).
    init_eps : float, optional
        Initial :math:`\epsilon` value, default: ``0``.
    learn_eps : bool, optional
        If True, :math:`\epsilon` will be a learnable parameter. Default: ``False``.

    Example
    -------
    >>> import dgl
    >>> import numpy as np
    >>> import torch as th
    >>> from dgl.nn import GINConv
    >>>
    >>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3]))
    >>> feat = th.ones(6, 10)
    >>> lin = th.nn.Linear(10, 10)
    >>> conv = GINConv(lin, 'max')
    >>> res = conv(g, feat)
    >>> res
    tensor([[-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
            0.8843, -0.8764],
            [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
            0.8843, -0.8764],
            [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
            0.8843, -0.8764],
            [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
            0.8843, -0.8764],
            [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
            0.8843, -0.8764],
            [-0.1804,  0.0758, -0.5159,  0.3569, -0.1408, -0.1395, -0.2387,  0.7773,
            0.5266, -0.4465]], grad_fn=<AddmmBackward>)
    """
    def __init__(self,
                 apply_func,
                 aggregator_type,
                 init_eps=0,
                 learn_eps=False):
        super(GINConv, self).__init__()
        self.apply_func = apply_func
        self._aggregator_type = aggregator_type
        if aggregator_type == 'sum':
            self._reducer = fn.sum
        elif aggregator_type == 'max':
            self._reducer = fn.max
        elif aggregator_type == 'mean':
            self._reducer = fn.mean
        else:
            raise KeyError('Aggregator type {} not recognized.'.format(aggregator_type))
        # to specify whether eps is trainable or not.
        if learn_eps:
            self.eps = th.nn.Parameter(th.FloatTensor([init_eps]))
        else:
            self.register_buffer('eps', th.FloatTensor([init_eps]))

    def forward(self, graph, feat):
        r"""

        Description
        -----------
        Compute Graph Isomorphism Network layer.

        Parameters
        ----------
        graph : DGLGraph
            The graph.
        feat : torch.Tensor or pair of torch.Tensor
            If a torch.Tensor is given, the input feature of shape :math:`(N, D_{in})` where
            :math:`D_{in}` is size of input feature, :math:`N` is the number of nodes.
            If a pair of torch.Tensor is given, the pair must contain two tensors of shape
            :math:`(N_{in}, D_{in})` and :math:`(N_{out}, D_{in})`.
            If ``apply_func`` is not None, :math:`D_{in}` should
            fit the input dimensionality requirement of ``apply_func``.

        Returns
        -------
        torch.Tensor
            The output feature of shape :math:`(N, D_{out})` where
            :math:`D_{out}` is the output dimensionality of ``apply_func``.
            If ``apply_func`` is None, :math:`D_{out}` should be the same
            as input dimensionality.
        """
        with graph.local_scope():
            feat_src, feat_dst = expand_as_pair(feat, graph)
            graph.srcdata['h'] = feat_src
            graph.update_all(fn.copy_u('h', 'm'), self._reducer('m', 'neigh'))
            rst = (1 + self.eps) * feat_dst + graph.dstdata['neigh']
            if self.apply_func is not None:
                rst = self.apply_func(rst)
            return rst
```



Discussion:
Thanks for reporting this bug, we'll fix it soon.
> Thanks for reporting this bug, we'll fix it soon.

Hi, 
    In the file ginconv.py, I think using the format of map.get(attr, default_value) is more compatible than directly accessing the attributes,
so the line 114 in ginconv file could be changed to:
```python
 rst = (1 + self.eps) * feat_dst + graph.dstdata.get('neigh', th.zeros_like(feat_dst))
```
So, even the graph is empty, we can directly return the features of neighbors with all zeros. 
You can use it as a workaround but dgl should yield an empty tensor in the node frame.
> You can use it as a workaround but dgl should yield an empty tensor in the node frame.

I agree, and it would be more concise. Thanks~

Resolving patch:
diff --git a/python/dgl/core.py b/python/dgl/core.py
--- a/python/dgl/core.py
+++ b/python/dgl/core.py
@@ -274,11 +274,8 @@ def message_passing(g, mfunc, rfunc, afunc):
     dict[str, Tensor]
         Results from the message passing computation.
     """
-    if g.number_of_edges() == 0:
-        # No message passing is triggered.
-        ndata = {}
-    elif (is_builtin(mfunc) and is_builtin(rfunc) and
-          getattr(ops, '{}_{}'.format(mfunc.name, rfunc.name), None) is not None):
+    if (is_builtin(mfunc) and is_builtin(rfunc) and
+            getattr(ops, '{}_{}'.format(mfunc.name, rfunc.name), None) is not None):
         # invoke fused message passing
         ndata = invoke_gspmm(g, mfunc, rfunc)
     else:


Test patch: