Diff Coverage

Diff: origin/master...HEAD, staged and unstaged changes

Source File Diff Coverage (%) Missing Lines
hyper_parallel/components/functional/lightning_indexer.py 71.9% 45,47,50,52,54,98,109,146-152,155-156,162-166,176,202,204,208
hyper_parallel/components/functional/sparse_flash_mla.py 87.3% 81,140,154-160,254,325-327,332,364,366,371,386-389
hyper_parallel/components/functional/sparse_lightning_indexer_kl_loss_grad.py 63.1% 47,70,72,75,86-87,89-90,94,148,170,237,282-285,288,291,295-299,312-313,343-351,356,363-365,369,381-384,387-391,396-401,406,434-435,439-450,454-455,459
hyper_parallel/components/modules/shared_compressed_dsa_attention.py 68.9% 241,280-283,311-319,329,333,426-430,465,652,728,940,942,944,1054,1058,1120,1324-1326,1335,1345,1348-1350,1441-1442,1554,1590-1595
hyper_parallel/components/functional/lightning_indexer.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    Raises:
        ValueError: Tensor layouts, devices or query/key dtypes disagree.
    """
    if query.ndim != 4:
        raise ValueError(f"Indexer query must have shape [B, Q, H, D], got {tuple(query.shape)}")
    if key.ndim != 3 or key.shape[::2] != (query.shape[0], query.shape[-1]):
        raise ValueError(f"Indexer key must have shape [B, K, D] matching Q, got Q={tuple(query.shape)}, "
                         f"K={tuple(key.shape)}")
    if weights.shape != query.shape[:3]:
        raise ValueError(f"Indexer weights must have shape {tuple(query.shape[:3])}, got {tuple(weights.shape)}")
    if key.device != query.device or weights.device != query.device:
        raise ValueError(f"Indexer Q/K/weights must share a device, got {query.device}, {key.device}, {weights.device}")
    if key.dtype != query.dtype:
        raise ValueError(f"Indexer Q/K must share a dtype, got {query.dtype} and {key.dtype}")


def sort_key_indices(indices: torch.Tensor, maximum: int) -> torch.Tensor:
    """Sort key IDs without losing integer precision or changing their dtype.
 94
 95
 96
 97
 98
 99
100
101
102
            "use_fused_ops=True requires cann_ops_transformer.lightning_indexer_metadata on this device. "
            "See hyper_parallel/components/functional/README.md for ops-transformer installation, "
            "environment setup and tested versions."
        ) from err
    return lightning_indexer_metadata


@lru_cache(maxsize=16)
def _uses_indexer_metadata(device: torch.device) -> bool:
105
106
107
108
109
110
111
112
113
    LI V2's arch22 kernel computes its split internally; arch35 requires
    metadata. Keep the standard metadata call on other devices rather than
    assuming they share the verified 910B behavior. TND still needs cu_seqlens.
    """
    return "910B" not in torch.npu.get_device_name(device)


@lru_cache(maxsize=128)
@torch.inference_mode(False)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        topk: Output selection width, including invalid tail slots.
        segment: Local Q start/end, global compressed K start/end, and raw
            prefix length modulo ratio, shared by all physical batch items.
    """
    batch, length, heads, dim = query.shape
    result = torch.full((batch, length, topk), -1, device=query.device, dtype=torch.int32)
    start, end, key_start, key_end, residual = segment
    if key_end <= key_start or start == end:
        return result
    indexer = _load_indexer_op()
    residue, metadata = _bsnd_indexer_metadata(
        query.device, batch, end-start, heads, dim, key_end-key_start, topk, ratio, residual,
    )
    with torch.autocast(device_type=query.device.type, enabled=False):
        indices, _ = indexer(
            query[:, start:end].contiguous(), key[:, key_start:key_end].unsqueeze(2).contiguous(),
            weights[:, start:end].float().contiguous(), topk,
            cmp_residual_k=residue, metadata=metadata,
            layout_q="BSND", layout_k="BSND", mask_mode=3, cmp_ratio=ratio,
158
159
160
161
162
163
164
165
166
167
168
169
170
            weights[:, start:end].float().contiguous(), topk,
            cmp_residual_k=residue, metadata=metadata,
            layout_q="BSND", layout_k="BSND", mask_mode=3, cmp_ratio=ratio,
        )
    indices = indices[:, :, 0]
    indices = torch.where(indices >= 0, indices + key_start, key.shape[1])
    indices = sort_key_indices(indices, key.shape[1])
    result[:, start:end] = indices.masked_fill(indices == key.shape[1], -1)
    return result


@lru_cache(maxsize=128)
@torch.inference_mode(False)
172
173
174
175
176
177
178
179
180
    """Cache native selection metadata for the complete packed document partition."""
    options = plan.indexer_options(device, ratio)
    metadata = None
    if _uses_indexer_metadata(device):
        metadata = _load_indexer_metadata_op()(
            heads, 1, dim, topk, batch_size=len(plan.segments),
            max_seqlen_q=max(plan.q_lengths), max_seqlen_k=max(plan.k_lengths), **options,
        )
    return options, metadata
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    Returns:
        Sorted global key IDs [batch, queries, topk], with invalid slots -1.
    """
    if len(segments) == 1:
        return _bsnd_lightning_indexer(query, key, weights, ratio, topk, segments[0])
    if query.shape[0] != 1:
        raise ValueError("Packed Lightning Indexer requires batch size one")
    result = torch.full((*query.shape[:2], topk), -1, device=query.device, dtype=torch.int32)
    plan = csa_tnd_plan(segments, ratio)
    if plan is None:
        return result
    options, metadata = _tnd_indexer_metadata(plan, query.device, query.shape[2], query.shape[3], topk, ratio)
    with torch.autocast(device_type=query.device.type, enabled=False):
        indices, _ = _load_indexer_op()(
            plan.query(query), plan.key(key).unsqueeze(1), plan.query(weights).float(), topk,
hyper_parallel/components/functional/sparse_flash_mla.py
77
78
79
80
81
82
83
84
85
        selected = self.query(indices)
        offsets = self.lengths(indices.device)[3]
        selected = torch.where(selected >= 0, selected - offsets, -1).int()
        if selected.shape[-1] < padded_width:
            selected = torch.nn.functional.pad(selected, (0, padded_width-selected.shape[-1]), value=-1)
        return selected.unsqueeze(1).contiguous()

    def covers_queries(self, length: int) -> bool:
        """Return whether packed rows already follow the complete original Q order."""
136
137
138
139
140
141
142
143
144
            "and sparse_flash_mla_metadata. "
            "See hyper_parallel/components/functional/README.md for ops-transformer installation, "
            "environment setup and tested versions."
        ) from err
    return sparse_flash_mla_op, sparse_flash_mla_grad, sparse_flash_mla_metadata


def supports_fused_attention_inputs(query: torch.Tensor, ratio: int, window: int, topk: int = 512) -> bool:
    """Check the validated attention input contract without importing operators.
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        topk: Logical compressed selection width, before padding.
    """
    if query.device.type != "npu" or query.dtype != torch.bfloat16:
        return False
    if query.shape[-1] != 512 or window != 128 or ratio not in (0, 1, 2):
        return False
    if query.shape[1] not in (1, 2, 4, 8, 16, 32, 64, 128) or query.shape[2] == 0:
        return False
    if ratio and not 1 <= topk <= 512:
        return False
    return True


@lru_cache(maxsize=128)
@torch.inference_mode(False)
250
251
252
253
254
255
256
257
258
    Returns:
        Attention output in [B, Q, H, D].
    """
    if query.dtype != torch.bfloat16 or query.shape[-1] != 512 or window != 128:
        raise ValueError("V4 attention requires BF16, head_dim=512, and window=128")
    start, end, key_start, key_end, residual = segment
    document_start = key_start * max(ratio, 1)
    raw_left = max(document_start, query_offset + start - window + 1)
    raw_right = query_offset + end
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

    @staticmethod
    def backward(ctx: Any, grad_output: torch.Tensor) -> tuple:
        """Return each physical bank's gradient to its autograd communication owner."""
        query, raw, main, indices, sinks, output, lse, cu_q, cu_raw, cu_k, residuals = ctx.saved_tensors
        _, backward, _ = _load_attention_ops()
        dq, dr, dm, ds, _, _ = backward(
            query, grad_output.contiguous(), output, lse, ori_kv=raw, cmp_kv=main,
            cmp_sparse_indices=indices, sinks=sinks, cu_seqlens_q=cu_q, cu_seqlens_ori_kv=cu_raw,
            cu_seqlens_cmp_kv=cu_k, cmp_residual_kv=residuals, **ctx.options,
        )
        return dq, dr, dm if main is not None else None, None, ds, None, None, None, None, None


def sparse_flash_mla(
        query: torch.Tensor, raw_key: torch.Tensor, main_key: torch.Tensor | None,
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
        return _bsnd_sparse_flash_mla(
            query, raw_key, main_key, indices, sinks, scale, ratio, window, query_offset, segments[0],
        )
    if query.shape[0] != 1:
        raise ValueError("Packed Sparse Flash MLA requires batch size one")
    if query.dtype != torch.bfloat16 or query.shape[-1] != 512 or window != 128:
        raise ValueError("V4 attention requires BF16, head_dim=512, and window=128")
    output = None
    for has_keys in (True, False):
        plan = csa_tnd_plan(segments, ratio, has_keys)
        if plan is None:
            continue
        raw_ranges = tuple(
            (max(ks * max(ratio, 1), query_offset+s-window+1), query_offset+e)
            for s, e, ks, _, _ in plan.segments
        )
382
383
384
385
386
387
388
389
        )
        # A complete Q partition is already ordered; a canvas would retain another full output.
        if plan.covers_queries(query.shape[2]):
            return part.unsqueeze(0)
        if output is None:
            output = torch.zeros_like(query.transpose(1, 2))
        plan.restore(part, output)
    return torch.zeros_like(query.transpose(1, 2)) if output is None else output
hyper_parallel/components/functional/sparse_lightning_indexer_kl_loss_grad.py
43
44
45
46
47
48
49
50
51
            "and sparse_lightning_indexer_kl_loss_grad_metadata. "
            "See hyper_parallel/components/functional/README.md for ops-transformer installation, "
            "environment setup and tested versions."
        ) from err
    return sparse_lightning_indexer_kl_loss_grad, sparse_lightning_indexer_kl_loss_grad_metadata


def validate_fused_kl_inputs(
        query: torch.Tensor, key: torch.Tensor, weights: torch.Tensor, indices: torch.Tensor,
66
67
68
69
70
71
72
73
74
75
76
77
78
        RuntimeError: Explicit fusion was requested on a non-NPU device.
    """
    validate_indexer_tensors(query, key, weights)
    if indices.ndim != 3 or indices.shape[:2] != query.shape[:2]:
        raise ValueError("Fused KL indices must have shape [B, Q, TopK] matching Indexer Q")
    if key.shape[1] == 0 or indices.shape[-1] != min(512, key.shape[1]):
        raise ValueError(f"Fused KL requires ordinary TopK512 selections from a nonempty bank; "
                         f"got K={key.shape[1]}, TopK={indices.shape[-1]}")
    if indices.device != query.device:
        raise ValueError(f"Fused KL indices must share Indexer Q's device, got {indices.device} and {query.device}")
    if query.device.type != "npu":
        raise RuntimeError(f"Fused KL requires NPU inputs; got device={query.device}")

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        device: torch.device, batch: int, length: int, heads: int, dim: int,
        key_length: int, topk: int, ratio: int, residual: int,
) -> tuple:
    """Cache shape-only causal metadata without retaining activations."""
    residue = torch.full((batch,), residual, device=device, dtype=torch.int32) if ratio > 1 else None
    options = {"layout_q": "BSND", "layout_k": "BSND", "mask_mode": 3,
               "cmp_ratio": ratio, "cmp_residual_k": residue}
    _, metadata_op = _load_kl_ops()
    metadata = metadata_op(
        heads, 1, dim, batch_size=batch, max_seqlen_q=length, max_seqlen_k=key_length,
        topk=topk, **options,
    )
    return options, metadata


def gather_selected_keys(bank: torch.Tensor, indices: torch.Tensor) -> torch.Tensor:
    """Gather batch-local nonnegative indices without multidimensional indexing.
144
145
146
147
148
149
150
151
152
    with torch.no_grad(), torch.autocast(device_type=query.device.type, enabled=False):
        batch, heads, length, dim = query.shape
        width = indices.shape[-1]
        if width == 0:
            return query.new_zeros(indices.shape, dtype=torch.float32)
        valid = indices >= 0
        safe = indices.clamp_min(0).long()
        selected_chunk = min(width, max(1, _FP32_TEMPORARY_BYTES // max(1, batch * length * dim * 4)))
        head_chunk = min(heads, max(1, _FP32_TEMPORARY_BYTES // max(1, batch * length * width * 4)))
166
167
168
169
170
171
172
173
174
            probabilities = torch.cat((scores, sink_logits), dim=-1).softmax(dim=-1)
            target.add_(probabilities[..., :-1].masked_fill(~valid.unsqueeze(1), 0).sum(dim=1))
            del scores, probabilities, queries
        if reduce_sum is not None:
            target = reduce_sum(target)
        return target / target.sum(dim=-1, keepdim=True).clamp_min(torch.finfo(torch.float32).tiny)


def causal_attention_teacher(
233
234
235
236
237
238
239
240
241
    log_prediction = prediction.masked_fill(~valid, 1).log()
    # The native interface does not return student LSE. One boundary check
    # avoids silently clamping the scalar objective when probabilities vanish.
    if not bool(bad.any()):
        return log_prediction
    for batch in range(query.shape[0]):
        rows = bad[batch].nonzero().flatten()
        for start in range(0, rows.numel(), chunk_size):
            chosen = rows[start:start+chunk_size]
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

        Returns:
            Scalar KL normalized by this document segment's batch and query count.
        """
        batch, length, heads, dim = query.shape
        kl_grad, _ = _load_kl_ops()
        with torch.no_grad(), torch.autocast(device_type=query.device.type, enabled=False):
            target = causal_attention_teacher(
                attention_query, main_key, indices, sinks, scale, ratio, residual, chunk_size,
            )
            options, metadata = _kl_metadata(
                query.device, batch, length, heads, dim, key.shape[1], indices.shape[-1], ratio, residual,
            )
            dq, dk, dw, prediction = kl_grad(
                query.contiguous(), key.unsqueeze(2).contiguous(), weights.float().contiguous(),
                indices.unsqueeze(2).int().contiguous(), target.unsqueeze(2), metadata=metadata, **options,
            )
            log_prediction = _stable_prediction_log(prediction[:, :, 0], query, key, weights, indices, chunk_size)
            loss = (target * (target.clamp_min(torch.finfo(torch.float32).tiny).log() - log_prediction)).sum()
            ctx.save_for_backward(dq, dk.squeeze(2), dw)
            ctx.factor = coefficient / (batch * length)
        return loss * ctx.factor

    @staticmethod
    def backward(ctx: Any, seed: torch.Tensor) -> tuple:
        """Apply the caller's seed in FP32, without differentiating the teacher.
308
309
310
311
312
313
314
315
316
317

        Returns:
            Student Q/K/weight gradients and None for teacher and sequence metadata.
        """
        gradients = tuple(value.float() * (seed * ctx.factor) for value in ctx.saved_tensors)
        return (*gradients, *((None,) * 9))


def _bsnd_sparse_lightning_indexer_kl_loss(
        query: torch.Tensor, key: torch.Tensor, weights: torch.Tensor,
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
        segment: Local Q start/end, document K start/prefix end and residual,
            shared by all physical batch items.
    """
    # Zero-length views attach zero gradients even when the segment has no key.
    total = query[..., :0].float().sum() + key[..., :0].float().sum() + weights[..., :0].float().sum()
    start, end, key_start, key_end, residual = segment
    if key_end <= key_start or start == end:
        return total
    selected = indices[:, start:end]
    selected = torch.where(selected >= 0, selected - key_start, -1)
    if selected.shape[-1] < 512:
        selected = torch.nn.functional.pad(selected, (0, 512-selected.shape[-1]), value=-1)
    loss = _SparseLightningIndexerKLLoss.apply(
        query[:, start:end], key[:, key_start:key_end], weights[:, start:end],
        attention_query[:, :, start:end], main_key[:, key_start:key_end], selected, sinks,
        scale, coefficient, chunk_size, ratio, residual,
    )
    return total + loss * ((end-start) / query.shape[1])


@lru_cache(maxsize=128)
@torch.inference_mode(False)
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
@lru_cache(maxsize=128)
@torch.inference_mode(False)
def _tnd_kl_metadata(plan, device, heads, dim, ratio):
    """Cache KL metadata using the same Q/K partition as native selection."""
    options = plan.indexer_options(device, ratio)
    _, metadata_op = _load_kl_ops()
    metadata = metadata_op(
        heads, 1, dim, topk=512, batch_size=len(plan.segments),
        max_seqlen_q=max(plan.q_lengths), max_seqlen_k=max(plan.k_lengths), **options,
    )
    return options, metadata


class _SparseLightningIndexerKLLossTND(torch.autograd.Function):
    """Fuse the student gradients for several documents, retaining Hyper's teacher."""
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
            ctx: Any, query: torch.Tensor, key: torch.Tensor, weights: torch.Tensor,
            indices: torch.Tensor, target: torch.Tensor, ratio: int, plan: CSATNDPlan, chunk_size: int,
    ) -> torch.Tensor:
        """Calculate native student gradients and the original scalar KL objective."""
        options, metadata = _tnd_kl_metadata(plan, query.device, query.shape[1], query.shape[2], ratio)
        kernel, _ = _load_kl_ops()
        with torch.autocast(device_type=query.device.type, enabled=False):
            dq, dk, dw, prediction = kernel(
                query, key.unsqueeze(1), weights.float(), indices, target.unsqueeze(1), metadata=metadata, **options,
            )
            logs = []
            q_start = k_start = 0
            for q_length, k_length in zip(plan.q_lengths, plan.k_lengths):
                q_slice = slice(q_start, q_start+q_length)
                logs.append(_stable_prediction_log(
                    prediction[q_slice, 0].unsqueeze(0), query[q_slice].unsqueeze(0),
                    key[k_start:k_start+k_length].unsqueeze(0), weights[q_slice].unsqueeze(0),
                    indices[q_slice, 0].unsqueeze(0), chunk_size,
                )[0])
                q_start += q_length
                k_start += k_length
            log_prediction = torch.cat(logs)
            loss = (target * (target.clamp_min(torch.finfo(torch.float32).tiny).log() - log_prediction)).sum()
        ctx.save_for_backward(dq, dk.squeeze(1), dw)
        return loss

    @staticmethod
    def backward(ctx: Any, seed: torch.Tensor) -> tuple:
        """Apply the upstream scalar seed to all three precomputed student gradients."""
        return (*(value.float() * seed for value in ctx.saved_tensors), None, None, None, None, None)


def sparse_lightning_indexer_kl_loss(
        query: torch.Tensor, key: torch.Tensor, weights: torch.Tensor, attention_query: torch.Tensor,
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    Returns:
        Scalar KL normalized by all local queries, including empty-prefix rows.
        The external teacher retains its per-document bounded computation.
    """
    if len(segments) == 1:
        return _bsnd_sparse_lightning_indexer_kl_loss(
            query, key, weights, attention_query, main_key, indices, sinks,
            scale, coefficient, chunk_size, ratio, segments[0],
        )
    if query.shape[0] != 1:
        raise ValueError("Packed Lightning Indexer KL requires batch size one")
    total = query[..., :0].float().sum() + key[..., :0].float().sum() + weights[..., :0].float().sum()
    plan = csa_tnd_plan(segments, ratio)
    if plan is None:
        return total
    selected = plan.local_indices(indices, padded_width=512)
    targets = []
    cursor = 0
    with torch.no_grad(), torch.autocast(device_type=query.device.type, enabled=False):
        for start, end, key_start, key_end, residual in plan.segments:
            targets.append(causal_attention_teacher(
                attention_query[:, :, start:end], main_key[:, key_start:key_end],
                selected[cursor:cursor+end-start, 0].unsqueeze(0), sinks, scale, ratio, residual, chunk_size,
            )[0])
            cursor += end-start
    loss = _SparseLightningIndexerKLLossTND.apply(
        plan.query(query), plan.key(key), plan.query(weights), selected,
        torch.cat(targets).contiguous(), ratio, plan, chunk_size,
    )
    return total + loss * (coefficient / query.shape[1])
hyper_parallel/components/modules/shared_compressed_dsa_attention.py
237
238
239
240
241
242
243
244
245
        device = torch.device(device)
        # Torch exposes tensor mutation tracking only through its version counter.
        version = None if self.cu_seq_lens.is_inference() else self.cu_seq_lens._version  # pylint: disable=protected-access
        if version is not None and self._snapshot.get("version") == version:
            prepared: SharedCompressedPackedSequence = self if self._snapshot.get("owned") else self._snapshot["value"]
        else:
            prepared = SharedCompressedPackedSequence(
                self.cu_seq_lens.detach().clone(), self.local_query_start,
                self.local_query_length, self.global_sequence_length,
276
277
278
279
280
281
282
283
284
285
286
287
        return self._prepared_positions[device]

    def segment_start_mask(self, device: torch.device) -> torch.Tensor:
        """Reuse the per-batch sample-start mask across model forwards."""
        device = torch.device(device)
        if device not in self._prepared_masks:
            self._prepared_masks[device] = self.local_positions(device) == self.local_segment_starts(device)
        return self._prepared_masks[device]

    def indexer_segments(self, ratio: int) -> tuple[tuple[int, int, int, int, int], ...]:
        """Describe packed Q intervals and completed K prefixes without per-layer synchronization."""
        if ratio not in self._indexer_segments:
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
            device: Device on which tensors and prepared geometry are consumed.
            compress_ratio: Compression ratio of the consuming layer.
            segment_starts: Optional precomputed global sample starts for local queries.
        """
        if compress_ratio <= 0:
            raise ValueError("minimum_key_indices requires a positive compression ratio")
        cached = self._prepared_minima.get((torch.device(device), compress_ratio))
        if cached is not None:
            return cached
        self.validate_compression_alignment(compress_ratio)
        if segment_starts is None:
            segment_starts = self.local_segment_starts(device)
        return segment_starts // compress_ratio

    def local_segment_starts(self, device: torch.device) -> torch.Tensor:
        """Return each local query's global packed-sample start position.
325
326
327
328
329
330
331
332
333
334
335
336
337
            device: Device on which tensors and prepared geometry are consumed.
        """
        cached = self._prepared_starts.get(torch.device(device))
        if cached is not None:
            return cached
        if self.local_query_start < 0 or self.local_query_length < 0 or (
                self.local_query_start + self.local_query_length > self.global_sequence_length
        ):
            raise ValueError("packed local query interval must lie within the global sequence")
        boundaries = self.cu_seq_lens.to(dtype=torch.long)
        if boundaries.ndim != 1 or boundaries.numel() < 2 or int(boundaries[0]) != 0:
            raise ValueError("packed cu_seq_lens must be one-dimensional and start with zero")
        if int(boundaries[-1]) != self.global_sequence_length:
422
423
424
425
426
427
428
429
430
431
432
433
434
        raise ValueError("Fused LightningIndexer requires query_segments when minimum_key_indices is set")
    validate_indexer_tensors(query, key, merge_weight)
    if query.device.type != "npu":
        raise RuntimeError(f"Fused LightningIndexer requires NPU inputs; got device={query.device}")
    if query_segments is None:
        length = query.shape[1]
        raw_end = query_offset + length
        query_segments = ((0, length, 0, raw_end // compress_ratio, raw_end % compress_ratio),)
    return lightning_indexer(query, key, merge_weight, compress_ratio, top_k, query_segments)


@torch.no_grad()
def compressed_causal_topk(
461
462
463
464
465
466
467
468
469
        raise ValueError(f"compress_ratio must be positive, got {compress_ratio}")
    if query_chunk_size <= 0:
        raise ValueError(f"query_chunk_size must be positive, got {query_chunk_size}")
    if query.ndim != 4:
        raise ValueError(f"Indexer query must have shape [B, Q, H, D], got {tuple(query.shape)}")
    batch_size, sequence_length, _, head_dim = query.shape
    if key.ndim != 3 or key.shape[::2] != (batch_size, head_dim):
        raise ValueError(
            "compressed index key must have shape [batch, compressed_sequence, head_dim]"
648
649
650
651
652
653
654
655
656
        raise ValueError(f"compress_ratio must be positive, got {compress_ratio}")
    if query_chunk_size <= 0:
        raise ValueError(f"query_chunk_size must be positive, got {query_chunk_size}")
    batch_size, sequence_length, _, head_dim = query.shape
    if key.ndim != 3 or key.shape[::2] != (batch_size, head_dim):
        raise ValueError(
            "compressed index key must have shape [batch, compressed_sequence, head_dim]"
        )
    if merge_weight.shape != query.shape[:3]:
724
725
726
727
728
729
730
731
732
        raise ValueError(f"block_size must be positive, got {block_size}")
    if query_chunk_size <= 0:
        raise ValueError(f"query_chunk_size must be positive, got {query_chunk_size}")
    batch_size, sequence_length, _, head_dim = query.shape
    if key.ndim != 3 or key.shape[::2] != (batch_size, head_dim):
        raise ValueError(
            "compressed index key must have shape [batch, compressed_sequence, head_dim]"
        )
    if merge_weight.shape != query.shape[:3]:
936
937
938
939
940
941
942
943
944
945
946
947
948
    if topk_indices.shape[:2] != index_query.shape[:2]:
        raise ValueError("topk_indices and index_query must share batch/query dimensions")
    if use_fused:
        if tp_context is not None:
            raise NotImplementedError("Fused KL does not support TP score/teacher reduction")
        if query_segments is None:
            raise ValueError("Fused KL requires document-local causal query_segments")
        validate_fused_kl_inputs(index_query, index_key, merge_weight, topk_indices)
        return sparse_lightning_indexer_kl_loss(
            index_query, index_key, merge_weight, attention_query.detach(), compressed_key.detach(),
            topk_indices, sinks.detach(), attention_scale, loss_coeff, query_chunk_size,
            compress_ratio, query_segments,
        )
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
        merge_weight = merge_weight * (self.head_dim**-0.5 * self.global_num_heads**-0.5)
        reduce_sum = None if tp_context is None else tp_context.reduce_sum
        if key_handle is not None:
            key = key_handle.wait()
        topk_indices, candidate_blocks = self._select_indices(
            query, key, merge_weight, candidate_blocks, query_offset,
            reduce_sum, minimum_key_indices, query_segments,
        )
        return SharedCompressedIndexerOutput(
            topk_indices=topk_indices,
            index_key=key,
            candidate_blocks=candidate_blocks,
            index_query=query,
1116
1117
1118
1119
1120
1121
1122
1123
1124
                minimum_key_indices=minimum_key_indices,
                query_segments=query_segments,
                use_fused=self.use_fused,
            )
        return topk_indices, candidate_blocks


def reference_sparse_attention(
        query: torch.Tensor,
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
    ) -> tuple[torch.Tensor | None, torch.Tensor | None, tuple]:
        """Validate packed document boundaries and derive local Q/K segments."""
        segment_starts = None
        minimum_key_indices = None
        ratio = max(1, self.compress_ratio)
        raw_end = query_offset + sequence_length
        query_segments = ((0, sequence_length, 0, raw_end // ratio, raw_end % ratio),)
        if packed_sequence is not None:
            if batch_size != 1:
                raise ValueError("V4.1 compact packed attention currently requires micro_batch_size=1")
            if (
1331
1332
1333
1334
1335
1336
1337
1338
1339
                    packed_sequence.local_query_start != query_offset
                    or packed_sequence.local_query_length != sequence_length
                    or packed_sequence.global_sequence_length != global_sequence_length
            ):
                packed_shard = (
                    packed_sequence.local_query_start,
                    packed_sequence.local_query_length,
                    packed_sequence.global_sequence_length,
                )
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
                    "packed sequence geometry does not match the local CP shard: "
                    f"packed={packed_shard}, "
                    f"attention={(query_offset, sequence_length, global_sequence_length)}"
                )
            packed_sequence = packed_sequence.prepare(device, (self.compress_ratio,))
            segment_starts = packed_sequence.local_segment_starts(device)
            if self.compress_ratio:
                minimum_key_indices = packed_sequence.minimum_key_indices(device, self.compress_ratio)
            query_segments = packed_sequence.indexer_segments(ratio)
        return segment_starts, minimum_key_indices, query_segments

    def _compute_compressed_kv(
            self,
            hidden_states: torch.Tensor,
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
            window.masked_fill_(window < segment_starts.unsqueeze(-1), -1)
        if compressed_kv is None:
            return key_value, window
        combined_key_value = torch.cat((key_value, compressed_kv.unsqueeze(1)), dim=2)
        compressed_indices = torch.where(topk_indices >= 0, topk_indices.long() + global_sequence_length, -1)
        return combined_key_value, torch.cat((window, compressed_indices), dim=-1)

    def _apply_indexer_loss(
            self,
            indexer_output: Any | None,
1550
1551
1552
1553
1554
1555
1556
1557
1558
                sequence_length,
                self.compress_ratio,
                self.is_kv_source,
            )
        segment_starts, minimum_key_indices, query_segments = self._resolve_packed_segments(
            packed_sequence,
            batch_size,
            sequence_length,
            query_offset,
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
            shared_state.publish_compressed_kv(self.layer_idx, compressed_handle.wait())
        if key_value is None:
            raise RuntimeError("raw KV all-gather did not produce a tensor")

        compressed_kv = None
        topk_indices = None
        if self.compress_ratio:
            compressed_kv = shared_state.require_compressed_kv(self.kv_source_layer_idx, self.layer_idx)
            topk_indices = shared_state.require_topk_indices(self.index_source_layer_idx, self.layer_idx)
        query = self._apply_indexer_loss(indexer_output, query, compressed_kv, tp_context, query_segments)
        attention_output = self._run_sparse_attention(
            query, key_value, compressed_kv, topk_indices, query_offset, global_sequence_length,
            segment_starts, query_segments,
        )