"""Deck-owned loopback worker using the unmodified official Strata server.""" import http.client,json,os,secrets,signal,socket,subprocess,threading,time,urllib.request from pathlib import Path from image_test import probe from inference import InferenceError,MemoryAdmissionError from strata_runtime import RECIPE def performance_tuning(quant,devices,parameters,cpu=None): """Apply measured tuning only to the qualified hardware and layer layout.""" tuning=RECIPE.get("performance",{}).get(quant,{}) if not tuning or parameters.get("strata_experts")!=tuning.get("experts","resident") or [g.get("name") for g in devices]!=tuning.get("gpu_names") or parameters["strata_layer_split"]!=tuning.get("layer_split") or parameters.get("context",8192)>tuning.get("max_context",131072):return {} if cpu is None: try:cpu=Path("/proc/cpuinfo").read_text() except OSError:return {} if not tuning.get("cpu_model") or tuning["cpu_model"] not in cpu:return {} return tuning class StrataWorker: def __init__(self,root,catalog,runtime): self.root=Path(root);self.catalog=catalog;self.runtime=runtime;self.lock=threading.RLock();self.process=None;self.profile=None;self.port=None;self.key=None;self.devices=[];self.state='stopped';self.error=None;self.phase='Gestoppt';self.memory_plan=None def blockers(self,p): try: if not self.runtime.status()['installed']:raise ValueError('Strata-Laufzeit fehlt. Unter Laufzeiten → Strata installieren.') _,m=self.runtime.prepared(self.catalog.entry(p['model_id'])) if p['parameters'].get('vision_projector'):self.runtime.vision_config(p['parameters']['vision_projector'],self.runtime.storage/m['native']) return [] except ValueError as e:return [str(e)] def status(self): with self.lock: if self.process and self.process.poll() is not None:self.state='failed';self.error='Strata-Modellprozess beendet. Speicher und eigenes Worker-Protokoll prüfen.' return dict(runtime='strata',state=self.state,profile_id=self.profile['id'] if self.profile else None,profile_name=self.profile['name'] if self.profile else None,port=self.port,error=self.error,gpus=self.devices,memory_plan=self.memory_plan,phase=self.phase) def plan(self,p,cancel=lambda:False): errors=self.blockers(p) if errors:raise InferenceError('; '.join(errors)) params=p['parameters'];gpus=probe();ids=params['gpu_devices'] if len(ids)!=2:raise InferenceError('Dieses Strata-Rezept benötigt zwei explizit ausgewählte GPUs. Erste GPU: Anfangslayer; zweite GPU: restliche Layer und MTP/Ausgabekopf.') selected=[next((g for g in gpus if g['uuid']==i),None) for i in ids] if any(g is None for g in selected):raise InferenceError('Eine ausgewählte Strata-GPU ist nicht verfügbar.') q,_=self.runtime.prepared(self.catalog.entry(p['model_id']));tuning=performance_tuning(q,selected,params) reserve=[params['gpu_reserve_mib'].get(i,768) if params['gpu_reserve_mode']=='manual' else 768 for i in ids] if params['gpu_reserve_mode']=='auto' and tuning:reserve=[tuning['auto_reserve_mib']]*len(ids) if any(g['free_mib']