Instructions to use masahiroid/contriever-mlx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use masahiroid/contriever-mlx with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download masahiroid/contriever-mlx --local-dir contriever-mlx
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download build.py from masahiroid/contriever-mlx: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/masahiroid/contriever-mlx/resolve/main/build.py
- Command line
-
hf download hf://masahiroid/contriever-mlx/build.py
-
curl -L -o build.py https://huggingface.co/masahiroid/contriever-mlx/resolve/main/build.py
3.7 kB
| import argparse,json,shutil,traceback | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from common import * | |
| def metadata(name,fmt,model): | |
| p=ROOT/'publish'/f'{name}-{fmt}'; p.mkdir(parents=True,exist_ok=True) | |
| model.config.save_pretrained(p) | |
| if name!='chronos-bolt-small': AutoTokenizer.from_pretrained(ROOT/'sources'/name,use_fast=name!='mdeberta-v3-base').save_pretrained(p) | |
| return p | |
| def build(name,fmt): | |
| print('START',name,fmt,flush=True) | |
| model=load(name); wrapper=Wrapper(model,name).eval() | |
| p=metadata(name,fmt,model); tests=cases(name); refs=[reference(name,c,wrapper) for c in tests] | |
| if fmt in ('mlx','gguf'): | |
| import mlx.core as mx | |
| from runtime_mlx import Model | |
| weights={k:v.detach().float().numpy() for k,v in model.state_dict().items()} | |
| if fmt=='mlx': mx.save_safetensors(str(p/'model.safetensors'),{k:mx.array(v) for k,v in weights.items()}) | |
| else: | |
| from gguf import GGUFWriter | |
| writer=GGUFWriter(str(p/'model-f32.gguf'),'hf-native-mlx-v1') | |
| writer.add_name(name); writer.add_string('runtime','runtime_mlx.py (custom; not llama.cpp)') | |
| for k,v in weights.items(): writer.add_tensor(k,v) | |
| writer.write_header_to_file(); writer.write_kv_data_to_file(); writer.write_tensors_to_file(); writer.close() | |
| shutil.copy(ROOT/'runtime_mlx.py',p/'runtime_mlx.py') | |
| converted=Model(p,gguf=fmt=='gguf') | |
| outs=[] | |
| for c in tests: | |
| inp={k:v for k,v in c.items() if k in ('context','input_ids','attention_mask')} | |
| y=converted(**inp); mx.eval(y); outs.append(np.array(y)) | |
| elif fmt=='coreml': | |
| import coremltools as ct | |
| if name=='mdeberta-v3-base': | |
| import math | |
| import transformers.models.deberta_v2.modeling_deberta_v2 as d | |
| # Fixed head dimension: fold the scripted integer sqrt into an FP32 constant. | |
| d.scaled_size_sqrt=lambda q,f: torch.tensor(math.sqrt(64*f),dtype=q.dtype,device=q.device) | |
| # Self-attention has equal query/key lengths; fold the scripted branch. | |
| d.build_rpos=lambda q,k,r,*unused:r | |
| args=args_for(name,tests[0]); traced=torch.jit.trace(ExportWrapper(model,name).eval(),args,strict=False,check_trace=False) | |
| names=['context'] if name=='chronos-bolt-small' else ['input_ids','attention_mask'] | |
| inputs=[ct.TensorType(name=k,shape=tuple(v.shape),dtype=np.float32 if k=='context' else np.int32) for k,v in zip(names,args)] | |
| converted=ct.convert(traced,inputs=inputs,outputs=[ct.TensorType(name='output')],convert_to='mlprogram', | |
| compute_precision=ct.precision.FLOAT32,minimum_deployment_target=ct.target.macOS14) | |
| converted.save(str(p/'model.mlpackage')) | |
| outs=[converted.predict({k:c[k].astype(np.float32 if k=='context' else np.int32) for k in names})['output'] for c in tests] | |
| else: raise ValueError(fmt) | |
| result=[metrics(r,y) for r,y in zip(refs,outs)] | |
| print('METRICS',result,flush=True) | |
| assert all(m['max_abs']<0.02 and m['cosine']>0.999 for m in result), result | |
| (p/'validation.json').write_text(json.dumps(dict(status='passed',cases=result,input_shape=list(next(iter(tests[0].values())).shape),precision='float32'),indent=2)) | |
| np.savez(p/'validation_vectors.npz',**{f'reference_{i}':v for i,v in enumerate(refs)},**{f'input_{i}_{k}':v for i,c in enumerate(tests) for k,v in c.items() if k in ('context','input_ids','attention_mask')}) | |
| print('PASS',name,fmt,flush=True) | |
| if __name__=='__main__': | |
| a=argparse.ArgumentParser(); a.add_argument('name',choices=NAMES); a.add_argument('format'); x=a.parse_args(); build(x.name,x.format) | |