Skip to content

Commit 62125ae

Browse files
authored
zig : fix build (#840)
1 parent 2aae01f commit 62125ae

File tree

5 files changed

+295
-359
lines changed

5 files changed

+295
-359
lines changed

build.zig

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ const builtin = @import("builtin");
44
// Zig Version: 0.11.0
55
// Zig Build Command: zig build
66
// Zig Run Command: zig build -h
7-
// zig build run_dolly-v2
8-
// zig build run_gpt-2
97
// zig build run_gpt-j
10-
// zig build run_gpt-neox
118
// zig build run_mnist
12-
// zig build run_mpt
13-
// zig build run_replit
14-
// zig build run_starcoder
9+
// zig build run_magika
1510
// zig build run_test-grad0
1611
// zig build run_test-mul-mat0
1712
// zig build run_test-mul-mat2
@@ -25,33 +20,35 @@ const builtin = @import("builtin");
2520
// zig build run_zig_test1
2621
// zig build run_zig_test2
2722
// zig build run_zig_test3
28-
pub fn build(b: *std.build.Builder) void {
23+
pub fn build(b: *std.Build) void {
2924
const target = b.standardTargetOptions(.{});
3025
const optimize = b.standardOptimizeOption(.{});
3126
const lib = b.addStaticLibrary(.{
3227
.name = "ggml",
3328
.target = target,
3429
.optimize = optimize,
3530
});
36-
lib.addIncludePath(.{ .path = "./include" });
37-
lib.addIncludePath(.{ .path = "./include/ggml" });
38-
lib.addCSourceFiles(&.{
31+
lib.addIncludePath(b.path("./include"));
32+
lib.addIncludePath(b.path("./include/ggml"));
33+
lib.addCSourceFiles(.{ .files = &.{
3934
"src/ggml.c",
40-
}, &.{"-std=c11"});
35+
"src/ggml-alloc.c",
36+
"src/ggml-backend.c",
37+
"src/ggml-quants.c",
38+
}, .flags = &.{
39+
"-std=c11",
40+
"-D_GNU_SOURCE",
41+
"-D_XOPEN_SOURCE=600",
42+
} });
4143
lib.linkLibC();
4244
lib.linkLibCpp();
4345
b.installArtifact(lib);
4446

4547
// examples
4648
const examples = .{
47-
"dolly-v2",
48-
"gpt-2",
4949
"gpt-j",
50-
"gpt-neox",
50+
"magika",
5151
"mnist",
52-
"mpt",
53-
"replit",
54-
"starcoder",
5552
// "whisper",
5653
};
5754
inline for (examples) |name| {
@@ -60,16 +57,19 @@ pub fn build(b: *std.build.Builder) void {
6057
.target = target,
6158
.optimize = optimize,
6259
});
63-
exe.addIncludePath(.{ .path = "./include" });
64-
exe.addIncludePath(.{ .path = "./include/ggml" });
65-
exe.addIncludePath(.{ .path = "./examples" });
60+
exe.addIncludePath(b.path("./include"));
61+
exe.addIncludePath(b.path("./include/ggml"));
62+
exe.addIncludePath(b.path("./examples"));
6663
// exe.addIncludePath("./examples/whisper");
67-
exe.addCSourceFiles(&.{
68-
std.fmt.comptimePrint("examples/{s}/main.cpp", .{name}),
69-
"examples/common.cpp",
70-
"examples/common-ggml.cpp",
71-
// "examples/whisper/whisper.cpp",
72-
}, &.{"-std=c++11"});
64+
exe.addCSourceFiles(.{
65+
.files = &.{
66+
std.fmt.comptimePrint("examples/{s}/main.cpp", .{name}),
67+
"examples/common.cpp",
68+
"examples/common-ggml.cpp",
69+
// "examples/whisper/whisper.cpp",
70+
},
71+
.flags = &.{"-std=c++11"},
72+
});
7373
exe.linkLibrary(lib);
7474
b.installArtifact(exe);
7575
const run_cmd = b.addRunArtifact(exe);
@@ -88,7 +88,7 @@ pub fn build(b: *std.build.Builder) void {
8888
"test-mul-mat2",
8989
// "test-opt",
9090
// "test-svd0",
91-
// "test-vec0",
91+
"test-vec0",
9292
"test-vec1",
9393
// "test-vec2",
9494
"test0",
@@ -117,11 +117,13 @@ pub fn build(b: *std.build.Builder) void {
117117
.target = target,
118118
.optimize = optimize,
119119
});
120-
exe.addIncludePath(.{ .path = "./include" });
121-
exe.addIncludePath(.{ .path = "./include/ggml" });
122-
exe.addCSourceFiles(&.{
120+
exe.addIncludePath(b.path("./include"));
121+
exe.addIncludePath(b.path("./include/ggml"));
122+
exe.addCSourceFiles(.{ .files = &.{
123123
std.fmt.comptimePrint("tests/{s}.c", .{name}),
124-
}, &.{"-std=c11"});
124+
}, .flags = &.{
125+
"-std=c11",
126+
} });
125127
exe.linkLibrary(lib);
126128
b.installArtifact(exe);
127129
const run_cmd = b.addRunArtifact(exe);
@@ -141,12 +143,12 @@ pub fn build(b: *std.build.Builder) void {
141143
inline for (zig_tests) |name| {
142144
const exe = b.addExecutable(.{
143145
.name = name,
144-
.root_source_file = .{ .path = std.fmt.comptimePrint("tests/{s}.zig", .{name}) },
146+
.root_source_file = b.path(std.fmt.comptimePrint("tests/{s}.zig", .{name})),
145147
.target = target,
146148
.optimize = optimize,
147149
});
148-
exe.addIncludePath(.{ .path = "./include" });
149-
exe.addIncludePath(.{ .path = "./include/ggml" });
150+
exe.addIncludePath(b.path("./include"));
151+
exe.addIncludePath(b.path("./include/ggml"));
150152
exe.linkLibrary(lib);
151153
b.installArtifact(exe);
152154
const run_cmd = b.addRunArtifact(exe);

tests/test0.zig

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const c = @cImport({
55

66
pub fn main() !void {
77
const params = .{
8-
.mem_size = 128*1024*1024,
8+
.mem_size = 128 * 1024 * 1024,
99
.mem_buffer = null,
10-
.no_alloc = false,
10+
.no_alloc = false,
1111
};
1212

1313
const ctx0 = c.ggml_init(params);
@@ -17,23 +17,23 @@ pub fn main() !void {
1717
const t2 = c.ggml_new_tensor_2d(ctx0, c.GGML_TYPE_I16, 10, 20);
1818
const t3 = c.ggml_new_tensor_3d(ctx0, c.GGML_TYPE_I32, 10, 20, 30);
1919

20-
try std.testing.expect(t1.*.n_dims == 1);
21-
try std.testing.expect(t1.*.ne[0] == 10);
22-
try std.testing.expect(t1.*.nb[1] == 10*@sizeOf(f32));
23-
24-
try std.testing.expect(t2.*.n_dims == 2);
25-
try std.testing.expect(t2.*.ne[0] == 10);
26-
try std.testing.expect(t2.*.ne[1] == 20);
27-
try std.testing.expect(t2.*.nb[1] == 10*@sizeOf(i16));
28-
try std.testing.expect(t2.*.nb[2] == 10*20*@sizeOf(i16));
29-
30-
try std.testing.expect(t3.*.n_dims == 3);
31-
try std.testing.expect(t3.*.ne[0] == 10);
32-
try std.testing.expect(t3.*.ne[1] == 20);
33-
try std.testing.expect(t3.*.ne[2] == 30);
34-
try std.testing.expect(t3.*.nb[1] == 10*@sizeOf(i32));
35-
try std.testing.expect(t3.*.nb[2] == 10*20*@sizeOf(i32));
36-
try std.testing.expect(t3.*.nb[3] == 10*20*30*@sizeOf(i32));
20+
try std.testing.expect(c.ggml_n_dims(t1) == 1);
21+
try std.testing.expect(t1.*.ne[0] == 10);
22+
try std.testing.expect(t1.*.nb[1] == 10 * @sizeOf(f32));
23+
24+
try std.testing.expect(c.ggml_n_dims(t2) == 2);
25+
try std.testing.expect(t2.*.ne[0] == 10);
26+
try std.testing.expect(t2.*.ne[1] == 20);
27+
try std.testing.expect(t2.*.nb[1] == 10 * @sizeOf(i16));
28+
try std.testing.expect(t2.*.nb[2] == 10 * 20 * @sizeOf(i16));
29+
30+
try std.testing.expect(c.ggml_n_dims(t3) == 3);
31+
try std.testing.expect(t3.*.ne[0] == 10);
32+
try std.testing.expect(t3.*.ne[1] == 20);
33+
try std.testing.expect(t3.*.ne[2] == 30);
34+
try std.testing.expect(t3.*.nb[1] == 10 * @sizeOf(i32));
35+
try std.testing.expect(t3.*.nb[2] == 10 * 20 * @sizeOf(i32));
36+
try std.testing.expect(t3.*.nb[3] == 10 * 20 * 30 * @sizeOf(i32));
3737

3838
c.ggml_print_objects(ctx0);
3939

0 commit comments

Comments
 (0)