Skip to content

Commit 97b11e8

Browse files
committed
implement context initialization
1 parent 3eab3e5 commit 97b11e8

11 files changed

+112
-29
lines changed

godot/addons/godot-llama-cpp/autoloads/llama-backend-autoload.gd

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extends Node
2+
3+
var backend: LlamaBackend = LlamaBackend.new()
4+
5+
func _enter_tree() -> void:
6+
backend.init()
7+
8+
func _exit_tree() -> void:
9+
backend.deinit()

godot/addons/godot-llama-cpp/godot-llama-cpp.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ extends EditorPlugin
44

55
func _enter_tree():
66
# Initialization of the plugin goes here.
7-
add_autoload_singleton("__LlamaBackendAutoload", "res://addons/godot-llama-cpp/autoloads/llama-backend-autoload.gd")
7+
add_autoload_singleton("__LlamaBackend", "res://addons/godot-llama-cpp/autoloads/llama-backend.gd")
88

99

1010
func _exit_tree():
1111
# Clean-up of the plugin goes here.
12-
remove_autoload_singleton("__LlamaBackendAutoload")
12+
remove_autoload_singleton("__LlamaBackend")

godot/main.gd

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends Node
2+
3+
4+
# Called when the node enters the scene tree for the first time.
5+
func _ready() -> void:
6+
pass
7+
8+
9+
# Called every frame. 'delta' is the elapsed time since the previous frame.
10+
func _process(delta: float) -> void:
11+
pass

godot/main.tscn

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
[gd_scene load_steps=2 format=3 uid="uid://7oo8yj56scb1"]
1+
[gd_scene load_steps=3 format=3 uid="uid://7oo8yj56scb1"]
22

33
[ext_resource type="Texture2D" uid="uid://dplw232htshgc" path="res://addons/godot-llama-cpp/assets/godot-llama-cpp-1024x1024.svg" id="1_ojdoj"]
4+
[ext_resource type="Script" path="res://main.gd" id="1_wiuk3"]
45

56
[node name="Node" type="Node"]
7+
script = ExtResource("1_wiuk3")
68

79
[node name="Sprite2D" type="Sprite2D" parent="."]
810
position = Vector2(601, 325)

godot/project.godot

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ config_version=5
1010

1111
[application]
1212

13-
config/name="godot cpp template"
13+
config/name="godot-llama-cpp"
1414
config/features=PackedStringArray("4.2", "Forward Plus")
1515
config/icon="res://icon.svg"
1616

1717
[autoload]
1818

19-
__LlamaBackendAutoload="*res://addons/godot-llama-cpp/autoloads/llama-backend-autoload.gd"
19+
__LlamaBackend="*res://addons/godot-llama-cpp/autoloads/llama-backend.gd"
2020

2121
[editor_plugins]
2222

src/llama_backend.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
using namespace godot;
66

7-
void LlamaBackend::_enter_tree() {
7+
void LlamaBackend::init() {
88
llama_backend_init();
99
llama_numa_init(ggml_numa_strategy::GGML_NUMA_STRATEGY_DISABLED);
1010
}
1111

12-
void LlamaBackend::_exit_tree() {
12+
void LlamaBackend::deinit() {
1313
llama_backend_free();
14+
}
15+
16+
void LlamaBackend::_bind_methods() {
17+
ClassDB::bind_method(D_METHOD("init"), &LlamaBackend::init);
18+
ClassDB::bind_method(D_METHOD("deinit"), &LlamaBackend::deinit);
1419
}

src/llama_backend.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#ifndef LLAMA_BACKEND_H
22
#define LLAMA_BACKEND_H
33

4-
#include <godot_cpp/classes/node.hpp>
4+
#include <godot_cpp/classes/ref_counted.hpp>
55

66
namespace godot {
7-
class LlamaBackend : public Node {
8-
GDCLASS(LlamaBackend, Node)
7+
class LlamaBackend : public RefCounted {
8+
GDCLASS(LlamaBackend, RefCounted)
99

1010
protected:
11-
static void _bind_methods(){};
11+
static void _bind_methods();
1212

1313
public:
14-
virtual void _enter_tree() override;
15-
virtual void _exit_tree() override;
14+
void init();
15+
void deinit();
1616
};
1717
} //namespace godot
1818

src/llama_context.cpp

+53-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
1-
#include "llama_context.h"
1+
#include "llama_context.h"
2+
#include "llama.h"
3+
#include "llama_model.h"
4+
#include <godot_cpp/classes/engine.hpp>
5+
#include <godot_cpp/classes/os.hpp>
6+
#include <godot_cpp/core/class_db.hpp>
7+
#include <godot_cpp/variant/utility_functions.hpp>
8+
9+
using namespace godot;
10+
11+
void LlamaContext::set_model(const Ref<LlamaModel> p_model) {
12+
model = p_model;
13+
}
14+
15+
Ref<LlamaModel> LlamaContext::get_model() {
16+
return model;
17+
}
18+
19+
void LlamaContext::_ready() {
20+
// TODO: remove this and use runtime classes once godot 4.3 lands, see https://github.com/godotengine/godot/pull/82554
21+
if (Engine::get_singleton()->is_editor_hint()) {
22+
return;
23+
}
24+
25+
if (model->model == NULL) {
26+
UtilityFunctions::printerr(vformat("%s: Failed to initialize llama context, model property not defined", __func__));
27+
return;
28+
}
29+
30+
ctx_params.n_ctx = 2048;
31+
int32_t n_threads = OS::get_singleton()->get_processor_count();
32+
ctx_params.n_threads = n_threads;
33+
ctx_params.n_threads_batch = n_threads;
34+
35+
ctx = llama_new_context_with_model(model->model, ctx_params);
36+
if (ctx == NULL) {
37+
UtilityFunctions::printerr(vformat("%s: Failed to initialize llama context, null ctx", __func__));
38+
return;
39+
}
40+
UtilityFunctions::print(vformat("%s: Context initialized", __func__));
41+
}
42+
43+
LlamaContext::~LlamaContext() {
44+
if (ctx) {
45+
llama_free(ctx);
46+
}
47+
}
48+
49+
void LlamaContext::_bind_methods() {
50+
ClassDB::bind_method(D_METHOD("set_model", "model"), &LlamaContext::set_model);
51+
ClassDB::bind_method(D_METHOD("get_model"), &LlamaContext::get_model);
52+
ClassDB::add_property("LlamaContext", PropertyInfo(Variant::OBJECT, "model", PROPERTY_HINT_RESOURCE_TYPE, "LlamaModel"), "set_model", "get_model");
53+
}

src/llama_context.h

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
#ifndef LLAMA_CONTEXT_H
22
#define LLAMA_CONTEXT_H
33

4-
#include <godot_cpp/classes/node.hpp>
4+
#include "llama.h"
55
#include "llama_model.h"
6+
#include <godot_cpp/classes/node.hpp>
67

78
namespace godot {
8-
class LlamaContext : public Node {
9-
GDCLASS(LlamaContext, Node)
9+
class LlamaContext : public Node {
10+
GDCLASS(LlamaContext, Node)
11+
12+
private:
13+
Ref<LlamaModel> model;
14+
llama_context *ctx = nullptr;
15+
llama_context_params ctx_params = llama_context_default_params();
1016

11-
private:
12-
Ref<LlamaModel> model;
17+
protected:
18+
static void _bind_methods();
1319

14-
protected:
15-
static void _bind_methods(){};
16-
};
17-
}
20+
public:
21+
void set_model(const Ref<LlamaModel> model);
22+
Ref<LlamaModel> get_model();
23+
virtual void _ready() override;
24+
~LlamaContext();
25+
};
26+
} //namespace godot
1827

1928
#endif

src/llama_model.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ namespace godot {
99
class LlamaModel : public Resource {
1010
GDCLASS(LlamaModel, Resource)
1111

12-
private:
13-
llama_model *model = nullptr;
14-
1512
protected:
1613
static void _bind_methods();
1714

1815
public:
16+
llama_model *model = nullptr;
1917
void load_model( const String &path );
2018
~LlamaModel();
2119
};

0 commit comments

Comments
 (0)