AttributeError: '_OpNamespace' 'dgl_sparse' object has no attribute 'from_coo'

Hi DGL team,

I am interested in the new dgl sparse api, and experimenting with the tutorial examples. However, I received the attribute error when I ran the following code:

import torch
import dgl.sparse as dglsp

i = torch.tensor([[1, 1, 2],
[0, 2, 0]])
A = dglsp.spmatrix(i)

Environment:

  • System: MacOS 13.2

  • Python: 3.9.6

  • PyTorch: 1.13.1

  • DGL: 1.0.0

Could you please take a look? Thanks a lot.

The full error message is


RuntimeError Traceback (most recent call last)
File ~/Library/Python/3.9/lib/python/site-packages/torch/_ops.py:501, in _OpNamespace.getattr(self, op_name)
500 try:
→ 501 op, overload_names = torch._C._jit_get_operation(qualified_op_name)
502 except RuntimeError as e:
503 # Turn this into AttributeError so getattr(obj, key, default)
504 # works (this is called by TorchScript with origin)

RuntimeError: No such operator dgl_sparse::from_coo

The above exception was the direct cause of the following exception:

AttributeError Traceback (most recent call last)
Cell In [6], line 1
----> 1 A = dglsp.spmatrix(i)

File ~/Library/Python/3.9/lib/python/site-packages/dgl/sparse/sparse_matrix.py:524, in spmatrix(indices, val, shape)
456 def spmatrix(
457 indices: torch.Tensor,
458 val: Optional[torch.Tensor] = None,
459 shape: Optional[Tuple[int, int]] = None,
460 ) → SparseMatrix:
461 r""“Creates a sparse matrix from Coordinate format indices.
462
463 Parameters
(…)
522 shape=(3, 5), nnz=3, val_size=(2,))
523 “””
→ 524 return from_coo(indices[0], indices[1], val, shape)

File ~/Library/Python/3.9/lib/python/site-packages/dgl/sparse/sparse_matrix.py:611, in from_coo(row, col, val, shape)
605 val = torch.ones(row.shape[0]).to(row.device)
607 assert (
608 val.dim() <= 2
609 ), “The values of a SparseMatrix can only be scalars or vectors.”
→ 611 return SparseMatrix(torch.ops.dgl_sparse.from_coo(row, col, val, shape))

File ~/Library/Python/3.9/lib/python/site-packages/torch/_ops.py:505, in _OpNamespace.getattr(self, op_name)
501 op, overload_names = torch._C._jit_get_operation(qualified_op_name)
502 except RuntimeError as e:
503 # Turn this into AttributeError so getattr(obj, key, default)
504 # works (this is called by TorchScript with origin)
→ 505 raise AttributeError(
506 f"’_OpNamespace’ ‘{self.name}’ object has no attribute ‘{op_name}’"
507 ) from e
509 # let the script frontend know that op is identical to the builtin op
510 # with qualified_op_name
511 torch.jit._builtins._register_builtin(op, qualified_op_name)

AttributeError: ‘_OpNamespace’ ‘dgl_sparse’ object has no attribute ‘from_coo’

I find a workaround. After explicitly loading the dgl_sparse library, the example works fine:

torch.classes.load_library(‘Library/Python/3.9/lib/python/site-packages/dgl/dgl_sparse/libdgl_sparse_pytorch_1.13.1.dylib’)

There are still some issues with MacOS release and we are actively working on it. CC @BarclayII for viz.

Hi Simo, we’ve already fixed this issue for MAC in DGL 1.0.1, please update your dgl version and try again. Please let us know if you encounter any other issues.

1 Like

Thanks a lot. The code works OK after updating to DGL 1.0.1.

Hi Minjie, does the team have any plan to extend g-SPMM and g-SDDMM to support torch.ops? Thanks.

Hey Simo, do you mean you want to have some APIs like dgl.gspmm to directly take in PyTorch tensors?

Hi Minjie, I am wondering whether dgl.ops.u_sub_v for example will look like u_sub_v(A, x, y) or u_sub_v(indices, x, y), and all inputs are torch.Tensors?

Actually, I am looking for a convenient solution to deploying PyTorch models using C++. But I am still not sure how it can be achieved with DGL and its new feature perhaps. It seems to me that TorchScript is not supported yet. Any advice would be much appreciated. Thank you.

We are working on JIT support. The idea is to first support it for the newly released sparse API. You can watch the tracking issue here Support TorchScript for DGL Sparse · Issue #5275 · dmlc/dgl · GitHub .

Message passing APIs like u_sub_v can be directly translated to sparse operators:

A = ...    # sparse adjacency matrix
x, y = ...   # dense node feature tensors
# conduct u_sub_v
z = x[A.row] - y[A.col]
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.