Skip to content

Commit 3997ae9

Browse files
metascroyjackzhxng
andauthored
support phi4 in ios demo app (#10659)
As titled --------- Co-authored-by: Jack <32371937+jackzhxng@users.noreply.github.com>
1 parent 7ba879d commit 3997ae9

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ You are a helpful assistant.
2525
"""
2626

2727
public static let llama3PromptTemplate = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>%@<|eot_id|><|start_header_id|>assistant<|end_header_id|>"
28+
29+
public static let phi4PromptTemplate = "<|user|>%@<|end|><|assistant|>"
2830
}

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ContentView.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct ContentView: View {
8686
case llama
8787
case llava
8888
case qwen3
89+
case phi4
8990

9091
static func fromPath(_ path: String) -> ModelType {
9192
let filename = (path as NSString).lastPathComponent.lowercased()
@@ -95,8 +96,10 @@ struct ContentView: View {
9596
return .llava
9697
} else if filename.hasPrefix("qwen3") {
9798
return .qwen3
99+
} else if filename.hasPrefix("phi4") {
100+
return .phi4
98101
}
99-
print("Unknown model type in path: \(path). Model filename should start with one of: llama, llava, or qwen3")
102+
print("Unknown model type in path: \(path). Model filename should start with one of: llama, llava, qwen3, or phi4")
100103
exit(1)
101104
}
102105
}
@@ -343,15 +346,15 @@ struct ContentView: View {
343346
}
344347

345348
switch modelType {
346-
case .llama, .qwen3:
349+
case .llama, .qwen3, .phi4:
347350
runnerHolder.runner = runnerHolder.runner ?? Runner(modelPath: modelPath, tokenizerPath: tokenizerPath)
348351
case .llava:
349352
runnerHolder.llavaRunner = runnerHolder.llavaRunner ?? LLaVARunner(modelPath: modelPath, tokenizerPath: tokenizerPath)
350353
}
351354

352355
guard !shouldStopGenerating else { return }
353356
switch modelType {
354-
case .llama, .qwen3:
357+
case .llama, .qwen3, .phi4:
355358
if let runner = runnerHolder.runner, !runner.isLoaded() {
356359
var error: Error?
357360
let startLoadTime = Date()
@@ -474,12 +477,14 @@ struct ContentView: View {
474477
prompt = String(format: Constants.llama3PromptTemplate, text)
475478
case .llava:
476479
prompt = String(format: Constants.llama3PromptTemplate, text)
480+
case .phi4:
481+
prompt = String(format: Constants.phi4PromptTemplate, text)
477482
}
478483

479484
try runnerHolder.runner?.generate(prompt, sequenceLength: seq_len) { token in
480485

481486
if token != prompt {
482-
if token == "<|eot_id|>" {
487+
if token == "<|eot_id|>" {
483488
// hack to fix the issue that extension/llm/runner/text_token_generator.h
484489
// keeps generating after <|eot_id|>
485490
shouldStopShowingToken = true

examples/demo-apps/apple_ios/LLaMA/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ Download already exported LLaMA/LLaVA models along with tokenizers from [Hugging
3232
```bash
3333
open examples/demo-apps/apple_ios/LLaMA/LLaMA.xcodeproj
3434
```
35-
35+
3636
3. Click the Play button to launch the app in the Simulator.
3737

3838
4. To run on a device, ensure you have it set up for development and a provisioning profile with the `increased-memory-limit` entitlement. Update the app's bundle identifier to match your provisioning profile with the required capability.
3939
40-
5. After successfully launching the app, copy the exported ExecuTorch model (`.pte`) and tokenizer (`.model`) files to the iLLaMA folder. Three models are currently supported at the moment - Llama, Qwen3, and Llava multimodal. Please ensure that your model `.pte` file starts with `llama`, `qwen3`, or `llava` so that the app selects the correct model type.
40+
5. After successfully launching the app, copy the exported ExecuTorch model (`.pte`) and tokenizer (`.model`) files to the iLLaMA folder. Four models are currently supported at the moment - Llama, Qwen3, Phi4-mini, and Llava multimodal. Please ensure that your model `.pte` file starts with `llama`, `qwen3`, `phi4` or `llava` so that the app selects the correct model type.
4141
4242
- **For the Simulator:** Drag and drop both files onto the Simulator window and save them in the `On My iPhone > iLLaMA` folder.
4343
- **For a Device:** Open a separate Finder window, navigate to the Files tab, drag and drop both files into the iLLaMA folder, and wait for the copying to finish.

0 commit comments

Comments
 (0)