code: def test_dense_to_sparse(seq, device):
      matrix = torch.rand(seq, seq, device=device)
      matrix[matrix > 0.9] = 0
      baseline_res = _baseline_dense_to_sparse(matrix.cpu().numpy())
      res = _dense_to_sparse(matrix, device=device)
  
      _idx_to_name = [
          "values",
          "row_indices",
          "row_offsets",
          "column_indices",
      ]
  
      for idx, (bi, i) in enumerate(zip(baseline_res, res)):
          if idx != 1:
              # row_indices is the result of an argsort, which is not stable
              # for same number of elements
              assert torch.allclose(
                  bi.to(device), i
              ), f"error in {_idx_to_name[idx]}"
          assert bi.dtype == i.dtype
          assert i.device == device
  
file_path: xformers/tests/test_sparsecs.py
imports: import pytest
  import torch
  from xformers.components.attention import maybe_sparsify
  from xformers.components.attention._sputnik_sparse import _dense_to_sparse
  from xformers.components.attention.core import SparseCS, _create_random_sparsity
label_desc: Use image node_id 5 for calling a global function with example usage: test_dense_to_sparse(seq, device) without return types