# LFMputer decoder runtime. Source numerical operations are preserved. """Single-model conditional LoRA and circuit-internal binary switch. One request at a time. Routing is an internal frozen, tool-free backbone pass; this is conditional model execution, not an external classifier service. """ from pathlib import Path import contextlib, json, math, shutil, sys, time import torch from torch import nn import torch.nn.functional as F from lfmputer_runtime.analytics.model import AnalyticLFMComputer from lfmputer_runtime.analytics.engine import AnalyticEngine, FEATURES class BinarySwitch(nn.Module): def __init__(self, hidden, width=128): super().__init__() self.net=nn.Sequential(nn.LayerNorm(hidden),nn.Linear(hidden,width),nn.SiLU(),nn.Linear(width,1)) nn.init.zeros_(self.net[-1].weight);nn.init.constant_(self.net[-1].bias,-2.) self.register_buffer('threshold',torch.tensor(.5)) def forward(self, features):return self.net(features.float()).squeeze(-1) class GatedAnalyticEngine(AnalyticEngine): def prepare(self): # Preserve the circuit precision/frozen-weight contract; the switch alone learns. torch.set_float32_matmul_precision('highest') if self.device.type=='cuda':torch.backends.cuda.matmul.allow_tf32=False for name,p in self.named_parameters(): if name.startswith('switch.'):continue double=name.startswith(('layers.polynomial.','layers.units.units.','layers.units.combine.')) if p.dtype!=(torch.float64 if double else torch.float32) or p.requires_grad: raise TypeError('Circuit precision/frozen-weight violation: '+name) class ConditionalLoRA(nn.Module): def __init__(self, base, rank=8, alpha=16): super().__init__();self.base=base;self.rank=rank;self.scale=alpha/rank;self.active=False self.A=nn.Parameter(torch.empty(rank,base.in_features,device=base.weight.device,dtype=torch.float32)) self.B=nn.Parameter(torch.zeros(base.out_features,rank,device=base.weight.device,dtype=torch.float32)) nn.init.kaiming_uniform_(self.A,a=math.sqrt(5)) @property def weight(self):return self.base.weight @property def bias(self):return self.base.bias def forward(self,x): y=self.base(x) if self.active: # Compute adapter branch in FP32, preserving frozen BF16 base weights. z=F.linear(F.linear(x.float(),self.A),self.B)*self.scale y=y+z.to(y.dtype) return y def install_lora(lm, rank, alpha, targets): found=[] for name,module in list(lm.named_modules()): if isinstance(module,nn.Linear) and name.rsplit('.',1)[-1] in targets: parent,key=name.rsplit('.',1);setattr(lm.get_submodule(parent),key,ConditionalLoRA(module,rank,alpha));found.append(name) if not found:raise RuntimeError('No matching LoRA modules; architecture mismatch') return found def torch_conv_training(lm): # Use the model's differentiable PyTorch convolution, avoiding inference-only kernels. module=sys.modules[type(lm).__module__] if hasattr(module,'can_use_lfm3_conv'):module.can_use_lfm3_conv=lambda *a,**k:False class LFMputer(AnalyticLFMComputer): @classmethod def attach(cls, base, rank=8, alpha=16, width=128, targets=('q_proj','v_proj','o_proj','w1','w2','w3')): base.__class__=cls for p in base.parameters():p.requires_grad_(False) torch_conv_training(base.language_model) base.switch_spec=dict(rank=rank,alpha=alpha,width=width,targets=list(targets),version=1) base.lora_names=install_lora(base.language_model,rank,alpha,set(targets)) base.analytics.__class__=GatedAnalyticEngine base.analytics.switch=BinarySwitch(base.language_model.config.hidden_size,width).to(next(base.language_model.parameters()).device) base._route_context=False;base._route_force=None base.context_limit=8192 return base def adapters(self, enabled): for m in self.language_model.modules(): if isinstance(m,ConditionalLoRA):m.active=bool(enabled) def plain_messages(self,prompt):return [{'role':'user','content':prompt}] def feature_batch(self,prompts,max_length=4096): tok=self.tokenizer texts=[tok.apply_chat_template(self.plain_messages(p),tokenize=False,add_generation_prompt=True,enable_thinking=False) for p in prompts] enc=tok(texts,return_tensors='pt',padding=True,truncation=False,add_special_tokens=False) if enc.input_ids.shape[1]>max_length:raise ValueError('Gate input exceeds max_length; do not silently truncate') enc=enc.to(next(self.language_model.parameters()).device) self.adapters(False) was=self.language_model.training;self.language_model.eval() try: with torch.no_grad(): h=self.language_model.model(**enc,use_cache=False).last_hidden_state positions=torch.arange(h.shape[1],device=h.device)[None].expand(h.shape[0],-1) last=positions.masked_fill(enc.attention_mask==0,-1).max(-1).values last_features=h[torch.arange(h.shape[0],device=h.device),last].detach().float() if self.switch_spec.get('feature_pooling','last')=='last_mean': weights=enc.attention_mask[...,None].float() mean_features=(h.float()*weights).sum(1)/weights.sum(1).clamp_min(1) return torch.cat((last_features,mean_features),-1) return last_features finally:self.language_model.train(was) def forward(self,input_ids=None,*,mode='language',prompt=None,force_gate=None,**kwargs): if mode=='route': f=self.feature_batch([prompt]);logit=self.analytics.switch(f) return {'logits':logit,'probability':logit.sigmoid()} if mode=='chat': return self._chat_inside(prompt,force_gate=force_gate,**kwargs) if mode in ('analytic','analytic_tensor') and not self._route_context: raise RuntimeError('Circuits are disabled outside an open model request') return super().forward(input_ids=input_ids,mode=mode,**kwargs) @torch.inference_mode() def _chat_inside(self,prompt,force_gate=None,max_new_tokens=512,total_new_tokens=1536,max_calls=3): started=time.perf_counter() self.eval();r=self.forward(mode='route',prompt=prompt) probability=float(r['probability'][0]);opened=probability>=float(self.analytics.switch.threshold) if force_gate is not None:opened=bool(force_gate) self.adapters(opened);self._route_context=opened self.analytics.set_enabled(FEATURES if opened else ()) self.include_computer_tools=opened;self.external_tools=[];self.external_dispatch={} before=self.analytics.calls.clone() try: if not opened: # Exactly tool-free input; no circuit schemas or extra system prompt. enc=self.tokenizer.apply_chat_template(self.plain_messages(prompt),tokenize=True, add_generation_prompt=True,enable_thinking=False,return_dict=True,return_tensors='pt') enc=enc.to(next(self.language_model.parameters()).device) allowance=min(total_new_tokens,self.context_limit-enc.input_ids.shape[1]) if allowance<=0:raise RuntimeError('Configured context budget exhausted') out=self.language_model.generate(**enc,max_new_tokens=allowance,do_sample=False,repetition_penalty=1.1,logits_to_keep=1, pad_token_id=self.tokenizer.pad_token_id,eos_token_id=self.language_model.generation_config.eos_token_id) answer=self.tokenizer.decode(out[0,enc.input_ids.shape[1]:],skip_special_tokens=True) suffix=out[0,enc.input_ids.shape[1]:] eos=self.language_model.generation_config.eos_token_id or self.tokenizer.eos_token_id eos=[eos] if isinstance(eos,int) else eos finished=bool(suffix.numel() and int(suffix[-1]) in eos) result={'answer':answer,'events':[],'generated_tokens':int(suffix.numel()),'prompt_tokens':enc.input_ids.shape[1],'truncated':not finished,'stop_reason':'eos' if finished else 'generation_budget','seconds':time.perf_counter()-started} else: result=super().chat(super().new_session(),prompt,max_new_tokens=max_new_tokens, total_new_tokens=total_new_tokens,max_calls=max_calls,verbose=False) result.update(gate_open=opened,gate_probability=probability, circuit_calls={n:int(v) for n,v in zip(FEATURES,(self.analytics.calls-before).tolist())}) return result finally: self.adapters(False);self._route_context=False;self.analytics.set_enabled(()) def save_pretrained(self,path): from lfmputer_runtime.checkpoint import save_checkpoint return save_checkpoint(self,path) @classmethod def load(cls,path,device='cuda'): from accelerate import init_empty_weights from safetensors.torch import load_file from transformers import AutoConfig,AutoModelForCausalLM,AutoTokenizer from lfmputer_runtime.analytics.model import CompiledComputer,RAMBridge path=Path(path);cfg=json.loads((path/'computer_config.json').read_text());spec=json.loads((path/'routing_config.json').read_text()) config=AutoConfig.from_pretrained(path/'backbone',trust_remote_code=True,local_files_only=True) config._attn_implementation='sdpa' with init_empty_weights(): lm=AutoModelForCausalLM.from_config(config,trust_remote_code=True) bridge=RAMBridge(spec=cfg['ram_bridge']) if cfg.get('ram_bridge') else None base=AnalyticLFMComputer(lm,CompiledComputer(cfg['circuit']),cfg['memory'],cfg['manifest'],cfg['ram_spec'],bridge,cfg['analytic_spec']) # Attach creates a CPU switch; never try to move a parameter onto a meta device. for p in base.parameters():p.requires_grad_(False) base.__class__=cls;base.switch_spec={k:spec[k] for k in ['rank','alpha','width','targets','version']} base.switch_spec['feature_pooling']=spec.get('feature_pooling','last') torch_conv_training(lm);base.lora_names=install_lora(lm,spec['rank'],spec['alpha'],set(spec['targets'])) base.analytics.__class__=GatedAnalyticEngine;base.analytics.switch=BinarySwitch(config.hidden_size*(2 if spec.get('feature_pooling')=='last_mean' else 1),spec['width']) from lfmputer_runtime.decoder import install_decoder_stack, logical_to_decoder_key install_decoder_stack(base) values=load_file(str(path/'model.safetensors')) for alias,original in spec['aliases'].items():values[alias]=values[original] values={logical_to_decoder_key(k,base.decoder_layout):v for k,v in values.items()} base.load_state_dict(values,strict=True,assign=True);base.language_model.tie_weights() base._route_context=False;base._route_force=None;base.context_limit=8192 base.tokenizer=AutoTokenizer.from_pretrained(path,config=config,local_files_only=True,trust_remote_code=True,fix_mistral_regex=False) base.definition_path=path/'backbone';base.source_snapshot=str(path);base.source_revision=spec['source_revision'] base.to(device).eval();base.computer.prepare('torch');base.analytics.prepare();base.adapters(False) return base