Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0929b2f

Browse files
authored
Merge pull request bigcode-project#1 from pcuenca/swift-app
Swift app
2 parents e46c9a0 + 313959e commit 0929b2f

File tree

20 files changed

+1708
-0
lines changed

20 files changed

+1708
-0
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ models/*
2121

2222
arm_neon.h
2323
compile_commands.json
24+
25+
project.xcworkspace/xcuserdata/
26+
xcuserdata/

‎README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ You can also try to quantize the `ggml` models via 4-bit integer quantization.
109109
| --- | --- | --- | --- |
110110
| `bigcode/gpt_bigcode-santacoder` | 5396.45 MB | 1026.83 MB | 4-bit integer (q4_1) |
111111
| `bigcode/starcoder` | 71628.23 MB | 13596.23 MB | 4-bit integer (q4_1) |
112+
113+
## iOS App
114+
115+
The repo includes a proof-of-concept iOS app in the `StarCoderApp` directory. You need to provide the converted (and possibly quantized) model weights, placing a file called `bigcode_ggml_model.bin.bin` inside that folder. This is what it looks like on an iPhone:
116+
117+
![starcoder-ios-screenshot](assets/starcoder-ios.png)
118+

‎StarCoderApp/StarCoder.xcodeproj/project.pbxproj

Lines changed: 567 additions & 0 deletions
Large diffs are not rendered by default.

‎StarCoderApp/StarCoder.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "BigCode Logo Transparent.png",
5+
"idiom" : "universal",
6+
"platform" : "ios",
7+
"size" : "1024x1024"
8+
}
9+
],
10+
"info" : {
11+
"author" : "xcode",
12+
"version" : 1
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}

‎StarCoderApp/StarCoder/Assets.xcassets/logo.imageset/BigCode Logo.svg

Lines changed: 24 additions & 0 deletions
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"filename" : "BigCode Logo.svg",
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//
2+
// ContentView.swift
3+
// StarCoder
4+
//
5+
// Created by Pedro Cuenca on 15/5/23.
6+
//
7+
8+
import SwiftUI
9+
import Dispatch
10+
11+
class ModelState: ObservableObject {
12+
@Published var model: OpaquePointer? = nil
13+
14+
var modelPath: String {
15+
Bundle.main.path(forResource: "bigcode_ggml_model", ofType: "bin")!
16+
}
17+
18+
func load() {
19+
DispatchQueue.global(qos: .userInitiated).async {
20+
let begin = Date()
21+
let model = load_model(self.modelPath)
22+
DispatchQueue.main.async { self.model = model }
23+
print("Loaded \(String(describing: model)) in \(Date().timeIntervalSince(begin))")
24+
}
25+
}
26+
}
27+
28+
struct ContentView: View {
29+
@State private var prompt: String = "def fibonacci("
30+
@State private var generated: String = ""
31+
@State private var generating: Bool = false
32+
@State private var status: String = ""
33+
34+
@StateObject private var modelState = ModelState()
35+
private var modelIsLoaded: Bool { modelState.model != nil }
36+
37+
func listenToNotifications() {
38+
NotificationCenter.default.addObserver(forName: NSNotification.Name("decoded.token.received"), object: nil, queue: nil) { decoded in
39+
generated = generated.appending(decoded.object as! String)
40+
}
41+
}
42+
43+
func complete(from text: String) {
44+
guard let model = modelState.model else { return }
45+
46+
generating.toggle()
47+
generated = text
48+
status = ""
49+
50+
DispatchQueue.global(qos: .userInteractive).async {
51+
func token_callback(char_ptr: UnsafePointer<CChar>?) {
52+
guard let char_ptr = char_ptr else { return }
53+
54+
// We can't pass a "function that captures context" as a C function pointer, so we resort to posting a notification
55+
DispatchQueue.main.async {
56+
let str = String(cString: char_ptr)
57+
NotificationCenter.default.post(name: NSNotification.Name("decoded.token.received"), object: str)
58+
}
59+
}
60+
61+
let msPerToken = generate(model, text, token_callback)
62+
DispatchQueue.main.async {
63+
status = String(format: "%.2f ms/token", msPerToken)
64+
generating = false
65+
}
66+
}
67+
}
68+
69+
var body: some View {
70+
VStack {
71+
Image("logo").resizable().aspectRatio(contentMode: .fit)
72+
HStack {
73+
TextField("Prompt", text: $prompt, axis: .vertical).lineLimit(2...5)
74+
.textFieldStyle(.roundedBorder)
75+
Button("Complete") {
76+
complete(from: prompt)
77+
}.buttonStyle(.borderedProminent).disabled(!modelIsLoaded)
78+
}
79+
Text(generated).frame(maxWidth: .infinity, alignment: .leading)//.multilineTextAlignment(.leading)
80+
if generating {
81+
ProgressView().padding()
82+
}
83+
Spacer()
84+
if status != "" {
85+
Text(status).font(.system(size: 14)).padding().frame(maxWidth: .infinity).background(Color(white: 0.9))
86+
}
87+
}
88+
.padding()
89+
.onAppear {
90+
listenToNotifications()
91+
modelState.load()
92+
}
93+
}
94+
}
95+
96+
struct ContentView_Previews: PreviewProvider {
97+
static var previews: some View {
98+
ContentView()
99+
}
100+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+
5+
#import <starcoderlib/starcoder.h>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// StarCoderApp.swift
3+
// StarCoder
4+
//
5+
// Created by Pedro Cuenca on 15/5/23.
6+
//
7+
8+
import SwiftUI
9+
10+
@main
11+
struct StarCoderApp: App {
12+
var body: some Scene {
13+
WindowGroup {
14+
ContentView()
15+
}
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// starcoder.xcconfig
3+
// StarCoderApp
4+
//
5+
// Created by Pedro Cuenca on 15/5/23.
6+
//
7+
8+
// Configuration settings file format documentation can be found at:
9+
// https://help.apple.com/xcode/#/dev745c5c974
10+
11+
OTHER_CFLAGS = -O3 -DNDEBUG -std=c11 -fPIC -pthread -mf16c -mfma -mavx -mavx2 -DGGML_USE_ACCELERATE
12+
OTHER_CPLUSPLUSFLAGS = -O3 -DNDEBUG -std=c++11 -fPIC -pthread
13+
14+
GCC_OPTIMIZATION_LEVEL = 3
15+
ENABLE_TESTABILITY = NO
16+
DEBUG_INFORMATION_FORMAT = dwarf-with-dsym

0 commit comments

Comments
 (0)