Pretrained models¶
Loaders that import pretrained weights and fuse the model into one ANE program.
This covers the vision, encoder, reranker, CLIP, GPT-2, and Whisper loaders in
aneforge.models. Decoder LLMs load via af.load_llm (see the LLM guide)
and ONNX models via af.load_onnx (see ONNX import).
models ¶
Pretrained-model loaders (load and CrossEncoder for BERT/RoBERTa encoders and rerankers,
load_resnet/load_vit image classifiers, load_gpt2 text generation) and trainable-graph
builders (group_norm_train, conv_block, cifar_cnn). See docs/developer/models.md.
Vision ¶
Source code in aneforge/models.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
Encoder ¶
Source code in aneforge/models.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
CrossEncoder ¶
Reranker: scores (query, passage) pairs with a BERT-, RoBERTa-, or DistilBERT-family sequence-classification
model, the transformer running on the ANE. Mirrors sentence_transformers.CrossEncoder:
CrossEncoder(name).predict([(query, passage), ...]) returns one relevance score per pair
(raw logits -- order is what a reranker needs). Higher is more relevant.
BERT and RoBERTa share the encoder graph and _BERT_KEYS; DistilBERT uses the same graph
operations with a family-specific key map. The families differ only in weight selection and
host-side head plumbing, rather than requiring separate graph code paths:
- Head. See
_seqcls_head: BERT/RoBERTa use tanh; DistilBERT uses ReLU. - Position ids. See
_position_ids: RoBERTa counts frompadding_idx + 1; BERT and DistilBERT count from 0.
Source code in aneforge/models.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
predict ¶
pairs is a (query, passage) tuple or a list of them; returns a relevance score each.
Source code in aneforge/models.py
ViT ¶
Vision Transformer image classifier from Hugging Face, running on the ANE.
ViT(name)(image) returns logits [1, num_labels]; .classify(image) returns top labels.
Scope: ViT-family classifiers with a CLS token and a pre-norm encoder (ViTForImageClassification
and compatible DeiT/BEiT-style models).
Source code in aneforge/models.py
classify ¶
Top-k (label, logit) for an image (PIL, path, or preprocessed pixel array).
Source code in aneforge/models.py
GPT2 ¶
GPT-2 causal LM from Hugging Face, running on the ANE via the unified LLM runner with
resident KV-cache decode, LayerNorm, and learned positional embeddings. Activations fp16;
int8=True quantizes weights per-channel int8. GPT2(name)(ids) -> logits [S, vocab];
.generate(prompt, K) autoregressively generates K tokens using the resident KV cache.
Source code in aneforge/models.py
CLIP ¶
CLIP dual-encoder model from Hugging Face (CLIPModel), running both vision and text encoders on the ANE.
.encode_image(image) -> [1, proj_dim] normalized image embedding.
.encode_text(texts) -> [N, proj_dim] normalized text embeddings.
.classify(image, candidate_labels) -> sorted list of (label, probability).
Source code in aneforge/models.py
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
encode_image ¶
Encode an image -> L2-normalized embedding vector [1, proj_dim].
encode_text ¶
Encode text string(s) -> L2-normalized embedding vectors [N, proj_dim].
Source code in aneforge/models.py
classify ¶
Zero-shot image classification: score image against candidate labels, returning sorted (label, prob) pairs.
Source code in aneforge/models.py
Whisper ¶
OpenAI Whisper (encoder-decoder ASR) on the ANE: one fused encoder program per clip, and a single-token
decoder program per greedy step against a resident KV cache. Greedy matches HF generate. Host-side log-mel
and tokenization only. Default whisper-base.en. See docs/developer/models.md.
.transcribe(audio) -> str; .encode(audio) -> audio features [1500, 512].
Source code in aneforge/models.py
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 | |
encode ¶
Audio features [1500, D] on the ANE for a 16 kHz mono clip (truncated/padded to 30 s).
Source code in aneforge/models.py
transcribe ¶
Greedy transcript of a 16 kHz mono clip, both towers on the ANE. Matches HF generate up to max_dec tokens.
Source code in aneforge/models.py
load ¶
Load a BERT-family sentence encoder from HF weights as an ANE embedder; pooling in mean/cls/max.
load_resnet ¶
load_resnet(name_or_depth: int | str = 18, int8: bool = False, compress: str | None = None, compress_atol: float = 0.05, build_dir: str | None = None, weights: str = 'IMAGENET1K_V1') -> 'Vision'
Load a ResNet as a fused ANE classifier; BatchNorm folded into the preceding conv at load.
name_or_depth takes a torchvision depth (50, "50", "resnet50" -> 18/34/50/101 ImageNet weights)
or a Hugging Face ResNet repo id (contains "/", e.g. "microsoft/resnet-50").
Source code in aneforge/models.py
load_resnet18 ¶
load_resnet18(int8: bool = False, compress: str | None = None, compress_atol: float = 0.05, build_dir: str | None = None) -> 'Vision'
Load torchvision ResNet-18 (ImageNet) as a fused ANE classifier; BatchNorm folded into the preceding conv at load.
Source code in aneforge/models.py
load_vit ¶
Load a Hugging Face ViT image classifier (ViTForImageClassification) as a fused ANE program.
load_gpt2 ¶
Load a Hugging Face GPT-2 causal LM as fused ANE programs: a pre-norm transformer
(native causal SDPA) plus the tied lm_head tiled along vocab. max_layers trims the
stack (the compile-fallback knob examples use when the full model will not fit).
Source code in aneforge/models.py
load_clip ¶
Load a Hugging Face CLIP dual-encoder model (CLIPModel) for zero-shot image/text classification on the ANE.
load_whisper ¶
Load a Hugging Face Whisper speech-to-text model with both towers (audio encoder + text
decoder) running on the ANE. Whisper(name).transcribe(audio) -> greedy English transcript.
Source code in aneforge/models.py
group_norm_train ¶
Any-batch GroupNorm with trainable affine, built from VJP-bearing primitives; x is [N,C,H,W], gamma/beta [1,C,1,1].
Source code in aneforge/models.py
conv_block ¶
conv2d(pad=1) -> GroupNorm(train) -> ReLU -> optional max_pool(pool); pool=0 skips pooling.
Source code in aneforge/models.py
cifar_cnn ¶
Build the CIFAR-10 CNN graph; returns (x_input, logits, params) with params in fixed trainable order.