-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathall.py
More file actions
57 lines (46 loc) · 1.68 KB
/
all.py
File metadata and controls
57 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import infinicore
from infinicore.lib import _infinicore
from infinicore.tensor import Tensor
def all(
input: Tensor,
dim: int | tuple[int] | list[int] | None = None,
keepdim: bool = False,
*,
out=None,
) -> Tensor:
r"""Computes the logical AND of all elements."""
if infinicore.use_ntops and input.device.type in ("cuda", "musa"):
return infinicore.ntops.torch.all(input, dim=dim, keepdim=keepdim, out=out)
if dim is None:
if out is None:
return Tensor(_infinicore.all_global(input._underlying))
_infinicore.all_global_(input._underlying, out._underlying)
return out
else:
if isinstance(dim, int):
dims = [dim]
else:
dims = list(dim)
ndim = input.ndim
normalized_dims = sorted(
[d if d >= 0 else d + ndim for d in dims], reverse=True
)
current_input = input
if len(normalized_dims) == 1 and out is not None:
_infinicore.all_reduce_(
current_input._underlying, out._underlying, normalized_dims[0], keepdim
)
return out
for i, target_dim in enumerate(normalized_dims):
is_last_step = i == len(normalized_dims) - 1
if is_last_step and out is not None:
_infinicore.all_reduce_(
current_input._underlying, out._underlying, target_dim, keepdim
)
return out
else:
res_ptr = _infinicore.all_reduce(
current_input._underlying, target_dim, keepdim
)
current_input = Tensor(res_ptr)
return current_input