Releasing Swift Transformers: Run On-Device LLMs in Apple Devices
Hugging Face released swift-transformers, an alpha Swift package with a transformers-like API for on-device text generation, along with the swift-chat demo app, updated exporters and transformers-to-coreml conversion tools, and pre-converted Llama 2 7B and Falcon 7B Core ML models. The post walks through converting Llama 2 to Core ML via three routes: the no-code Space, exporters, or raw coremltools. Lessons from those conversions include using coremltools 7.0b1, dropping softmax from outputs, and using fixed sequence lengths, because flexible shapes only run on the CPU. Planned optimizations are KV caching, discrete input shapes and palettization or mixed-bit quantization, and the library already supports Hub-configured BPE tokenizers plus greedy and top-k generation.
Related posts
- Swift Transformers Reaches 1.0 – and Looks to the Future
Hugging Face easyNoviceswift-transformers, Hugging Face's Swift library for running local models on Apple Silicon, reaches 1.0. The release makes Tokenizers and Hub standalone top-level modules and adopts a rewritten swift-jinja for chat templates that is orders of magnitude faster. It also moves to modern Core ML APIs with stateful models for KV caching and MLTensor, drops the CLI targets and the swift-argument-parser dependency, and fully supports Swift 6. The release includes breaking API changes, and future work will focus on MLX integration and agentic use cases including MCP.
- WWDC 24: Running Mistral 7B with Core ML
Hugging Face mediumAdeptMistral 7B Instruct can run on a Mac with Core ML using features new in iOS 18 and macOS Sequoia: the MLTensor Swift type for tensor pre- and post-processing, stateful buffers that keep the kv-cache on the GPU instead of passing it in and out on every call, and block-wise 4-bit quantization. The conversion patches the transformers Mistral attention to use a pre-allocated, in-place kv-cache, traces it with torch.jit, and converts it with coremltools RangeDim inputs and StateType caches. Int4 per-block linear quantization (block size 32) shrinks the model from about 14GB in float16 to about 3.8GB. Inference then runs through the preview branch of swift-transformers.
- AnyLanguageModel 1.0: One Year of Local Models on Apple Platforms
Hugging Face mediumAdeptAnyLanguageModel 1.0 is a Swift package that acts as a drop-in replacement for Apple's Foundation Models framework, letting apps swap the model and provider by changing a single import. Over the past year it gained tool calling, guided generation including nested @Generable types, and image support across every provider. It covers local models via llama.cpp, MLX and Core ML as well as hosted APIs (OpenAI, Anthropic, Gemini, Ollama, Hugging Face Inference Providers, Private Cloud Compute), and reasoning blocks are omitted automatically when switching to a provider that can't replay them. API keys now come from a closure so apps can fetch short-lived tokens, the library runs on Linux, and swift-huggingface and swift-xet 1.0 ship alongside it for faster model downloads from the Hub.
- Introducing AnyLanguageModel: One API for Local and Remote LLMs on Apple Platforms
Hugging Face mediumAdeptAnyLanguageModel is a Swift package that mirrors Apple's Foundation Models API, so a single `import` swap lets apps target Apple's system model, Core ML, MLX, llama.cpp, Ollama, or cloud providers (OpenAI, Anthropic, Gemini, Hugging Face Inference Providers) through the same session-based interface. Building on Foundation Models rather than a new abstraction keeps the API minimal and familiar. Swift 6.1 package traits let developers opt in only to the heavy local backends they need. The library already extends the API with image inputs for vision-language models, and tool calling, MCP and guided generation across providers are planned before 1.0.
- Using Stable Diffusion with Core ML on Apple Silicon
Hugging Face mediumAdeptStable Diffusion can run on Apple Silicon via Core ML using Apple's ml-stable-diffusion repo, with pre-converted checkpoints for v1.4, v1.5, v2 base and v2.1 base hosted on the Hugging Face Hub. Each model comes in 'original' attention (CPU/GPU only, sometimes faster) or 'split_einsum' attention (also runs on the Neural Engine), packaged either as ML Packages for Python or compiled mlmodelc bundles for Swift. The best result on an M1 Max was 18s per image, using original attention with all compute units on macOS Ventura 13.1, which is required to avoid black images. Step-by-step download and inference commands are given for Python and Swift, plus a pointer to Apple's conversion script for custom fine-tuned models.