Skip to content

Commit 29ccbc7

Browse files
committed
Write NewContextCtl
1 parent cd3406f commit 29ccbc7

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

lib/src/ensemble_llama_base.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class Llama {
9999
_controlPort.send(ctl);
100100
final resp =
101101
await _response.firstWhere((e) => e is NewContextResp && e.id == ctl.id)
102-
as NewContextResp;
103-
return resp.ctx!; // TODO
102+
as NewContextResp
103+
..throwIfErr();
104+
return resp.ctx!;
104105
}
105106

106107
// Future<void> freeContext(Context ctx) async {

lib/src/llama_cpp_isolate_wrapper.dart

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ class ExitCtl extends ControlMessage {
107107

108108
class LoadModelCtl extends ControlMessage {
109109
final String path;
110-
final ContextParams ctxParams;
111-
LoadModelCtl(this.path, this.ctxParams);
110+
final ContextParams params;
111+
LoadModelCtl(this.path, this.params);
112112

113113
LoadModelResp done(Model model) => LoadModelResp(id, model: model);
114114

@@ -219,9 +219,8 @@ class _Allocations<E> {
219219
}
220220
}
221221

222-
// key: rawModelPointer
223222
final _modelAllocs = _Allocations<int>();
224-
final _ctxAllocs = _Allocations<int>();
223+
// final _ctxAllocs = _Allocations<int>();
225224

226225
late final SendPort _log;
227226
late final SendPort _response;
@@ -259,34 +258,33 @@ void _onControl(ControlMessage ctl) {
259258

260259
case LoadModelCtl():
261260
final Set<Pointer> allocs = {};
262-
final pd = ctl.ctxParams;
263-
final pc = libllama.llama_context_default_params()..setSimpleFrom(pd);
261+
final params = libllama.llama_context_default_params()
262+
..setSimpleFrom(ctl.params);
264263

265264
// TODO: can't do this until we track contexts to manage memory allocation
266265
// pc.tensor_split
267266

268-
pc.progress_callback = Pointer.fromFunction(_onModelLoadProgress);
267+
params.progress_callback = Pointer.fromFunction(_onModelLoadProgress);
269268
final idPointer = calloc.allocate<Uint32>(sizeOf<Uint32>());
270269
allocs.add(idPointer);
271270
idPointer.value = ctl.id;
272-
pc.progress_callback_user_data = idPointer.cast<Void>();
271+
params.progress_callback_user_data = idPointer.cast<Void>();
273272

274-
final rawModelPointer = libllama
273+
final rawModel = libllama
275274
.llama_load_model_from_file(
276275
ctl.path.toNativeUtf8().cast<Char>(),
277-
pc,
276+
params,
278277
)
279278
.address;
280279

281-
if (rawModelPointer == 0) {
282-
_response.send(ctl.error(
283-
Exception("failed loading model: ${ctl.path}"),
284-
));
280+
if (rawModel == 0) {
281+
_response
282+
.send(ctl.error(Exception("failed loading model: ${ctl.path}")));
285283
return;
286284
}
287285

288-
_modelAllocs[rawModelPointer] = allocs;
289-
_response.send(ctl.done(Model._(rawModelPointer)));
286+
_modelAllocs[rawModel] = allocs;
287+
_response.send(ctl.done(Model._(rawModel)));
290288

291289
case FreeModelCtl():
292290
assert(ctl.model._rawPointer != 0);
@@ -300,7 +298,19 @@ void _onControl(ControlMessage ctl) {
300298

301299
case NewContextCtl():
302300
assert(ctl.model._rawPointer != 0);
303-
// libllama.llama_new_context_with_model(model, params)
301+
final params = libllama.llama_context_default_params()
302+
..setSimpleFrom(ctl.params);
303+
304+
final rawCtx = libllama
305+
.llama_new_context_with_model(ctl.model._ffiPointer, params)
306+
.address;
307+
308+
if (rawCtx == 0) {
309+
_response.send(ctl.error(Exception("failed creating context")));
310+
return;
311+
}
312+
313+
_response.send(ctl.done(Context._(rawCtx)));
304314

305315
case FreeContextCtl():
306316
}

0 commit comments

Comments
 (0)