Module neuralops.attentions.block_transformer_hiba
Block Transformer with Hierarchical Intra-Block and Inter-Block Attention (HIBA).
Production-hardened implementation supporting: 1. Robust Pretrained Weight Extraction across DistilBERT, RoBERTa/BERT nested encoders, GPT-2 (fused Conv1D), and Qwen/LLaMA/Mistral. 2. Nested Encoder Detection to prevent double output projection signal attenuation in RoBERTa/BERT. 3. Native Grouped-Query Attention (GQA) support with kv_repeat broadcasting. 4. Rotary Position Embedding (RoPE) support with partial rotary dimension coordinate slicing. 5. Strict Causal Masking in all attention paths (intra-block, inter-block, and rectification) for decoders. 6. Parameter-Efficient Recovery Fine-Tuning with trainable residual LoRA adapters (lora_A, lora_B). 7. Cross-Attention support for encoder-decoder architectures. 8. Numerical stability via FP32 attention logits, nan_to_num guardrails, and -inf masking. 9. Static, stable layer indexing across forward passes.
Functions
def apply_rotary_pos_emb(q: torch.Tensor,
k: torch.Tensor,
cos: torch.Tensor,
sin: torch.Tensor,
position_ids: torch.Tensor | None = None) ‑> tuple[torch.Tensor, torch.Tensor]-
Expand source code
def apply_rotary_pos_emb( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, position_ids: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: r""" Applies rotary position embeddings to query and key tensors with partial-RoPE dimension slicing. Supports coordinate layouts where head_dim > rotary_dim (e.g., Qwen3.5: head_dim=256, rotary_dim=64). Rotates the first `rotary_dim` coordinates and concatenates unrotated pass-through coordinates. """ if cos is None or sin is None: return q, k # Ensure 4D broadcasting: (B, 1, L, rot_dim) or (1, 1, L, rot_dim) if cos.dim() == 2: cos = cos.unsqueeze(0).unsqueeze(0) sin = sin.unsqueeze(0).unsqueeze(0) elif cos.dim() == 3: cos = cos.unsqueeze(1) sin = sin.unsqueeze(1) rotary_dim = cos.shape[-1] head_dim = q.shape[-1] if rotary_dim < head_dim: q_rot, q_pass = q[..., :rotary_dim], q[..., rotary_dim:] k_rot, k_pass = k[..., :rotary_dim], k[..., rotary_dim:] q_rot_embed = (q_rot * cos) + (_rotate_half(q_rot) * sin) k_rot_embed = (k_rot * cos) + (_rotate_half(k_rot) * sin) q = torch.cat([q_rot_embed, q_pass], dim=-1) k = torch.cat([k_rot_embed, k_pass], dim=-1) else: q = (q * cos) + (_rotate_half(q) * sin) k = (k * cos) + (_rotate_half(k) * sin) return q, kApplies rotary position embeddings to query and key tensors with partial-RoPE dimension slicing.
Supports coordinate layouts where head_dim > rotary_dim (e.g., Qwen3.5: head_dim=256, rotary_dim=64). Rotates the first
rotary_dimcoordinates and concatenates unrotated pass-through coordinates. def inject_block_transformer_hiba(layer: Any = None,
model: Any = None,
model_config: Any = None,
comp_config: Any = None,
layer_idx: int = 0,
total_layers: int = 12,
old_attn: torch.nn.modules.module.Module | None = None,
**kwargs: Any)-
Expand source code
@ATTENTION_REGISTRY.register("block_transformer_hiba_fn") @ATTENTION_REGISTRY.register("block_transformer_hiba_inject") def inject_block_transformer_hiba( layer: Any = None, model: Any = None, model_config: Any = None, comp_config: Any = None, layer_idx: int = 0, total_layers: int = 12, old_attn: nn.Module | None = None, **kwargs: Any, ): target = layer if layer is not None else model if target is None: target = kwargs.get("model", kwargs.get("layer")) if target is None: return None cfg = model_config if model_config is not None else getattr(target, "config", None) # Whole-model traversal layer_list = None if hasattr(target, "distilbert") and hasattr(target.distilbert, "transformer"): layer_list = target.distilbert.transformer.layer elif hasattr(target, "roberta") and hasattr(target.roberta, "encoder"): layer_list = target.roberta.encoder.layer elif hasattr(target, "transformer") and hasattr(target.transformer, "h"): layer_list = target.transformer.h elif hasattr(target, "h") and isinstance(target.h, (torch.nn.ModuleList, list)): layer_list = target.h elif hasattr(target, "model") and hasattr(target.model, "layers"): layer_list = target.model.layers elif hasattr(target, "encoder") and hasattr(target.encoder, "layer"): layer_list = target.encoder.layer elif ( hasattr(target, "bert") and hasattr(target.bert, "encoder") and hasattr(target.bert.encoder, "layer") ): layer_list = target.bert.encoder.layer elif hasattr(target, "layers") and isinstance(target.layers, (torch.nn.ModuleList, list)): layer_list = target.layers if layer_list is not None: num_injected = 0 tot = len(layer_list) for idx, lyr in enumerate(layer_list): res = inject_block_transformer_hiba( layer=lyr, model_config=cfg, comp_config=( comp_config if "comp_config" in locals() or "comp_config" in kwargs else None ), layer_idx=idx, total_layers=tot, **kwargs, ) if res is not None: num_injected += 1 if num_injected > 0: if not hasattr(target, "compression_stats"): target.compression_stats = {} target.compression_stats["num_compressed"] = num_injected target.compression_stats["num_fallback"] = 0 return target if num_injected > 0 else None layer = target """Injection factory function for BlockTransformerHIBA attention blocks with safe pre-testing.""" if old_attn is None and layer is not None: old_attn = getattr(layer, "attention", getattr(layer, "self", getattr(layer, "attn", None))) hidden_size = ( getattr(old_attn, "hidden_size", None) or getattr(old_attn, "embed_dim", None) or ( old_attn.q_proj.in_features if hasattr(old_attn, "q_proj") and hasattr(old_attn.q_proj, "in_features") else None ) or (layer.in_features if hasattr(layer, "in_features") else None) or getattr(model_config, "hidden_size", 64) ) num_heads = ( getattr(old_attn, "num_heads", None) or getattr(old_attn, "num_attention_heads", None) or getattr(model_config, "num_attention_heads", 4) ) try: new_attn = BlockTransformerHIBA( config=model_config, old_attn=old_attn, comp_config=comp_config, layer_id=layer_idx, hidden_size=hidden_size, num_heads=num_heads, num_attention_heads=num_heads, **kwargs, ) if hasattr(layer, "attention"): if hasattr(layer.attention, "self"): layer.attention.self = new_attn else: layer.attention = new_attn elif hasattr(layer, "self"): layer.self = new_attn elif hasattr(layer, "attn"): layer.attn = new_attn return new_attn except Exception: return None
Classes
class BlockTransformerHIBA (config: Any | None = None,
rank: int = 64,
block_size: int = 64,
top_k_inter: int = 4,
rectification_interval: int = 4,
layer_id: int | None = None,
layer_idx: int | None = None,
old_attn: torch.nn.modules.module.Module | None = None,
pretrained_attn: torch.nn.modules.module.Module | None = None,
comp_config: Any | None = None,
**kwargs: Any)-
Expand source code
@ATTENTION_REGISTRY.register("block_transformer_hiba") class BlockTransformerHIBA(BaseAttention): r""" Block Transformer with Hierarchical Intra-Block and Inter-Block Attention (HIBA). Hardened for production use across DistilBERT, RoBERTa/BERT, GPT-2, and Qwen/LLaMA/Mistral. """ is_custom_attention: bool = True def __init__( self, config: Any | None = None, rank: int = 64, block_size: int = 64, top_k_inter: int = 4, rectification_interval: int = 4, layer_id: int | None = None, layer_idx: int | None = None, old_attn: nn.Module | None = None, pretrained_attn: nn.Module | None = None, comp_config: Any | None = None, **kwargs: Any, ) -> None: super().__init__(config, rank) source_attn = ( old_attn if old_attn is not None else ( pretrained_attn if pretrained_attn is not None else kwargs.get("old_attn", kwargs.get("pretrained_attn")) ) ) self.dim: int = kwargs.get("hidden_size") or getattr( config, "hidden_size", getattr(config, "d_model", getattr(config, "dim", 768)), ) self.hidden_size: int = self.dim self.num_heads: int = ( kwargs.get("num_heads") or kwargs.get("num_attention_heads") or getattr( config, "num_attention_heads", getattr(config, "num_heads", getattr(config, "n_heads", 12)), ) ) self.num_kv_heads: int = getattr( config, "num_key_value_heads", getattr(config, "num_kv_heads", self.num_heads), ) self.head_dim: int = getattr( config, "head_dim", getattr(config, "kv_channels", self.dim // max(1, self.num_heads)), ) self.all_head_size: int = self.num_heads * self.head_dim self.all_kv_head_size: int = self.num_kv_heads * self.head_dim self.kv_repeat: int = max(1, self.num_heads // max(1, self.num_kv_heads)) self.supports_gqa: bool = True self.block_size: int = block_size self.top_k_inter: int = top_k_inter self.rectification_interval: int = max(1, rectification_interval) # Static layer index (not mutated inside forward) resolved_layer_id = ( layer_id if layer_id is not None else ( layer_idx if layer_idx is not None else kwargs.get("layer_id", kwargs.get("layer_idx", 0)) ) ) self.layer_idx: int = int(resolved_layer_id) # LoRA adaptation configuration self.lora_rank: int = getattr( config, "lora_rank", getattr(comp_config, "lora_rank", kwargs.get("lora_rank", 16)), ) self.lora_alpha: float = getattr( config, "lora_alpha", getattr(comp_config, "lora_alpha", kwargs.get("lora_alpha", 32.0)), ) self.freeze_base: bool = self.lora_rank > 0 # Decoder & nested encoder detection model_type = getattr(config, "model_type", "").lower() is_dec = getattr( config, "is_decoder", getattr(config, "is_causal", kwargs.get("is_causal", False)), ) _mt = (model_type or "").lower() _decoder_prefixes = ( "gpt", "qwen", "llama", "mistral", "gemma", "phi", "opt", "bloom", "falcon", "olmo", "mpt", "stablelm", ) if ( any(_mt.startswith(p) for p in _decoder_prefixes) or hasattr(config, "architectures") and any( "GPT" in str(a) or "CausalLM" in str(a) for a in (getattr(config, "architectures", []) or []) ) ): is_dec = True if kwargs.get("is_decoder", False) or kwargs.get("is_causal", False): is_dec = True self.is_decoder: bool = bool(is_dec) self.is_causal: bool = bool(is_dec) self.is_nested_encoder: bool = False # Multi-Head Attention Projections equipped with trainable residual LoRA adapters self.W_q = LoRALinear( self.dim, self.all_head_size, bias=True, lora_rank=self.lora_rank, lora_alpha=self.lora_alpha, freeze_base=self.freeze_base, ) self.W_k = LoRALinear( self.dim, self.all_kv_head_size, bias=True, lora_rank=self.lora_rank, lora_alpha=self.lora_alpha, freeze_base=self.freeze_base, ) self.W_v = LoRALinear( self.dim, self.all_kv_head_size, bias=True, lora_rank=self.lora_rank, lora_alpha=self.lora_alpha, freeze_base=self.freeze_base, ) self.out_proj = LoRALinear( self.all_head_size, self.dim, bias=True, lora_rank=self.lora_rank, lora_alpha=self.lora_alpha, freeze_base=self.freeze_base, ) dropout_p = ( getattr(config, "dropout", getattr(config, "attention_probs_dropout_prob", 0.1)) if config else 0.1 ) self.dropout = nn.Dropout(dropout_p) self.rotary_emb = None self.comp_config: Any | None = comp_config if source_attn is not None: self.copy_weights(source_attn) # -------------------------------------------------------------------------- # Compatibility properties for parameter inspection & framework integration # -------------------------------------------------------------------------- @property def q_proj(self) -> LoRALinear: return self.W_q @property def k_proj(self) -> LoRALinear: return self.W_k @property def v_proj(self) -> LoRALinear: return self.W_v @property def o_proj(self) -> LoRALinear: return self.out_proj @property def q_lin(self) -> LoRALinear: return self.W_q @property def k_lin(self) -> LoRALinear: return self.W_k @property def v_lin(self) -> LoRALinear: return self.W_v @property def out_lin(self) -> LoRALinear: return self.out_proj @property def query(self) -> LoRALinear: return self.W_q @property def key(self) -> LoRALinear: return self.W_k @property def value(self) -> LoRALinear: return self.W_v @property def dense(self) -> LoRALinear: return self.out_proj @property def c_proj(self) -> LoRALinear: return self.out_proj def copy_weights(self, pretrained_attn: nn.Module) -> None: r""" Extracts and copies pretrained weights across DistilBERT, RoBERTa/BERT, GPT-2, and Qwen/LLaMA. Strictly validates that copied weights are non-empty and verifies reconstruction norms. """ if pretrained_attn is None: return # 1. Detect nested encoder architectures (RoBERTa / BERT) cls_name = pretrained_attn.__class__.__name__ has_out_lin = hasattr(pretrained_attn, "out_lin") is_nested = False if ( ( hasattr(pretrained_attn, "self") and hasattr(pretrained_attn.self, "query") and hasattr(pretrained_attn, "output") and hasattr(pretrained_attn.output, "dense") ) or ( cls_name in [ "BertSelfAttention", "RobertaSelfAttention", "XLMRobertaSelfAttention", "ElectraSelfAttention", "DebertaSelfAttention", ] or (cls_name.endswith("SelfAttention") and "distil" not in cls_name.lower()) ) and not has_out_lin ): is_nested = True self.is_nested_encoder = bool(is_nested) # Inherit rotary embeddings if present if hasattr(pretrained_attn, "rotary_emb"): self.rotary_emb = pretrained_attn.rotary_emb extracted: dict[str, tuple[torch.Tensor | None, torch.Tensor | None]] = { "q": (None, None), "k": (None, None), "v": (None, None), "o": (None, None), } # 2. GPT-2: Fused c_attn + c_proj if hasattr(pretrained_attn, "c_attn") and pretrained_attn.c_attn is not None: c_attn_mod = pretrained_attn.c_attn if hasattr(c_attn_mod, "merge") and not getattr(c_attn_mod, "merged", False): try: c_attn_mod.merge() except Exception as e: print(f"copy_weights failed: {e}") logger.warning(f"copy_weights failed: {e}") base_c = getattr(c_attn_mod, "base_layer", c_attn_mod) c_w = getattr(base_c, "weight", getattr(c_attn_mod, "weight", None)) if c_w is not None: c_w = c_w.data.clone() if ( (c_attn_mod.__class__.__name__ == "Conv1D") or (base_c.__class__.__name__ == "Conv1D") or (c_w.ndim == 2 and c_w.shape[0] < c_w.shape[1]) ): c_w = c_w.t().contiguous() split_size = c_w.size(0) // 3 bias_mod = getattr(base_c, "bias", getattr(c_attn_mod, "bias", None)) c_b = bias_mod.data.clone() if bias_mod is not None else None extracted["q"] = ( c_w[:split_size, :], c_b[:split_size] if c_b is not None else None, ) extracted["k"] = ( c_w[split_size : 2 * split_size, :], c_b[split_size : 2 * split_size] if c_b is not None else None, ) extracted["v"] = ( c_w[2 * split_size :, :], c_b[2 * split_size :] if c_b is not None else None, ) # GPT-2 Output: c_proj if hasattr(pretrained_attn, "c_proj") and pretrained_attn.c_proj is not None: c_proj_mod = pretrained_attn.c_proj if hasattr(c_proj_mod, "merge") and not getattr(c_proj_mod, "merged", False): try: c_proj_mod.merge() except Exception as e: print(f"copy_weights failed: {e}") logger.warning(f"copy_weights failed: {e}") base_o = getattr(c_proj_mod, "base_layer", c_proj_mod) o_w = getattr(base_o, "weight", getattr(c_proj_mod, "weight", None)) if o_w is not None: o_w = o_w.data.clone() if ( (c_proj_mod.__class__.__name__ == "Conv1D") or (base_o.__class__.__name__ == "Conv1D") or (o_w.ndim == 2 and o_w.shape[0] < o_w.shape[1]) ): o_w = o_w.t().contiguous() bias_o = getattr(base_o, "bias", getattr(c_proj_mod, "bias", None)) extracted["o"] = ( o_w, bias_o.data.clone() if bias_o is not None else None, ) # 3. RoBERTa / BERT nested self-attention (pretrained_attn.self) elif hasattr(pretrained_attn, "self") and hasattr(pretrained_attn.self, "query"): s = pretrained_attn.self for proj_key, sub_name in [("q", "query"), ("k", "key"), ("v", "value")]: mod = getattr(s, sub_name, None) if mod is not None and hasattr(mod, "weight"): extracted[proj_key] = ( mod.weight.data.clone(), mod.bias.data.clone() if mod.bias is not None else None, ) # Output dense from outer container if hasattr(pretrained_attn, "output") and hasattr(pretrained_attn.output, "dense"): out_mod = pretrained_attn.output.dense extracted["o"] = ( out_mod.weight.data.clone(), out_mod.bias.data.clone() if out_mod.bias is not None else None, ) # 4. Standard linear projections (DistilBERT, Qwen, LLaMA) else: proj_candidates = { "q": ["q_lin", "q_proj", "query", "W_q"], "k": ["k_lin", "k_proj", "key", "W_k"], "v": ["v_lin", "v_proj", "value", "W_v"], "o": ["out_lin", "out_proj", "dense", "o_proj"], } for proj_key, names in proj_candidates.items(): for name in names: if hasattr(pretrained_attn, name): mod = getattr(pretrained_attn, name) if mod is not None and hasattr(mod, "weight"): extracted[proj_key] = ( mod.weight.data.clone(), mod.bias.data.clone() if mod.bias is not None else None, ) break # 5. Fallback via UniversalAdapter missing = [ k for k, (w, _) in extracted.items() if w is None and (k != "o" or not self.is_nested_encoder) ] if missing: try: from neuralops.core.adapters import UniversalAdapter adapter = UniversalAdapter() for k in missing: w, b = adapter.get_weight_bias(pretrained_attn, k) if w is not None: extracted[k] = (w.clone(), b.clone() if b is not None else None) except Exception: pass # Verify all mandatory projections were extracted missing_after = [ k for k, (w, _) in extracted.items() if w is None and (k != "o" or not self.is_nested_encoder) ] if missing_after: logger.warning( f"BlockTransformerHIBA copy_weights failed: missing mandatory projections {missing_after} " f"from module {pretrained_attn.__class__.__name__} ({pretrained_attn}). Proceeding with initialized weights." ) # 6. Copy weights into target LoRALinear layers with strict dimension safety target_map = { "q": self.W_q, "k": self.W_k, "v": self.W_v, "o": self.out_proj, } with torch.no_grad(): for proj_key, target_mod in target_map.items(): w, b = extracted[proj_key] if w is None: if proj_key == "o" and self.is_nested_encoder: # Inner nested self-attention bypasses out_proj; initialize to identity nn.init.eye_(target_mod.weight) if target_mod.bias is not None: nn.init.zeros_(target_mod.bias) continue if w.ndim != 2: w = w.reshape(w.size(0), -1) # Handle transposed orientation if necessary (only if shape does not match already) if ( w.shape != target_mod.weight.shape and target_mod.weight.shape == w.t().contiguous().shape ): w = w.t().contiguous() out_d = min(target_mod.weight.size(0), w.size(0)) in_d = min(target_mod.weight.size(1), w.size(1)) target_mod.weight.data[:out_d, :in_d].copy_( w[:out_d, :in_d].to( device=target_mod.weight.device, dtype=target_mod.weight.dtype ) ) if b is not None and target_mod.bias is not None: min_b = min(target_mod.bias.numel(), b.numel()) target_mod.bias.data[:min_b].copy_( b[:min_b].to(device=target_mod.bias.device, dtype=target_mod.bias.dtype) ) elif target_mod.bias is not None: nn.init.zeros_(target_mod.bias) # Weight verification: ensure weights are non-trivial w_norm = float(torch.norm(target_mod.weight.data).item()) if w_norm < 1e-4: logger.warning( f"Weight copy verification failed for projection '{proj_key}': norm too small ({w_norm:.2e})" ) if self.freeze_base: target_mod.freeze_base_weights() def _convert_mask_to_bool( self, attention_mask: torch.Tensor | None, B: int, L: int, device: torch.device, ) -> torch.Tensor: if attention_mask is None: return torch.ones(B, L, device=device, dtype=torch.bool) if attention_mask.dim() == 2: return ( attention_mask.bool() if attention_mask.dtype == torch.bool else attention_mask > 0 ) elif attention_mask.dim() == 3: if attention_mask.shape[1] == 1: m = attention_mask[:, 0, :] return ( m > -1000.0 if (m.dtype != torch.bool and m.min() < -100.0) else (m if m.dtype == torch.bool else m > 0) ) else: m = ( attention_mask > -1000.0 if (attention_mask.dtype != torch.bool and attention_mask.min() < -100.0) else ( attention_mask if attention_mask.dtype == torch.bool else attention_mask > 0 ) ) return m.any(dim=1) elif attention_mask.dim() == 4: m = ( attention_mask > -1000.0 if (attention_mask.dtype != torch.bool and attention_mask.min() < -100.0) else (attention_mask if attention_mask.dtype == torch.bool else attention_mask > 0) ) if m.shape[1] == 1: m = m.squeeze(1) else: m = m.any(dim=1) return m.any(dim=-2) return torch.ones(B, L, device=device, dtype=torch.bool) def _intra_block_attention( self, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, key_padding_mask: torch.Tensor, causal: bool = False, head_mask: torch.Tensor | None = None, ) -> torch.Tensor: B, H, L_b, D_h = q.shape scale = 1.0 / math.sqrt(D_h) attn = torch.matmul(q.float(), k.float().transpose(-2, -1).contiguous()) * scale if causal: causal_mask = torch.triu( torch.full((L_b, L_b), -10000.0, device=q.device, dtype=torch.float32), diagonal=1, ) attn = attn + causal_mask.unsqueeze(0).unsqueeze(1) pad_mask = key_padding_mask.unsqueeze(1).unsqueeze(2) attn = attn.masked_fill(pad_mask, -10000.0) probs = F.softmax(attn, dim=-1) probs = torch.nan_to_num(probs, nan=0.0, posinf=1.0, neginf=0.0) probs = self.dropout(probs) if head_mask is not None: probs = probs * head_mask return torch.matmul(probs.to(v.dtype), v) def _inter_block_sparse( self, q_b: torch.Tensor, k_all: torch.Tensor, v_all: torch.Tensor, block_idx: int, num_blocks: int, valid_mask: torch.Tensor, head_mask: torch.Tensor | None = None, ) -> torch.Tensor: B, H, L_b, D_h = q_b.shape total_len = k_all.size(2) start_b = block_idx * self.block_size end_b = min((block_idx + 1) * self.block_size, total_len) k_select: list[torch.Tensor] = [] v_select: list[torch.Tensor] = [] valid_select: list[torch.Tensor] = [] # Previous tokens: includes block 0 (CLS at index 0) if block_idx > 0: k_select.append(k_all[:, :, :start_b, :]) v_select.append(v_all[:, :, :start_b, :]) valid_select.append(valid_mask[:, :start_b]) # Subsequent tokens (only for bidirectional non-causal encoders) if (block_idx + 1) * self.block_size < total_len and not self.is_decoder: k_select.append(k_all[:, :, end_b:, :]) v_select.append(v_all[:, :, end_b:, :]) valid_select.append(valid_mask[:, end_b:]) if not k_select: return torch.zeros_like(q_b) k_neighbors = torch.cat(k_select, dim=2) v_neighbors = torch.cat(v_select, dim=2) valid_neighbors = torch.cat(valid_select, dim=1) scale = 1.0 / math.sqrt(D_h) scores = ( torch.matmul(q_b.float(), k_neighbors.float().transpose(-2, -1).contiguous()) * scale ) pad_mask = ~valid_neighbors.unsqueeze(1).unsqueeze(2) scores = scores.masked_fill(pad_mask, -10000.0) # Guarantee CLS block inclusion for block_idx > 0 scores_for_topk = scores.clone() if block_idx > 0: scores_for_topk[:, :, :, 0] = scores_for_topk[:, :, :, 0] + 1e4 top_k = min(self.top_k_inter, k_neighbors.size(2)) _, topk_idx = torch.topk(scores_for_topk, top_k, dim=-1) sparse_mask = torch.zeros_like(scores).scatter_(-1, topk_idx, 1.0) scores = scores.masked_fill(sparse_mask == 0, -10000.0) probs = F.softmax(scores, dim=-1) probs = torch.nan_to_num(probs, nan=0.0, posinf=1.0, neginf=0.0) probs = self.dropout(probs) if head_mask is not None: probs = probs * head_mask return torch.matmul(probs.to(v_neighbors.dtype), v_neighbors) def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor | None = None, head_mask: torch.Tensor | None = None, encoder_hidden_states: torch.Tensor | None = None, encoder_attention_mask: torch.Tensor | None = None, past_key_value: Any | None = None, output_attentions: bool | None = False, output_hidden_states: bool | None = False, return_dict: bool | None = True, *args: Any, **kwargs: Any, ) -> tuple[torch.Tensor, torch.Tensor | None]: B, L, D = hidden_states.shape H = self.num_heads is_cross_attention = encoder_hidden_states is not None kv_states = encoder_hidden_states if is_cross_attention else hidden_states S = kv_states.shape[1] # 1. Base Projections q = self.W_q(hidden_states).view(B, L, H, self.head_dim).transpose(1, 2).contiguous() k = ( self.W_k(kv_states) .view(B, S, self.num_kv_heads, self.head_dim) .transpose(1, 2) .contiguous() ) v = ( self.W_v(kv_states) .view(B, S, self.num_kv_heads, self.head_dim) .transpose(1, 2) .contiguous() ) # 2. RoPE Embeddings applied before GQA expansion pos_emb = kwargs.get("position_embeddings") cos = kwargs.get( "cos", (pos_emb[0] if isinstance(pos_emb, (tuple, list)) and len(pos_emb) == 2 else None), ) sin = kwargs.get( "sin", (pos_emb[1] if isinstance(pos_emb, (tuple, list)) and len(pos_emb) == 2 else None), ) if cos is not None and sin is not None: q, k = apply_rotary_pos_emb(q, k, cos, sin, kwargs.get("position_ids")) elif hasattr(self, "rotary_emb") and self.rotary_emb is not None: try: from neuralops.utils.compat import maybe_apply_rotary q, k = maybe_apply_rotary( q, k, getattr(self, "adapter", None), self.rotary_emb, kwargs.get("position_ids"), ) except Exception: pass # 3. GQA Expansion if self.kv_repeat > 1: k = repeat_kv(k, self.kv_repeat) v = repeat_kv(v, self.kv_repeat) # 4. Safe head mask reshaping if head_mask is not None: if head_mask.dim() == 1: head_mask = head_mask.view(1, -1, 1, 1) elif head_mask.dim() == 2: head_mask = head_mask.view(head_mask.shape[0], head_mask.shape[1], 1, 1) elif head_mask.dim() == 3: head_mask = head_mask.unsqueeze(-1) # Valid token mask active_mask = encoder_attention_mask if is_cross_attention else attention_mask valid_mask = self._convert_mask_to_bool(active_mask, B, S, hidden_states.device) num_blocks = (L + self.block_size - 1) // self.block_size # Static rectification decision (does not increment layer_idx across forward passes) use_rectify = ( (self.layer_idx % self.rectification_interval == 0) or (num_blocks <= 2) or is_cross_attention ) if use_rectify: # Dense attention pathway (full rectification) scale = 1.0 / math.sqrt(self.head_dim) # Memory-efficient: use SDPA to avoid OOM on large seqs try: _qs = q.half() if q.dtype == torch.float32 else q _ks = k.half() if k.dtype == torch.float32 else k _vs = v.half() if v.dtype == torch.float32 else v _is_causal = getattr(self, "is_decoder", False) and not is_cross_attention and L > 1 _sdpa_out = torch.nn.functional.scaled_dot_product_attention( _qs, _ks, _vs, is_causal=_is_causal, dropout_p=self.dropout.p if self.training else 0.0, ) _sdpa_out = ( _sdpa_out.to(hidden_states.dtype) .transpose(1, 2) .contiguous() .view(B, L, self.all_head_size) ) _sdpa_out = torch.nan_to_num(_sdpa_out, nan=0.0, posinf=1e4, neginf=-1e4) return ( self.out_proj(_sdpa_out) if not getattr(self, "is_nested_encoder", False) else _sdpa_out ), None except Exception: pass scores = torch.matmul(q.float(), k.float().transpose(-2, -1).contiguous()) * scale # If a 4D attention mask is provided, it already incorporates causal + padding constraints if active_mask is not None and active_mask.dim() == 4: if active_mask.dtype == torch.bool: scores = scores.masked_fill(~active_mask, -10000.0) else: scores = scores + active_mask.to(scores.dtype) else: # Causal mask for decoders if self.is_decoder and not is_cross_attention: causal_mask = torch.triu( torch.full((L, S), -10000.0, device=scores.device, dtype=torch.float32), diagonal=1 + (S - L), ) scores = scores + causal_mask.unsqueeze(0).unsqueeze(1) # Padding mask pad_mask = ~valid_mask.unsqueeze(1).unsqueeze(2) scores = scores.masked_fill(pad_mask, -10000.0) attn_probs = F.softmax(scores, dim=-1) attn_probs = torch.nan_to_num(attn_probs, nan=0.0, posinf=1.0, neginf=0.0) attn_probs = self.dropout(attn_probs) if head_mask is not None: attn_probs = attn_probs * head_mask out = torch.matmul(attn_probs.to(v.dtype), v) return_attn = attn_probs if output_attentions else None else: # Hierarchical Intra-Block and Inter-Block Attention out_list = [] for b_idx in range(num_blocks): start = b_idx * self.block_size end = min(start + self.block_size, L) q_b = q[:, :, start:end, :] k_b = k[:, :, start:end, :] v_b = v[:, :, start:end, :] block_valid = valid_mask[:, start:end] block_pad_mask = ~block_valid intra_out = self._intra_block_attention( q_b, k_b, v_b, block_pad_mask, causal=(self.is_decoder and not is_cross_attention), head_mask=head_mask, ) inter_out = self._inter_block_sparse( q_b, k, v, b_idx, num_blocks, valid_mask, head_mask=head_mask ) out_b = intra_out + inter_out out_list.append(out_b) out = torch.cat(out_list, dim=2) return_attn = None # Merge heads: (B, H, L, head_dim) -> (B, L, all_head_size) out = out.transpose(1, 2).contiguous().view(B, L, self.all_head_size) # 5. Output projection with nested encoder bypass if self.is_nested_encoder: # Outer RobertaAttention.output.dense performs the dense projection; bypass out_proj final_out = out else: final_out = self.out_proj(out) return final_out, return_attnBlock Transformer with Hierarchical Intra-Block and Inter-Block Attention (HIBA).
Hardened for production use across DistilBERT, RoBERTa/BERT, GPT-2, and Qwen/LLaMA/Mistral.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Ancestors
- BaseAttention
- torch.nn.modules.module.Module
Class variables
var is_custom_attention : bool-
The type of the None singleton.
Instance variables
prop c_proj : LoRALinear-
Expand source code
@property def c_proj(self) -> LoRALinear: return self.out_proj prop dense : LoRALinear-
Expand source code
@property def dense(self) -> LoRALinear: return self.out_proj prop k_lin : LoRALinear-
Expand source code
@property def k_lin(self) -> LoRALinear: return self.W_k prop k_proj : LoRALinear-
Expand source code
@property def k_proj(self) -> LoRALinear: return self.W_k prop key : LoRALinear-
Expand source code
@property def key(self) -> LoRALinear: return self.W_k prop o_proj : LoRALinear-
Expand source code
@property def o_proj(self) -> LoRALinear: return self.out_proj prop out_lin : LoRALinear-
Expand source code
@property def out_lin(self) -> LoRALinear: return self.out_proj prop q_lin : LoRALinear-
Expand source code
@property def q_lin(self) -> LoRALinear: return self.W_q prop q_proj : LoRALinear-
Expand source code
@property def q_proj(self) -> LoRALinear: return self.W_q prop query : LoRALinear-
Expand source code
@property def query(self) -> LoRALinear: return self.W_q prop v_lin : LoRALinear-
Expand source code
@property def v_lin(self) -> LoRALinear: return self.W_v prop v_proj : LoRALinear-
Expand source code
@property def v_proj(self) -> LoRALinear: return self.W_v prop value : LoRALinear-
Expand source code
@property def value(self) -> LoRALinear: return self.W_v
Methods
def copy_weights(self, pretrained_attn: torch.nn.modules.module.Module) ‑> None-
Expand source code
def copy_weights(self, pretrained_attn: nn.Module) -> None: r""" Extracts and copies pretrained weights across DistilBERT, RoBERTa/BERT, GPT-2, and Qwen/LLaMA. Strictly validates that copied weights are non-empty and verifies reconstruction norms. """ if pretrained_attn is None: return # 1. Detect nested encoder architectures (RoBERTa / BERT) cls_name = pretrained_attn.__class__.__name__ has_out_lin = hasattr(pretrained_attn, "out_lin") is_nested = False if ( ( hasattr(pretrained_attn, "self") and hasattr(pretrained_attn.self, "query") and hasattr(pretrained_attn, "output") and hasattr(pretrained_attn.output, "dense") ) or ( cls_name in [ "BertSelfAttention", "RobertaSelfAttention", "XLMRobertaSelfAttention", "ElectraSelfAttention", "DebertaSelfAttention", ] or (cls_name.endswith("SelfAttention") and "distil" not in cls_name.lower()) ) and not has_out_lin ): is_nested = True self.is_nested_encoder = bool(is_nested) # Inherit rotary embeddings if present if hasattr(pretrained_attn, "rotary_emb"): self.rotary_emb = pretrained_attn.rotary_emb extracted: dict[str, tuple[torch.Tensor | None, torch.Tensor | None]] = { "q": (None, None), "k": (None, None), "v": (None, None), "o": (None, None), } # 2. GPT-2: Fused c_attn + c_proj if hasattr(pretrained_attn, "c_attn") and pretrained_attn.c_attn is not None: c_attn_mod = pretrained_attn.c_attn if hasattr(c_attn_mod, "merge") and not getattr(c_attn_mod, "merged", False): try: c_attn_mod.merge() except Exception as e: print(f"copy_weights failed: {e}") logger.warning(f"copy_weights failed: {e}") base_c = getattr(c_attn_mod, "base_layer", c_attn_mod) c_w = getattr(base_c, "weight", getattr(c_attn_mod, "weight", None)) if c_w is not None: c_w = c_w.data.clone() if ( (c_attn_mod.__class__.__name__ == "Conv1D") or (base_c.__class__.__name__ == "Conv1D") or (c_w.ndim == 2 and c_w.shape[0] < c_w.shape[1]) ): c_w = c_w.t().contiguous() split_size = c_w.size(0) // 3 bias_mod = getattr(base_c, "bias", getattr(c_attn_mod, "bias", None)) c_b = bias_mod.data.clone() if bias_mod is not None else None extracted["q"] = ( c_w[:split_size, :], c_b[:split_size] if c_b is not None else None, ) extracted["k"] = ( c_w[split_size : 2 * split_size, :], c_b[split_size : 2 * split_size] if c_b is not None else None, ) extracted["v"] = ( c_w[2 * split_size :, :], c_b[2 * split_size :] if c_b is not None else None, ) # GPT-2 Output: c_proj if hasattr(pretrained_attn, "c_proj") and pretrained_attn.c_proj is not None: c_proj_mod = pretrained_attn.c_proj if hasattr(c_proj_mod, "merge") and not getattr(c_proj_mod, "merged", False): try: c_proj_mod.merge() except Exception as e: print(f"copy_weights failed: {e}") logger.warning(f"copy_weights failed: {e}") base_o = getattr(c_proj_mod, "base_layer", c_proj_mod) o_w = getattr(base_o, "weight", getattr(c_proj_mod, "weight", None)) if o_w is not None: o_w = o_w.data.clone() if ( (c_proj_mod.__class__.__name__ == "Conv1D") or (base_o.__class__.__name__ == "Conv1D") or (o_w.ndim == 2 and o_w.shape[0] < o_w.shape[1]) ): o_w = o_w.t().contiguous() bias_o = getattr(base_o, "bias", getattr(c_proj_mod, "bias", None)) extracted["o"] = ( o_w, bias_o.data.clone() if bias_o is not None else None, ) # 3. RoBERTa / BERT nested self-attention (pretrained_attn.self) elif hasattr(pretrained_attn, "self") and hasattr(pretrained_attn.self, "query"): s = pretrained_attn.self for proj_key, sub_name in [("q", "query"), ("k", "key"), ("v", "value")]: mod = getattr(s, sub_name, None) if mod is not None and hasattr(mod, "weight"): extracted[proj_key] = ( mod.weight.data.clone(), mod.bias.data.clone() if mod.bias is not None else None, ) # Output dense from outer container if hasattr(pretrained_attn, "output") and hasattr(pretrained_attn.output, "dense"): out_mod = pretrained_attn.output.dense extracted["o"] = ( out_mod.weight.data.clone(), out_mod.bias.data.clone() if out_mod.bias is not None else None, ) # 4. Standard linear projections (DistilBERT, Qwen, LLaMA) else: proj_candidates = { "q": ["q_lin", "q_proj", "query", "W_q"], "k": ["k_lin", "k_proj", "key", "W_k"], "v": ["v_lin", "v_proj", "value", "W_v"], "o": ["out_lin", "out_proj", "dense", "o_proj"], } for proj_key, names in proj_candidates.items(): for name in names: if hasattr(pretrained_attn, name): mod = getattr(pretrained_attn, name) if mod is not None and hasattr(mod, "weight"): extracted[proj_key] = ( mod.weight.data.clone(), mod.bias.data.clone() if mod.bias is not None else None, ) break # 5. Fallback via UniversalAdapter missing = [ k for k, (w, _) in extracted.items() if w is None and (k != "o" or not self.is_nested_encoder) ] if missing: try: from neuralops.core.adapters import UniversalAdapter adapter = UniversalAdapter() for k in missing: w, b = adapter.get_weight_bias(pretrained_attn, k) if w is not None: extracted[k] = (w.clone(), b.clone() if b is not None else None) except Exception: pass # Verify all mandatory projections were extracted missing_after = [ k for k, (w, _) in extracted.items() if w is None and (k != "o" or not self.is_nested_encoder) ] if missing_after: logger.warning( f"BlockTransformerHIBA copy_weights failed: missing mandatory projections {missing_after} " f"from module {pretrained_attn.__class__.__name__} ({pretrained_attn}). Proceeding with initialized weights." ) # 6. Copy weights into target LoRALinear layers with strict dimension safety target_map = { "q": self.W_q, "k": self.W_k, "v": self.W_v, "o": self.out_proj, } with torch.no_grad(): for proj_key, target_mod in target_map.items(): w, b = extracted[proj_key] if w is None: if proj_key == "o" and self.is_nested_encoder: # Inner nested self-attention bypasses out_proj; initialize to identity nn.init.eye_(target_mod.weight) if target_mod.bias is not None: nn.init.zeros_(target_mod.bias) continue if w.ndim != 2: w = w.reshape(w.size(0), -1) # Handle transposed orientation if necessary (only if shape does not match already) if ( w.shape != target_mod.weight.shape and target_mod.weight.shape == w.t().contiguous().shape ): w = w.t().contiguous() out_d = min(target_mod.weight.size(0), w.size(0)) in_d = min(target_mod.weight.size(1), w.size(1)) target_mod.weight.data[:out_d, :in_d].copy_( w[:out_d, :in_d].to( device=target_mod.weight.device, dtype=target_mod.weight.dtype ) ) if b is not None and target_mod.bias is not None: min_b = min(target_mod.bias.numel(), b.numel()) target_mod.bias.data[:min_b].copy_( b[:min_b].to(device=target_mod.bias.device, dtype=target_mod.bias.dtype) ) elif target_mod.bias is not None: nn.init.zeros_(target_mod.bias) # Weight verification: ensure weights are non-trivial w_norm = float(torch.norm(target_mod.weight.data).item()) if w_norm < 1e-4: logger.warning( f"Weight copy verification failed for projection '{proj_key}': norm too small ({w_norm:.2e})" ) if self.freeze_base: target_mod.freeze_base_weights()Extracts and copies pretrained weights across DistilBERT, RoBERTa/BERT, GPT-2, and Qwen/LLaMA. Strictly validates that copied weights are non-empty and verifies reconstruction norms.
Inherited members
class LoRALinear (in_features: int,
out_features: int,
bias: bool = True,
lora_rank: int = 16,
lora_alpha: float = 32.0,
freeze_base: bool = False,
device: torch.device | None = None,
dtype: torch.dtype | None = None)-
Expand source code
class LoRALinear(nn.Module): r""" Linear projection layer equipped with frozen base weights and trainable residual LoRA adapters. Base path: y_base = x @ W^T + b (frozen when freeze_base=True) LoRA path: y_lora = (x @ lora_A^T) @ lora_B^T * scaling (trainable) Total output: y = y_base + y_lora Uses standard `lora_A` and `lora_B` parameter naming recognized by framework recovery optimizers. """ def __init__( self, in_features: int, out_features: int, bias: bool = True, lora_rank: int = 16, lora_alpha: float = 32.0, freeze_base: bool = False, device: torch.device | None = None, dtype: torch.dtype | None = None, ) -> None: super().__init__() factory_kwargs = {"device": device, "dtype": dtype} self.in_features = in_features self.out_features = out_features self.lora_rank = lora_rank self.lora_alpha = lora_alpha self.lora_scaling = (lora_alpha / lora_rank) if lora_rank > 0 else 1.0 self.freeze_base = freeze_base self.weight = nn.Parameter(torch.empty((out_features, in_features), **factory_kwargs)) if bias: self.bias = nn.Parameter(torch.empty(out_features, **factory_kwargs)) else: self.register_parameter("bias", None) if self.lora_rank > 0: self.lora_A = nn.Parameter(torch.empty((lora_rank, in_features), **factory_kwargs)) self.lora_B = nn.Parameter(torch.empty((out_features, lora_rank), **factory_kwargs)) else: self.register_parameter("lora_A", None) self.register_parameter("lora_B", None) self.reset_parameters() if freeze_base: self.freeze_base_weights() def reset_parameters(self) -> None: nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0 nn.init.uniform_(self.bias, -bound, bound) if self.lora_rank > 0 and self.lora_A is not None: nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5)) self.lora_A.requires_grad = True if self.lora_rank > 0 and self.lora_B is not None: nn.init.zeros_(self.lora_B) self.lora_B.requires_grad = True def freeze_base_weights(self) -> None: """Freeze base projection parameters so only LoRA parameters adapt.""" self.weight.requires_grad = False if self.bias is not None: self.bias.requires_grad = False if self.lora_rank > 0 and self.lora_A is not None: self.lora_A.requires_grad = True if self.lora_rank > 0 and self.lora_B is not None: self.lora_B.requires_grad = True def unfreeze_base_weights(self) -> None: """Unfreeze base projection parameters.""" self.weight.requires_grad = True if self.bias is not None: self.bias.requires_grad = True def forward(self, x: torch.Tensor) -> torch.Tensor: w = self.weight.to(x.dtype) if self.weight.dtype != x.dtype else self.weight b = ( self.bias.to(x.dtype) if (self.bias is not None and self.bias.dtype != x.dtype) else self.bias ) out = F.linear(x, w, b) if self.lora_rank > 0 and self.lora_A is not None and self.lora_B is not None: a = self.lora_A.to(x.dtype) if self.lora_A.dtype != x.dtype else self.lora_A bb = self.lora_B.to(x.dtype) if self.lora_B.dtype != x.dtype else self.lora_B lora_out = F.linear(F.linear(x, a), bb) * self.lora_scaling out = out + lora_out return outLinear projection layer equipped with frozen base weights and trainable residual LoRA adapters.
Base path: y_base = x @ W^T + b (frozen when freeze_base=True) LoRA path: y_lora = (x @ lora_A^T) @ lora_B^T * scaling (trainable) Total output: y = y_base + y_lora
Uses standard
lora_Aandlora_Bparameter naming recognized by framework recovery optimizers.Initialize internal Module state, shared by both nn.Module and ScriptModule.
Ancestors
- torch.nn.modules.module.Module
Methods
def forward(self, x: torch.Tensor) ‑> torch.Tensor-
Expand source code
def forward(self, x: torch.Tensor) -> torch.Tensor: w = self.weight.to(x.dtype) if self.weight.dtype != x.dtype else self.weight b = ( self.bias.to(x.dtype) if (self.bias is not None and self.bias.dtype != x.dtype) else self.bias ) out = F.linear(x, w, b) if self.lora_rank > 0 and self.lora_A is not None and self.lora_B is not None: a = self.lora_A.to(x.dtype) if self.lora_A.dtype != x.dtype else self.lora_A bb = self.lora_B.to(x.dtype) if self.lora_B.dtype != x.dtype else self.lora_B lora_out = F.linear(F.linear(x, a), bb) * self.lora_scaling out = out + lora_out return outDefine the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them. def freeze_base_weights(self) ‑> None-
Expand source code
def freeze_base_weights(self) -> None: """Freeze base projection parameters so only LoRA parameters adapt.""" self.weight.requires_grad = False if self.bias is not None: self.bias.requires_grad = False if self.lora_rank > 0 and self.lora_A is not None: self.lora_A.requires_grad = True if self.lora_rank > 0 and self.lora_B is not None: self.lora_B.requires_grad = TrueFreeze base projection parameters so only LoRA parameters adapt.
def reset_parameters(self) ‑> None-
Expand source code
def reset_parameters(self) -> None: nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0 nn.init.uniform_(self.bias, -bound, bound) if self.lora_rank > 0 and self.lora_A is not None: nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5)) self.lora_A.requires_grad = True if self.lora_rank > 0 and self.lora_B is not None: nn.init.zeros_(self.lora_B) self.lora_B.requires_grad = True def unfreeze_base_weights(self) ‑> None-
Expand source code
def unfreeze_base_weights(self) -> None: """Unfreeze base projection parameters.""" self.weight.requires_grad = True if self.bias is not None: self.bias.requires_grad = TrueUnfreeze base projection parameters.