123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- const std = @import("std");
- const c = @cImport({
- @cInclude("ggml/ggml.h");
- });
- pub fn main() !void {
- const n_threads = 2;
- const params = .{
- .mem_size = 128*1024*1024,
- .mem_buffer = null,
- .no_alloc = false,
- };
- const ctx0 = c.ggml_init(params);
- defer c.ggml_free(ctx0);
- {
- const x = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- c.ggml_set_param(ctx0, x);
- const a = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const b = c.ggml_mul(ctx0, x, x);
- const f = c.ggml_mul(ctx0, b, a);
- // a*x^2
- // 2*a*x
- c.ggml_print_objects(ctx0);
- const gf = c.ggml_build_forward(f);
- const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
- _ = c.ggml_set_f32(x, 2.0);
- _ = c.ggml_set_f32(a, 3.0);
- 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);
- 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);
- _ = c.ggml_set_f32(x, 3.0);
- 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);
- 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);
- c.ggml_graph_dump_dot(&gf, null, "test1-1-forward.dot");
- c.ggml_graph_dump_dot(&gb, &gf, "test1-1-backward.dot");
- }
- /////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const x3 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- _ = c.ggml_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 1.0);
- _ = c.ggml_set_f32(x3, 0.0);
- c.ggml_set_param(ctx0, x1);
- c.ggml_set_param(ctx0, x2);
- 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);
- 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);
- 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);
- const g1 = x1.*.grad;
- const g2 = x2.*.grad;
- const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), 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);
- 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);
- c.ggml_graph_dump_dot(&gf, null, "test1-2-forward.dot");
- c.ggml_graph_dump_dot(&gb, &gf, "test1-2-backward.dot");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- c.ggml_set_param(ctx0, x1);
- c.ggml_set_param(ctx0, x2);
- 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);
- _ = c.ggml_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 4.0);
- 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);
- 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);
- c.ggml_graph_dump_dot(&gf, null, "test1-3-forward.dot");
- c.ggml_graph_dump_dot(&gb, &gf, "test1-3-backward.dot");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- const x3 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
- c.ggml_set_param(ctx0, x1);
- c.ggml_set_param(ctx0, x2);
- c.ggml_set_param(ctx0, x3);
- 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);
- _ = 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_set_f32(y.*.grad, 1.0);
- 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);
- const g1 = x1.*.grad;
- const g2 = x2.*.grad;
- const g3 = x3.*.grad;
- const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), 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_set_f32(g3.*.grad, 1.0);
- 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),
- });
- 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");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- c.ggml_set_param(ctx0, x1);
- c.ggml_set_param(ctx0, x2);
- 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);
- _ = c.ggml_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 5.0);
- 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);
- 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");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- c.ggml_set_param(ctx0, x1);
- 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_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 5.0);
- 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);
- 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");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- c.ggml_set_param(ctx0, x1);
- 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_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 5.0);
- 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);
- 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");
- }
- ///////////////////////////////////////////////////////////////
- {
- const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
- c.ggml_set_param(ctx0, x1);
- c.ggml_set_param(ctx0, x2);
- const y =
- 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);
- _ = c.ggml_set_f32(x1, 3.0);
- _ = c.ggml_set_f32(x2, 5.0);
- 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);
- 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_set_f32(x1, 7.0);
- _ = c.ggml_set_f32(x2, 5.0);
- 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);
- 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");
- }
- _ = try std.io.getStdIn().reader().readByte();
- }
|