john-rocky/swift-litert-lm
概要
Gemma 4 on iPhone as a Swift package: one dependency line, five lines of code, ~50 tok/s on iPhone 17 Pro. Metal GPU, text + image + audio, in-app download. Also an Apple Foundation Models backend (iOS 27).
デザイン・メディア計測したスター増加
- 24時間
- 0
- 7日間
- -1
- 15日間
- -1
- 1か月
- 履歴を収集中
期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。
README.md
オリジナルswift-litert-lm
Gemma 4 on iPhone as a Swift package. On 2026-11-05, Gemma 4 E2B decoded at about 88 tokens per second once warm, on an iPhone 18 Pro with LiteRT-LM v0.19.0. Add one dependency and your app downloads the model, then chats on the Metal GPU with text, images and audio. Other .litertlm models load the same way, and the package can also act as a backend for Apple's Foundation Models API on iOS 27.
Gemma 4 E2B on an iPhone, with text, photos, microphone audio and video frames in one chat.
This is a community package over Google's LiteRT-LM runtime. It downloads the model, checks that the device has the memory for it and sets up the GPU. Fetching LiteRT-LM as a SwiftPM dependency needs git-lfs, and this package does not. It is not affiliated with Google or Apple.
Quickstart (Easy mode)
The simplest way in is LiteRTChat. In Xcode, paste the repo URL into File → Add Package Dependencies… and add the LiteRTFoundation product. In a Package.swift:
.package(url: "https://github.com/john-rocky/swift-litert-lm", from: "0.3.1")
Then stream a Gemma 4 chat. The first run downloads about 2.6 GB from Hugging Face, and later runs reuse it:
import LiteRTFoundation
let chat = try await LiteRTChat(.gemma4_E2B) { progress in
print("Downloading model… \(Int(progress.fraction * 100))%")
}
for try await token in chat.stream("Explain quantum computing in one sentence.") {
print(token, terminator: "")
}
Run it on a physical device with at least 7 GB of RAM. Below that, LiteRTChat refuses to load the model instead of letting iOS kill the app, and the iOS Simulator falls back to the CPU.
On an iPhone 18 Pro with iOS 27.2 and LiteRT-LM v0.19.0, Gemma 4 E2B decodes at about 88 tok/s once warm.
For a photo, or for text, image and audio together:
let chat = try await LiteRTChat(.gemma4_E2B, modalities: .textImage)
let answer = try await chat.respond("What's in this photo?", image: jpegData)
let chat = try await LiteRTChat(.gemma4_E2B, modalities: .all)
let answer = try await chat.respond("Transcribe and summarize.", audio: .file(wavURL))
Video goes in as frames. VideoFrameSampler samples them, and respond takes them as images:.
FM mode: a backend for Apple's Foundation Models API
Apple opened Foundation Models to custom backends in iOS 27. LiteRTLanguageModel drives LiteRT-LM through LanguageModelSession(model:), with @Generable output, tool calling and image attachments:
import FoundationModels
import LiteRTFoundation
let model = try await LiteRTLanguageModel(.gemma4_E2B)
let session = LanguageModelSession(model: model)
// Plain text.
let answer = try await session.respond(to: "Explain on-device AI in one sentence.")
// Structured output. PrimaryColors is your own @Generable type.
let colors = try await session.respond(generating: PrimaryColors.self) {
"List three primary colors."
}
// Tools. WeatherTool is your own Tool, and the framework runs it for the model.
let withTool = LanguageModelSession(model: model, tools: [WeatherTool()])
let weather = try await withTool.respond(to: "What's the weather in Tokyo?")
Audio and video only work in Easy mode, because the Foundation Models API has no way to pass them.
Before iOS 27, anylanguagemodel-litert-lm gives you the same LanguageModelSession API on iOS 17+ or macOS 14+, through AnyLanguageModel.
Google merged this adapter into LiteRT-LM in July 2026.
Sample app
Samples/LiteRTDemo is a SwiftUI chat with photo, microphone and video attachments. Its FM API screen runs the same model through Foundation Models, including a live tool call:
cd Samples/LiteRTDemo
open LiteRTDemo.xcodeproj # set your DEVELOPMENT_TEAM, run on a device
Shipping the model
Gemma 4 E2B is about 2.6 GB, so don't commit it or bundle it in your app. By default, the package downloads the .litertlm on first launch into Application Support/LiteRTModels, excluded from iCloud backup, and reuses it afterward.
Any .litertlm on Hugging Face works:
let chat = try await LiteRTChat(
huggingFaceRepo: "litert-community/gemma-4-E4B-it-litert-lm",
fileName: "gemma-4-E4B-it.litertlm") // text-only by default; pass modalities: .all if it has them
To load a file that is already on the device:
// Easy mode.
let chat = try await LiteRTChat(modelFileURL: url, modalities: .all)
// FM mode.
let model = try LiteRTLanguageModel(modelFileURL: url)
let session = LanguageModelSession(model: model)
Requirements
- iOS 16+ / macOS 13+ for Easy mode. FM mode requires the iOS 27 SDK.
- An iPhone or a Mac for Metal GPU inference, not the Simulator.
- No memory entitlement: the sample app has none.
- Version 0.3.1 runs LiteRT-LM v0.19.1. Version 0.3.0 runs LiteRT-LM v0.19.0.
Documentation
- Fine-tuned model to iPhone: convert with hf-to-litertlm, add it to an app, verify
- Other ways to ship the model: your own host, bundling, On-Demand Resources
- LiteRT-LM v0.19.0: what changes for an app, and what to do after a turn throws
- Verified on device: the full table and how it was measured
- Sample app details: self-tests and the demo-video mode
- What Easy mode handles: the downloader, memory checks and the model catalog
- Why the wrapper is vendored
- Roadmap
Projects using swift-litert-lm
- VivaDicta: speech-to-text for iOS and watchOS, with Gemma 4 on-device from a fork of this package
- EyePal: an assistive iOS app that captions images and answers questions about them for people with visual impairments
License
Apache-2.0. Includes software developed by Google LLC (the vendored LiteRT-LM Swift wrapper and the prebuilt runtime binaries). See LICENSE and NOTICE. Each model keeps its own license.
関連プロジェクトを探す
読み込み中…