@@ -40,53 +40,51 @@ def concat_and_cache_mla_kernel(
4040 slot_idx = tl .load (slot_mapping_ptr + token_idx )
4141
4242 # Skip padded tokens
43- if slot_idx < 0 :
44- return
45-
46- # Calculate cache position
47- block_id = slot_idx // block_size
48- block_offset = slot_idx % block_size
49- cache_base = block_id * block_stride + block_offset * entry_stride
50-
51- # Preload scale if needed
52- if kv_dtype != FP8_KV_CACHE_DATA_TYPE_AUTO :
53- scale_val = tl .load (scale_ptr )
54-
55- # Process kv_c section
56- for i in range (0 , kv_lora_rank , BLOCK_SIZE ):
57- idx = i + tl .arange (0 , BLOCK_SIZE )
58- mask = idx < kv_lora_rank
59-
60- src_ptr = kv_c_ptr + token_idx * kv_c_stride + idx
61- dst_ptr = kv_cache_ptr + cache_base + idx
62-
63- val = tl .load (src_ptr , mask = mask , other = 0 )
64-
65- if kv_dtype != FP8_KV_CACHE_DATA_TYPE_AUTO :
66- if kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E4M3 :
67- val = (val / scale_val ).to (tl .float8e4nv )
68- elif kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E5M2 :
69- val = (val / scale_val ).to (tl .float8e5 )
70- val = val .to (tl .uint8 , bitcast = True )
71- tl .store (dst_ptr , val , mask = mask )
72-
73- # Process k_pe section
74- for j in range (0 , pe_dim , BLOCK_SIZE ):
75- idx = j + tl .arange (0 , BLOCK_SIZE )
76- mask = idx < pe_dim
77-
78- src_ptr = k_pe_ptr + token_idx * k_pe_stride + idx
79- dst_ptr = kv_cache_ptr + cache_base + kv_lora_rank + idx
80-
81- val = tl .load (src_ptr , mask = mask , other = 0 )
43+ if slot_idx >= 0 :
44+ # Calculate cache position
45+ block_id = slot_idx // block_size
46+ block_offset = slot_idx % block_size
47+ cache_base = block_id * block_stride + block_offset * entry_stride
8248
49+ # Preload scale if needed
8350 if kv_dtype != FP8_KV_CACHE_DATA_TYPE_AUTO :
84- if kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E4M3 :
85- val = (val / scale_val ).to (tl .float8e4nv )
86- elif kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E5M2 :
87- val = (val / scale_val ).to (tl .float8e5 )
88- val = val .to (tl .uint8 , bitcast = True )
89- tl .store (dst_ptr , val , mask = mask )
51+ scale_val = tl .load (scale_ptr )
52+
53+ # Process kv_c section
54+ for i in range (0 , kv_lora_rank , BLOCK_SIZE ):
55+ idx = i + tl .arange (0 , BLOCK_SIZE )
56+ mask = idx < kv_lora_rank
57+
58+ src_ptr = kv_c_ptr + token_idx * kv_c_stride + idx
59+ dst_ptr = kv_cache_ptr + cache_base + idx
60+
61+ val = tl .load (src_ptr , mask = mask , other = 0 )
62+
63+ if kv_dtype != FP8_KV_CACHE_DATA_TYPE_AUTO :
64+ if kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E4M3 :
65+ val = (val / scale_val ).to (tl .float8e4nv )
66+ elif kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E5M2 :
67+ val = (val / scale_val ).to (tl .float8e5 )
68+ val = val .to (tl .uint8 , bitcast = True )
69+ tl .store (dst_ptr , val , mask = mask )
70+
71+ # Process k_pe section
72+ for j in range (0 , pe_dim , BLOCK_SIZE ):
73+ idx = j + tl .arange (0 , BLOCK_SIZE )
74+ mask = idx < pe_dim
75+
76+ src_ptr = k_pe_ptr + token_idx * k_pe_stride + idx
77+ dst_ptr = kv_cache_ptr + cache_base + kv_lora_rank + idx
78+
79+ val = tl .load (src_ptr , mask = mask , other = 0 )
80+
81+ if kv_dtype != FP8_KV_CACHE_DATA_TYPE_AUTO :
82+ if kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E4M3 :
83+ val = (val / scale_val ).to (tl .float8e4nv )
84+ elif kv_dtype == FP8_KV_CACHE_DATA_TYPE_FP8E5M2 :
85+ val = (val / scale_val ).to (tl .float8e5 )
86+ val = val .to (tl .uint8 , bitcast = True )
87+ tl .store (dst_ptr , val , mask = mask )
9088
9189
9290class ConcatAndCacheMla (torch .autograd .Function ):
0 commit comments