@@ -99,16 +99,16 @@ def _nearest_kernel(
9999 offs_n = pid * BLOCK_N + tl .arange (0 , BLOCK_N ) # X indices.
100100 mask_x = offs_n < N # Valid x rows.
101101 if EVEN_N :
102- tl .multiple_of (offs_n , 8 )
103- tl .max_contiguous (offs_n , 8 )
102+ tl .multiple_of (offs_n , 8 ) # Hint contiguous access.
103+ tl .max_contiguous (offs_n , 8 ) # Hint vectorization.
104104
105105 best_dist = tl .full ((BLOCK_N ,), float ('inf' ), tl .float32 ) # Best dist.
106106 best_idx = tl .zeros ((BLOCK_N ,), dtype = tl .int32 ) # Best y index.
107107
108108 if USE_BATCH :
109- batch_id = tl .load (batch_x_ptr + offs_n , mask = mask_x , other = 0 )
110- left = tl .load (ptr_y_ptr + batch_id , mask = mask_x , other = 0 )
111- right = tl .load (ptr_y_ptr + batch_id + 1 , mask = mask_x , other = 0 )
109+ batch_id = tl .load (batch_x_ptr + offs_n , mask = mask_x , other = 0 ) # Batch id per x.
110+ left = tl .load (ptr_y_ptr + batch_id , mask = mask_x , other = 0 ) # y range start.
111+ right = tl .load (ptr_y_ptr + batch_id + 1 , mask = mask_x , other = 0 ) # y range end.
112112 else :
113113 left = 0 # Full y range.
114114 right = M
@@ -194,7 +194,7 @@ def _nearest_kernel(
194194 inv_y = tl .rsqrt (y_sq + EPS ) # 1/||y||.
195195 dist = 1.0 - acc * (inv_y [:, None ] * inv_x [None , :])
196196 else :
197- dist = tl .fma (- 2.0 , acc , y_sq [:, None ] + x_sq [None , :])
197+ dist = tl .fma (- 2.0 , acc , y_sq [:, None ] + x_sq [None , :]) # L2^2 distance.
198198
199199 if full_y :
200200 valid = tl .broadcast_to (mask_x [None , :], (BLOCK_M , BLOCK_N ))
@@ -218,7 +218,7 @@ def _nearest_kernel(
218218 best_dist = tl .where (better , block_min , best_dist )
219219 best_idx = tl .where (better , block_idx , best_idx )
220220
221- tl .store (out_ptr + offs_n , best_idx .to (tl .int64 ), mask = mask_x )
221+ tl .store (out_ptr + offs_n , best_idx .to (tl .int64 ), mask = mask_x ) # Write output.
222222
223223
224224@triton .autotune (
0 commit comments