Views
No views yet
.mlpackage converted from meta-llama/Llama-3.2-1B-Instruct for use with HuggingFace's AnyLanguageModel Swift framework and swift-transformers ≥ 1.0.inputIds / attentionMask / logits convention required by swift-transformers 1.x LanguageModel. Drop-in compatible with CoreMLLanguageModel(url:computeUnits:chatTemplateHandler:).grio-llama-3.2-1b-coreml-anyLM-seq512 (this repo)
Fixed [1, 512] — short-context transcript cleanup and post-processing. Fastest Llama profile. Always pays for full 512 positions per step.grio-llama-3.2-1b-coreml-anyLM-seq1024
Fixed [1, 1024] — medium-context copy-editing, translation.| Spec | Value |
|---|---|
| Base | meta-llama/Llama-3.2-1B-Instruct |
| Precision | Float16 (mlprogram) |
| Context | 512 tokens, fixed shape (prompt + completion combined) |
| Inputs | inputIds, attentionMask |
| Input shape | Int32 [1, 512] |
| Output | logits: Float16 [1, 512, 128256] (rank-3, per-position) |
| Min OS | iOS 18 / macOS 15 |
| Compute | .all recommended; fixed shape is ANE-friendly on Llama 3.2 |
| Format | .mlpackage (compiled on first load) |
| Toolchain | coremltools 9.0 + torch 2.7 + transformers 5.8.1 |
.mlpackage under .cpuAndGPU. Output is byte-identical to the matching seq1024 variant in this collection (same weights, same graph topology; only the static seq dimension differs)."You are a helpful assistant. Answer concisely."
User: "What is the capital of France?"
Output: "The capital of France is Paris." (stops at <|eot_id|> after 8 tokens)| Compute | Predict | Load | Result |
|---|---|---|---|
.cpuAndGPU | ~580 | ~18s | ✅ Clean. Production-recommended for this shape. |
.all | — | — | ✅ Clean — fixed shape is ANE-friendly. |
attentionMask = 0 on padded positions.logits is per-position, rank-3 [1, 512, 128256]. Pick the row at the last real prompt token for greedy/sampled decode.generation_config.json: <|end_of_text|> (id 128001) and <|eot_id|> (id 128009). tokenizer.eos_token_id returns <|eot_id|>. If your runtime stops only on the single tokenizer EOS id, it may decode past <|eot_id|> into out-of-distribution territory. Make sure your generation loop checks both.seq512 variant, it runs roughly 2× faster per step than the seq1024 variant on equivalent inputs.attn_implementation="sdpa". SDPA produces a much cleaner MIL graph that lowers reliably to both GPU and Apple Neural Engine.torch.export.default_decompositions() to ensure standard graph representation and avoid conversion scaling errors during export.1import AnyLanguageModel
2
3let modelURL: URL = // path to this .mlpackage on disk
4let lm = try await CoreMLLanguageModel(
5 url: modelURL,
6 computeUnits: .all,
7 chatTemplateHandler: { instructions, prompt in
8 // Llama 3.2 Instruct uses the tokenizer-owned header/chat template;
9 // tokenizer_config.json and tokenizer files must stay alongside the model
10 var messages: [Message] = []
11 if let system = instructions?.description, !system.isEmpty {
12 messages.append(["role": "system", "content": system])
13 }
14 messages.append(["role": "user", "content": prompt.description])
15 return messages
16 }
17)
18let session = LanguageModelSession(model: lm, instructions: "You are a helpful assistant.")
19let response = try await session.respond(to: "Improve this text: ...")
20print(response.content)tokenizer.json, tokenizer_config.json, config.json, and generation_config.json (all bundled in this repo) as siblings of the .mlpackage on disk. swift-transformers reads the chat template from tokenizer_config.json at runtime.coremltools==9.0, torch==2.7.0, transformers==5.8.1. Approximate single-call recipe:1import coremltools as ct, torch, torch.nn as nn
2from transformers import AutoModelForCausalLM
3
4model = AutoModelForCausalLM.from_pretrained(
5 "meta-llama/Llama-3.2-1B-Instruct",
6 torch_dtype=torch.float16,
7 attn_implementation="sdpa",
8)
9model.eval()
10
11class Wrapper(nn.Module):
12 def __init__(self, m): super().__init__(); self.m = m
13 def forward(self, inputIds, attentionMask):
14 return self.m(input_ids=inputIds, attention_mask=attentionMask, use_cache=False).logits
15
16wrapper = Wrapper(model).eval()
17ep = torch.export.export(
18 wrapper,
19 (torch.randint(0, 128256, (1, 128), dtype=torch.int32),
20 torch.ones((1, 128), dtype=torch.int32)),
21).run_decompositions(torch.export.default_decompositions())
22
23ct.convert(
24 ep,
25 inputs=[
26 ct.TensorType(name="inputIds", shape=(1, 512), dtype=int),
27 ct.TensorType(name="attentionMask", shape=(1, 512), dtype=int),
28 ],
29 outputs=[ct.TensorType(name="logits")],
30 minimum_deployment_target=ct.target.iOS18,
31 compute_precision=ct.precision.FLOAT16,
32 convert_to="mlprogram",
33)meta-llama/Llama-3.2-1B-Instruct by Meta.