Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisMik committed Apr 19, 2024
1 parent ab8b6e9 commit eba2654
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 37 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ios-demos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: iOS Demos

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- 'demo/ios/**'
- '.github/workflows/ios-demos.yml'
pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- 'demo/ios/**'
- '.github/workflows/ios-demos.yml'

defaults:
run:
working-directory: demo/ios/PicoLLMDemo

jobs:
build:
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Cocoapods
run: gem install cocoapods

- name: Run Cocoapods
run: pod install

- name: Build
run: xcrun xcodebuild build
-configuration Debug
-workspace PicoLLMDemo.xcworkspace
-sdk iphoneos
-scheme PicoLLMDemo
-derivedDataPath ddp
CODE_SIGNING_ALLOWED=NO
26 changes: 26 additions & 0 deletions .github/workflows/swift-codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Swift Codestyle

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- '**/*.swift'
- '.github/workflows/swift-codestyle.yml'
pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- '**/*.swift'
- '.github/workflows/swift-codestyle.yml'

jobs:
check-switch-codestyle:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Check swift codestyle
uses: norio-nomura/[email protected]
with:
args: lint --config resources/.lint/swift/.swiftlint.yml --strict
12 changes: 7 additions & 5 deletions binding/ios/PicoLLM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum PicoLLMEndpoint {
case endOfSentence
case completionTokenLimitReached
case stopPhraseEncountered

public static func fromC(cEndpoint: pv_picollm_endpoint_t) -> PicoLLMEndpoint? {
switch cEndpoint {
case PV_PICOLLM_ENDPOINT_END_OF_SENTENCE:
Expand Down Expand Up @@ -88,9 +88,9 @@ public struct PicoLLMCompletion {
}
}

private var streamCallbackFunc: ((String) -> Void)? = nil
private var streamCallbackFunc: ((String) -> Void)?
private func cStreamCallback (completion: UnsafePointer<CChar>?) {
if (streamCallbackFunc != nil) {
if streamCallbackFunc != nil {
streamCallbackFunc!(String(cString: completion!))
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public class PicoLLM {
handle = nil
}
}

public func generate(
prompt: String,
completionTokenLimit: Int32 = -1,
Expand Down Expand Up @@ -221,7 +221,9 @@ public class PicoLLM {
token: String(cString: cCompletionToken.token.token),
logProb: cCompletionToken.token.log_prob)
var topChoices = [PicoLLMToken]()
for cTopChoice in UnsafeBufferPointer(start: cCompletionToken.top_choices, count: Int(cCompletionToken.num_top_choices)) {
for cTopChoice in UnsafeBufferPointer(
start: cCompletionToken.top_choices,
count: Int(cCompletionToken.num_top_choices)) {
let topChoice = PicoLLMToken(
token: String(cString: cTopChoice.token),
logProb: cTopChoice.log_prob)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import XCTest
import PicoLLM

class PicoLLMAppTestUITests: BaseTest {

func testInitSuccess() throws {
var modelPath: String = ""
try downloadFileSync(url: modelURL!) { (path, _) in
modelPath = path!
}

let m = try PicoLLM.init(accessKey: accessKey, modelPath: modelPath)
XCTAssert(PicoLLM.version != "")
XCTAssert(try m.model() != "")
Expand All @@ -31,12 +31,12 @@ class PicoLLMAppTestUITests: BaseTest {
try downloadFileSync(url: modelURL!) { (path, _) in
modelPath = path!
}

let m = try PicoLLM.init(accessKey: accessKey, modelPath: modelPath)

let result = try m.generate(prompt: "Hello my name is", completionTokenLimit: 10)
XCTAssert(result.completion == " John and I am a student at XYZ school")

m.delete()
}
}
10 changes: 5 additions & 5 deletions demo/ios/PicoLLMDemo/PicoLLMDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI

struct ContentView: View {
@StateObject var viewModel = ViewModel()

let activeBlue = Color(red: 55/255, green: 125/255, blue: 1, opacity: 1)
let dangerRed = Color(red: 1, green: 14/255, blue: 14/255, opacity: 1)
let secondaryGrey = Color(red: 118/255, green: 131/255, blue: 142/255, opacity: 1)
Expand Down Expand Up @@ -39,7 +39,7 @@ struct ContentView: View {
)
.padding(12)
.disabled(isError || !viewModel.enableLoadModelButton)

Button(action: viewModel.generate) {
Text("Generate")
.background(btnColor)
Expand All @@ -64,11 +64,11 @@ struct ContentView: View {
case .success(let files):
viewModel.selectedModelUrl = files[0]
viewModel.loadPicollm()
case .failure(_):
case .failure:
break
}
}

Text(viewModel.completionText)
.frame(minWidth: 0, maxWidth: UIScreen.main.bounds.width - 50)
.padding(.vertical, 10)
Expand All @@ -77,7 +77,7 @@ struct ContentView: View {
.cornerRadius(.infinity)

Spacer()

Text(viewModel.statsText)
.frame(minWidth: 0, maxWidth: UIScreen.main.bounds.width - 50)
.padding(.vertical, 10)
Expand Down
44 changes: 22 additions & 22 deletions demo/ios/PicoLLMDemo/PicoLLMDemo/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ import Combine
class ViewModel: ObservableObject {

private let ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}"

private var picollm: PicoLLM?

private var timerTick = CFAbsoluteTimeGetCurrent()
private var timerTock = CFAbsoluteTimeGetCurrent()
private var numTokens = 0

@Published var promptText = ""

@Published var completionText = ""
@Published var statsText = ""

@Published var errorMessage = ""

@Published var showFileImporter = false
@Published var enableLoadModelButton = true
@Published var enableGenerateButton = false
@Published var selectedModelUrl: URL? = nil
@Published var selectedModelUrl: URL?

deinit {
if (picollm != nil) {
if picollm != nil {
picollm!.delete()
}
}

public func extractModelFile() {
showFileImporter = true
}
Expand All @@ -46,14 +46,14 @@ class ViewModel: ObservableObject {
completionText = "Loading Picollm..."
enableLoadModelButton = false
enableGenerateButton = false

let modelAccess = selectedModelUrl!.startAccessingSecurityScopedResource()
if (!modelAccess) {
if !modelAccess {
errorMessage = "Cann't get permissions to access model file"
enableLoadModelButton = true
return
}

DispatchQueue.global(qos: .userInitiated).async { [self] in
do {
picollm = try PicoLLM(accessKey: ACCESS_KEY, modelPath: selectedModelUrl!.path)
Expand All @@ -67,31 +67,31 @@ class ViewModel: ObservableObject {
enableLoadModelButton = true
}
}

DispatchQueue.main.async { [self] in
selectedModelUrl!.stopAccessingSecurityScopedResource()
}
}
}

private func streamCallback(completion: String) {
DispatchQueue.main.async { [self] in
completionText += completion
if (numTokens == 0) {

if numTokens == 0 {
timerTick = CFAbsoluteTimeGetCurrent()
}

timerTock = CFAbsoluteTimeGetCurrent()
numTokens += 1
}
}

public func generate() {
enableGenerateButton = false
completionText = promptText
numTokens = 0

DispatchQueue.global(qos: .userInitiated).async { [self] in
do {
let result = try picollm!.generate(
Expand All @@ -107,24 +107,24 @@ class ViewModel: ObservableObject {
enableLoadModelButton = true
}
}

DispatchQueue.main.async { [self] in
enableGenerateButton = true
}
}
}

public func updateStats(result: PicoLLMCompletion) {
let secondsElapsed: Double = (timerTock - timerTick)
let tokensPerSecond: Double = Double(numTokens) / secondsElapsed

statsText = """
Usage:
\t \(result.usage.promptTokens) prompt tokens
\t \(result.usage.completionTokens) completion tokens
Perormance:
\t \(tokensPerSecond) tokens per second
"""

}
}
11 changes: 11 additions & 0 deletions resources/.lint/swift/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
disabled_rules:
- identifier_name
- function_body_length
- force_cast
- implicit_getter
- cyclomatic_complexity
- function_parameter_count
- file_length
- type_body_length
excluded:
- ${PWD}/**/Pods

0 comments on commit eba2654

Please sign in to comment.