@@ -119,6 +119,7 @@ def mm_kernel_general(
119119 BLOCK_N : tl .constexpr ,
120120 BLOCK_K : tl .constexpr ,
121121 GROUP_M : tl .constexpr ,
122+ IS_FP64 : tl .constexpr = False ,
122123):
123124 # matrix multiplication
124125 pid = tle .program_id (0 )
@@ -167,11 +168,17 @@ def mm_kernel_general(
167168 block_shape = [BLOCK_M , BLOCK_N ],
168169 )
169170
170- acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float32 )
171+ if IS_FP64 :
172+ acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float64 )
173+ else :
174+ acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float32 )
171175 for k in range (0 , tl .cdiv (K , BLOCK_K )):
172176 a = a_desc .load ([offset_am .to (tl .int32 ), offset_k .to (tl .int32 )])
173177 b = b_desc .load ([offset_k .to (tl .int32 ), offset_bn .to (tl .int32 )])
174- acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
178+ if IS_FP64 :
179+ acc += tl .dot (a , b , allow_tf32 = False )
180+ else :
181+ acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
175182 offset_k += BLOCK_K
176183
177184 acc = acc .to (a_desc .dtype )
@@ -187,15 +194,21 @@ def mm_kernel_general(
187194 rn = rn .to (tl .int64 )
188195 prev_multiple = prev_multiple_of (K , BLOCK_K )
189196
190- acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float32 )
197+ if IS_FP64 :
198+ acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float64 )
199+ else :
200+ acc = tl .zeros ((BLOCK_M , BLOCK_N ), dtype = tl .float32 )
191201 for start_k in range (0 , prev_multiple , BLOCK_K ):
192202 rk = (start_k + tl .arange (0 , BLOCK_K )).to (tl .int64 )
193203 a = tl .load (A + (ram [:, None ] * stride_am + rk [None , :] * stride_ak ))
194204 b = tl .load (B + (rk [:, None ] * stride_bk + rbn [None , :] * stride_bn ))
195205 if a .dtype != b .dtype :
196206 a = a .to (C .dtype .element_ty )
197207 b = b .to (C .dtype .element_ty )
198- acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
208+ if IS_FP64 :
209+ acc += tl .dot (a , b , allow_tf32 = False )
210+ else :
211+ acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
199212
200213 # loop peeling
201214 rk = (prev_multiple + tl .arange (0 , BLOCK_K )).to (tl .int64 )
@@ -213,7 +226,10 @@ def mm_kernel_general(
213226 if a .dtype != b .dtype :
214227 a = a .to (C .dtype .element_ty )
215228 b = b .to (C .dtype .element_ty )
216- acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
229+ if IS_FP64 :
230+ acc += tl .dot (a , b , allow_tf32 = False )
231+ else :
232+ acc += tl .dot (a , b , out_dtype = tl .float32 , allow_tf32 = False )
217233
218234 acc = acc .to (C .dtype .element_ty )
219235 # rematerialize rm and rn to save registers
@@ -343,7 +359,7 @@ def mm_kernel_general_host_tma(
343359
344360
345361def get_higher_dtype (a , b ):
346- _ordered_datatypes = [torch .float16 , torch .bfloat16 , torch .float32 ]
362+ _ordered_datatypes = [torch .float16 , torch .bfloat16 , torch .float32 , torch . float64 ]
347363
348364 if a is b :
349365 return a
@@ -435,6 +451,7 @@ def alloc_fn(size: int, align: int, stream: Optional[int]):
435451 c .stride (0 ),
436452 c .stride (1 ),
437453 GROUP_M = 8 ,
454+ IS_FP64 = a .dtype == torch .float64 ,
438455 )
439456 return c
440457
@@ -471,6 +488,7 @@ def gemv_kernel(
471488 stride_bk ,
472489 BLOCK_M : tl .constexpr ,
473490 BLOCK_K : tl .constexpr ,
491+ IS_FP64 : tl .constexpr = False ,
474492):
475493 """Optimized kernel for matrix-vector multiplication (N=1 case)"""
476494 pid = tl .program_id (0 )
@@ -481,7 +499,10 @@ def gemv_kernel(
481499 row_mask = row_offset < M
482500
483501 # Accumulator for this block of rows
484- acc = tl .zeros ((BLOCK_M ,), dtype = tl .float32 )
502+ if IS_FP64 :
503+ acc = tl .zeros ((BLOCK_M ,), dtype = tl .float64 )
504+ else :
505+ acc = tl .zeros ((BLOCK_M ,), dtype = tl .float32 )
485506
486507 # Iterate over K dimension
487508 for k_start in range (0 , K , BLOCK_K ):
@@ -525,6 +546,7 @@ def gemv_mm(a, b, c, M, K):
525546 a .stride (0 ),
526547 a .stride (1 ),
527548 b .stride (0 ),
549+ IS_FP64 = a .dtype == torch .float64 ,
528550 )
529551 return c
530552
0 commit comments