code: def from_dgl(
      g: Any,
  ) -> Union[
      "torch_geometric.data.Data", "torch_geometric.data.HeteroData"
  ]:
      r"""Converts a :obj:`dgl` graph object to a
      :class:`torch_geometric.data.Data` or
      :class:`torch_geometric.data.HeteroData` instance.
  
      Args:
          g (dgl.DGLGraph): The :obj:`dgl` graph object.
  
      Example:
          >>> g = dgl.graph(([0, 0, 1, 5], [1, 2, 2, 0]))
          >>> g.ndata['x'] = torch.randn(g.num_nodes(), 3)
          >>> g.edata['edge_attr'] = torch.randn(g.num_edges(), 2)
          >>> data = from_dgl(g)
          >>> data
          Data(x=[6, 3], edge_attr=[4, 2], edge_index=[2, 4])
  
          >>> g = dgl.heterograph({
          >>> g = dgl.heterograph({
          ...     ('author', 'writes', 'paper'): ([0, 1, 1, 2, 3, 3, 4],
          ...                                     [0, 0, 1, 1, 1, 2, 2])})
          >>> g.nodes['author'].data['x'] = torch.randn(5, 3)
          >>> g.nodes['paper'].data['x'] = torch.randn(5, 3)
          >>> data = from_dgl(g)
          >>> data
          HeteroData(
          author={ x=[5, 3] },
          paper={ x=[3, 3] },
          (author, writes, paper)={ edge_index=[2, 7] }
          )
      """
      import dgl
  
      from torch_geometric.data import Data, HeteroData
  
      if not isinstance(g, dgl.DGLGraph):
          raise ValueError(f"Invalid data type (got '{type(g)}')")
  
      data: Union[Data, HeteroData]
  
      if g.is_homogeneous:
          data = Data()
          data.edge_index = torch.stack(g.edges(), dim=0)
  
          for attr, value in g.ndata.items():
              data[attr] = value
          for attr, value in g.edata.items():
              data[attr] = value
  
          return data
  
      data = HeteroData()
  
      for node_type in g.ntypes:
          for attr, value in g.nodes[node_type].data.items():
              data[node_type][attr] = value
  
      for edge_type in g.canonical_etypes:
          row, col = g.edges(form="uv", etype=edge_type)
          data[edge_type].edge_index = torch.stack([row, col], dim=0)
          for attr, value in g.edge_attr_schemes(edge_type).items():
              data[edge_type][attr] = value
  
      return data
  
file_path: pytorch_geometric/torch_geometric/utils/convert.py
imports: from collections import defaultdict
  from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple, Union
  import scipy.sparse
  import torch
  from torch import Tensor
  from torch.utils.dlpack import from_dlpack, to_dlpack
  import torch_geometric
  from torch_geometric.utils.num_nodes import maybe_num_nodes
label_desc: Use image node_id 12 for calling a global function with example usage: from_dgl(g) and returns: data, data