-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathargmin.py
More file actions
185 lines (163 loc) · 5.48 KB
/
Copy pathargmin.py
File metadata and controls
185 lines (163 loc) · 5.48 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import math
import torch
import triton
import triton.language as tl
from flag_gems.runtime import torch_device_fn
from flag_gems.utils import libentry
from flag_gems.utils import triton_lang_extension as ext
from flag_gems.utils.limits import get_dtype_max
logger = logging.getLogger(__name__)
@libentry()
@triton.jit
def argmin_kernel_1(
inp,
mid_value,
mid_index,
M,
BLOCK_SIZE: tl.constexpr,
):
pid = ext.program_id(0)
offset = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
inp_ptrs = inp + offset
mask = offset < M
max_value = get_dtype_max(inp.type.element_ty)
inp_val = tl.load(inp_ptrs, mask=mask, other=max_value)
min_val, min_index = tl.min(inp_val, axis=0, return_indices=True)
min_index = min_index + pid * BLOCK_SIZE
mid_value_ptr = mid_value + pid
min_index_ptr = mid_index + pid
tl.store(mid_value_ptr, min_val)
tl.store(min_index_ptr, min_index)
@libentry()
@triton.jit
def argmin_kernel_2(
mid_value,
mid_index,
out,
mid_size,
BLOCK_MID: tl.constexpr,
):
offset = tl.arange(0, BLOCK_MID)
mid_ptrs = mid_value + offset
mask = offset < mid_size
max_value = get_dtype_max(mid_value.type.element_ty)
mid_val = tl.load(mid_ptrs, mask=mask, other=max_value)
index_val = tl.argmin(mid_val, axis=0)
mid_index_ptrs = mid_index + index_val
out_val = tl.load(mid_index_ptrs)
tl.store(out, out_val)
@libentry()
@triton.autotune(
configs=[
triton.Config({"BLOCK_M": 1, "BLOCK_N": 512}),
triton.Config({"BLOCK_M": 4, "BLOCK_N": 256}),
triton.Config({"BLOCK_M": 8, "BLOCK_N": 128}),
],
key=["M", "N", "K"],
)
@triton.jit
def argmin_kernel(
inp,
out_index,
M,
N,
K,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
):
pid_m = ext.program_id(0)
pid_k = ext.program_id(1)
m_offset = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
dtype = inp.type.element_ty
acc_type = tl.float32 if dtype is tl.bfloat16 else dtype
max_value = get_dtype_max(dtype)
min_values = tl.full([BLOCK_M], dtype=acc_type, value=max_value)
argmin_values = tl.full([BLOCK_M], dtype=tl.int64, value=0)
for start_n in range(0, N, BLOCK_N):
n_offset = start_n + tl.arange(0, BLOCK_N)
offset = m_offset[:, None] * N * K + n_offset[None, :] * K + pid_k
mask = m_offset[:, None] < M and n_offset[None, :] < N
inp_ptrs = inp + offset
inp_vals = tl.load(inp_ptrs, mask=mask, other=max_value)
local_min, local_argmin = tl.min(
inp_vals, 1, return_indices=True, return_indices_tie_break_left=True
)
update = local_min < min_values
min_values = tl.where(update, local_min, min_values)
argmin_values = tl.where(update, start_n + local_argmin, argmin_values)
offset_index = m_offset * K + pid_k
out_index_ptrs = out_index + offset_index
mask1 = m_offset < M
tl.store(out_index_ptrs, argmin_values, mask=mask1)
def argmin(inp, dim=None, keepdim=False, *, dtype=None):
logger.debug("GEMS_ASCEND ARGMIN")
if inp.dtype == torch.bfloat16:
result = argmin(inp.to(torch.float32), dim=dim, keepdim=keepdim, dtype=dtype)
return result
if dim is None:
inp = inp.contiguous()
M = inp.numel()
if dtype is None:
dtype = inp.dtype
block_size = triton.next_power_of_2(math.ceil(math.sqrt(M)))
mid_size = triton.cdiv(M, block_size)
block_mid = triton.next_power_of_2(mid_size)
mid_value = torch.empty((mid_size,), dtype=dtype, device=inp.device)
mid_index = torch.empty((mid_size,), dtype=torch.int64, device=inp.device)
out = torch.empty([], dtype=torch.int64, device=inp.device)
with torch_device_fn.device(inp.device):
argmin_kernel_1[(mid_size, 1, 1)](
inp,
mid_value,
mid_index,
M,
block_size,
)
argmin_kernel_2[(1, 1, 1)](
mid_value,
mid_index,
out,
mid_size,
block_mid,
)
return out
else:
assert dim >= -inp.ndim and dim < inp.ndim, "Invalid dim"
shape = inp.shape
dim = dim % inp.ndim
N = shape[dim]
M = math.prod(shape[:dim])
K = inp.numel() // M // N
inp = inp.contiguous()
shape_list = list(shape)
shape_list[dim] = 1
out_index = torch.empty(shape_list, dtype=torch.int64, device=inp.device)
if not keepdim:
out_index = torch.squeeze(out_index, dim)
grid = lambda meta: (
triton.cdiv(M, meta["BLOCK_M"]),
K,
)
with torch_device_fn.device(inp.device):
argmin_kernel[grid](
inp,
out_index,
M,
N,
K,
)
return out_index