1+ import functools
2+
13import ninetoothed
24from ninetoothed import Tensor
35
46import ops .ninetoothed .kernels .mm as mm
7+ from ops .ninetoothed .kernels ._common import build
58
69
7- def arrangement (input , mat1 , mat2 , beta , alpha , output ):
8- _ , _ , input_arranged = mm .arrangement (mat1 , mat2 , input )
9-
10- mat1_arranged , mat2_arranged , output_arranged = mm .arrangement (mat1 , mat2 , output )
10+ def arrangement (input , mat1 , mat2 , beta , alpha , output , ** block_sizes ):
11+ _ , _ , input_arranged = mm .arrangement (mat1 , mat2 , input , ** block_sizes )
12+ mat1_arranged , mat2_arranged , output_arranged = mm .arrangement (
13+ mat1 , mat2 , output , ** block_sizes
14+ )
1115
1216 return input_arranged , mat1_arranged , mat2_arranged , beta , alpha , output_arranged
1317
@@ -17,6 +21,76 @@ def application(input, mat1, mat2, beta, alpha, output):
1721 output = beta * input + alpha * output
1822
1923
20- tensors = (Tensor (2 ), Tensor (2 ), Tensor (2 ), Tensor (0 ), Tensor (0 ), Tensor (2 ))
24+ def premake (m , n , k , dtype , block_size_m , block_size_n , block_size_k ):
25+ arrangement_ = functools .partial (
26+ arrangement ,
27+ block_size_m = block_size_m ,
28+ block_size_n = block_size_n ,
29+ block_size_k = block_size_k ,
30+ )
31+ tensors = (
32+ Tensor (shape = (m , n ), dtype = dtype ),
33+ Tensor (shape = (m , k ), dtype = dtype ),
34+ Tensor (shape = (k , n ), dtype = dtype ),
35+ Tensor (0 , dtype = ninetoothed .float32 ),
36+ Tensor (0 , dtype = ninetoothed .float32 ),
37+ Tensor (shape = (m , n ), dtype = dtype ),
38+ )
39+
40+ return arrangement_ , application , tensors
41+
42+
43+ # Compile the square shapes used by the benchmark; other shapes use the generic
44+ # make-based fallback below.
45+ configs = tuple (
46+ (
47+ (),
48+ {
49+ "m" : s ,
50+ "n" : s ,
51+ "k" : s ,
52+ "dtype" : ninetoothed .float16 ,
53+ "block_size_m" : bm ,
54+ "block_size_n" : bn ,
55+ "block_size_k" : bk ,
56+ },
57+ {"num_warps" : nw , "num_stages" : 3 },
58+ )
59+ for s in (128 * i for i in range (2 , 33 ))
60+ for bm in (64 , 128 )
61+ for bn in (64 , 128 )
62+ for bk in (32 , 64 )
63+ for nw in (4 , 8 )
64+ )
65+
66+ _build_kernel = build (
67+ premake ,
68+ configs ,
69+ meta_parameters = ("block_size_m" , "block_size_n" , "block_size_k" ),
70+ kernel_name = "addmm" ,
71+ )
72+
73+ _BUILD_CONFIGS = frozenset (
74+ (kwargs ["m" ], kwargs ["n" ], kwargs ["k" ], kwargs ["dtype" ])
75+ for _ , kwargs , _ in configs
76+ )
77+
78+ _fallback_kernel = ninetoothed .make (
79+ arrangement ,
80+ application ,
81+ (
82+ Tensor (2 ),
83+ Tensor (2 ),
84+ Tensor (2 ),
85+ Tensor (0 ),
86+ Tensor (0 ),
87+ Tensor (2 ),
88+ ),
89+ )
90+
91+
92+ def kernel (input , mat1 , mat2 , beta , alpha , output , m , n , k , dtype ):
93+ if (m , n , k , dtype ) in _BUILD_CONFIGS :
94+ return _build_kernel (input , mat1 , mat2 , beta , alpha , output , m , n , k , dtype )
2195
22- kernel = ninetoothed . make ( arrangement , application , tensors )
96+ return _fallback_kernel ( input , mat1 , mat2 , beta , alpha , output )
0 commit comments