From 67d2bd24668efddcedcdf582cfca645eb06432a9 Mon Sep 17 00:00:00 2001 From: ensan Date: Fri, 1 Dec 2023 14:43:00 +0900 Subject: [PATCH 1/2] Fix token_to_piece implementation in Swift --- examples/batched.swift/Sources/main.swift | 10 ++++++++++ .../llama.swiftui/llama.cpp.swift/LibLlama.swift | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/batched.swift/Sources/main.swift b/examples/batched.swift/Sources/main.swift index ba15197aecf09..aa6f7bebac5f3 100644 --- a/examples/batched.swift/Sources/main.swift +++ b/examples/batched.swift/Sources/main.swift @@ -242,6 +242,16 @@ private func token_to_piece(token: llama_token, buffer: inout [CChar]) -> String Int32(result.count) ) assert(check == nTokens) + } else if nTokens > 8 { + result.removeAll() + result = [CChar](repeating: 0, count: nTokens) + let check = llama_token_to_piece( + model, + token, + &result, + Int32(result.count) + ) + assert(check == nTokens) } else { result.removeLast(result.count - Int(nTokens)) } diff --git a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift index aaef09611bc90..387d3f9c3c113 100644 --- a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift +++ b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift @@ -165,7 +165,18 @@ actor LlamaContext { let result = UnsafeMutablePointer.allocate(capacity: 8) result.initialize(repeating: Int8(0), count: 8) - let _ = llama_token_to_piece(model, token, result, 8) + let nTokens = llama_token_to_piece(model, token, result, 8) + + if nTokens > 8 { + result.removeAll() + result = [CChar](repeating: 0, count: nTokens) + _ = llama_token_to_piece( + model, + token, + &result, + Int32(result.count) + ) + } let resultStr = String(cString: result) From 4283a889a7176f3c804d36f34ac8953d46dba955 Mon Sep 17 00:00:00 2001 From: ensan-hcl Date: Fri, 1 Dec 2023 23:58:19 +0900 Subject: [PATCH 2/2] Fix errors --- examples/batched.swift/Sources/main.swift | 20 ++----------- .../llama.cpp.swift/LibLlama.swift | 29 +++++++++---------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/examples/batched.swift/Sources/main.swift b/examples/batched.swift/Sources/main.swift index aa6f7bebac5f3..ce9d80d9b6c4a 100644 --- a/examples/batched.swift/Sources/main.swift +++ b/examples/batched.swift/Sources/main.swift @@ -230,28 +230,15 @@ private func token_to_piece(token: llama_token, buffer: inout [CChar]) -> String var result = [CChar](repeating: 0, count: 8) let nTokens = llama_token_to_piece(model, token, &result, Int32(result.count)) if nTokens < 0 { - if result.count >= -Int(nTokens) { - result.removeLast(-Int(nTokens)) - } else { - result.removeAll() - } - let check = llama_token_to_piece( - model, - token, - &result, - Int32(result.count) - ) - assert(check == nTokens) - } else if nTokens > 8 { - result.removeAll() - result = [CChar](repeating: 0, count: nTokens) + let actualTokensCount = -Int(nTokens) + result = .init(repeating: 0, count: actualTokensCount) let check = llama_token_to_piece( model, token, &result, Int32(result.count) ) - assert(check == nTokens) + assert(check == actualTokensCount) } else { result.removeLast(result.count - Int(nTokens)) } @@ -269,5 +256,4 @@ private func token_to_piece(token: llama_token, buffer: inout [CChar]) -> String buffer = [] return bufferString } - return nil } diff --git a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift index 387d3f9c3c113..09b36d9e65b17 100644 --- a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift +++ b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift @@ -164,24 +164,21 @@ actor LlamaContext { private func token_to_piece(token: llama_token) -> String { let result = UnsafeMutablePointer.allocate(capacity: 8) result.initialize(repeating: Int8(0), count: 8) - + defer { + result.deallocate() + } let nTokens = llama_token_to_piece(model, token, result, 8) - if nTokens > 8 { - result.removeAll() - result = [CChar](repeating: 0, count: nTokens) - _ = llama_token_to_piece( - model, - token, - &result, - Int32(result.count) - ) + if nTokens < 0 { + let newResult = UnsafeMutablePointer.allocate(capacity: Int(-nTokens)) + newResult.initialize(repeating: Int8(0), count: Int(-nTokens)) + defer { + newResult.deallocate() + } + _ = llama_token_to_piece(model, token, newResult, -nTokens) + return String(cString: newResult) + } else { + return String(cString: result) } - - let resultStr = String(cString: result) - - result.deallocate() - - return resultStr } }