2023-11-27 15:56:52 +01:00
|
|
|
import Foundation
|
2024-01-07 09:20:50 +01:00
|
|
|
import llama
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
enum LlamaError: Error {
|
|
|
|
case couldNotInitializeContext
|
|
|
|
}
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
func llama_batch_clear(_ batch: inout llama_batch) {
|
|
|
|
batch.n_tokens = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func llama_batch_add(_ batch: inout llama_batch, _ id: llama_token, _ pos: llama_pos, _ seq_ids: [llama_seq_id], _ logits: Bool) {
|
|
|
|
batch.token [Int(batch.n_tokens)] = id
|
|
|
|
batch.pos [Int(batch.n_tokens)] = pos
|
|
|
|
batch.n_seq_id[Int(batch.n_tokens)] = Int32(seq_ids.count)
|
|
|
|
for i in 0..<seq_ids.count {
|
|
|
|
batch.seq_id[Int(batch.n_tokens)]![Int(i)] = seq_ids[i]
|
|
|
|
}
|
|
|
|
batch.logits [Int(batch.n_tokens)] = logits ? 1 : 0
|
|
|
|
|
|
|
|
batch.n_tokens += 1
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:56:52 +01:00
|
|
|
actor LlamaContext {
|
|
|
|
private var model: OpaquePointer
|
|
|
|
private var context: OpaquePointer
|
2024-09-07 14:16:19 +02:00
|
|
|
private var sampling: UnsafeMutablePointer<llama_sampler>
|
2023-11-27 15:56:52 +01:00
|
|
|
private var batch: llama_batch
|
|
|
|
private var tokens_list: [llama_token]
|
2024-07-20 15:09:37 +02:00
|
|
|
var is_done: Bool = false
|
2023-12-17 18:38:41 +01:00
|
|
|
|
2023-12-04 17:03:49 +01:00
|
|
|
/// This variable is used to store temporarily invalid cchars
|
|
|
|
private var temporary_invalid_cchars: [CChar]
|
2023-11-27 15:56:52 +01:00
|
|
|
|
2024-07-20 15:09:37 +02:00
|
|
|
var n_len: Int32 = 1024
|
2023-11-27 15:56:52 +01:00
|
|
|
var n_cur: Int32 = 0
|
2023-12-17 18:38:41 +01:00
|
|
|
|
2023-11-27 15:56:52 +01:00
|
|
|
var n_decode: Int32 = 0
|
|
|
|
|
|
|
|
init(model: OpaquePointer, context: OpaquePointer) {
|
|
|
|
self.model = model
|
|
|
|
self.context = context
|
|
|
|
self.tokens_list = []
|
|
|
|
self.batch = llama_batch_init(512, 0, 1)
|
2023-12-04 17:03:49 +01:00
|
|
|
self.temporary_invalid_cchars = []
|
2024-09-07 14:16:19 +02:00
|
|
|
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.4))
|
|
|
|
llama_sampler_chain_add(self.sampling, llama_sampler_init_dist(1234))
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2024-09-07 14:16:19 +02:00
|
|
|
llama_sampler_free(sampling)
|
2023-12-17 18:38:41 +01:00
|
|
|
llama_batch_free(batch)
|
2023-11-27 15:56:52 +01:00
|
|
|
llama_free(context)
|
|
|
|
llama_free_model(model)
|
|
|
|
llama_backend_free()
|
|
|
|
}
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
static func create_context(path: String) throws -> LlamaContext {
|
2024-02-16 10:31:07 +01:00
|
|
|
llama_backend_init()
|
2023-12-17 18:38:41 +01:00
|
|
|
var model_params = llama_model_default_params()
|
2023-11-27 15:56:52 +01:00
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
#if targetEnvironment(simulator)
|
|
|
|
model_params.n_gpu_layers = 0
|
|
|
|
print("Running on simulator, force use n_gpu_layers = 0")
|
|
|
|
#endif
|
2023-11-27 15:56:52 +01:00
|
|
|
let model = llama_load_model_from_file(path, model_params)
|
|
|
|
guard let model else {
|
|
|
|
print("Could not load model at \(path)")
|
|
|
|
throw LlamaError.couldNotInitializeContext
|
|
|
|
}
|
2023-12-17 18:38:41 +01:00
|
|
|
|
|
|
|
let n_threads = max(1, min(8, ProcessInfo.processInfo.processorCount - 2))
|
|
|
|
print("Using \(n_threads) threads")
|
|
|
|
|
2023-11-27 15:56:52 +01:00
|
|
|
var ctx_params = llama_context_default_params()
|
|
|
|
ctx_params.n_ctx = 2048
|
Threadpool: take 2 (#8672)
* Introduce ggml_compute_threadpool
- OpenMP functional: check
- Vanilla ggml functional: Check
- ggml w/threadpool functional: Check
- OpenMP no regression: No glaring problems
- Vanilla ggml no regression: No glaring problems
- ggml w/threadpool no regression: No glaring problems
* Minor fixes
* fixed use after release bug
* fixed a harmless race condition
* Fix Android bulid issue
* fix more race conditions
* fix deadlock for cases where cgraph.n_nodes == 1
and fix --poll case
* threadpool: use cpu_get_num_math to set the default number of threadpool threads
This way we avoid using E-Cores and Hyperthreaded siblings.
* bench: create fresh threadpool for each test
For benchmarking it's better to start a fresh pool for each test with the exact number of threads
needed for that test. Having larger pools is suboptimal (causes more load, etc).
* atomics: always use stdatomics with clang and use relaxed memory order when polling in ggml_barrier
This also removes sched_yield() calls from ggml_barrier() to match OpenMP behavior.
* threadpool: make polling the default to match openmp behavior
All command line args now allow for setting poll to 0 (false).
* threadpool: do not wakeup threads in already paused threadpool
* fix potential race condition in check_for_work
* threadpool: do not create two threadpools if their params are identical
* threadpool: reduce pause/resume/wakeup overhead in common cases
We now start threadpool in paused state only if we have two.
The resume is now implicit (ie new work) which allows for reduced locking and context-switch overhead.
* threadpool: add support for hybrid polling
poll params (--poll, ...) now specify "polling level", i.e. how aggresively we poll before waiting on cond.var.
poll=0 means no polling, 1 means poll for 128K rounds then wait, 2 for 256K rounds, ...
The default value of 50 (ie 50x128K rounds) seems like a decent default across modern platforms.
We can tune this further as things evolve.
* threadpool: reduce the number of barrier required
New work is now indicated with an atomic counter that is incremented for
each new graph that needs to be computed.
This removes the need for extra barrier for clearing the "new_work" and
removes the special case for trivial graphs.
* threadpool: remove special-casing for disposable threadpools
With the efficient hybrid polling there is no need to make disposable pools any different.
This simplifies the overall logic and reduces branching.
Include n_threads in debug print for disposable threadpool.
Declare pause and stop flags as atomic_bool
This doesn't actually generate any memory barriers and simply informs
the thread sanitizer that these flags can be written & read by different
threads without locking.
* threadpool: do not clear barrier counters between graphs computes (fixes race with small graphs)
This fixes the race condition with very small graphs where the main thread happens to
start a new graph while the workers are just about to exit from barriers.
* threadpool: use relaxed order for chunk sync
Full memory barrier is an overkill for this since each thread works on different chunk
* threadpool: remove abort_callback from threadpool state
* threadpool: better naming for thread/cpumask releated functions
* threadpool: consistent use of int type for n_threads params
* threadpool: add support for ggml_threadpool_params_default/init
Also removes the need for explicit mask_specified param.
all-zero cpumask means use default (usually inherited) cpu affinity mask.
* threadpool: move typedef into ggml.h
* threadpool: fix apply_priority() function name
* threadpool: fix swift wrapper errors due to n_threads int type cleanup
* threadpool: enable --cpu-mask and other threadpool related options only if threadpool is enabled
* threadpool: replace checks for compute_thread ret code with proper status check
* threadpool: simplify threadpool init logic and fix main thread affinity application
Most of the init code is now exactly the same between threadpool and openmp.
* threadpool: update threadpool resume/pause function names
* threadpool: enable openmp by default for now
* threadpool: don't forget to free workers state when omp is enabled
* threadpool: avoid updating process priority on the platforms that do not require it
On Windows we need to change overall process priority class in order to set thread priorities,
but on Linux, Mac, etc we do not need to touch the overall process settings.
* threadpool: update calling thread prio and affinity only at start/resume
This avoids extra syscalls for each graph_compute()
* llama-bench: turn threadpool params into vectors, add output headers, etc
* llama-bench: add support for cool off between tests --delay
This helps for long running tests on platforms that are thermally limited (phones, laptops, etc).
--delay (disabled by default) introduces the sleep for N seconds before starting each test.
* threadpool: move process priority setting into the apps (bench and cli)
This avoids changing the overall process priority on Windows for the apps
that use ggml/llama.cpp directy.
* threadpool: move all pause/resume logic into ggml
* threadpool: futher api cleanup and prep for future refactoring
All threadpool related functions and structs use ggml_threadpool prefix.
* threadpool: minor indent fixes
* threadpool: improve setprioty error message
* Update examples/llama-bench/llama-bench.cpp
Co-authored-by: slaren <slarengh@gmail.com>
* threadpool: fix indent in set_threadpool call
* use int32_t for n_thread type in public llama.cpp API
* threadpool: use _new and _free instead of _create and _release
* fix two more public APIs to use int32_t for n_threads
* build: set _GNU_SOURCE for Adroid
---------
Co-authored-by: Max Krasnyansky <quic_maxk@quicinc.com>
Co-authored-by: fmz <quic_fzaghlou@quic.com>
Co-authored-by: Max Krasnyansky <max.krasnyansky@gmail.com>
Co-authored-by: slaren <slarengh@gmail.com>
2024-08-30 01:20:53 +02:00
|
|
|
ctx_params.n_threads = Int32(n_threads)
|
|
|
|
ctx_params.n_threads_batch = Int32(n_threads)
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
let context = llama_new_context_with_model(model, ctx_params)
|
|
|
|
guard let context else {
|
|
|
|
print("Could not load context!")
|
|
|
|
throw LlamaError.couldNotInitializeContext
|
|
|
|
}
|
|
|
|
|
|
|
|
return LlamaContext(model: model, context: context)
|
|
|
|
}
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
func model_info() -> String {
|
|
|
|
let result = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
|
|
|
|
result.initialize(repeating: Int8(0), count: 256)
|
|
|
|
defer {
|
|
|
|
result.deallocate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is probably very stupid way to get the string from C
|
|
|
|
|
|
|
|
let nChars = llama_model_desc(model, result, 256)
|
|
|
|
let bufferPointer = UnsafeBufferPointer(start: result, count: Int(nChars))
|
|
|
|
|
|
|
|
var SwiftString = ""
|
|
|
|
for char in bufferPointer {
|
|
|
|
SwiftString.append(Character(UnicodeScalar(UInt8(char))))
|
|
|
|
}
|
|
|
|
|
|
|
|
return SwiftString
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:56:52 +01:00
|
|
|
func get_n_tokens() -> Int32 {
|
|
|
|
return batch.n_tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
func completion_init(text: String) {
|
|
|
|
print("attempting to complete \"\(text)\"")
|
|
|
|
|
|
|
|
tokens_list = tokenize(text: text, add_bos: true)
|
2023-12-04 17:03:49 +01:00
|
|
|
temporary_invalid_cchars = []
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
let n_ctx = llama_n_ctx(context)
|
|
|
|
let n_kv_req = tokens_list.count + (Int(n_len) - tokens_list.count)
|
|
|
|
|
|
|
|
print("\n n_len = \(n_len), n_ctx = \(n_ctx), n_kv_req = \(n_kv_req)")
|
|
|
|
|
|
|
|
if n_kv_req > n_ctx {
|
|
|
|
print("error: n_kv_req > n_ctx, the required KV cache size is not big enough")
|
|
|
|
}
|
|
|
|
|
|
|
|
for id in tokens_list {
|
2023-12-04 17:03:49 +01:00
|
|
|
print(String(cString: token_to_piece(token: id) + [0]))
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
llama_batch_clear(&batch)
|
2023-11-27 15:56:52 +01:00
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
for i1 in 0..<tokens_list.count {
|
2023-11-27 15:56:52 +01:00
|
|
|
let i = Int(i1)
|
2023-12-17 18:38:41 +01:00
|
|
|
llama_batch_add(&batch, tokens_list[i], Int32(i), [0], false)
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
batch.logits[Int(batch.n_tokens) - 1] = 1 // true
|
|
|
|
|
|
|
|
if llama_decode(context, batch) != 0 {
|
|
|
|
print("llama_decode() failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
n_cur = batch.n_tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
func completion_loop() -> String {
|
|
|
|
var new_token_id: llama_token = 0
|
|
|
|
|
2024-09-07 14:16:19 +02:00
|
|
|
new_token_id = llama_sampler_sample(sampling, context, batch.n_tokens - 1)
|
2023-11-27 15:56:52 +01:00
|
|
|
|
2024-04-21 13:50:41 +02:00
|
|
|
if llama_token_is_eog(model, new_token_id) || n_cur == n_len {
|
2023-11-27 15:56:52 +01:00
|
|
|
print("\n")
|
2024-07-20 15:09:37 +02:00
|
|
|
is_done = true
|
2023-12-04 17:03:49 +01:00
|
|
|
let new_token_str = String(cString: temporary_invalid_cchars + [0])
|
|
|
|
temporary_invalid_cchars.removeAll()
|
|
|
|
return new_token_str
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:03:49 +01:00
|
|
|
let new_token_cchars = token_to_piece(token: new_token_id)
|
|
|
|
temporary_invalid_cchars.append(contentsOf: new_token_cchars)
|
|
|
|
let new_token_str: String
|
|
|
|
if let string = String(validatingUTF8: temporary_invalid_cchars + [0]) {
|
|
|
|
temporary_invalid_cchars.removeAll()
|
|
|
|
new_token_str = string
|
|
|
|
} else if (0 ..< temporary_invalid_cchars.count).contains(where: {$0 != 0 && String(validatingUTF8: Array(temporary_invalid_cchars.suffix($0)) + [0]) != nil}) {
|
|
|
|
// in this case, at least the suffix of the temporary_invalid_cchars can be interpreted as UTF8 string
|
|
|
|
let string = String(cString: temporary_invalid_cchars + [0])
|
|
|
|
temporary_invalid_cchars.removeAll()
|
|
|
|
new_token_str = string
|
|
|
|
} else {
|
|
|
|
new_token_str = ""
|
|
|
|
}
|
2023-11-27 15:56:52 +01:00
|
|
|
print(new_token_str)
|
|
|
|
// tokens_list.append(new_token_id)
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
llama_batch_clear(&batch)
|
|
|
|
llama_batch_add(&batch, new_token_id, n_cur, [0], true)
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
n_decode += 1
|
2023-12-17 18:38:41 +01:00
|
|
|
n_cur += 1
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
if llama_decode(context, batch) != 0 {
|
|
|
|
print("failed to evaluate llama!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_token_str
|
|
|
|
}
|
|
|
|
|
2023-12-17 18:38:41 +01:00
|
|
|
func bench(pp: Int, tg: Int, pl: Int, nr: Int = 1) -> String {
|
|
|
|
var pp_avg: Double = 0
|
|
|
|
var tg_avg: Double = 0
|
|
|
|
|
|
|
|
var pp_std: Double = 0
|
|
|
|
var tg_std: Double = 0
|
|
|
|
|
2023-12-18 19:05:12 +01:00
|
|
|
for _ in 0..<nr {
|
2023-12-17 18:38:41 +01:00
|
|
|
// bench prompt processing
|
|
|
|
|
|
|
|
llama_batch_clear(&batch)
|
|
|
|
|
|
|
|
let n_tokens = pp
|
|
|
|
|
|
|
|
for i in 0..<n_tokens {
|
|
|
|
llama_batch_add(&batch, 0, Int32(i), [0], false)
|
|
|
|
}
|
|
|
|
batch.logits[Int(batch.n_tokens) - 1] = 1 // true
|
|
|
|
|
|
|
|
llama_kv_cache_clear(context)
|
|
|
|
|
2024-12-08 12:14:54 +01:00
|
|
|
let t_pp_start = DispatchTime.now().uptimeNanoseconds / 1000;
|
2023-12-17 18:38:41 +01:00
|
|
|
|
|
|
|
if llama_decode(context, batch) != 0 {
|
|
|
|
print("llama_decode() failed during prompt")
|
|
|
|
}
|
2024-03-13 18:54:21 +01:00
|
|
|
llama_synchronize(context)
|
2023-12-17 18:38:41 +01:00
|
|
|
|
2024-12-08 12:14:54 +01:00
|
|
|
let t_pp_end = DispatchTime.now().uptimeNanoseconds / 1000;
|
2023-12-17 18:38:41 +01:00
|
|
|
|
|
|
|
// bench text generation
|
|
|
|
|
|
|
|
llama_kv_cache_clear(context)
|
|
|
|
|
2024-12-08 12:14:54 +01:00
|
|
|
let t_tg_start = DispatchTime.now().uptimeNanoseconds / 1000;
|
2023-12-17 18:38:41 +01:00
|
|
|
|
|
|
|
for i in 0..<tg {
|
|
|
|
llama_batch_clear(&batch)
|
|
|
|
|
|
|
|
for j in 0..<pl {
|
|
|
|
llama_batch_add(&batch, 0, Int32(i), [Int32(j)], true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if llama_decode(context, batch) != 0 {
|
|
|
|
print("llama_decode() failed during text generation")
|
|
|
|
}
|
2024-03-13 18:54:21 +01:00
|
|
|
llama_synchronize(context)
|
2023-12-17 18:38:41 +01:00
|
|
|
}
|
|
|
|
|
2024-12-08 12:14:54 +01:00
|
|
|
let t_tg_end = DispatchTime.now().uptimeNanoseconds / 1000;
|
2023-12-17 18:38:41 +01:00
|
|
|
|
|
|
|
llama_kv_cache_clear(context)
|
|
|
|
|
|
|
|
let t_pp = Double(t_pp_end - t_pp_start) / 1000000.0
|
|
|
|
let t_tg = Double(t_tg_end - t_tg_start) / 1000000.0
|
|
|
|
|
|
|
|
let speed_pp = Double(pp) / t_pp
|
|
|
|
let speed_tg = Double(pl*tg) / t_tg
|
|
|
|
|
|
|
|
pp_avg += speed_pp
|
|
|
|
tg_avg += speed_tg
|
|
|
|
|
|
|
|
pp_std += speed_pp * speed_pp
|
|
|
|
tg_std += speed_tg * speed_tg
|
|
|
|
|
|
|
|
print("pp \(speed_pp) t/s, tg \(speed_tg) t/s")
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_avg /= Double(nr)
|
|
|
|
tg_avg /= Double(nr)
|
|
|
|
|
|
|
|
if nr > 1 {
|
|
|
|
pp_std = sqrt(pp_std / Double(nr - 1) - pp_avg * pp_avg * Double(nr) / Double(nr - 1))
|
|
|
|
tg_std = sqrt(tg_std / Double(nr - 1) - tg_avg * tg_avg * Double(nr) / Double(nr - 1))
|
|
|
|
} else {
|
|
|
|
pp_std = 0
|
|
|
|
tg_std = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
let model_desc = model_info();
|
|
|
|
let model_size = String(format: "%.2f GiB", Double(llama_model_size(model)) / 1024.0 / 1024.0 / 1024.0);
|
|
|
|
let model_n_params = String(format: "%.2f B", Double(llama_model_n_params(model)) / 1e9);
|
|
|
|
let backend = "Metal";
|
|
|
|
let pp_avg_str = String(format: "%.2f", pp_avg);
|
|
|
|
let tg_avg_str = String(format: "%.2f", tg_avg);
|
|
|
|
let pp_std_str = String(format: "%.2f", pp_std);
|
|
|
|
let tg_std_str = String(format: "%.2f", tg_std);
|
|
|
|
|
|
|
|
var result = ""
|
|
|
|
|
|
|
|
result += String("| model | size | params | backend | test | t/s |\n")
|
|
|
|
result += String("| --- | --- | --- | --- | --- | --- |\n")
|
|
|
|
result += String("| \(model_desc) | \(model_size) | \(model_n_params) | \(backend) | pp \(pp) | \(pp_avg_str) ± \(pp_std_str) |\n")
|
|
|
|
result += String("| \(model_desc) | \(model_size) | \(model_n_params) | \(backend) | tg \(tg) | \(tg_avg_str) ± \(tg_std_str) |\n")
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:56:52 +01:00
|
|
|
func clear() {
|
|
|
|
tokens_list.removeAll()
|
2023-12-04 17:03:49 +01:00
|
|
|
temporary_invalid_cchars.removeAll()
|
2023-12-17 18:38:41 +01:00
|
|
|
llama_kv_cache_clear(context)
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private func tokenize(text: String, add_bos: Bool) -> [llama_token] {
|
2023-12-04 14:43:45 +01:00
|
|
|
let utf8Count = text.utf8.count
|
2023-12-17 18:38:41 +01:00
|
|
|
let n_tokens = utf8Count + (add_bos ? 1 : 0) + 1
|
2023-11-27 15:56:52 +01:00
|
|
|
let tokens = UnsafeMutablePointer<llama_token>.allocate(capacity: n_tokens)
|
2023-12-04 14:43:45 +01:00
|
|
|
let tokenCount = llama_tokenize(model, text, Int32(utf8Count), tokens, Int32(n_tokens), add_bos, false)
|
2023-11-27 15:56:52 +01:00
|
|
|
|
|
|
|
var swiftTokens: [llama_token] = []
|
|
|
|
for i in 0..<tokenCount {
|
|
|
|
swiftTokens.append(tokens[Int(i)])
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens.deallocate()
|
|
|
|
|
|
|
|
return swiftTokens
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:03:49 +01:00
|
|
|
/// - note: The result does not contain null-terminator
|
|
|
|
private func token_to_piece(token: llama_token) -> [CChar] {
|
2023-11-27 15:56:52 +01:00
|
|
|
let result = UnsafeMutablePointer<Int8>.allocate(capacity: 8)
|
|
|
|
result.initialize(repeating: Int8(0), count: 8)
|
2023-12-01 19:19:45 +01:00
|
|
|
defer {
|
|
|
|
result.deallocate()
|
|
|
|
}
|
2024-07-05 19:01:35 +02:00
|
|
|
let nTokens = llama_token_to_piece(model, token, result, 8, 0, false)
|
2023-12-01 19:19:45 +01:00
|
|
|
|
|
|
|
if nTokens < 0 {
|
|
|
|
let newResult = UnsafeMutablePointer<Int8>.allocate(capacity: Int(-nTokens))
|
|
|
|
newResult.initialize(repeating: Int8(0), count: Int(-nTokens))
|
|
|
|
defer {
|
|
|
|
newResult.deallocate()
|
|
|
|
}
|
2024-07-05 19:01:35 +02:00
|
|
|
let nNewTokens = llama_token_to_piece(model, token, newResult, -nTokens, 0, false)
|
2023-12-04 17:03:49 +01:00
|
|
|
let bufferPointer = UnsafeBufferPointer(start: newResult, count: Int(nNewTokens))
|
|
|
|
return Array(bufferPointer)
|
2023-12-01 19:19:45 +01:00
|
|
|
} else {
|
2023-12-04 17:03:49 +01:00
|
|
|
let bufferPointer = UnsafeBufferPointer(start: result, count: Int(nTokens))
|
|
|
|
return Array(bufferPointer)
|
2023-12-01 19:19:45 +01:00
|
|
|
}
|
2023-11-27 15:56:52 +01:00
|
|
|
}
|
|
|
|
}
|