Skip to content

Commit 249c730

Browse files
committed
optimizing build
1 parent de338ef commit 249c730

8 files changed

+134
-75
lines changed

build.zig

+44-45
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub fn build(b: *std.Build) !void {
3434
"The compute backend to use.",
3535
) orelse ComputeBackend.cpu;
3636

37+
const gen_run = b.addSystemCommand(&.{ "python", "binding_generator.py" });
38+
gen_run.addFileArg(b.path("godot_cpp/gdextension/extension_api.json"));
39+
const gen_out = gen_run.addOutputDirectoryArg("godot-cpp-gen");
40+
3741
// godot-llama-cpp
3842
const plugin = b.addSharedLibrary(.{
3943
.name = b.fmt("{s}-{s}-{s}", .{ extension_name, triple, @tagName(optimize) }),
@@ -42,14 +46,16 @@ pub fn build(b: *std.Build) !void {
4246
});
4347
b.installArtifact(plugin);
4448

45-
plugin.addCSourceFiles(.{ .files = try findFilesRecursive(b, "src/", &cfiles_exts) });
46-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "src/" } });
47-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/gdextension/" } });
48-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/include/" } });
49-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/gen/include" } });
50-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "llama.cpp/include" } });
51-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "llama.cpp/common" } });
52-
plugin.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "llama.cpp/ggml/include" } });
49+
plugin.addCSourceFiles(.{ .files = try findFilesRecursive(b, "src", &cfiles_exts) });
50+
plugin.addIncludePath(b.path("src"));
51+
plugin.addIncludePath(b.path("godot_cpp/gdextension"));
52+
plugin.addIncludePath(b.path("godot_cpp/include"));
53+
plugin.addIncludePath(gen_out.path(b, "gen/include"));
54+
plugin.addIncludePath(b.path("llama.cpp/src"));
55+
plugin.addIncludePath(b.path("llama.cpp/include"));
56+
plugin.addIncludePath(b.path("llama.cpp/common"));
57+
plugin.addIncludePath(b.path("llama.cpp/ggml/include"));
58+
plugin.addIncludePath(b.path("llama.cpp/ggml/src"));
5359

5460
// godot-cpp
5561
const lib_godot = b.addStaticLibrary(.{
@@ -58,26 +64,25 @@ pub fn build(b: *std.Build) !void {
5864
.optimize = optimize,
5965
});
6066
plugin.linkLibrary(lib_godot);
61-
62-
b.build_root.handle.access("godot_cpp/gen", .{}) catch |e| {
63-
switch (e) {
64-
error.FileNotFound => {
65-
_ = try std.process.Child.run(.{
66-
.allocator = b.allocator,
67-
.argv = &.{ "python", "binding_generator.py", "godot_cpp/gdextension/extension_api.json", "godot_cpp" },
68-
.cwd_dir = b.build_root.handle,
69-
});
70-
},
71-
else => {},
72-
}
73-
};
7467
lib_godot.linkLibCpp();
75-
lib_godot.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/gdextension/" } });
76-
lib_godot.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/include/" } });
77-
lib_godot.addIncludePath(.{ .src_path = .{ .owner = b, .sub_path = "godot_cpp/gen/include" } });
68+
lib_godot.step.dependOn(&gen_run.step);
69+
70+
lib_godot.addIncludePath(b.path("godot_cpp/gdextension"));
71+
lib_godot.addIncludePath(b.path("godot_cpp/include"));
72+
lib_godot.addIncludePath(gen_out.path(b, "gen/include"));
73+
74+
const concat_gen_exe = b.addExecutable(.{
75+
.name = "concat_gen",
76+
.target = target,
77+
.optimize = optimize,
78+
.root_source_file = b.path("tools/concat_files.zig"),
79+
});
80+
var concat_gen_run = b.addRunArtifact(concat_gen_exe);
81+
concat_gen_run.addDirectoryArg(gen_out.path(b, "gen/src"));
82+
const concat_gen_out = concat_gen_run.addOutputFileArg("gen_concat.cpp");
7883
const lib_godot_sources = try findFilesRecursive(b, "godot_cpp/src", &cfiles_exts);
79-
const lib_godot_gen_sources = try findFilesRecursive(b, "godot_cpp/gen/src", &cfiles_exts);
80-
lib_godot.addCSourceFiles(.{ .files = lib_godot_gen_sources, .flags = &.{ "-std=c++17", "-fno-exceptions" } });
84+
85+
lib_godot.addCSourceFile(.{ .file = concat_gen_out, .flags = &.{ "-std=c++17", "-fno-exceptions" } });
8186
lib_godot.addCSourceFiles(.{ .files = lib_godot_sources, .flags = &.{ "-std=c++17", "-fno-exceptions" } });
8287

8388
// llama.cpp
@@ -91,18 +96,20 @@ pub fn build(b: *std.Build) !void {
9196
var base_flags = std.ArrayList([]const u8).init(b.allocator);
9297
var c_flags = std.ArrayList([]const u8).init(b.allocator);
9398
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
94-
var include_paths = std.ArrayList([]const u8).init(b.allocator);
99+
var include_paths = std.ArrayList(std.Build.LazyPath).init(b.allocator);
95100
var system_libs = std.ArrayList([]const u8).init(b.allocator);
96101
var library_paths = std.ArrayList(std.Build.LazyPath).init(b.allocator);
97102

98103
var sources = std.ArrayList(Source).init(b.allocator);
99104

100105
try c_flags.append("-std=c11");
101-
try cpp_flags.append("-std=c++11");
106+
try cpp_flags.append("-std=c++17");
102107
try include_paths.appendSlice(&.{
103-
"llama.cpp/include",
104-
"llama.cpp/common",
105-
"llama.cpp/ggml/include",
108+
b.path("llama.cpp/src"),
109+
b.path("llama.cpp/include"),
110+
b.path("llama.cpp/common"),
111+
b.path("llama.cpp/ggml/include"),
112+
b.path("llama.cpp/ggml/src"),
106113
});
107114

108115
switch (target.result.os.tag) {
@@ -127,13 +134,13 @@ pub fn build(b: *std.Build) !void {
127134
const expand_metal = b.addExecutable(.{
128135
.name = "expand_metal",
129136
.target = target,
130-
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "tools/expand_metal.zig" } },
137+
.root_source_file = b.path("tools/expand_metal.zig"),
131138
});
132139
var run_expand_metal = b.addRunArtifact(expand_metal);
133140
run_expand_metal.addArg("--metal-file");
134-
run_expand_metal.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = "llama.cpp/ggml/src/ggml-metal.metal" } });
141+
run_expand_metal.addFileArg(b.path("llama.cpp/ggml/src/ggml-metal.metal"));
135142
run_expand_metal.addArg("--common-file");
136-
run_expand_metal.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = "llama.cpp/ggml/src/ggml-common.h" } });
143+
run_expand_metal.addFileArg(b.path("llama.cpp/ggml/src/ggml-common.h"));
137144
run_expand_metal.addArg("--output-file");
138145
const metal_expanded = run_expand_metal.addOutputFileArg("ggml-metal.metal");
139146
const install_metal = b.addInstallFileWithDir(metal_expanded, .lib, "ggml-metal.metal");
@@ -148,7 +155,7 @@ pub fn build(b: *std.Build) !void {
148155

149156
const vk_library_path = b.pathJoin(&.{ vulkan_sdk, "lib" });
150157
const vk_include_path = b.pathJoin(&.{ vulkan_sdk, "include" });
151-
try include_paths.append(vk_include_path);
158+
try include_paths.append(std.Build.LazyPath{ .cwd_relative = vk_include_path });
152159
try library_paths.append(.{ .cwd_relative = vk_library_path });
153160
try system_libs.append("vulkan");
154161

@@ -195,7 +202,6 @@ pub fn build(b: *std.Build) !void {
195202
.name = source.name,
196203
.target = target,
197204
.optimize = optimize,
198-
.pic = true,
199205
});
200206
lib_llama_cpp.addObject(obj);
201207
if (source.dependencies) |deps| {
@@ -206,12 +212,7 @@ pub fn build(b: *std.Build) !void {
206212
const file = if (source.root) |root|
207213
root.path(b, source.source_file)
208214
else
209-
std.Build.LazyPath{
210-
.src_path = .{
211-
.owner = b,
212-
.sub_path = source.source_file,
213-
},
214-
};
215+
b.path(source.source_file);
215216
const extension = std.meta.stringToEnum(
216217
Extension,
217218
std.fs.path.extension(source.source_file),
@@ -225,9 +226,7 @@ pub fn build(b: *std.Build) !void {
225226
.flags = flags,
226227
});
227228
for (include_paths.items) |path| {
228-
obj.addIncludePath(.{
229-
.src_path = .{ .owner = b, .sub_path = path },
230-
});
229+
obj.addIncludePath(path);
231230
}
232231
for (system_libs.items) |lib| {
233232
obj.linkSystemLibrary(lib);

godot_cpp

Submodule godot_cpp updated 111 files

src/llama_context.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void LlamaContext::_ready() {
6767
return;
6868
}
6969

70+
model->load_model();
71+
7072
if (model->model == NULL) {
7173
UtilityFunctions::printerr(vformat("%s: Failed to initialize llama context, model property not defined", __func__));
7274
return;
@@ -269,24 +271,24 @@ Ref<LlamaModel> LlamaContext::get_model() {
269271
return model;
270272
}
271273

272-
int LlamaContext::get_seed() {
274+
uint32_t LlamaContext::get_seed() {
273275
return ctx_params.seed;
274276
}
275-
void LlamaContext::set_seed(int seed) {
277+
void LlamaContext::set_seed(uint32_t seed) {
276278
ctx_params.seed = seed;
277279
}
278280

279-
int LlamaContext::get_n_ctx() {
281+
uint32_t LlamaContext::get_n_ctx() {
280282
return ctx_params.n_ctx;
281283
}
282-
void LlamaContext::set_n_ctx(int n_ctx) {
284+
void LlamaContext::set_n_ctx(uint32_t n_ctx) {
283285
ctx_params.n_ctx = n_ctx;
284286
}
285287

286-
int LlamaContext::get_n_len() {
288+
int32_t LlamaContext::get_n_len() {
287289
return n_len;
288290
}
289-
void LlamaContext::set_n_len(int n_len) {
291+
void LlamaContext::set_n_len(int32_t n_len) {
290292
this->n_len = n_len;
291293
}
292294

@@ -334,7 +336,11 @@ void LlamaContext::_exit_tree() {
334336
if (ctx) {
335337
llama_free(ctx);
336338
}
337-
338-
llama_sampling_free(sampling_ctx);
339+
if (model->model) {
340+
llama_free_model(model->model);
341+
}
342+
if (sampling_ctx) {
343+
llama_sampling_free(sampling_ctx);
344+
}
339345
llama_backend_free();
340346
}

src/llama_context.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LlamaContext : public Node {
2525
llama_sampling_context *sampling_ctx = nullptr;
2626
llama_context_params ctx_params;
2727
llama_sampling_params sampling_params;
28-
int n_len = 1024;
28+
int32_t n_len = 1024;
2929
int request_id = 0;
3030
Vector<completion_request> completion_requests;
3131

@@ -45,12 +45,12 @@ class LlamaContext : public Node {
4545
int request_completion(const String &prompt);
4646
void __thread_loop();
4747

48-
int get_seed();
49-
void set_seed(int seed);
50-
int get_n_ctx();
51-
void set_n_ctx(int n_ctx);
52-
int get_n_len();
53-
void set_n_len(int n_len);
48+
uint32_t get_seed();
49+
void set_seed(uint32_t seed);
50+
uint32_t get_n_ctx();
51+
void set_n_ctx(uint32_t n_ctx);
52+
int32_t get_n_len();
53+
void set_n_len(int32_t n_len);
5454
float get_temperature();
5555
void set_temperature(float temperature);
5656
float get_top_p();

src/llama_model.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using namespace godot;
88

99
void LlamaModel::_bind_methods() {
10-
ClassDB::bind_method(D_METHOD("load_model", "path"), &LlamaModel::load_model);
10+
ClassDB::bind_method(D_METHOD("load_model"), &LlamaModel::load_model);
1111

1212
ClassDB::bind_method(D_METHOD("get_n_gpu_layers"), &LlamaModel::get_n_gpu_layers);
1313
ClassDB::bind_method(D_METHOD("set_n_gpu_layers", "n"), &LlamaModel::set_n_gpu_layers);
@@ -18,12 +18,12 @@ LlamaModel::LlamaModel() {
1818
model_params = llama_model_default_params();
1919
}
2020

21-
void LlamaModel::load_model(const String &path) {
21+
void LlamaModel::load_model() {
2222
if (model) {
23-
llama_free_model(model);
23+
return;
2424
}
2525

26-
String absPath = ProjectSettings::get_singleton()->globalize_path(path);
26+
String absPath = ProjectSettings::get_singleton()->globalize_path(get_path());
2727

2828
model = llama_load_model_from_file(absPath.utf8().get_data(), model_params);
2929

@@ -35,11 +35,11 @@ void LlamaModel::load_model(const String &path) {
3535
UtilityFunctions::print(vformat("%s: Model loaded from %s", __func__, absPath));
3636
}
3737

38-
int LlamaModel::get_n_gpu_layers() {
38+
int32_t LlamaModel::get_n_gpu_layers() {
3939
return model_params.n_gpu_layers;
4040
}
4141

42-
void LlamaModel::set_n_gpu_layers(int n) {
42+
void LlamaModel::set_n_gpu_layers(int32_t n) {
4343
model_params.n_gpu_layers = n;
4444
}
4545

src/llama_model.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class LlamaModel : public Resource {
1717

1818
public:
1919
llama_model *model = nullptr;
20-
void load_model(const String &path);
20+
void load_model();
2121

22-
int get_n_gpu_layers();
23-
void set_n_gpu_layers(int n);
22+
int32_t get_n_gpu_layers();
23+
void set_n_gpu_layers(int32_t n);
2424

2525
LlamaModel();
2626
~LlamaModel();

src/llama_model_loader.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ Variant godot::LlamaModelLoader::_load(const String &path, const String &origina
1919
return ERR_FILE_NOT_FOUND;
2020
}
2121

22-
if (Engine::get_singleton()->is_editor_hint()) {
23-
return { model };
24-
}
25-
26-
model->load_model(path);
22+
model->set_path(path);
2723

2824
return { model };
2925
}

tools/concat_files.zig

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const std = @import("std");
2+
3+
const usage = "Usage: ./concat_files <input-dir-1> <input-dir-2> ... <output-file>";
4+
5+
pub fn main() !void {
6+
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
7+
defer arena_state.deinit();
8+
const arena = arena_state.allocator();
9+
10+
const args = try std.process.argsAlloc(arena);
11+
12+
if (args.len < 3) {
13+
std.debug.panic("expected at least 2 arguments", .{});
14+
}
15+
16+
var input_dirs = std.ArrayList([]const u8).init(arena);
17+
var output_file_path: []const u8 = undefined;
18+
19+
{
20+
var i: usize = 1;
21+
while (i < args.len) : (i += 1) {
22+
const arg = args[i];
23+
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
24+
try std.io.getStdOut().writeAll(usage);
25+
return std.process.cleanExit();
26+
} else if (i == args.len - 1) {
27+
output_file_path = arg;
28+
} else {
29+
try input_dirs.append(arg);
30+
}
31+
}
32+
}
33+
34+
const cwd = std.fs.cwd();
35+
const output = try cwd.createFile(output_file_path, .{});
36+
var pos: u64 = 0;
37+
38+
for (input_dirs.items) |dir_path| {
39+
var dir = try cwd.openDir(dir_path, .{ .iterate = true });
40+
defer dir.close();
41+
42+
var walker = try dir.walk(arena);
43+
defer walker.deinit();
44+
45+
while (try walker.next()) |entry| {
46+
if (entry.kind != .file) continue;
47+
const file = try entry.dir.openFile(entry.basename, .{});
48+
defer file.close();
49+
50+
const len = (try file.stat()).size;
51+
const bytes_copied = try file.copyRangeAll(0, output, pos, len);
52+
pos += bytes_copied;
53+
54+
try output.pwriteAll("\n", pos);
55+
pos += 1;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)