The torch.all() is a condition checker function that tests whether all the elements of an input tensor evaluate to True or meet a certain condition. It can operate on the entire tensor or along specific dimensions.

Depending on the input, the output is either a single boolean value or a boolean tensor containing boolean values. This is helpful for tasks like validating tensor conditions, filtering data, or implementing logical checks.
Syntax
torch.all(input, dim=None, keepdim=False, out=None)
Parameters
Argument | Description |
input (Tensor) | It represents an input tensor. It can be boolean or numeric. The boolean treats non-zero values as True. |
dim (int, optional) | It is a single dimension or dimensions to reduce.
It represents the dimension along which to perform the reduction. If you don’t pass any or None, it will apply the condition to an entire tensor. |
keepdim (bool, optional) | By default, it is False, but if set to True, it retains the reduced dimension with size 1. |
out (Tensor, optional) | It is an optional output result to store the result. |
Checking all true elements
Let’s create a tensor of True values and check with torch.all() method to evaluate it.
import torch tensor_true = torch.tensor([True, True, True]) evaluation = torch.all(tensor_true) print(evaluation) # Output: tensor(True)
Since all the values are True and not a single value is False, it returns a scalar-valued tensor, which is tensor(True).
Let’s handle the False element. What if one of the input tensor’s elements is False? Let’s check that scenario.
import torch tensor = torch.tensor([True, False, True]) evaluation = torch.all(tensor) print(evaluation) # Output: tensor(False)
If .all() finds even a single False value within a tensor, it returns the output as False.
Non-boolean tensor

Python counts zero (0) as False and all other than 0, True—for example, any non-zero numbers.
import torch tensor = torch.tensor([1, 2, 0], dtype=torch.float32) evaluation = torch.all(tensor) print(evaluation) # Output: tensor(False)
In the above code, not all the elements are non-zero; there is one zero, which is why it returns False.
Now, if each element of the input tensor is a non-zero numeric value, it will return True.
import torch num_tensor = torch.tensor([1, 2, 21], dtype=torch.float32) output = torch.all(num_tensor) print(output) # Output: tensor(True)
Conditional operation
Let’s check out if our tensor has < 18 values.import torch tensor = torch.tensor([19, 17, 21], dtype=torch.float32) conditional_output = torch.all(tensor > 18) print(conditional_output) # Output: tensor(False)
We got the output tensor(False), which means there is at least one value less than 18, and yes, 17 is that value. So, we checked the criteria for the whole tensor and found that it does not satisfy that condition.
Let’s say we have a matrix, which is a 2D tensor, and we want to check it row-wise along dim=1.
If we find 0 or False along dim=1, it will return a 1D boolean tensor suggesting whether all elements are non-zero for each row. For example, if the first row has all non-zero elements, it returns True. If the second row contains zero elements, it will return False.
import torch tensor_2d = torch.tensor([[11, 21, 13], [4, 0, 6]]) check_along_rows = torch.all(tensor_2d, dim=1) print(check_along_rows) # Output: tensor([ True, False])
dim=0

For checking along columns, we need to pass dim=0 to the .all() method.
import torch tensor_2d = torch.tensor([[11, 21, 13], [4, 0, 6]]) check_along_cols = torch.all(tensor_2d, dim=0) print(check_along_cols) # Output: tensor([ True, False, True])
You can see that the first column does not have a non-zero element. So, the first value of the output tensor is True. The second column has 0, so False, and the third column does not contain zero, so True.
Retain the reduced dimension
import torch tensor = torch.tensor([[1, 2], [3, 4]]) output_keeping_dim = torch.all(tensor, dim=1, keepdim=True) print(output_keeping_dim) # Output: tensor([[True], # [True]])
The above output shows that the output tensor retains the reduced dimension as size 1.
Empty tensors
Conventionally, .all([]) is vacuously True. So, it returns True.import torch empty_tensor = torch.tensor([]) handling_empty = torch.all(empty_tensor) print(handling_empty) # Output: tensor(True)
It also takes a float(‘nan’) as a non-zero value, returning True here.
import torch nan_tensor = torch.tensor([float('nan')]) handling_nan = torch.all(nan_tensor) print(handling_nan) # Output: tensor(True)That’s all!