For reference, this is the native function I added:
aten/src/ATen/native/TensorShape.cpp
Tensor my_op(const Tensor& self) {
  if(self.is_sparse()){
    printf("%ld\n", self._sparseDims()); // prints "2"
    printf("%lld\n", _get_sparse_impl(self)->sparseDims()); // prints "140495002593472" on CPU; segfault on CUDA
  }
}

native_functions.yaml
func: my_op(Tensor self) -> Tensor
variants: method