code: def to_dgl(
      data: Union[
          "torch_geometric.data.Data", "torch_geometric.data.HeteroData"
      ]
  ) -> Any:
      r"""Converts a :class:`torch_geometric.data.Data` or
      :class:`torch_geometric.data.HeteroData` instance to a :obj:`dgl` graph
      object.
  
      Args:
          data (torch_geometric.data.Data or torch_geometric.data.HeteroData):
              The data object.
  
      Example:
          >>> edge_index = torch.tensor([[0, 1, 1, 2, 3, 0], [1, 0, 2, 1, 4, 4]])
          >>> x = torch.randn(5, 3)
          >>> edge_attr = torch.randn(6, 2)
          >>> data = Data(x=x, edge_index=edge_index, edge_attr=y)
          >>> g = to_dgl(data)
          >>> g
          Graph(num_nodes=5, num_edges=6,
              ndata_schemes={'x': Scheme(shape=(3,))}
              edata_schemes={'edge_attr': Scheme(shape=(2, ))})
  
          >>> data = HeteroData()
          >>> data['paper'].x = torch.randn(5, 3)
          >>> data['author'].x = torch.ones(5, 3)
          >>> edge_index = torch.tensor([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]])
          >>> data['author', 'cites', 'paper'].edge_index = edge_index
          >>> g = to_dgl(data)
          >>> g
          Graph(num_nodes={'author': 5, 'paper': 5},
              num_edges={('author', 'cites', 'paper'): 5},
              metagraph=[('author', 'paper', 'cites')])
      """
      import dgl
  
      from torch_geometric.data import Data, HeteroData
  
      if isinstance(data, Data):
          if data.edge_index is not None:
              row, col = data.edge_index
          else:
              row, col, _ = data.adj_t.t().coo()
  
          g = dgl.graph((row, col))
  
          for attr in data.node_attrs():
              g.ndata[attr] = data[attr]
          for attr in data.edge_attrs():
              if attr in ["edge_index", "adj_t"]:
                  continue
              g.edata[attr] = data[attr]
  
          return g
  
      if isinstance(data, HeteroData):
          data_dict = {}
          for edge_type, edge_store in data.edge_items():
              if edge_store.get("edge_index") is not None:
                  row, col = edge_store.edge_index
              else:
                  row, col, _ = edge_store["adj_t"].t().coo()
  
              data_dict[edge_type] = (row, col)
  
          g = dgl.heterograph(data_dict)
  
          for node_type, node_store in data.node_items():
              for attr, value in node_store.items():
                  g.nodes[node_type].data[attr] = value
  
          for edge_type, edge_store in data.edge_items():
              for attr, value in edge_store.items():
                  if attr in ["edge_index", "adj_t"]:
                      continue
                  g.edges[edge_type].data[attr] = value
  
          return g
  
      raise ValueError(f"Invalid data type (got '{type(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 11 for calling a global function with example usage: to_dgl(data) and returns: g, g