Skip to content

Commit d9f0335

Browse files
committed
Fix operator correctness on Triton and CUDA
1 parent aea0253 commit d9f0335

205 files changed

Lines changed: 800 additions & 684 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/ntops_lab/kernels/creation/affine_grid.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ def _component_kernel(height, width):
1212
scale_x = 2.0 / float(width)
1313

1414
def arrangement(theta_row, out_component, scale_y_tensor, scale_x_tensor):
15-
theta_arr = theta_row[:, None, None, :].expand((-1, height, width, -1)).flatten(end_dim=3)
15+
theta_arr = theta_row[:, None, None, :, :].expand((-1, height, width, -1, -1)).flatten(end_dim=4)
1616
out_arr = out_component.flatten()
1717
return theta_arr.tile((1, 3)), out_arr.tile((1,)), scale_y_tensor, scale_x_tensor
1818

1919
def application(theta, out, scale_y_tensor, scale_x_tensor):
2020
x = ((out.offsets(2) + 0.5) * scale_x_tensor) - 1.0
2121
y = ((out.offsets(1) + 0.5) * scale_y_tensor) - 1.0
22-
t0 = ntl.sum(theta * (theta.offsets(1) == 0), axis=1)
23-
t1 = ntl.sum(theta * (theta.offsets(1) == 1), axis=1)
24-
t2 = ntl.sum(theta * (theta.offsets(1) == 2), axis=1)
22+
t0 = ntl.sum(theta * (theta.offsets(2) == 0), axis=1)
23+
t1 = ntl.sum(theta * (theta.offsets(2) == 1), axis=1)
24+
t2 = ntl.sum(theta * (theta.offsets(2) == 2), axis=1)
2525
out = t0 * x + t1 * y + t2
2626

2727
return ninetoothed.make(
2828
arrangement,
2929
application,
3030
(
31-
Tensor(2),
3231
Tensor(3),
32+
Tensor(4),
3333
Tensor(0, constexpr=True, value=scale_y, name="scale_y"),
3434
Tensor(0, constexpr=True, value=scale_x, name="scale_x"),
3535
),
@@ -46,6 +46,5 @@ def run(theta, size, align_corners=False):
4646
kernel = _component_kernel(height, width)
4747
scale_y = 2.0 / float(height)
4848
scale_x = 2.0 / float(width)
49-
kernel(theta[:, 0, :], out[..., 0], scale_y, scale_x)
50-
kernel(theta[:, 1, :], out[..., 1], scale_y, scale_x)
49+
kernel(theta, out, scale_y, scale_x)
5150
return out

‎src/ntops_lab/kernels/fused/general/_flash_attention.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def application(q, k, v, is_causal, softmax_scale, out):
3535
m_i = ntl.full((q.shape[-2],), float("-inf"), dtype=ntl.float32)
3636

3737
for i in range(k.shape[0]):
38-
qk = ntl.dot(q_loaded, ntl.trans(k[i]))
38+
qk = ntl.dot(q_loaded, ntl.trans(k[i])).to(ntl.float32)
3939
qk = ntl.where(k[i].offsets(-2) < k.source.shape[-2], qk, float("-inf"))
4040

4141
if is_causal:

‎src/ntops_lab/kernels/fused/general/add_layer_norm_sigmoid.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def application(x, residual, gamma, beta, out, hidden):
1616
centered = y - mean[:, None]
1717
var = ntl.sum(centered * centered, axis=1) / hidden
1818
value = centered * ntl.rsqrt(var[:, None] + 1.0e-5) * gamma + beta
19-
out = ntl.sigmoid(value)
19+
out = (1.0 / (1.0 + ntl.exp(-(value))))
2020

2121
@functools.cache
2222
def _kernel(hidden):

‎src/ntops_lab/kernels/fused/general/add_layer_norm_silu.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def application(x, residual, gamma, beta, out, hidden):
1616
centered = y - mean[:, None]
1717
var = ntl.sum(centered * centered, axis=1) / hidden
1818
value = centered * ntl.rsqrt(var[:, None] + 1.0e-5) * gamma + beta
19-
out = value * ntl.sigmoid(value)
19+
out = value * (1.0 / (1.0 + ntl.exp(-(value))))
2020

2121
@functools.cache
2222
def _kernel(hidden):

‎src/ntops_lab/kernels/fused/general/add_rms_norm_sigmoid.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def application(x, residual, weight, out, hidden):
1313
y = x + residual
1414
rrms = ntl.rsqrt(ntl.sum(y * y, axis=1) / hidden + 1.0e-5)
1515
value = y * rrms[:, None] * weight
16-
out = ntl.sigmoid(value)
16+
out = (1.0 / (1.0 + ntl.exp(-(value))))
1717

1818
@functools.cache
1919
def _kernel(hidden):

‎src/ntops_lab/kernels/fused/general/add_rms_norm_silu.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def application(x, residual, weight, out, hidden):
1313
y = x + residual
1414
rrms = ntl.rsqrt(ntl.sum(y * y, axis=1) / hidden + 1.0e-5)
1515
value = y * rrms[:, None] * weight
16-
out = value * ntl.sigmoid(value)
16+
out = value * (1.0 / (1.0 + ntl.exp(-(value))))
1717

1818
@functools.cache
1919
def _kernel(hidden):

‎src/ntops_lab/kernels/fused/general/fused_addmm_bias_abs.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import ninetoothed.language as ntl
44
from ninetoothed import Tensor, block_size
55

6-
BM = block_size()
7-
BN = block_size()
8-
BK = block_size()
6+
BM = block_size(upper_bound=64)
7+
BN = block_size(upper_bound=64)
8+
BK = block_size(upper_bound=64)
99

1010
def arrangement(c, a, b, bias, out):
1111
out_arr = out.tile((BM, BN))
@@ -14,7 +14,7 @@ def arrangement(c, a, b, bias, out):
1414
a_arr.dtype = a_arr.dtype.squeeze(0)
1515
b_arr = b.tile((BK, BN)).tile((-1, 1)).expand((out_arr.shape[0], -1))
1616
b_arr.dtype = b_arr.dtype.squeeze(1)
17-
bias_arr = bias.tile((BN,)).unsqueeze(0).expand((out_arr.shape[0], -1))
17+
bias_arr = bias[None, :].expand((out.shape[0], -1)).tile((BM, BN))
1818
return c_arr, a_arr, b_arr, bias_arr, out_arr
1919

2020
def application(c, a, b, bias, out):

‎src/ntops_lab/kernels/fused/general/fused_addmm_bias_clamp.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import ninetoothed.language as ntl
44
from ninetoothed import Tensor, block_size
55

6-
BM = block_size()
7-
BN = block_size()
8-
BK = block_size()
6+
BM = block_size(upper_bound=64)
7+
BN = block_size(upper_bound=64)
8+
BK = block_size(upper_bound=64)
99

1010
def arrangement(c, a, b, bias, out):
1111
out_arr = out.tile((BM, BN))
@@ -14,7 +14,7 @@ def arrangement(c, a, b, bias, out):
1414
a_arr.dtype = a_arr.dtype.squeeze(0)
1515
b_arr = b.tile((BK, BN)).tile((-1, 1)).expand((out_arr.shape[0], -1))
1616
b_arr.dtype = b_arr.dtype.squeeze(1)
17-
bias_arr = bias.tile((BN,)).unsqueeze(0).expand((out_arr.shape[0], -1))
17+
bias_arr = bias[None, :].expand((out.shape[0], -1)).tile((BM, BN))
1818
return c_arr, a_arr, b_arr, bias_arr, out_arr
1919

2020
def application(c, a, b, bias, out):

‎src/ntops_lab/kernels/fused/general/fused_addmm_bias_cube.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import ninetoothed.language as ntl
44
from ninetoothed import Tensor, block_size
55

6-
BM = block_size()
7-
BN = block_size()
8-
BK = block_size()
6+
BM = block_size(upper_bound=64)
7+
BN = block_size(upper_bound=64)
8+
BK = block_size(upper_bound=64)
99

1010
def arrangement(c, a, b, bias, out):
1111
out_arr = out.tile((BM, BN))
@@ -14,7 +14,7 @@ def arrangement(c, a, b, bias, out):
1414
a_arr.dtype = a_arr.dtype.squeeze(0)
1515
b_arr = b.tile((BK, BN)).tile((-1, 1)).expand((out_arr.shape[0], -1))
1616
b_arr.dtype = b_arr.dtype.squeeze(1)
17-
bias_arr = bias.tile((BN,)).unsqueeze(0).expand((out_arr.shape[0], -1))
17+
bias_arr = bias[None, :].expand((out.shape[0], -1)).tile((BM, BN))
1818
return c_arr, a_arr, b_arr, bias_arr, out_arr
1919

2020
def application(c, a, b, bias, out):

‎src/ntops_lab/kernels/fused/general/fused_addmm_bias_elu.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import ninetoothed.language as ntl
44
from ninetoothed import Tensor, block_size
55

6-
BM = block_size()
7-
BN = block_size()
8-
BK = block_size()
6+
BM = block_size(upper_bound=64)
7+
BN = block_size(upper_bound=64)
8+
BK = block_size(upper_bound=64)
99

1010
def arrangement(c, a, b, bias, out):
1111
out_arr = out.tile((BM, BN))
@@ -14,7 +14,7 @@ def arrangement(c, a, b, bias, out):
1414
a_arr.dtype = a_arr.dtype.squeeze(0)
1515
b_arr = b.tile((BK, BN)).tile((-1, 1)).expand((out_arr.shape[0], -1))
1616
b_arr.dtype = b_arr.dtype.squeeze(1)
17-
bias_arr = bias.tile((BN,)).unsqueeze(0).expand((out_arr.shape[0], -1))
17+
bias_arr = bias[None, :].expand((out.shape[0], -1)).tile((BM, BN))
1818
return c_arr, a_arr, b_arr, bias_arr, out_arr
1919

2020
def application(c, a, b, bias, out):

0 commit comments

Comments
 (0)