hex/ios/HEXShared/CleanupTagger.swift
Ryan Vogel efb7dcafb5
iOS: clean up what I say with an on-device tagger (experiment) (#2023)
* ios: clean up what I say with Hex's on-device dictation tagger (experiment)

What changed
- Experiments › Clean up what I say (off by default, App Group key
  experiments.cleanupTagger): Hex's trained cleanup tagger removes ums,
  repeats, and the words a spoken correction replaces ("3pm, actually 4pm"),
  on the iPhone, in in-keyboard dictation and in app dictation. It runs after
  transcription and learned fixes (in the app also after custom vocabulary),
  before Apple Intelligence cleanup, translation, and formatting, and never
  for Verbatim. When both cleanups are on, Apple Intelligence now receives the
  tagged text instead of the raw transcript; nothing else changes while the
  experiment is off.
- Model: the BERT L-4/H-512 token classifier (ckpt-1200k-l4h512) converted to
  an int8 Core ML ML Program (coremltools 9, fp16 compute, enumerated lengths
  16/32/64/128 plus a length input that masks the padding; the output is
  logit(DELETE) - logit(KEEP)), 28.5 MB, with its WordPiece vocabulary. Both
  ship once, in HEXKeyboard.appex (HEXKeyboard/CleanupTagger/), and the app
  loads them from its embedded keyboard extension. Core ML codegen is off for
  the keyboard target.
- HEXShared: CleanupTokenizer (Hugging Face BertTokenizer: special tokens,
  BertNormalizer, BertPreTokenizer, WordPiece, byte-exact vocabulary keys,
  HF's Unicode 8.0 character classes), CleanupReconstruction (a port of
  js/reconstruct.js with JavaScript's UTF-16 casing), and CleanupTagger
  (chunks of whole words within 128 tokens, P(DELETE) > 0.98, one serial
  queue, 150 ms budget). A timeout, an error, or a result with no words left
  keeps the transcript. The model loads when a dictation starts recording with
  the experiment on, stays loaded, and is freed on a memory warning. It runs
  on the Neural Engine or the CPU, never the GPU.
- Call sites: one each in KeyboardLocalDictation (after DictationCorrections,
  before translation) and DictationController (after VocabularySpelling,
  before the Apple Intelligence pass). prepareIfEnabled runs at recording
  start, in KeyboardViewController and in DictationController.startRecording.

Why
- In the simulator the first load took 0.15 s on the CPU and 0.8 s on the GPU
  path, both over the 150 ms budget. So loading starts when recording starts,
  and the GPU isn't used (it also added 30 MB of footprint, against 3 MB on the
  CPU, which keeps the int8 weights memory-mapped).
- Threshold 0.98, not the exported 0.95: 1/45 false deletes on hand-written
  clean sentences against 3/45, with the same self-correction accuracy.

Verified
- xcodebuild test on the iPhone 18 Pro simulator: 168 tests, 0 failures,
  including the new CleanupTaggerTests and DictationControllerTests
  testCleanupTaggerRunsAfterLearnedFixesAndSkipsVerbatim. After the fixtures
  were rewritten one case per line (same content), CleanupTaggerTests and
  DictationControllerTests passed again (28 tests).
- Parity on the 137 hand-written sentences at 0.98: labels match the fp32
  checkpoint on all 1,128 words, and 136/137 outputs match the ONNX int8
  pipeline. #120 ("The cat keeps trying to scratch that chair.") differs on
  two words that ONNX keeps (P 0.9725/0.9794) and Core ML deletes (0.984).
  The fp32 checkpoint also deletes them (0.9879/0.9853), so the difference
  comes from ONNX's int8 activations. 128/137 outputs match the hand-written
  targets. The tokenizer matches HF on 1,891 words, and reconstruction matches
  reconstruct.js on 670 cases.
- Simulator, CPU path: load 140-161 ms, first sentence after load about 8 ms,
  then 3.3 ms median and 7.5 ms p95 per sentence; phys_footprint +2.9 MB.
- The Release generic iOS build with CODE_SIGNING_ALLOWED=NO succeeds.
  HEXKeyboard.appex is 30.2 MB (model 27.8 MB, vocabulary 0.2 MB).
- Not verified on a physical iPhone: Neural Engine load time, latency, and the
  keyboard extension's footprint next to Apple Speech.

Note: this adds a 28.4 MB binary (weight.bin) to git; the repo has no LFS.

* Fetch the cleanup tagger's Core ML model at build time instead of committing it

The 27 MB mlpackage would have been ~30x the largest file in the repo, kept in history forever. It is now the ios-model-cleanup-tagger-1 release asset; ios/scripts/fetch-cleanup-model.sh downloads it, checks its SHA-256, and skips when the pinned copy is already in place. Xcode Cloud runs it in ci_post_clone.sh before xcodegen; the folder is gitignored.

Verified the fetched package is byte-identical to the one the tests ran against (diff -r), and a second run is a no-op.
2026-11-20 01:29:41 +00:00

299 lines
12 KiB
Swift

import CoreML
import Foundation
import UIKit
/// The Clean up what I say experiment's switch in the shared App Group.
nonisolated struct CleanupTaggerSettings {
private enum Key {
static let enabled = "experiments.cleanupTagger"
}
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// Experiment: an on-device tagger removes ums, repeats, and spoken corrections after
/// dictation. Off by default.
var isEnabled: Bool {
get { defaults?.bool(forKey: Key.enabled) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.enabled) }
}
}
/// Experiment (Clean up what I say): a small BERT token classifier (4 layers, 512 wide,
/// int8 Core ML weights) labels each spoken word KEEP or DELETE, and
/// `CleanupReconstruction` joins what is kept. It runs on this iPhone only.
///
/// The model and vocabulary ship once, inside the keyboard extension; the app reads them
/// from its embedded `HEXKeyboard.appex`. The model loads when a dictation starts
/// recording with the experiment on (`prepareIfEnabled`), so the load (about 150 ms on the
/// CPU in the simulator) is over before the transcript arrives, stays loaded while the
/// process lives, and is released on a memory warning.
nonisolated final class CleanupTagger: @unchecked Sendable {
static let shared = CleanupTagger()
/// P(DELETE) a word must exceed to be removed (1/45 false deletes on hand-written
/// clean sentences, against 3/45 at 0.95, with the same self-correction accuracy).
static let deleteThreshold = 0.98
/// Longest Core ML input, [CLS] and [SEP] included; longer transcripts are tagged in
/// chunks of whole words.
static let maxTokens = 128
/// The model's enumerated sequence lengths; each chunk is padded to the next one.
static let sequenceLengths = [16, 32, 64, 128]
/// Dictation waits this long for a cleanup before inserting the transcript as heard.
static let timeBudget: TimeInterval = 0.150
static let modelName = "HexCleanupTagger"
static let vocabularyName = "HexCleanupTaggerVocab"
private static let keyboardBundleName = "HEXKeyboard.appex"
enum TaggerError: Error {
case missingResource(String)
case badOutput
}
private struct Loaded {
let model: MLModel
let tokenizer: CleanupTokenizer
}
/// Owns `loaded`: every load, prediction, and release runs here, one at a time.
private let queue = DispatchQueue(label: "anomaly.hex.cleanup-tagger", qos: .userInitiated)
private let computeUnits: MLComputeUnits
private var loaded: Loaded?
private var memoryWarningObserver: NSObjectProtocol?
/// The Neural Engine where there is one, else the CPU. Not the GPU: in the simulator its
/// Metal pipeline took 0.8 s to load and added 30 MB to the footprint, while the CPU path
/// loads in 0.15 s and adds 3 MB, keeping the int8 weights memory-mapped.
init(computeUnits: MLComputeUnits = .cpuAndNeuralEngine) {
self.computeUnits = computeUnits
memoryWarningObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didReceiveMemoryWarningNotification,
object: nil,
queue: nil
) { [weak self] _ in
self?.unload()
}
}
deinit {
if let memoryWarningObserver {
NotificationCenter.default.removeObserver(memoryWarningObserver)
}
}
/// Whether the model is in memory now.
var isLoaded: Bool {
queue.sync { loaded != nil }
}
/// Loads the model in the background if it isn't loaded yet.
func prepare() {
queue.async {
_ = try? self.loadedOnQueue()
}
}
/// Frees the model; the next cleanup loads it again.
func unload() {
queue.async {
self.loaded = nil
}
}
/// The cleaned transcript, or `text` itself when nothing is removed. Blocks the caller
/// until the model has loaded and run; never call it on the main thread.
func clean(_ text: String) throws -> String {
try queue.sync { try cleanOnQueue(text) }
}
/// P(DELETE) for each whitespace-separated word, in order.
func deleteProbabilities(for words: [String]) throws -> [Double] {
try queue.sync { try probabilitiesOnQueue(words) }
}
/// The cleaned transcript within `budget`, else `text` unchanged: any failure, a late
/// answer, or a cleanup that removes every word keeps the transcript as heard. A late
/// run still finishes in the background, so the model is ready for the next dictation.
func clean(_ text: String, within budget: TimeInterval) async -> String {
let gate = CleanupTaggerOnceGate()
return await withCheckedContinuation { (continuation: CheckedContinuation<String, Never>) in
queue.async {
let cleaned = (try? self.cleanOnQueue(text)) ?? text
let result = cleaned.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? text : cleaned
if gate.claim() {
continuation.resume(returning: result)
}
}
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + budget) {
if gate.claim() {
continuation.resume(returning: text)
}
}
}
}
/// Clean up what I say, as dictation runs it: after transcription and learned fixes,
/// before translation and formatting. Off, Verbatim, or blank keeps `text` as is.
static func cleanIfEnabled(
_ text: String,
style: DictationStyle,
settings: CleanupTaggerSettings = CleanupTaggerSettings(),
tagger: CleanupTagger = .shared
) async -> String {
guard settings.isEnabled,
style != .verbatim,
!text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
return text
}
return await tagger.clean(text, within: timeBudget)
}
/// Starts loading the model when a dictation begins recording, so cleanup is ready by
/// the time the transcript is. Does nothing while the experiment is off or for Verbatim.
static func prepareIfEnabled(
style: DictationStyle,
settings: CleanupTaggerSettings = CleanupTaggerSettings(),
tagger: CleanupTagger = .shared
) {
guard settings.isEnabled, style != .verbatim else { return }
tagger.prepare()
}
// MARK: On the queue
private func cleanOnQueue(_ text: String) throws -> String {
let words = Self.words(in: text)
guard !words.isEmpty else { return text }
let deletes = try probabilitiesOnQueue(words).map { $0 > Self.deleteThreshold }
guard deletes.contains(true) else { return text }
return CleanupReconstruction.reconstruct(words, deletes: deletes)
}
private func probabilitiesOnQueue(_ words: [String]) throws -> [Double] {
let state = try loadedOnQueue()
var probabilities: [Double] = []
probabilities.reserveCapacity(words.count)
for chunk in Self.chunks(words.map(state.tokenizer.encode)) {
probabilities += try predict(chunk, model: state.model)
}
return probabilities
}
private func loadedOnQueue() throws -> Loaded {
if let loaded { return loaded }
guard let modelURL = Self.resourceURL(Self.modelName, withExtension: "mlmodelc") else {
throw TaggerError.missingResource(Self.modelName)
}
guard let vocabularyURL = Self.resourceURL(Self.vocabularyName, withExtension: "txt") else {
throw TaggerError.missingResource(Self.vocabularyName)
}
let tokenizer = try CleanupTokenizer(vocabulary: String(contentsOf: vocabularyURL, encoding: .utf8))
let configuration = MLModelConfiguration()
configuration.computeUnits = computeUnits
let model = try MLModel(contentsOf: modelURL, configuration: configuration)
let state = Loaded(model: model, tokenizer: tokenizer)
loaded = state
return state
}
/// One model run: [CLS] ids [SEP], zero-padded to an enumerated length, with the real
/// length masking the padding. Returns P(DELETE) at each word's first subword.
private func predict(_ chunk: Chunk, model: MLModel) throws -> [Double] {
let count = chunk.ids.count
guard let length = Self.sequenceLengths.first(where: { $0 >= count }) else {
throw TaggerError.badOutput
}
let inputIDs = try MLMultiArray(shape: [1, NSNumber(value: length)], dataType: .int32)
inputIDs.withUnsafeMutableBufferPointer(ofType: Int32.self) { buffer, strides in
let step = strides[1]
for position in 0..<length {
buffer[position * step] = position < count ? chunk.ids[position] : CleanupTokenizer.padID
}
}
let realLength = try MLMultiArray(shape: [1], dataType: .int32)
realLength[0] = NSNumber(value: count)
let input = try MLDictionaryFeatureProvider(dictionary: [
"input_ids": MLFeatureValue(multiArray: inputIDs),
"length": MLFeatureValue(multiArray: realLength),
])
let output = try model.prediction(from: input)
guard let logits = output.featureValue(for: "delete_logit")?.multiArrayValue,
logits.count >= count else {
throw TaggerError.badOutput
}
return chunk.firstTokens.map { position in
guard let position else { return 0 } // a word with no tokens is kept
let logit = logits[[0, NSNumber(value: position)]].doubleValue
return 1 / (1 + exp(-logit))
}
}
// MARK: Words and chunks
struct Chunk: Equatable {
/// [CLS], the words' WordPiece ids, [SEP].
var ids: [Int32]
/// Where each word's first subword sits in `ids`; nil for a word with no tokens.
var firstTokens: [Int?]
}
/// The transcript's whitespace-separated words.
static func words(in text: String) -> [String] {
text.split(whereSeparator: \.isWhitespace).map(String.init)
}
/// Whole words per chunk, each chunk within `maxTokens` with [CLS] and [SEP]. A single
/// word longer than that keeps only its first tokens; only its first subword is scored.
static func chunks(_ words: [[Int32]]) -> [Chunk] {
var chunks: [Chunk] = []
var current = Chunk(ids: [CleanupTokenizer.clsID], firstTokens: [])
for var tokens in words {
if !current.firstTokens.isEmpty, current.ids.count + 1 + tokens.count > maxTokens {
current.ids.append(CleanupTokenizer.sepID)
chunks.append(current)
current = Chunk(ids: [CleanupTokenizer.clsID], firstTokens: [])
}
if tokens.count > maxTokens - 2 {
tokens = Array(tokens.prefix(maxTokens - 2))
}
current.firstTokens.append(tokens.isEmpty ? nil : current.ids.count)
current.ids += tokens
}
if !current.firstTokens.isEmpty {
current.ids.append(CleanupTokenizer.sepID)
chunks.append(current)
}
return chunks
}
/// The keyboard extension finds the model in its own bundle, the app in the keyboard
/// extension it embeds.
static func resourceURL(_ name: String, withExtension ext: String) -> URL? {
if let url = Bundle.main.url(forResource: name, withExtension: ext) {
return url
}
guard let plugIns = Bundle.main.builtInPlugInsURL,
let keyboard = Bundle(url: plugIns.appendingPathComponent(keyboardBundleName)) else {
return nil
}
return keyboard.url(forResource: name, withExtension: ext)
}
}
/// Resumes a cleanup race exactly once, whichever of the tagger or the budget finishes first.
private final class CleanupTaggerOnceGate: @unchecked Sendable {
private let lock = NSLock()
private var claimed = false
func claim() -> Bool {
lock.lock()
defer { lock.unlock() }
guard !claimed else { return false }
claimed = true
return true
}
}