count_nonzero(input: Tensor, dim: int | Sequence[int] | None = None)Count non-zero elements along dim (or whole tensor when None).
Parameters
inputTensorInput. Non-zero is determined by element-wise comparison to
zero (NaN counts as non-zero).
dimint, sequence of int, or None= NoneAxis or axes to reduce.
None (default) reduces the entire
tensor to a 0-D scalar.Returns
Tensorint64 count(s). Shape mirrors lucid.sum over the
same dim argument.
Notes
The sum of the bool mask x != 0, which is int64 and exact. It
used to build a float32 1.0/0.0 mask with where and cast the
float sum back — three full-size temporaries and ten times a plain
reduction, from a time the CPU could not sum a bool.