Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 13 min read How-to mediumAdept

WWDC 24: Running Mistral 7B with Core ML

Mistral 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.

Related posts

  • Releasing Swift Transformers: Run On-Device LLMs in Apple Devices

    Hugging Face mediumAdept

    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.

  • Faster Stable Diffusion with Core ML on iPhone, iPad, and Mac

    Hugging Face mediumAdept

    Core ML in iOS 17 and macOS 14 adds 6-bit weight palettization for running Stable Diffusion faster and with less memory. Weights are indices into a lookup table and are decompressed layer by layer during inference rather than at load time, which cuts memory transfer on the critical path. Apple's ml-stable-diffusion now supports a --quantize-nbits conversion flag (8/6/4/2 bits) and a SPLIT_EINSUM_V2 attention variant that chunks queries into 512-length pieces for 10–30% Neural Engine speedups. Pre-converted 6-bit models for SD 1.4–2.1 are on the Hub, alongside conversion commands for custom models and a note that training-time compression can reach 4 or 2 bits with less quality loss.

  • Swift Transformers Reaches 1.0 – and Looks to the Future

    Hugging Face easyNovice

    swift-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.

  • Stable Diffusion XL on Mac with Advanced Core ML Quantization

    Hugging Face mediumAdept

    Stable Diffusion XL base has been ported to Core ML for Apple Silicon Macs (macOS 14 beta), with end-to-end latency ranging from 46 s on an M1 Max to 20 s on an M2 Ultra using CPU+GPU and the ORIGINAL attention implementation. The main technique is mixed-bit palettization, a post-training quantization method that picks a bit depth per layer (1, 2, 4, 8 or float16) based on each layer's measured PSNR impact against the float16 model. Recipes at 6.55, 4.50 and 3.41 bits per parameter all beat naive 8-bit linear quantization on PSNR, and the 4.5-bit recipe shrinks the UNet from 4.8 GB to 1.4 GB. Aggressive compression can degrade prompt adherence in ways PSNR does not capture. Analysis and apply scripts plus precomputed recipes for SD 1.5, 2.1 and XL are published.

  • Using Stable Diffusion with Core ML on Apple Silicon

    Hugging Face mediumAdept

    Stable 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.

The quest for software engineering knowledge.

Look:
Layout: