-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathmean.py
More file actions
117 lines (99 loc) · 3.32 KB
/
Copy pathmean.py
File metadata and controls
117 lines (99 loc) · 3.32 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
# 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 import runtime
from flag_gems.runtime import torch_device_fn
from flag_gems.utils import dim_compress, libentry
from flag_gems.utils import triton_lang_extension as ext
logger = logging.getLogger(__name__)
@libentry()
@triton.jit
def mean_kernel_1(
inp,
mid,
M,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(0)
off = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = off < M
inp_val = tl.load(inp + off, mask=mask, other=0.0).to(tl.float32)
partial_sum = tl.sum(inp_val, axis=0)
tl.store(mid + pid, partial_sum)
def mean(inp, *, dtype=None):
logger.debug("GEMS_ASCEND MEAN")
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)))
block_size = min(block_size, 2048)
out = torch.zeros([], dtype=torch.float32, device=inp.device)
num_ctas = triton.cdiv(M, block_size)
mid = torch.zeros([num_ctas], dtype=torch.float32, device=inp.device)
with torch_device_fn.device(inp.device):
mean_kernel_1[(num_ctas, 1, 1)](inp, mid, M, block_size)
out = mid.sum() / M
return out.to(dtype)
@libentry()
@triton.autotune(
configs=runtime.get_tuned_config("mean"),
key=["M", "N"],
)
@triton.jit
def mean_dim_kernel(X, Mean, M, N, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
# Map the program id to the row of X it should compute.
pid = ext.program_id(0) * BLOCK_M + tl.arange(0, BLOCK_M)[:, None]
X = X + pid * N
Mean = Mean + pid
row_mask = pid < M
# Compute mean
_mean = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
for off in range(0, N, BLOCK_N):
cols = off + tl.arange(0, BLOCK_N)[None, :]
col_mask = cols < N
mask = row_mask and col_mask
a = tl.load(X + cols, mask, other=0.0).to(tl.float32)
_mean += a
mean = tl.sum(_mean, axis=1) / N
mean = mean[:, None]
tl.store(Mean, mean, row_mask)
def mean_dim(x, dim, keepdim=False, *, dtype=None):
logger.debug("GEMS_ASCEND MEAN_DIM")
if dtype is None:
dtype = x.dtype
if dim is None:
out = mean(x, dtype=dtype)
if not keepdim:
out = out.reshape([1] * x.ndim)
return out
shape = list(x.shape)
dim = [d % x.ndim for d in dim]
x = dim_compress(x, dim)
N = 1
for i in dim:
N *= shape[i]
shape[i] = 1
M = x.numel() // N
out = torch.empty(shape, dtype=dtype, device=x.device)
grid = lambda META: (triton.cdiv(M, META["BLOCK_M"]),)
with torch_device_fn.device(x.device):
mean_dim_kernel[grid](x, out, M, N)
if not keepdim:
out = out.squeeze(dim)
return out