"""AMOS Qwen3.5 Core AI adapter. No PyTorch neural model is loaded.""" import json,sys from pathlib import Path import numpy as np import torch from coreai.runtime import AIModel,NDArray,SpecializationOptions from transformers import AutoTokenizer,Qwen2VLImageProcessorPil class AmosDecoder: @classmethod async def load(cls,bundle): self=cls();self.root=Path(bundle).resolve();sys.path.insert(0,str(self.root/'serve')) import amos.decoder as decoder decoder.IMAGE_PIXELS=224*224 self.manifest=json.loads((self.root/'manifest.json').read_text()) self.codec=decoder.Codec(AutoTokenizer.from_pretrained(self.root/'model'),Qwen2VLImageProcessorPil.from_pretrained(self.root/'model',size={'shortest_edge':224*224,'longest_edge':224*224}),255) self.codec.echo=self.manifest['echo'] self.temperature=decoder.calibration(self.root) or {};self.span=decoder.span_decoding(self.root) self.models=[];self.functions={} for name in ['text','vision','heads']: m=await AIModel.load(self.root/(name+'.aimodel'),SpecializationOptions.default());self.models.append(m);self.functions[name]=m.load_function('main') return self async def raw(self,state,questions,images=()): from amos.decoder import expand from PIL import Image def resize(im):return im.convert('RGB').resize((224,224),Image.Resampling.BICUBIC) images=[resize(im) for im in images] questions=[{**q,'options':[resize(o) if isinstance(o,Image.Image) else o for o in q['options']]} if 'options' in q else q for q in questions] flat,owner=expand(questions) ids,at,extra=self.codec.encode_case(state,flat,images) n=len(ids);L=self.manifest['sequence_length'];d=self.manifest['hidden_size'] if n>L:raise ValueError(f'Request needs {n} tokens; Core AI capacity is {L}.') args={'input_ids':np.full((1,L),self.codec.pad,np.int32),'attention_mask':np.zeros((1,L),np.int32),'position_ids':np.zeros((4,1,L),np.int32),'visual_embeddings':np.zeros((1,L,d),np.float32),'visual_mask':np.zeros((1,L,1),bool)} args['input_ids'][0,:n]=ids;args['attention_mask'][0,:n]=1;args['position_ids'][0,0,:n]=np.arange(n) image_at={s:(rows,cols) for s,rows,cols in extra.get('images',[])} features=[] if image_at: pv=extra['pixel_values'].numpy() for start in range(0,len(pv),196): out=await self.functions['vision']({'pixel_values':NDArray(pv[start:start+196].astype(np.float32))});features.append(out[self.manifest['vision_output']].numpy()) cursor=0;offset=0;fi=0 while cursor