From 5f5b983dc6a5079dc9b9d391511731f3dd99353c Mon Sep 17 00:00:00 2001 From: Tomislav Eric Date: Thu, 15 Jan 2026 18:20:16 +0100 Subject: [PATCH] update to llama.cpp and fix deprecations --- .../xcschemes/LLamaSwift.xcscheme | 79 +++++++++++++++++++ Package.resolved | 13 +-- Package.swift | 17 ++-- Sources/llama-cpp-swift/LLama.swift | 9 ++- Sources/llama-cpp-swift/Model.swift | 8 +- 5 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/LLamaSwift.xcscheme diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/LLamaSwift.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/LLamaSwift.xcscheme new file mode 100644 index 0000000..52fa605 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/LLamaSwift.xcscheme @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Package.resolved b/Package.resolved index b783b41..2361209 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,21 +1,12 @@ { "pins" : [ - { - "identity" : "llama.cpp", - "kind" : "remoteSourceControl", - "location" : "https://github.com/ggerganov/llama.cpp", - "state" : { - "branch" : "master", - "revision" : "26a8406ba9198eb6fdd8329fa717555b4f77f05f" - } - }, { "identity" : "swift-log", "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-log.git", "state" : { - "revision" : "96a2f8a0fa41e9e09af4585e2724c4e825410b91", - "version" : "1.6.2" + "revision" : "7ee16e465622412764b0ff0c1301801dc71b8f61", + "version" : "1.9.0" } } ], diff --git a/Package.swift b/Package.swift index b403c5b..e5769b2 100644 --- a/Package.swift +++ b/Package.swift @@ -11,21 +11,24 @@ let package = Package( .visionOS(.v1) ], products: [ - .library( - name: "LLamaSwift", - targets: ["LLamaSwift"]), + .library(name: "LLamaSwift", targets: ["LLamaSwift"]), ], dependencies: [ - .package(url: "https://github.com/ggerganov/llama.cpp", branch: "master"), .package(url: "https://github.com/apple/swift-log.git", from: "1.6.1"), ], targets: [ + .binaryTarget( + name: "llama", + url: "https://github.com/ggml-org/llama.cpp/releases/download/b7642/llama-b7642-xcframework.zip", + checksum: "59b372b1991470cf6e3b48b49052495ddf5e1f39551b595dabb011a24b08b4fc" + ), .target( - name: "LLamaSwift", + name: "LLamaSwift", dependencies: [ - .product(name: "llama", package: "llama.cpp"), + "llama", .product(name: "Logging", package: "swift-log"), - ] + ], + path: "Sources/llama-cpp-swift" ), .testTarget( name: "llama-cpp-swiftTests", diff --git a/Sources/llama-cpp-swift/LLama.swift b/Sources/llama-cpp-swift/LLama.swift index 3536514..5a2fdd4 100644 --- a/Sources/llama-cpp-swift/LLama.swift +++ b/Sources/llama-cpp-swift/LLama.swift @@ -19,9 +19,10 @@ public actor LLama { // Initialize sampling let sparams = llama_sampler_chain_default_params() self.sampling = llama_sampler_chain_init(sparams) - llama_sampler_chain_add(self.sampling, llama_sampler_init_temp(0.8)) - llama_sampler_chain_add(self.sampling, llama_sampler_init_softmax()) - llama_sampler_chain_add(self.sampling, llama_sampler_init_dist(1234)) + llama_sampler_chain_add(self.sampling, llama_sampler_init_top_k(40)); + llama_sampler_chain_add(self.sampling, llama_sampler_init_top_p(0.9, 1)); + llama_sampler_chain_add(self.sampling, llama_sampler_init_temp (0.4)); + llama_sampler_chain_add(self.sampling, llama_sampler_init_dist (1234)); } deinit { @@ -197,7 +198,7 @@ public actor LLama { var newTokenID: llama_token = 0 newTokenID = llama_sampler_sample(sampling, model.context, batch.n_tokens - 1) - if llama_token_is_eog(model.model, newTokenID) || nCur == nLen { + if llama_vocab_is_eog(model.model, newTokenID) || nCur == nLen { isDone = true let newTokenStr = String( decoding: Data(temporaryInvalidCChars.map { UInt8(bitPattern: $0) }), as: UTF8.self) diff --git a/Sources/llama-cpp-swift/Model.swift b/Sources/llama-cpp-swift/Model.swift index d2bc1a0..3c706db 100644 --- a/Sources/llama-cpp-swift/Model.swift +++ b/Sources/llama-cpp-swift/Model.swift @@ -16,7 +16,7 @@ public final class Model { logger.debug("Running on simulator, force use n_gpu_layers = 0") #endif - guard let model = llama_load_model_from_file(modelPath, modelParams) else { + guard let model = llama_model_load_from_file(modelPath, modelParams) else { llama_backend_free() throw InitializationError(message: "Failed to load model", code: .failedToLoadModel) } @@ -31,8 +31,8 @@ public final class Model { ctxParams.n_threads = Int32(nThreads) ctxParams.n_threads_batch = Int32(nThreads) - guard let context = llama_new_context_with_model(model, ctxParams) else { - llama_free_model(model) + guard let context = llama_init_from_model(model, ctxParams) else { + llama_model_free(model) llama_backend_free() throw InitializationError( message: "Failed to initialize context", code: .failedToInitializeContext) @@ -42,7 +42,7 @@ public final class Model { deinit { llama_free(context) - llama_free_model(model) + llama_model_free(model) llama_backend_free() } }