From: Emmanuel Durand Date: Wed, 5 Jun 2024 08:41:51 +0000 (-0400) Subject: zig : fix build (#840) X-Git-Tag: upstream/0.0.1642~631 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=62125ae6b88258477bac3ae40e24e7c4eac254bc;p=pkg%2Fggml%2Fsources%2Fggml zig : fix build (#840) --- diff --git a/build.zig b/build.zig index 5aa379dc..ffe471f2 100644 --- a/build.zig +++ b/build.zig @@ -4,14 +4,9 @@ const builtin = @import("builtin"); // Zig Version: 0.11.0 // Zig Build Command: zig build // Zig Run Command: zig build -h -// zig build run_dolly-v2 -// zig build run_gpt-2 // zig build run_gpt-j -// zig build run_gpt-neox // zig build run_mnist -// zig build run_mpt -// zig build run_replit -// zig build run_starcoder +// zig build run_magika // zig build run_test-grad0 // zig build run_test-mul-mat0 // zig build run_test-mul-mat2 @@ -25,7 +20,7 @@ const builtin = @import("builtin"); // zig build run_zig_test1 // zig build run_zig_test2 // zig build run_zig_test3 -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const lib = b.addStaticLibrary(.{ @@ -33,25 +28,27 @@ pub fn build(b: *std.build.Builder) void { .target = target, .optimize = optimize, }); - lib.addIncludePath(.{ .path = "./include" }); - lib.addIncludePath(.{ .path = "./include/ggml" }); - lib.addCSourceFiles(&.{ + lib.addIncludePath(b.path("./include")); + lib.addIncludePath(b.path("./include/ggml")); + lib.addCSourceFiles(.{ .files = &.{ "src/ggml.c", - }, &.{"-std=c11"}); + "src/ggml-alloc.c", + "src/ggml-backend.c", + "src/ggml-quants.c", + }, .flags = &.{ + "-std=c11", + "-D_GNU_SOURCE", + "-D_XOPEN_SOURCE=600", + } }); lib.linkLibC(); lib.linkLibCpp(); b.installArtifact(lib); // examples const examples = .{ - "dolly-v2", - "gpt-2", "gpt-j", - "gpt-neox", + "magika", "mnist", - "mpt", - "replit", - "starcoder", // "whisper", }; inline for (examples) |name| { @@ -60,16 +57,19 @@ pub fn build(b: *std.build.Builder) void { .target = target, .optimize = optimize, }); - exe.addIncludePath(.{ .path = "./include" }); - exe.addIncludePath(.{ .path = "./include/ggml" }); - exe.addIncludePath(.{ .path = "./examples" }); + exe.addIncludePath(b.path("./include")); + exe.addIncludePath(b.path("./include/ggml")); + exe.addIncludePath(b.path("./examples")); // exe.addIncludePath("./examples/whisper"); - exe.addCSourceFiles(&.{ - std.fmt.comptimePrint("examples/{s}/main.cpp", .{name}), - "examples/common.cpp", - "examples/common-ggml.cpp", - // "examples/whisper/whisper.cpp", - }, &.{"-std=c++11"}); + exe.addCSourceFiles(.{ + .files = &.{ + std.fmt.comptimePrint("examples/{s}/main.cpp", .{name}), + "examples/common.cpp", + "examples/common-ggml.cpp", + // "examples/whisper/whisper.cpp", + }, + .flags = &.{"-std=c++11"}, + }); exe.linkLibrary(lib); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -88,7 +88,7 @@ pub fn build(b: *std.build.Builder) void { "test-mul-mat2", // "test-opt", // "test-svd0", - // "test-vec0", + "test-vec0", "test-vec1", // "test-vec2", "test0", @@ -117,11 +117,13 @@ pub fn build(b: *std.build.Builder) void { .target = target, .optimize = optimize, }); - exe.addIncludePath(.{ .path = "./include" }); - exe.addIncludePath(.{ .path = "./include/ggml" }); - exe.addCSourceFiles(&.{ + exe.addIncludePath(b.path("./include")); + exe.addIncludePath(b.path("./include/ggml")); + exe.addCSourceFiles(.{ .files = &.{ std.fmt.comptimePrint("tests/{s}.c", .{name}), - }, &.{"-std=c11"}); + }, .flags = &.{ + "-std=c11", + } }); exe.linkLibrary(lib); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -141,12 +143,12 @@ pub fn build(b: *std.build.Builder) void { inline for (zig_tests) |name| { const exe = b.addExecutable(.{ .name = name, - .root_source_file = .{ .path = std.fmt.comptimePrint("tests/{s}.zig", .{name}) }, + .root_source_file = b.path(std.fmt.comptimePrint("tests/{s}.zig", .{name})), .target = target, .optimize = optimize, }); - exe.addIncludePath(.{ .path = "./include" }); - exe.addIncludePath(.{ .path = "./include/ggml" }); + exe.addIncludePath(b.path("./include")); + exe.addIncludePath(b.path("./include/ggml")); exe.linkLibrary(lib); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/tests/test0.zig b/tests/test0.zig index e47bf365..26994d89 100644 --- a/tests/test0.zig +++ b/tests/test0.zig @@ -5,9 +5,9 @@ const c = @cImport({ pub fn main() !void { const params = .{ - .mem_size = 128*1024*1024, + .mem_size = 128 * 1024 * 1024, .mem_buffer = null, - .no_alloc = false, + .no_alloc = false, }; const ctx0 = c.ggml_init(params); @@ -17,23 +17,23 @@ pub fn main() !void { const t2 = c.ggml_new_tensor_2d(ctx0, c.GGML_TYPE_I16, 10, 20); const t3 = c.ggml_new_tensor_3d(ctx0, c.GGML_TYPE_I32, 10, 20, 30); - try std.testing.expect(t1.*.n_dims == 1); - try std.testing.expect(t1.*.ne[0] == 10); - try std.testing.expect(t1.*.nb[1] == 10*@sizeOf(f32)); - - try std.testing.expect(t2.*.n_dims == 2); - try std.testing.expect(t2.*.ne[0] == 10); - try std.testing.expect(t2.*.ne[1] == 20); - try std.testing.expect(t2.*.nb[1] == 10*@sizeOf(i16)); - try std.testing.expect(t2.*.nb[2] == 10*20*@sizeOf(i16)); - - try std.testing.expect(t3.*.n_dims == 3); - try std.testing.expect(t3.*.ne[0] == 10); - try std.testing.expect(t3.*.ne[1] == 20); - try std.testing.expect(t3.*.ne[2] == 30); - try std.testing.expect(t3.*.nb[1] == 10*@sizeOf(i32)); - try std.testing.expect(t3.*.nb[2] == 10*20*@sizeOf(i32)); - try std.testing.expect(t3.*.nb[3] == 10*20*30*@sizeOf(i32)); + try std.testing.expect(c.ggml_n_dims(t1) == 1); + try std.testing.expect(t1.*.ne[0] == 10); + try std.testing.expect(t1.*.nb[1] == 10 * @sizeOf(f32)); + + try std.testing.expect(c.ggml_n_dims(t2) == 2); + try std.testing.expect(t2.*.ne[0] == 10); + try std.testing.expect(t2.*.ne[1] == 20); + try std.testing.expect(t2.*.nb[1] == 10 * @sizeOf(i16)); + try std.testing.expect(t2.*.nb[2] == 10 * 20 * @sizeOf(i16)); + + try std.testing.expect(c.ggml_n_dims(t3) == 3); + try std.testing.expect(t3.*.ne[0] == 10); + try std.testing.expect(t3.*.ne[1] == 20); + try std.testing.expect(t3.*.ne[2] == 30); + try std.testing.expect(t3.*.nb[1] == 10 * @sizeOf(i32)); + try std.testing.expect(t3.*.nb[2] == 10 * 20 * @sizeOf(i32)); + try std.testing.expect(t3.*.nb[3] == 10 * 20 * 30 * @sizeOf(i32)); c.ggml_print_objects(ctx0); diff --git a/tests/test1.zig b/tests/test1.zig index f331acbd..507562c4 100644 --- a/tests/test1.zig +++ b/tests/test1.zig @@ -7,9 +7,9 @@ pub fn main() !void { const n_threads = 2; const params = .{ - .mem_size = 128*1024*1024, + .mem_size = 128 * 1024 * 1024, .mem_buffer = null, - .no_alloc = false, + .no_alloc = false, }; const ctx0 = c.ggml_init(params); @@ -29,38 +29,40 @@ pub fn main() !void { c.ggml_print_objects(ctx0); - const gf = c.ggml_build_forward(f); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, f); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x, 2.0); _ = c.ggml_set_f32(a, 3.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(f.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("f = {d:.6}\n", .{c.ggml_get_f32_1d(f, 0)}); std.debug.print("df/dx = {d:.6}\n", .{c.ggml_get_f32_1d(x.*.grad, 0)}); - try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 12.0); - try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 12.0); _ = c.ggml_set_f32(x, 3.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(f.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("f = {d:.6}\n", .{c.ggml_get_f32_1d(f, 0)}); std.debug.print("df/dx = {d:.6}\n", .{c.ggml_get_f32_1d(x.*.grad, 0)}); - try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 27.0); - try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 18.0); + try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 27.0); + try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 18.0); - c.ggml_graph_dump_dot(&gf, null, "test1-1-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-1-backward.dot"); + c.ggml_graph_dump_dot(gf, null, "test1-1-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-1-backward.dot"); } ///////////////////////////////////////////////////////////// @@ -79,40 +81,44 @@ pub fn main() !void { const y = c.ggml_add(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x1, x2)); - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)}); std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)}); - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 7.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 7.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); const g1 = x1.*.grad; const g2 = x2.*.grad; - const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), true); + const gbb = c.ggml_graph_dup(ctx0, @constCast(gb)); - c.ggml_graph_reset(@constCast(&gb)); + c.ggml_build_backward_expand(ctx0, @constCast(gb), @constCast(gbb), true); + + c.ggml_graph_reset(@constCast(gb)); _ = c.ggml_set_f32(g1.*.grad, 1.0); _ = c.ggml_set_f32(g2.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gbb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gbb), n_threads); - std.debug.print("H * [1, 1] = [ {d:.6} {d:.6} ]\n", .{c.ggml_get_f32_1d(x1.*.grad, 0), c.ggml_get_f32_1d(x2.*.grad, 0)}); + std.debug.print("H * [1, 1] = [ {d:.6} {d:.6} ]\n", .{ c.ggml_get_f32_1d(x1.*.grad, 0), c.ggml_get_f32_1d(x2.*.grad, 0) }); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0); - c.ggml_graph_dump_dot(&gf, null, "test1-2-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-2-backward.dot"); + c.ggml_graph_dump_dot(gf, null, "test1-2-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-2-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -126,27 +132,29 @@ pub fn main() !void { const y = c.ggml_mul(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x1, x2)), x1); - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 3.0); _ = c.ggml_set_f32(x2, 4.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)}); std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)}); - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 63.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 51.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 9.0); + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 63.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 51.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 9.0); - c.ggml_graph_dump_dot(&gf, null, "test1-3-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-3-backward.dot"); + c.ggml_graph_dump_dot(gf, null, "test1-3-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-3-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -162,54 +170,57 @@ pub fn main() !void { const y = c.ggml_mul(ctx0, c.ggml_mul(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x2, x2)), x3); - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 1.0); _ = c.ggml_set_f32(x2, 2.0); _ = c.ggml_set_f32(x3, 3.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)}); std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)}); std.debug.print("df/dx3 = {d:.6}\n", .{c.ggml_get_f32_1d(x3.*.grad, 0)}); - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 24.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 12.0); - try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 4.0); + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 24.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 4.0); const g1 = x1.*.grad; const g2 = x2.*.grad; const g3 = x3.*.grad; - const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), true); + const gbb = c.ggml_graph_dup(ctx0, @constCast(gb)); + + c.ggml_build_backward_expand(ctx0, @constCast(gb), @constCast(gbb), true); - c.ggml_graph_reset(@constCast(&gb)); + c.ggml_graph_reset(@constCast(gb)); _ = c.ggml_set_f32(g1.*.grad, 1.0); _ = c.ggml_set_f32(g2.*.grad, 1.0); _ = c.ggml_set_f32(g3.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gbb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gbb), n_threads); - std.debug.print("H * [1, 1, 1] = [ {d:.6} {d:.6} {d:.6}]\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x3.*.grad, 0), - }); + std.debug.print("H * [1, 1, 1] = [ {d:.6} {d:.6} {d:.6}]\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x3.*.grad, 0), + }); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 56.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 34.0); - try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 12.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 56.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 34.0); + try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 12.0); - c.ggml_graph_dump_dot(&gf, null, "test1-4-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-4-backward.dot"); + c.ggml_graph_dump_dot(gf, null, "test1-4-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-4-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -223,41 +234,41 @@ pub fn main() !void { const y = c.ggml_sum(ctx0, c.ggml_mul(ctx0, x1, x2)); - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 3.0); _ = c.ggml_set_f32(x2, 5.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); - std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x1.*.grad, 1), - c.ggml_get_f32_1d(x1.*.grad, 2), - }); - std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 1), - c.ggml_get_f32_1d(x2.*.grad, 2), - }); - - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 45.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 5.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 5.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 5.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); - - c.ggml_graph_dump_dot(&gf, null, "test1-5-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-5-backward.dot"); + std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x1.*.grad, 1), + c.ggml_get_f32_1d(x1.*.grad, 2), + }); + std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 1), + c.ggml_get_f32_1d(x2.*.grad, 2), + }); + + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 45.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 5.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 5.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 5.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); + + c.ggml_graph_dump_dot(gf, null, "test1-5-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-5-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -270,51 +281,43 @@ pub fn main() !void { c.ggml_set_param(ctx0, x2); const y = - c.ggml_sum(ctx0, - c.ggml_add(ctx0, - c.ggml_mul(ctx0, x1, x2), - c.ggml_mul(ctx0, - c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1), - c.ggml_mul(ctx0, x1, x1) - ) - ) - ); - - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + c.ggml_sum(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, x1, x2), c.ggml_mul(ctx0, c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1), c.ggml_mul(ctx0, x1, x1)))); + + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 3.0); _ = c.ggml_set_f32(x2, 5.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); - std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x1.*.grad, 1), - c.ggml_get_f32_1d(x1.*.grad, 2), - }); - std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 1), - c.ggml_get_f32_1d(x2.*.grad, 2), - }); - - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == -9.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -7.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -7.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -7.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); - - c.ggml_graph_dump_dot(&gf, null, "test1-6-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-6-backward.dot"); + std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x1.*.grad, 1), + c.ggml_get_f32_1d(x1.*.grad, 2), + }); + std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 1), + c.ggml_get_f32_1d(x2.*.grad, 2), + }); + + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == -9.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -7.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -7.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -7.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); + + c.ggml_graph_dump_dot(gf, null, "test1-6-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-6-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -327,51 +330,43 @@ pub fn main() !void { c.ggml_set_param(ctx0, x2); const y = - c.ggml_sum(ctx0, - c.ggml_sub(ctx0, - c.ggml_mul(ctx0, x1, x2), - c.ggml_mul(ctx0, - c.ggml_mul(ctx0, x1, x1), - c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1) - ) - ) - ); - - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + c.ggml_sum(ctx0, c.ggml_sub(ctx0, c.ggml_mul(ctx0, x1, x2), c.ggml_mul(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1)))); + + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 3.0); _ = c.ggml_set_f32(x2, 5.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); - std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x1.*.grad, 1), - c.ggml_get_f32_1d(x1.*.grad, 2), - }); - std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 1), - c.ggml_get_f32_1d(x2.*.grad, 2), - }); - - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 99.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 17.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 17.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 17.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); - - c.ggml_graph_dump_dot(&gf, null, "test1-7-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-7-backward.dot"); + std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x1.*.grad, 1), + c.ggml_get_f32_1d(x1.*.grad, 2), + }); + std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 1), + c.ggml_get_f32_1d(x2.*.grad, 2), + }); + + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 99.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 17.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 17.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 17.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0); + + c.ggml_graph_dump_dot(gf, null, "test1-7-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-7-backward.dot"); } /////////////////////////////////////////////////////////////// @@ -384,75 +379,71 @@ pub fn main() !void { c.ggml_set_param(ctx0, x2); const y = - c.ggml_abs(ctx0, - c.ggml_sub(ctx0, x1, x2) - ); + c.ggml_abs(ctx0, c.ggml_sub(ctx0, x1, x2)); - const gf = c.ggml_build_forward(y); - const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false); + const gf = c.ggml_new_graph_custom(ctx0, c.GGML_DEFAULT_GRAPH_SIZE, true); + c.ggml_build_forward_expand(gf, y); + const gb = c.ggml_graph_dup(ctx0, @constCast(gf)); + c.ggml_build_backward_expand(ctx0, @constCast(gf), @constCast(gb), false); _ = c.ggml_set_f32(x1, 3.0); _ = c.ggml_set_f32(x2, 5.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); - std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x1.*.grad, 1), - c.ggml_get_f32_1d(x1.*.grad, 2), - }); - std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 1), - c.ggml_get_f32_1d(x2.*.grad, 2), - }); - - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -1.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -1.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 1.0); + std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x1.*.grad, 1), + c.ggml_get_f32_1d(x1.*.grad, 2), + }); + std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 1), + c.ggml_get_f32_1d(x2.*.grad, 2), + }); + + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -1.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -1.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 1.0); _ = c.ggml_set_f32(x1, 7.0); _ = c.ggml_set_f32(x2, 5.0); - c.ggml_graph_reset(@constCast(&gf)); + c.ggml_graph_reset(@constCast(gf)); _ = c.ggml_set_f32(y.*.grad, 1.0); - c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads); + _ = c.ggml_graph_compute_with_ctx(ctx0, @constCast(gb), n_threads); std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)}); - std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x1.*.grad, 0), - c.ggml_get_f32_1d(x1.*.grad, 1), - c.ggml_get_f32_1d(x1.*.grad, 2), - }); - std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", - .{ - c.ggml_get_f32_1d(x2.*.grad, 0), - c.ggml_get_f32_1d(x2.*.grad, 1), - c.ggml_get_f32_1d(x2.*.grad, 2), - }); - - try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 1.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 1.0); - try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == -1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == -1.0); - try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == -1.0); - - c.ggml_graph_dump_dot(&gf, null, "test1-8-forward.dot"); - c.ggml_graph_dump_dot(&gb, &gf, "test1-8-backward.dot"); + std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x1.*.grad, 0), + c.ggml_get_f32_1d(x1.*.grad, 1), + c.ggml_get_f32_1d(x1.*.grad, 2), + }); + std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n", .{ + c.ggml_get_f32_1d(x2.*.grad, 0), + c.ggml_get_f32_1d(x2.*.grad, 1), + c.ggml_get_f32_1d(x2.*.grad, 2), + }); + + try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == -1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == -1.0); + try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == -1.0); + + c.ggml_graph_dump_dot(gf, null, "test1-8-forward.dot"); + c.ggml_graph_dump_dot(gb, gf, "test1-8-backward.dot"); } _ = try std.io.getStdIn().reader().readByte(); diff --git a/tests/test2.zig b/tests/test2.zig index 667de967..7c68d6d1 100644 --- a/tests/test2.zig +++ b/tests/test2.zig @@ -5,23 +5,23 @@ const c = @cImport({ }); fn is_close(a: f32, b: f32, epsilon: f32) bool { - return std.math.fabs(a - b) < epsilon; + return @abs(a - b) < epsilon; } pub fn main() !void { const params = .{ - .mem_size = 128*1024*1024, + .mem_size = 128 * 1024 * 1024, .mem_buffer = null, - .no_alloc = false, + .no_alloc = false, }; - var opt_params = c.ggml_opt_default_params(c.GGML_OPT_LBFGS); - + var opt_params = c.ggml_opt_default_params(c.GGML_OPT_TYPE_LBFGS); + const nthreads = try Thread.getCpuCount(); opt_params.n_threads = @intCast(nthreads); std.debug.print("test2: n_threads:{}\n", .{opt_params.n_threads}); - const xi = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; + const xi = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; const yi = [_]f32{ 15.0, 25.0, 35.0, 45.0, 55.0, 65.0, 75.0, 85.0, 95.0, 105.0 }; const n = xi.len; @@ -49,60 +49,38 @@ pub fn main() !void { // f = sum_i[(t0 + t1*x_i - y_i)^2]/(2n) const f = - c.ggml_div(ctx0, - c.ggml_sum(ctx0, - c.ggml_sqr(ctx0, - c.ggml_sub(ctx0, - c.ggml_add(ctx0, - c.ggml_mul(ctx0, x, c.ggml_repeat(ctx0, t1, x)), - c.ggml_repeat(ctx0, t0, x)), - y) - ) - ), - c.ggml_new_f32(ctx0, @as(f32, 2.0)*n)); + c.ggml_div(ctx0, c.ggml_sum(ctx0, c.ggml_sqr(ctx0, c.ggml_sub(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, x, c.ggml_repeat(ctx0, t1, x)), c.ggml_repeat(ctx0, t0, x)), y))), c.ggml_new_f32(ctx0, @as(f32, 2.0) * n)); const res = c.ggml_opt(null, opt_params, f); std.debug.print("t0 = {d:.6}\n", .{c.ggml_get_f32_1d(t0, 0)}); std.debug.print("t1 = {d:.6}\n", .{c.ggml_get_f32_1d(t1, 0)}); - try std.testing.expect(res == c.GGML_OPT_OK); - try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 5.0, 1e-3)); + try std.testing.expect(res == c.GGML_OPT_RESULT_OK); + try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 5.0, 1e-3)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t1, 0), 10.0, 1e-3)); } { const t0 = c.ggml_new_f32(ctx0, -1.0); - const t1 = c.ggml_new_f32(ctx0, 9.0); + const t1 = c.ggml_new_f32(ctx0, 9.0); _ = c.ggml_set_param(ctx0, t0); _ = c.ggml_set_param(ctx0, t1); // f = 0.5*sum_i[abs(t0 + t1*x_i - y_i)]/n const f = - c.ggml_mul(ctx0, - c.ggml_new_f32(ctx0, @as(f32, 1.0)/(2*n)), - c.ggml_sum(ctx0, - c.ggml_abs(ctx0, - c.ggml_sub(ctx0, - c.ggml_add(ctx0, - c.ggml_mul(ctx0, x, c.ggml_repeat(ctx0, t1, x)), - c.ggml_repeat(ctx0, t0, x)), - y) - ) - ) - ); - + c.ggml_mul(ctx0, c.ggml_new_f32(ctx0, @as(f32, 1.0) / (2 * n)), c.ggml_sum(ctx0, c.ggml_abs(ctx0, c.ggml_sub(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, x, c.ggml_repeat(ctx0, t1, x)), c.ggml_repeat(ctx0, t0, x)), y)))); const res = c.ggml_opt(null, opt_params, f); - try std.testing.expect(res == c.GGML_OPT_OK); - try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 5.0, 1e-2)); + try std.testing.expect(res == c.GGML_OPT_RESULT_OK); + try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 5.0, 1e-2)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t1, 0), 10.0, 1e-2)); } { - const t0 = c.ggml_new_f32(ctx0, 5.0); + const t0 = c.ggml_new_f32(ctx0, 5.0); const t1 = c.ggml_new_f32(ctx0, -4.0); _ = c.ggml_set_param(ctx0, t0); @@ -110,15 +88,12 @@ pub fn main() !void { // f = t0^2 + t1^2 const f = - c.ggml_add(ctx0, - c.ggml_sqr(ctx0, t0), - c.ggml_sqr(ctx0, t1) - ); + c.ggml_add(ctx0, c.ggml_sqr(ctx0, t0), c.ggml_sqr(ctx0, t1)); const res = c.ggml_opt(null, opt_params, f); - try std.testing.expect(res == c.GGML_OPT_OK); - try std.testing.expect(is_close(c.ggml_get_f32_1d(f, 0), 0.0, 1e-3)); + try std.testing.expect(res == c.GGML_OPT_RESULT_OK); + try std.testing.expect(is_close(c.ggml_get_f32_1d(f, 0), 0.0, 1e-3)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 0.0, 1e-3)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t1, 0), 0.0, 1e-3)); } @@ -127,36 +102,19 @@ pub fn main() !void { { const t0 = c.ggml_new_f32(ctx0, -7.0); - const t1 = c.ggml_new_f32(ctx0, 8.0); + const t1 = c.ggml_new_f32(ctx0, 8.0); _ = c.ggml_set_param(ctx0, t0); _ = c.ggml_set_param(ctx0, t1); // f = (t0 + 2*t1 - 7)^2 + (2*t0 + t1 - 5)^2 const f = - c.ggml_add(ctx0, - c.ggml_sqr(ctx0, - c.ggml_sub(ctx0, - c.ggml_add(ctx0, - t0, - c.ggml_mul(ctx0, t1, c.ggml_new_f32(ctx0, 2.0))), - c.ggml_new_f32(ctx0, 7.0) - ) - ), - c.ggml_sqr(ctx0, - c.ggml_sub(ctx0, - c.ggml_add(ctx0, - c.ggml_mul(ctx0, t0, c.ggml_new_f32(ctx0, 2.0)), - t1), - c.ggml_new_f32(ctx0, 5.0) - ) - ) - ); + c.ggml_add(ctx0, c.ggml_sqr(ctx0, c.ggml_sub(ctx0, c.ggml_add(ctx0, t0, c.ggml_mul(ctx0, t1, c.ggml_new_f32(ctx0, 2.0))), c.ggml_new_f32(ctx0, 7.0))), c.ggml_sqr(ctx0, c.ggml_sub(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, t0, c.ggml_new_f32(ctx0, 2.0)), t1), c.ggml_new_f32(ctx0, 5.0)))); const res = c.ggml_opt(null, opt_params, f); - try std.testing.expect(res == c.GGML_OPT_OK); - try std.testing.expect(is_close(c.ggml_get_f32_1d(f, 0), 0.0, 1e-3)); + try std.testing.expect(res == c.GGML_OPT_RESULT_OK); + try std.testing.expect(is_close(c.ggml_get_f32_1d(f, 0), 0.0, 1e-3)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t0, 0), 1.0, 1e-3)); try std.testing.expect(is_close(c.ggml_get_f32_1d(t1, 0), 3.0, 1e-3)); } diff --git a/tests/test3.zig b/tests/test3.zig index d676961f..fe87df80 100644 --- a/tests/test3.zig +++ b/tests/test3.zig @@ -6,18 +6,18 @@ const c = @cImport({ }); fn is_close(a: f32, b: f32, epsilon: f32) bool { - return std.math.fabs(a - b) < epsilon; + return @abs(a - b) < epsilon; } pub fn main() !void { const params = .{ - .mem_size = 128*1024*1024, + .mem_size = 128 * 1024 * 1024, .mem_buffer = null, - .no_alloc = false, + .no_alloc = false, }; - var opt_params = c.ggml_opt_default_params(c.GGML_OPT_LBFGS); - + var opt_params = c.ggml_opt_default_params(c.GGML_OPT_TYPE_LBFGS); + const nthreads = try Thread.getCpuCount(); opt_params.n_threads = @intCast(nthreads); @@ -38,15 +38,14 @@ pub fn main() !void { const l_data_pointer: [*]f32 = @ptrCast(@alignCast(l.*.data)); const f_data_pointer: [*]f32 = @ptrCast(@alignCast(F.*.data)); for (0..NP) |j| { - const ll = if (j < NP/2) @as(f32, 1.0) else @as(f32, -1.0); + const ll = if (j < NP / 2) @as(f32, 1.0) else @as(f32, -1.0); l_data_pointer[j] = ll; - + for (0..NF) |i| { const c_rand: f32 = @floatFromInt(c.rand()); - f_data_pointer[j*NF + i] = - ((if (ll > 0 and i < NF/2) @as(f32, 1.0) else - if (ll < 0 and i >= NF/2) @as(f32, 1.0) else @as(f32, 0.0)) + - (c_rand/c.RAND_MAX - 0.5) * 0.1) / (0.5 * NF); + f_data_pointer[j * NF + i] = + ((if (ll > 0 and i < NF / 2) @as(f32, 1.0) else if (ll < 0 and i >= NF / 2) @as(f32, 1.0) else @as(f32, 0.0)) + + (c_rand / c.RAND_MAX - 0.5) * 0.1) / (0.5 * NF); } } @@ -58,39 +57,25 @@ pub fn main() !void { // f = sum[(fj*x - l)^2]/n + lambda*|x^2| const f = - c.ggml_add(ctx0, - c.ggml_div(ctx0, - c.ggml_sum(ctx0, - c.ggml_sqr(ctx0, - c.ggml_sub(ctx0, - c.ggml_mul_mat(ctx0, F, x), - l) - ) - ), - c.ggml_new_f32(ctx0, @as(f32, NP)) - ), - c.ggml_mul(ctx0, - c.ggml_sum(ctx0, c.ggml_sqr(ctx0, x)), - lambda) - ); + c.ggml_add(ctx0, c.ggml_div(ctx0, c.ggml_sum(ctx0, c.ggml_sqr(ctx0, c.ggml_sub(ctx0, c.ggml_mul_mat(ctx0, F, x), l))), c.ggml_new_f32(ctx0, @as(f32, NP))), c.ggml_mul(ctx0, c.ggml_sum(ctx0, c.ggml_sqr(ctx0, x)), lambda)); const res = c.ggml_opt(null, opt_params, f); - try std.testing.expect(res == c.GGML_OPT_OK); + try std.testing.expect(res == c.GGML_OPT_RESULT_OK); const x_data_pointer: [*]f32 = @ptrCast(@alignCast(x.*.data)); // print results for (0..16) |i| { - std.debug.print("x[{d:3}] = {d:.6}\n", .{i, x_data_pointer[i]}); + std.debug.print("x[{d:3}] = {d:.6}\n", .{ i, x_data_pointer[i] }); } std.debug.print("...\n", .{}); for (NF - 16..NF) |i| { - std.debug.print("x[{d:3}] = {d:.6}\n", .{i, x_data_pointer[i]}); + std.debug.print("x[{d:3}] = {d:.6}\n", .{ i, x_data_pointer[i] }); } std.debug.print("\n", .{}); for (0..NF) |i| { - if (i < NF/2) { + if (i < NF / 2) { try std.testing.expect(is_close(x_data_pointer[i], 1.0, 1e-2)); } else { try std.testing.expect(is_close(x_data_pointer[i], -1.0, 1e-2));