@@ -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
@@ -440,6 +456,7 @@ def alloc_fn(size: int, align: int, stream: Optional[int]):
440456 c .stride (0 ),
441457 c .stride (1 ),
442458 GROUP_M = 8 ,
459+ IS_FP64 = a .dtype == torch .float64 ,
443460 )
444461 return c
445462
@@ -476,6 +493,7 @@ def gemv_kernel(
476493 stride_bk ,
477494 BLOCK_M : tl .constexpr ,
478495 BLOCK_K : tl .constexpr ,
496+ IS_FP64 : tl .constexpr = False ,
479497):
480498 """Optimized kernel for matrix-vector multiplication (N=1 case)"""
481499 pid = tl .program_id (0 )
@@ -486,7 +504,10 @@ def gemv_kernel(
486504 row_mask = row_offset < M
487505
488506 # Accumulator for this block of rows
489- acc = tl .zeros ((BLOCK_M ,), dtype = tl .float32 )
507+ if IS_FP64 :
508+ acc = tl .zeros ((BLOCK_M ,), dtype = tl .float64 )
509+ else :
510+ acc = tl .zeros ((BLOCK_M ,), dtype = tl .float32 )
490511
491512 # Iterate over K dimension
492513 for k_start in range (0 , K , BLOCK_K ):
@@ -530,6 +551,7 @@ def gemv_mm(a, b, c, M, K):
530551 a .stride (0 ),
531552 a .stride (1 ),
532553 b .stride (0 ),
554+ IS_FP64 = a .dtype == torch .float64 ,
533555 )
534556 return c
535557
0 commit comments