bridge.models.qwen.modeling_qwen4_exp.qsa_selection#

Document-local QSA scores without replicating block keys for every query.

Module Contents#

Functions#

select_document_blocks

Return document-relative block bitsets with the dense selection shortcut.

API#

bridge.models.qwen.modeling_qwen4_exp.qsa_selection.select_document_blocks(
q: torch.Tensor,
pooled_keys: torch.Tensor,
block_valid: torch.Tensor,
doc_ids: torch.Tensor,
positions: torch.Tensor,
*,
compress_ratio: int,
block_topk: int,
workspace_bytes: int = 256 << 20,
unpacked_sequence_length: int | None = None,
) → tuple[torch.Tensor, bool]#

Return document-relative block bitsets with the dense selection shortcut.

Each document shares its block keys across query rows. This avoids building [queries, padded_blocks, head_dim] replicated key tensors. Query chunks bound the FP32 head-score tensor. Top-k retains the padded block width and torch’s tie behavior used by the original Megatron implementation.

unpacked_sequence_length is trusted layout metadata from the indexer: each batch row is one document, flattened in batch-major order with positions starting at zero. It avoids reading document boundaries back from the GPU. Leave it unset for packed or arbitrary document layouts.