"""Text-only Dyno backbone and decision readout, with mandatory default repetition.""" import math from pathlib import Path import torch from torch import nn from transformers import AutoProcessor, PreTrainedModel from transformers.models.qwen3_5.configuration_qwen3_5 import Qwen3_5TextConfig from transformers.models.qwen3_5.modeling_qwen3_5 import Qwen3_5TextModel from .configuration_dyno import DynoConfig from .protocol_dyno import encode from .boundary_dyno import BoundaryProcessor class DynoModel(PreTrainedModel): config_class = DynoConfig base_model_prefix = 'backbone' _supports_sdpa = True _keep_in_fp32_modules = ['readout', 'lora_A', 'lora_B'] _no_split_modules = ['Qwen3_5DecoderLayer'] def __init__(self, config): super().__init__(config) self._keep_in_fp32_modules = list(config.fp32_modules) text = Qwen3_5TextConfig(**config.text_config) text._attn_implementation = 'sdpa'; text.use_cache = False self.backbone = Qwen3_5TextModel(text) if config.native_lora: from peft import LoraConfig, inject_adapter_in_model inject_adapter_in_model(LoraConfig(**config.native_lora), self.backbone) self._lora_float() for m in self.backbone.modules(): if type(m).__name__ == 'Qwen3_5Attention': m.is_causal = False self.readout = nn.Linear(text.hidden_size, len(config.label_ids), bias=config.readout_bias, dtype=torch.float32) self._processor = None; self._buckets = (); self._compiled = False self.post_init() def _lora_float(self): for module in self.backbone.modules(): if hasattr(module, 'lora_A'): module.lora_A.float(); module.lora_B.float() @classmethod def from_pretrained(cls, pretrained_model_name_or_path, *args, **kwargs): obj = super().from_pretrained(pretrained_model_name_or_path, *args, **kwargs) # Device-map dispatch remains HF-owned; never silently CPU-offload a fast profile. obj._artifact_path = str(pretrained_model_name_or_path) obj._artifact_revision = getattr(obj.config, '_commit_hash', None) or kwargs.get('revision') obj._local_only = kwargs.get('local_files_only', False) obj._lora_float() obj.readout.float() return obj def get_input_embeddings(self): return self.backbone.embed_tokens def set_input_embeddings(self, value): self.backbone.embed_tokens = value @property def processor(self): if self._processor is None: self._processor = AutoProcessor.from_pretrained(self._artifact_path, local_files_only=getattr(self, '_local_only', True), revision=getattr(self, '_artifact_revision', None), trust_remote_code=True) self._processor.tokenizer.padding_side = 'left' if self._processor.tokenizer.pad_token_id is None: self._processor.tokenizer.pad_token_id = self._processor.tokenizer.eos_token_id self._boundary_processor = BoundaryProcessor(self._processor, self.config.labels) return self._processor def prepare_request(self, request, mode='default'): _ = self.processor return encode(self._boundary_processor, self.config.labels, self.config.label_ids, request, mode, self.config.max_length) def optimize(self, compile_mode='layer', buckets=(256, 512, 1024, 2048, 4096, 8192, 16384)): """Explicit opt-in. Run parity and latency checks before adopting this profile.""" if compile_mode not in ('none', 'layer'): raise ValueError('compile_mode must be none or layer') if any(x <= 0 or x > self.config.max_length for x in buckets): raise ValueError('Invalid bucket') if self._compiled: raise ValueError('Optimize only once on a fresh model instance') self._buckets = tuple(sorted(set(buckets))) if compile_mode == 'layer': if self.device.type != 'cuda': raise ValueError('CUDA layer compilation requested without CUDA') for layer in self.backbone.layers: layer.forward = torch.compile(layer.forward, backend='inductor', dynamic=False, options={'triton.cudagraphs': False}) self._compiled = True return self @torch.inference_mode() def _logits(self, items, bucket=True): """Private numerical core. Public forward/decide accept structured requests only.""" if not items: return [] width = max(len(r['ids']) for r in items) if width > self.config.max_length: raise ValueError('Internal sequence exceeds context limit') if bucket and self._buckets: width = next((n for n in self._buckets if n >= width), width) pad = self.processor.tokenizer.pad_token_id ids = torch.full((len(items), width), pad, dtype=torch.long, device=self.device) mask = torch.zeros_like(ids, dtype=torch.bool) for i, r in enumerate(items): n = len(r['ids']); ids[i, -n:] = torch.tensor(r['ids'], device=self.device); mask[i, -n:] = True # Host-known lengths choose the mask; no bool(CUDA_tensor) synchronization. padded = any(len(r['ids']) != width for r in items) mapping = {'full_attention': mask[:, None, None, :] if padded else None, 'linear_attention': mask if padded else None} with torch.autocast(self.device.type, dtype=torch.bfloat16, enabled=self.device.type == 'cuda'): hidden = self.backbone(input_ids=ids, attention_mask=mapping, use_cache=False).last_hidden_state[:, -1] # Keep the tiny head and softmax outside mixed-precision autocast. with torch.autocast(self.device.type, enabled=False): n = max(len(r['keys']) for r in items) logits = torch.nn.functional.linear(hidden.float(), self.readout.weight[:n], None if self.readout.bias is None else self.readout.bias[:n]) return [z[:len(r['keys'])] for z, r in zip(logits, items)] @torch.inference_mode() def decide(self, request, mode='default'): temperature = float(self.config.temperatures.get(mode, float('nan'))) if not math.isfinite(temperature) or temperature <= 0: raise ValueError('Invalid mode/temperature') items = self.prepare_request(request, mode) # One sequence per question preserves the trained protocol. No generation. answers = {}; usage = [] for item in items: z = self._logits([item])[0] probabilities = (z.float()/temperature).softmax(-1).cpu().tolist() p = dict(zip(item['keys'], probabilities)); selected = max(p, key=p.get) answers[item['qid']] = {'choice': selected, 'probabilities': p} usage.append(len(item['ids'])) return {'answers': answers, 'mode': mode, 'read_twice': mode == 'default', 'sequence_tokens': usage, 'calibrated': self.config.calibrated, 'release_id': self.config.release_id} def forward(self, request=None, mode='default', **kwargs): if kwargs: raise ValueError('Use structured request input; raw input_ids/generation bypasses are not the public API') return self.decide(request, mode)