Skip to content

Commit c9adf77

Browse files
committed
Updated swift tools, refactor
1 parent 1fa5de8 commit c9adf77

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.10.1
1+
// swift-tools-version: 6.0
22
import PackageDescription
33

44
let package = Package(
@@ -17,7 +17,7 @@ let package = Package(
1717
],
1818
dependencies: [
1919
.package(url: "https://github.com/ggerganov/llama.cpp", branch: "master"),
20-
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
20+
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.1"),
2121
],
2222
targets: [
2323
.target(
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
public struct InferError: Error {
1+
public struct InferError: Error, Sendable {
22
public let message: String
33
public let code: Code
44

5-
public enum Code: Int {
5+
public enum Code: Int, Sendable {
66
case cancelled = 1
7+
case kvCacheFailure
8+
case decodingFailure
79
}
810
}

Sources/llama-cpp-swift/InitializationError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
struct InitializationError: Error {
1+
struct InitializationError: Error, Sendable {
22
let message: String
33
let code: Code
44

5-
enum Code: Int {
5+
enum Code: Int, Sendable {
66
case failedToLoadModel = 1
77
case failedToInitializeContext
88
}

Sources/llama-cpp-swift/LLama.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Logging
33
import llama
44

5-
public actor LLama {
5+
public class LLama {
66
private let logger = Logger.llama
77
private let model: OpaquePointer
88
private let context: OpaquePointer
@@ -74,7 +74,7 @@ public actor LLama {
7474
// MARK: - Inference
7575

7676
public func infer(prompt: String, maxTokens: Int32 = 128) async throws -> String {
77-
completionInit(text: prompt)
77+
try completionInit(text: prompt)
7878
var generatedText = ""
7979

8080
while !isDone && nCur < nLen && nCur - batch.n_tokens < maxTokens {
@@ -109,7 +109,7 @@ public actor LLama {
109109
batch.n_tokens += 1
110110
}
111111

112-
private func completionInit(text: String) {
112+
private func completionInit(text: String) throws {
113113
logger.debug("Attempting to complete \"\(text)\"")
114114

115115
tokensList = tokenize(text: text, add_bos: true)
@@ -121,7 +121,8 @@ public actor LLama {
121121
logger.debug("\nn_len = \(self.nLen), n_ctx = \(nCtx), n_kv_req = \(nKvReq)")
122122

123123
if nKvReq > nCtx {
124-
print("Error: n_kv_req > n_ctx, the required KV cache size is not big enough")
124+
logger.error("Error: n_kv_req > n_ctx, the required KV cache size is not big enough")
125+
throw InferError(message: "KV cache too small", code: .kvCacheFailure)
125126
}
126127

127128
batch.clear()
@@ -134,7 +135,7 @@ public actor LLama {
134135
}
135136

136137
if llama_decode(context, batch) != 0 {
137-
print("llama_decode() failed")
138+
throw InferError(message: "llama_decode failed", code: .decodingFailure)
138139
}
139140

140141
nCur = batch.n_tokens
@@ -230,8 +231,8 @@ public actor LLama {
230231
}
231232
}
232233

233-
private extension llama_batch {
234-
mutating func clear() {
234+
extension llama_batch {
235+
fileprivate mutating func clear() {
235236
n_tokens = 0
236237
}
237238
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Logging
22

33
extension Logger {
4-
static var llama = Logger(label: "llama-cpp-swift")
4+
static var llama: Logger {
5+
Logger(label: "llama-cpp-swift")
6+
}
57
}

example/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.10
1+
// swift-tools-version: 6.0
22
import PackageDescription
33

44
let package = Package(

0 commit comments

Comments
 (0)