Views
No views yet
Package.swift:1.package(url: "https://github.com/john-rocky/CoreML-LLM", branch: "main"),
2
3// In your target:
4.product(name: "CoreMLLLM", package: "CoreML-LLM"),1import CoreMLLLM
2
3let llm = try await CoreMLLLM.load(repo: "mlboydaisuke/qwen3-vl-4b-stateful-coreml")
4
5let stream = try await llm.generate(
6 [CoreMLLLM.Message(role: .user, content: "Hello!")],
7 maxTokens: 256
8)
9for await chunk in stream { print(chunk, terminator: "") }1import CoreGraphics
2
3let cgImage: CGImage = ... // your CGImage
4
5let stream = try await llm.generate(
6 [CoreMLLLM.Message(role: .user,
7 content: "What's in this image?")],
8 image: cgImage,
9 maxTokens: 256
10)
11for await chunk in stream { print(chunk, terminator: "") }Qwen/Qwen3-VL-4B-Instruct
for iPhone / iPad / Mac Apple Neural Engine. Text + vision, 3.51 GB on disk.MLState, so it does not spill to GPU
memory as the context grows. Same layout as
qwen3-vl-2b-stateful-coreml,
scaled to 4B: 6 body chunks instead of 4.qwen3_vl_4b_stateful_chunks/
├── chunk_0.mlpackage … chunk_5.mlpackage ← body, multifunction: infer (T=1) + prefill_b8 (T=8)
├── chunk_0_vision.mlpackage ← chunk_0 + DeepStack injection
├── chunk_head.mlpackage ← final_norm + lm_head + in-graph argmax
└── embed_weight.bin ← raw fp16 embed table, Swift mmaps it
qwen3_vl_4b_vision/
└── vision.mlpackage ← image encoder + DeepStack taps| Component | Size |
|---|---|
| 6 body chunks + vision chunk | 2.12 GB |
| head | 195 MB |
| embed table | 778 MB |
| vision | 415 MB |
| total | 3.51 GB |
MLState once from the prefill instance and reuses it across both — Core ML
binds state by name and shape, not by MLModel instance.model_config.json — Core ML packs shapes into each .mlpackage, and
coremltools reads them directly.1from transformers import AutoTokenizer, AutoProcessor
2tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-VL-4B-Instruct")
3proc = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-4B-Instruct")1import coremltools as ct, numpy as np
2from huggingface_hub import snapshot_download
3
4local = snapshot_download("mlboydaisuke/qwen3-vl-4b-stateful-coreml")
5root = f"{local}/qwen3_vl_4b_stateful_chunks"
6
7prefill_chunks = [ct.models.MLModel(
8 f"{root}/chunk_{i}.mlpackage", function_name="prefill_b8"
9) for i in range(6)]
10decode_chunks = [ct.models.MLModel(
11 f"{root}/chunk_{i}.mlpackage", function_name="infer"
12) for i in range(6)]
13head = ct.models.MLModel(f"{root}/chunk_head.mlpackage")
14
15# State is created once and shared across infer + prefill_b8.
16state = prefill_chunks[0].make_state()