MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / _unpack_dequant_lastdim_kernel

Function _unpack_dequant_lastdim_kernel

lightx2v/common/kvcache/kernel.py:289–352  ·  view source on GitHub ↗
(
    code_ptr,
    scale_ptr,
    mn_ptr,
    out_ptr,
    total_rows,
    T: tl.constexpr,
    H: tl.constexpr,
    D: tl.constexpr,
    # code strides: [B, H, D, P]
    c_s0: tl.constexpr,
    c_s1: tl.constexpr,
    c_s2: tl.constexpr,
    c_s3: tl.constexpr,
    # scale/mn strides: [B, H, D, G]
    s_s0: tl.constexpr,
    s_s1: tl.constexpr,
    s_s2: tl.constexpr,
    s_s3: tl.constexpr,
    # output strides: [B, H, D, T]
    o_s0: tl.constexpr,
    o_s1: tl.constexpr,
    o_s2: tl.constexpr,
    o_s3: tl.constexpr,
    BITS: tl.constexpr,
    FEAT_PER_INT: tl.constexpr,
    GROUP_SIZE: tl.constexpr,
    BLOCK_M: tl.constexpr,
    BLOCK_T: tl.constexpr,
)

Source from the content-addressed store, hash-verified

287 physical = offsets // (heads * dim)
288 head = offsets // dim % heads
289 column = offsets % dim
290 start, sink, recent = tl.load(metadata), tl.load(metadata + 1), tl.load(metadata + 2)
291 logical = tl.where(physical < sink, physical, sink + (physical - sink - recent + capacity - sink) % (capacity - sink))
292 if split_half:
293 partner = tl.where(column < rotary_dim // 2, column + rotary_dim // 2, column - rotary_dim // 2)
294 frequency = column % (rotary_dim // 2)
295 first = column < rotary_dim // 2
296 else:
297 partner = column ^ 1
298 frequency = column // 2
299 first = column % 2 == 0
300 rotate = valid & (logical < visible) & (column < rotary_dim)
301 cosine = tl.load(freqs + logical * rotary_dim + frequency, rotate, 0).to(tl.float32)
302 sine = tl.load(freqs + logical * rotary_dim + rotary_dim // 2 + frequency, rotate, 0).to(tl.float32)
303 key = tl.load(raw_key + offsets, valid, 0).to(tl.float32)
304 other = tl.load(raw_key + (physical * heads + head) * dim + partner, rotate, 0).to(tl.float32)
305 if rope_fma:
306 rotated = tl.where(first, tl.fma(key, cosine, -other * sine), tl.fma(key, cosine, other * sine))
307 else:
308 rotated = tl.where(first, key * cosine - other * sine, key * cosine + other * sine)
309 tl.store(rotated_key + offsets, tl.where(column < rotary_dim, rotated, key), valid)
310 query_token = logical - start
311 current = valid & (logical < visible) & (query_token >= 0) & (query_token < query_tokens)
312 address = query_token * query_token_stride + head * query_head_stride
313 q = tl.load(query + address + column * query_dim_stride, current, 0).to(tl.float32)
314 q_other = tl.load(query + address + partner * query_dim_stride, current & (column < rotary_dim), 0).to(tl.float32)
315 if rope_fma:
316 q_rotated = tl.where(first, tl.fma(q, cosine, -q_other * sine), tl.fma(q, cosine, q_other * sine))
317 else:
318 q_rotated = tl.where(first, q * cosine - q_other * sine, q * cosine + q_other * sine)
319 tl.store(query + address + column * query_dim_stride, tl.where(column < rotary_dim, q_rotated, q), current)
320
321
322def rotate_ring_qk(
323 query: torch.Tensor,
324 raw_key: torch.Tensor,
325 rotated_key: torch.Tensor,
326 freqs: torch.Tensor,
327 metadata: torch.Tensor,
328 *,
329 rotary_dim=None,
330 rope_layout="interleaved",
331 rope_fma=True,
332) -> None:
333 """Rotate Q and physical-order K using each slot's current logical position."""
334 if torch.compiler.is_compiling():
335 _rotate_ring_qk_op(query, raw_key, rotated_key, freqs, metadata, rotary_dim or raw_key.shape[-1], rope_layout == "split_half", rope_fma)
336 return
337 rotary_dim = rotary_dim or raw_key.shape[-1]
338 if rotary_dim != raw_key.shape[-1] or rope_layout == "split_half" or not rope_fma:
339 # Split-half partners can live in different warps. Produce Q out of
340 # place before copying back, so one warp cannot overwrite another
341 # warp's input while it is still reading its rotation partner.
342 if query.numel():
343 query.copy_(rotate_token_rope(query, freqs, metadata, rotary_dim=rotary_dim, split_half=rope_layout == "split_half", rope_fma=rope_fma))
344 _rotate_ring_qk_partial[(triton.cdiv(rotated_key.numel(), 512),)](
345 query,
346 raw_key,

Callers

nothing calls this directly

Calls 2

toMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected