"""Packed metadata passed to the fused attention kernel.""" from __future__ import annotations from dataclasses import dataclass from typing import Any import torch @dataclass(frozen=True) class PackedLayout: """PAT row and memory geometry, shared by every layer.""" row_offsets: torch.Tensor row_pos_to_flat: torch.Tensor row_lengths: torch.Tensor memory_query_start: torch.Tensor row_memory_start_abs: torch.Tensor row_memory_length: torch.Tensor local_positions: torch.Tensor eoc_abs_idx: torch.Tensor memory_positions: torch.Tensor row_chunk_indices: torch.Tensor row_pos_to_flat_is_identity: bool max_row_length: int max_chunk_index: int query_positions: torch.Tensor | None row_token_memory_length: torch.Tensor | None row_exact_start_abs: torch.Tensor | None row_exact_length: torch.Tensor | None row_exact_query_phase_delta: torch.Tensor | None row_compressed_eoc_local_key_phase_delta: torch.Tensor | None planner_cache: Any = None planner_cache_key: Any = None token_context_schedule: str = "uniform" fixed_window_contiguous_position_width: int = 0 chronological_fixed_window: bool = False global_memory_slots_per_chunk: int = 0 active_local_row_start_abs: torch.Tensor | None = None active_local_row_starts: tuple[int, ...] | None = None active_token_count: int | None = None def __post_init__(self) -> None: for field_name in ("max_row_length", "max_chunk_index"): value = getattr(self, field_name) if isinstance(value, bool) or not isinstance(value, int): raise TypeError(f"PackedLayout.{field_name} must be a Python int") if not isinstance(self.chronological_fixed_window, bool): raise TypeError("PackedLayout.chronological_fixed_window must be a bool") if ( isinstance(self.global_memory_slots_per_chunk, bool) or not isinstance(self.global_memory_slots_per_chunk, int) or self.global_memory_slots_per_chunk < 0 ): raise TypeError( "PackedLayout.global_memory_slots_per_chunk must be a non-negative Python int" ) if self.row_count > 0: if self.max_row_length <= 0: raise ValueError("nonempty PackedLayout requires max_row_length > 0") if self.max_chunk_index < 0: raise ValueError("nonempty PackedLayout requires max_chunk_index >= 0") t2_metadata = ( ("row_token_memory_length", self.row_token_memory_length), ("row_exact_start_abs", self.row_exact_start_abs), ("row_exact_length", self.row_exact_length), ("row_exact_query_phase_delta", self.row_exact_query_phase_delta), ( "row_compressed_eoc_local_key_phase_delta", self.row_compressed_eoc_local_key_phase_delta, ), ) present = tuple((value is not None for _, value in t2_metadata)) if any(present) and (not all(present)): missing = ", ".join( ( name for (name, _), is_present in zip(t2_metadata, present, strict=True) if not is_present ) ) raise ValueError( f"PackedLayout T2 metadata must be all present or all None; missing: {missing}" ) for field_name, value in t2_metadata: if value is not None and (not torch.is_tensor(value)): raise TypeError(f"PackedLayout.{field_name} must be a tensor or None") if self.query_positions is not None and (not torch.is_tensor(self.query_positions)): raise TypeError("PackedLayout.query_positions must be a tensor or None") active_values = ( self.active_local_row_start_abs, self.active_local_row_starts, self.active_token_count, ) if any(value is not None for value in active_values) and ( not all(value is not None for value in active_values) ): raise ValueError("active layout requires both row bases and logical token count") if self.active_local_row_start_abs is not None: if ( not torch.is_tensor(self.active_local_row_start_abs) or self.active_local_row_start_abs.dim() != 1 or int(self.active_local_row_start_abs.numel()) != self.row_count ): raise ValueError("active local row bases must contain one value per row") if ( not isinstance(self.active_local_row_starts, tuple) or len(self.active_local_row_starts) != self.row_count or any( isinstance(value, bool) or not isinstance(value, int) or int(value) < 0 for value in self.active_local_row_starts ) ): raise ValueError( "active Python row bases must contain one non-negative integer per row" ) if ( isinstance(self.active_token_count, bool) or not isinstance(self.active_token_count, int) or int(self.active_token_count) <= 0 ): raise ValueError("active logical token count must be positive") @property def row_count(self) -> int: return int(self.row_lengths.numel()) @property def token_count(self) -> int: if self.active_token_count is not None: return int(self.active_token_count) return int(self.row_pos_to_flat.numel()) @property def memory_tail_slots(self) -> int: return int(self.eoc_abs_idx.size(1)) @property def local_qk_rope_positions(self) -> torch.Tensor: return self.local_positions @property def local_query_rope_positions(self) -> torch.Tensor: return self.local_positions if self.query_positions is None else self.query_positions @property def has_previous_exact_context(self) -> bool: return self.row_exact_length is not None @property def global_k_rope_positions(self) -> torch.Tensor: return self.memory_positions