code: def test_dense_to_sparse():
      adj = torch.tensor(
          [
              [3.0, 1.0],
              [2.0, 0.0],
          ]
      )
      edge_index, edge_attr = dense_to_sparse(adj)
      assert edge_index.tolist() == [[0, 0, 1], [0, 1, 0]]
      assert edge_attr.tolist() == [3, 1, 2]
  
      if is_full_test():
          jit = torch.jit.script(dense_to_sparse)
          edge_index, edge_attr = jit(adj)
          assert edge_index.tolist() == [[0, 0, 1], [0, 1, 0]]
          assert edge_attr.tolist() == [3, 1, 2]
  
      adj = torch.tensor(
          [
              [
                  [3.0, 1.0],
                  [2.0, 0.0],
              ],
              [
                  [0.0, 1.0],
                  [0.0, 2.0],
              ],
          ]
      )
      edge_index, edge_attr = dense_to_sparse(adj)
      assert edge_index.tolist() == [[0, 0, 1, 2, 3], [0, 1, 0, 3, 3]]
      assert edge_attr.tolist() == [3, 1, 2, 1, 2]
  
      if is_full_test():
          jit = torch.jit.script(dense_to_sparse)
          edge_index, edge_attr = jit(adj)
          assert edge_index.tolist() == [
              [0, 0, 1, 2, 3],
              [0, 1, 0, 3, 3],
          ]
          assert edge_attr.tolist() == [3, 1, 2, 1, 2]
  
      adj = torch.tensor(
          [
              [
                  [3.0, 1.0, 0.0],
                  [2.0, 0.0, 0.0],
                  [0.0, 0.0, 0.0],
              ],
              [
                  [0.0, 1.0, 0.0],
                  [0.0, 2.0, 3.0],
                  [0.0, 5.0, 0.0],
              ],
          ]
      )
      mask = torch.tensor([[True, True, False], [True, True, True]])
  
      edge_index, edge_attr = dense_to_sparse(adj, mask)
  
      assert edge_index.tolist() == [
          [0, 0, 1, 2, 3, 3, 4],
          [0, 1, 0, 3, 3, 4, 3],
      ]
      assert edge_attr.tolist() == [3, 1, 2, 1, 2, 3, 5]
  
      # There is a bug in TorchScript for PyTorch < 1.12 :(
      if torch_geometric.typing.WITH_PT112 and is_full_test():
          jit = torch.jit.script(dense_to_sparse)
          edge_index, edge_attr = jit(adj, mask)
          assert edge_index.tolist() == [
              [0, 0, 1, 2, 3, 3, 4],
              [0, 1, 0, 3, 3, 4, 3],
          ]
          assert edge_attr.tolist() == [3, 1, 2, 1, 2, 3, 5]
  
file_path: pytorch_geometric/test/utils/test_sparse.py
imports: import os.path
  import pytest
  import torch
  import torch_geometric.typing
  from torch_geometric.profile import benchmark
  from torch_geometric.testing import is_full_test, withCUDA, withPackage
  from torch_geometric.typing import SparseTensor
  from torch_geometric.utils import dense_to_sparse, is_sparse, is_torch_sparse_tensor, to_edge_index, to_torch_coo_tensor, to_torch_csc_tensor, to_torch_csr_tensor, to_torch_sparse_tensor
  from torch_geometric.utils.sparse import cat
label_desc: Use image node_id 1 for calling a global function with example usage: test_dense_to_sparse() without return types