Table of Contents
Shortcuts

# check_sparse_tensor_invariants

class torch.sparse.check_sparse_tensor_invariants(enable=True)

  A tool to control checking sparse tensor invariants.

  The following options exists to manage sparsr tensor invariants checking in sparse tensor construction:

  1. Using a context manager:

         with torch.sparse.check_sparse_tensor_invariants():
             run_my_model()

  2. Using a procedural approach:

         prev_checks_enabled = torch.sparse.check_sparse_tensor_invariants.is_enabled()
         torch.sparse.check_sparse_tensor_invariants.enable()

         run_my_model()

         if not prev_checks_enabled:
             torch.sparse.check_sparse_tensor_invariants.disable()

  3. Using function decoration:

         @torch.sparse.check_sparse_tensor_invariants()
         def run_my_model():
             ...

         run_my_model()

  4. Using `check_invariants` keyword argument in sparse tensor constructor call. For example:

         >>> torch.sparse_csr_tensor([0, 1, 3], [0, 1], [1, 2], check_invariants=True)
         Traceback (most recent call last):
           File "<stdin>", line 1, in <module>
         RuntimeError: `crow_indices[..., -1] == nnz` is not satisfied.

  static disable()

    Disable sparse tensor invariants checking in sparse tensor constructors.

    See `torch.sparse.check_sparse_tensor_invariants.enable()` for more information.

  static enable()

    Enable sparse tensor invariants checking in sparse tensor constructors.

    Note

    By default, the sparse tensor invariants checks are disabled. Use `torch.sparse.check_sparse_tensor_invariants.is_enabled()` to retrieve the current state of sparse tensor invariants checking.

    Note

    The sparse tensor invariants check flag is effective to all sparse tensor constructors, both in Python and ATen.

    The flag can be locally overridden by the `check_invariants` optional argument of the sparse tensor constructor functions.

  static is_enabled()

    Returns True if the sparse tensor invariants checking is enabled.

    Note

    Use `torch.sparse.check_sparse_tensor_invariants.enable()` or `torch.sparse.check_sparse_tensor_invariants.disable()` to manage the state of the sparse tensor invariants checks.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.