Instructions to use noahmclaughlin/Mixed-Messenger-4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use noahmclaughlin/Mixed-Messenger-4B with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download noahmclaughlin/Mixed-Messenger-4B --local-dir Mixed-Messenger-4B
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download mixed_model.py from noahmclaughlin/Mixed-Messenger-4B: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/noahmclaughlin/Mixed-Messenger-4B/resolve/main/mixed_model.py
- Command line
-
hf download hf://noahmclaughlin/Mixed-Messenger-4B/mixed_model.py
-
curl -L -o mixed_model.py https://huggingface.co/noahmclaughlin/Mixed-Messenger-4B/resolve/main/mixed_model.py
4.34 kB
| """Inference-only Mixed Messenger: no benchmark labels or lookup table are loaded.""" | |
| from pathlib import Path | |
| import hashlib,json,time | |
| import numpy as np | |
| import mlx.core as mx | |
| from mlx_lm import load,stream_generate | |
| from mlx_lm.models.base import create_attention_mask | |
| from mlx_lm.sample_utils import make_sampler | |
| KEYS=['performed+uniting','honest+uniting','performed+dividing','honest+dividing'] | |
| def simplex(p): | |
| u=np.sort(p)[::-1];css=np.cumsum(u)-1;n=np.sum(u-css/np.arange(1,5)>0) | |
| return np.maximum(p-css[n-1]/n,0) | |
| def parse_base(raw): | |
| """Match upstream strict JSON/value/sum validation; invalid internal feature is uniform.""" | |
| try: | |
| obj=json.loads(raw) | |
| assert isinstance(obj,dict) and set(obj)==set(KEYS) | |
| assert all(type(obj[k]) in (int,float) and np.isfinite(obj[k]) and 0<=obj[k]<=1 for k in KEYS) | |
| p=np.array([obj[k] for k in KEYS],dtype=np.float64) | |
| assert abs(p.sum()-1)<=.001+np.finfo(float).eps*4 | |
| return p/p.sum(),True | |
| except (ValueError,TypeError,AssertionError):return np.full(4,.25),False | |
| class MixedModel: | |
| def __init__(self,folder,base_path): | |
| self.folder=Path(folder);self.config=json.loads((self.folder/'model.json').read_text()) | |
| for name,sha in self.config['artifacts'].items(): | |
| assert hashlib.sha256((self.folder/name).read_bytes()).hexdigest()==sha,f'Checksum mismatch: {name}' | |
| mx.set_memory_limit(10*1024**3);mx.set_cache_limit(256*1024**2) | |
| self.base,self.tok=load(base_path);self.base.eval() | |
| self.adapted,_=load(base_path,adapter_path=str(self.folder/self.config['adapters'][0]));self.adapted.eval() | |
| self.adapter_weights=[list(mx.load(str(self.folder/a/'adapters.safetensors')).items()) for a in self.config['adapters']] | |
| h=dict(np.load(self.folder/self.config['weights'],allow_pickle=False)) | |
| self.head={k:mx.array(v.astype(np.float32)) for k,v in h.items()} | |
| self.multiview=dict(np.load(self.folder/'multiview-head.npz',allow_pickle=False)) | |
| def predict(self,messages,max_tokens=8000): | |
| start=time.perf_counter();mx.random.seed(7) | |
| text=self.tok.apply_chat_template(messages,tokenize=False,add_generation_prompt=True) | |
| ids=self.tok.encode(text,add_special_tokens=False);tokens=mx.array([ids]) | |
| raw='';last=None | |
| for r in stream_generate(self.base,self.tok,text,max_tokens=max_tokens,sampler=make_sampler(temp=0)): | |
| raw+=r.text;last=r | |
| if last is None:raise RuntimeError('Base generation returned no tokens') | |
| base_p,valid=parse_base(raw);mx.clear_cache() | |
| hidden=self.base.model.embed_tokens(tokens);mask=create_attention_mask(hidden);views=[] | |
| for n,layer in enumerate(self.base.model.layers,1): | |
| hidden=layer(hidden,mask,None) | |
| if n in [18,36]: | |
| z=self.base.model.norm(hidden) if n==36 else hidden | |
| v=mx.mean(z[0],axis=0).astype(mx.float32);mx.eval(v) | |
| x=np.array(v,dtype=np.float64);views.append(x/max(np.linalg.norm(x),1e-12)) | |
| h=self.multiview;p=self.config['base_prior_weight']*base_p+h['bias'] | |
| for x,mean,scale,w in zip(views,h['means'],h['scales'],h['weights']):p=p+((x-mean)/scale)@w | |
| calibrated=simplex(p);mx.clear_cache();adapted=[] | |
| for weights in self.adapter_weights: | |
| self.adapted.load_weights(weights,strict=False) | |
| hidden=self.adapted.model(tokens);x=mx.mean(hidden,axis=1).astype(mx.float32) | |
| x=x/mx.maximum(mx.sqrt(mx.sum(x*x,axis=-1,keepdims=True)),1e-12) | |
| h=self.head;z=((x-h['mean'])/h['scale'])@h['weight']+h['bias'] | |
| p=mx.softmax(z,axis=-1,precise=True);mx.eval(p);q=np.array(p[0],dtype=np.float64);q/=q.sum();adapted.append(q);mx.clear_cache() | |
| mix=self.config['calibrated_weight'];p=mix*calibrated+(1-mix)*np.mean(adapted,axis=0) | |
| assert np.all(np.isfinite(p)) and np.all(p>=0) and abs(p.sum()-1)<1e-8 | |
| return dict(zip(KEYS,p.tolist())),{'seconds':time.perf_counter()-start,'prompt_tokens':len(ids),'completion_tokens':last.generation_tokens,'forward_passes':2+len(adapted),'peak_memory_gb':mx.get_peak_memory()/1e9,'base_raw':raw,'base_valid':valid,'base_probabilities':base_p.tolist(),'calibrated_probabilities':calibrated.tolist(),'adapted_probabilities':[q.tolist() for q in adapted]} | |