Repository: dmlc/dgl

Issue:
[Bug?] Empty cuda graph raise error when create_formats_
## 🐛 Bug

```python
import dgl
g = dgl.graph([])
g = g.to("cuda:0")
g.create_format_()
# raise error
```

```
~/dev/csr/dgl/python/dgl/heterograph_index.py in create_format_(self)
    912     def create_format_(self):
    913         """Create all sparse matrices allowed for the graph."""
--> 914         return _CAPI_DGLHeteroCreateFormat(self)
    915
    916     def reverse(self):

~/dev/csr/dgl/python/dgl/_ffi/_cython/function.pxi in dgl._ffi._cy3.core.FunctionBase.__call__()

~/dev/csr/dgl/python/dgl/_ffi/_cython/function.pxi in dgl._ffi._cy3.core.FuncCall()

~/dev/csr/dgl/python/dgl/_ffi/_cython/function.pxi in dgl._ffi._cy3.core.FuncCall3()

~/dev/csr/dgl/python/dgl/_ffi/_cython/base.pxi in dgl._ffi._cy3.core.CALL()

DGLError: [08:55:53] /home/ubuntu/dev/csr/dgl/src/array/cuda/./utils.h:26: Check failed: dim != 0 (0 vs. 0) :
Stack trace:
  [bt] (0) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x34) [0x7f233982251a]
  [bt] (1) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dgl::cuda::FindNumThreads(int, int)+0xd1) [0x7f233a8e6596]
  [bt] (2) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dgl::aten::CSRMatrix dgl::aten::impl::COOToCSR<(DLDeviceType)2, long>(dgl::aten::COOMatrix)+0x2a8) [0x7f233a92df0f]
  [bt] (3) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dgl::aten::COOToCSR(dgl::aten::COOMatrix)+0x4ab) [0x7f23398163e8]
  [bt] (4) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dgl::UnitGraph::GetOutCSR(bool) const+0x3d4) [0x7f233a894888]
  [bt] (5) /home/ubuntu/dev/csr/dgl/build/libdgl.so(dgl::UnitGraph::GetFormat(dgl::SparseFormat) const+0x49) [0x7f233a895477]
  [bt] (6) /home/ubuntu/dev/csr/dgl/build/libdgl.so(+0x22410b3) [0x7f233a7b00b3]
  [bt] (7) /home/ubuntu/dev/csr/dgl/build/libdgl.so(+0x224a70a) [0x7f233a7b970a]
  [bt] (8) /home/ubuntu/dev/csr/dgl/build/libdgl.so(std::function<void (dgl::runtime::DGLArgs, dgl::runtime::DGLRetValue*)>::operator()(dgl::runtime::DGLArgs, dgl::runtime::DGLRetValue*) const+0x5a) [0x7f233a7075de]
```

## To Reproduce

Steps to reproduce the behavior:

1.
1.
1.

<!-- 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. -->

## Environment

 - DGL Version (e.g., 1.0):
 - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3):
 - OS (e.g., Linux):
 - How you installed DGL (`conda`, `pip`, source):
 - Build command you used (if compiling from source):
 - Python version:
 - CUDA/cuDNN version (if applicable):
 - GPU models and configuration (e.g. V100):
 - Any other relevant information:

## Additional context

<!-- Add any other context about the problem here. -->



Discussion:
Yep 0 threads is not a desired behavior, we should add some checks in the future.

Resolving patch:
diff --git a/python/dgl/heterograph.py b/python/dgl/heterograph.py
--- a/python/dgl/heterograph.py
+++ b/python/dgl/heterograph.py
@@ -5279,6 +5279,9 @@ def create_format_(self):
         >>> g.format()
         {'created': ['coo', 'csr', 'csc'], 'not created': []}
         """
+        if self.num_edges() == 0:
+            return 0
+
         return self._graph.create_format_()
 
     def astype(self, idtype):


Test patch: