Skip to content

Commit cd3406f

Browse files
committed
Move Model FFI Pointer creation to Model class
1 parent f8ecd12 commit cd3406f

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/src/ensemble_llama_base.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ void main() async {
1919
progressCallback: (p) => stdout.write("."),
2020
);
2121

22-
print(model.rawPointer);
22+
print(model);
2323

24-
// final ctx = await llama.newContext(model, params);
24+
final ctx = await llama.newContext(model, params);
2525
// await llama.freeContext(ctx);
2626

2727
await llama.freeModel(model);
@@ -94,13 +94,14 @@ class Llama {
9494
await _response.firstWhere((e) => e is FreeModelResp && e.id == ctl.id);
9595
}
9696

97-
// Future<Context> newContext(Model model, ContextParams params) async {
98-
// final ctl = NewContextCtl(model, params);
99-
// _controlPort.send(ctl);
100-
// final resp =
101-
// await _response.firstWhere((e) => e is NewContextResp && e.id == ctl.id)
102-
// as NewContextResp;
103-
// }
97+
Future<Context> newContext(Model model, ContextParams params) async {
98+
final ctl = NewContextCtl(model, params);
99+
_controlPort.send(ctl);
100+
final resp =
101+
await _response.firstWhere((e) => e is NewContextResp && e.id == ctl.id)
102+
as NewContextResp;
103+
return resp.ctx!; // TODO
104+
}
104105

105106
// Future<void> freeContext(Context ctx) async {
106107
// final ctl = FreeContextCtl(ctx);

lib/src/llama_cpp_isolate_wrapper.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extension on llama_context_params {
3636
embedding = p.willUseEmbedding;
3737
}
3838
}
39+
3940
class ContextParams {
4041
final int seed;
4142
final int contextSizeTokens;
@@ -191,8 +192,12 @@ class EntryArgs {
191192
}
192193

193194
class Model {
194-
final int rawPointer;
195-
const Model._(this.rawPointer);
195+
final int _rawPointer;
196+
const Model._(this._rawPointer);
197+
Pointer<llama_model> get _ffiPointer =>
198+
Pointer.fromAddress(_rawPointer).cast<llama_model>();
199+
@override
200+
String toString() => "Model{$_rawPointer}";
196201
}
197202

198203
class Context {
@@ -284,20 +289,18 @@ void _onControl(ControlMessage ctl) {
284289
_response.send(ctl.done(Model._(rawModelPointer)));
285290

286291
case FreeModelCtl():
287-
assert(ctl.model.rawPointer != 0);
288-
_modelAllocs[ctl.model.rawPointer]?.forEach((p) {
292+
assert(ctl.model._rawPointer != 0);
293+
_modelAllocs[ctl.model._rawPointer]?.forEach((p) {
289294
calloc.free(p);
290295
});
291-
_modelAllocs.clear(ctl.model.rawPointer);
292-
293-
final modelPointer =
294-
Pointer.fromAddress(ctl.model.rawPointer).cast<llama_model>();
295-
libllama.llama_free_model(modelPointer);
296+
_modelAllocs.clear(ctl.model._rawPointer);
296297

298+
libllama.llama_free_model(ctl.model._ffiPointer);
297299
_response.send(ctl.done());
298300

299301
case NewContextCtl():
300-
assert(ctl.model.rawPointer != 0);
302+
assert(ctl.model._rawPointer != 0);
303+
// libllama.llama_new_context_with_model(model, params)
301304

302305
case FreeContextCtl():
303306
}

0 commit comments

Comments
 (0)