dmlc / dgl

Python package built to ease deep learning on graph, on top of existing DL frameworks.
http://dgl.ai
Apache License 2.0
13.41k stars 3k forks source link

Building dgl from source completely without pytorch #7509

Closed Darkdragon84 closed 2 months ago

Darkdragon84 commented 2 months ago

🐛 Bug

I am trying to build dgl from source completely without pytorch for tensorflow backend. I am able to successfully build and install dgl, but a simple import dgl then fails due to missing torch.

To Reproduce

Steps to reproduce the behavior:

  1. Clone source code (latest master in my case was 5b320e2145e9f9e300d29c6e8df3b02e1975f92c
    git clone --recurse-submodules https://github.com/dmlc/dgl.git
  2. Build for tensorflow backend only, disabling everything that seems to depend on torch
    export DGLBACKEND=tensorflow
    mkdir build && cd build
    cmake -DUSE_CUDA=OFF -DBUILD_TORCH=OFF -DBUILD_SPARSE=OFF -DBUILD_GRAPHBOLT=OFF ..
    make -j 4
    cd ..
  3. Create virtual environment and preinstall tensorflow (I used 2.8.4 for this example)
    python -m venv dgl
    source dgl/bin/activate
    pip install tensorflow==2.8.* 
  4. Build and install the python bindings as per the doc
    cd python
    python setup.py install
    # Build Cython extension
    python setup.py build_ext --inplace
  5. open a Python REPL and import dgl
    
    In [1]: import dgl
    2024-07-08 13:01:20.124484: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    ---------------------------------------------------------------------------
    ModuleNotFoundError                       Traceback (most recent call last)
    Cell In[1], line 1
    ----> 1 import dgl

File ~/python/dgl/python/dgl/init.py:16 13 from .logging import enable_verbose_logging # usort: skip 14 from .backend import backend_name, load_backend # usort: skip ---> 16 from . import ( 17 container, 18 cuda, 19 dataloading, 20 function, 21 ops, 22 random, 23 sampling, 24 storages, 25 ) 26 from ._ffi.base import version, DGLError 27 from ._ffi.function import ( 28 extract_ext_funcs, 29 get_global_func, 30 list_global_func_names, 31 register_func, 32 )

File ~/python/dgl/python/dgl/dataloading/init.py:4 2 from .. import backend as F 3 from . import negative_sampler ----> 4 from .base import 5 from .cluster_gcn import 6 from .graphsaint import *

File ~/python/dgl/python/dgl/dataloading/base.py:9 7 from ..convert import heterograph 8 from ..frame import LazyFeature ----> 9 from ..transforms import compact_graphs 10 from ..utils import context_of, recursive_apply 13 def _set_lazy_features(x, xdata, feature_names):

File ~/python/dgl/python/dgl/transforms/init.py:2 1 """Transform for structures and features""" ----> 2 from .functional import 3 from .module import 4 from .to_block import *

File ~/python/dgl/python/dgl/transforms/functional.py:55 46 from ..heterograph_index import ( 47 create_heterograph_from_relations, 48 create_metagraph_index, 49 ) 50 from ..partition import ( 51 metis_partition, 52 metis_partition_assignment, 53 partition_graph_with_halo, 54 ) ---> 55 from ..sampling.neighbor import sample_neighbors 57 all = [ 58 "line_graph", 59 "khop_adj", (...) 96 "svd_pe", 97 ] 100 def pairwise_squared_distance(x):

File ~/python/dgl/python/dgl/sampling/init.py:10 8 from .randomwalks import 9 from .pinsage import ---> 10 from .neighbor import 11 from .labor import 12 from .node2vec_randomwalk import *

File ~/python/dgl/python/dgl/sampling/neighbor.py:5 1 """Neighbor sampling APIs""" 3 import os ----> 5 import torch 7 from .. import backend as F, ndarray as nd, utils 8 from .._ffi.function import _init_api

ModuleNotFoundError: No module named 'torch'


## Expected behavior

`dgl` should import without errors and basic core functionality which doesn't depend on pytorch should be available.

## Environment

 - DGL Version: current latest master 5b320e2145e9f9e300d29c6e8df3b02e1975f92c
 - Backend Library & Version: tensorflow 2.8.4
 - OS: Ubuntu 22.04 6.5.0-41-generic
 - How you installed DGL: source
 - Build command you used (if compiling from source): see above
 - Python version: 3.10.14

## Additional context

Is there any way to just use dgl's core functionalities (especially `dgl.DGLGraph`) completely independent of pytorch? It seems the general scaffolding to use a general backend is there (similar to how Keras does it), but there are still some imports from modules who seem to explicitly depend on and import `torch`.

It would be great if `dgl` could be disentangled from pytorch in such a way that one can also use it (at least its core features) completely without pytorch, depending on which backend is specified.

Thanks!
mfbalin commented 2 months ago

The line below is the only line using torch. It needs to be changed to use F. alternative. Should be a simple fix. If you fix it, I can review and approve it. https://github.com/dmlc/dgl/blob/5b320e2145e9f9e300d29c6e8df3b02e1975f92c/python/dgl/sampling/neighbor.py#L611

mfbalin commented 2 months ago

Tensorflow and mxnet support are deprecated and I think the CI does not run their tests anymore. Unless this decision is reversed, I would not rely on DGL working with backends other than pytorch. @frozenbugs

frozenbugs commented 2 months ago

Yes, we just don't have staff to remove all the TF nad MXNet code.

Darkdragon84 commented 2 months ago

oh? that is unfortunate, I didn't see this mentioned anywhere prominently. Perhaps it would be good to emphasize on the landing page that support for other backends is deprecated. Thanks for the clarification though :-)

Darkdragon84 commented 2 months ago

I could submit a fix PR, but if TF support is deprecated anyway I suppose this is useless work?

frozenbugs commented 2 months ago

oh? that is unfortunate, I didn't see this mentioned anywhere prominently. Perhaps it would be good to emphasize on the landing page that support for other backends is deprecated. Thanks for the clarification though :-)

Sorry, we announced it in our slack channel. Yes, feel free to leave it as it.