Skip to content
项目

关于

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.

LiteRTDemo on iPhone 17 Pro

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:

Tool calling through the Foundation Models API, on-device

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

Projects using swift-litert-lm

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.

探索相关项目

加载中…