test1.zig 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. const std = @import("std");
  2. const c = @cImport({
  3. @cInclude("ggml/ggml.h");
  4. });
  5. pub fn main() !void {
  6. const n_threads = 2;
  7. const params = .{
  8. .mem_size = 128*1024*1024,
  9. .mem_buffer = null,
  10. .no_alloc = false,
  11. };
  12. const ctx0 = c.ggml_init(params);
  13. defer c.ggml_free(ctx0);
  14. {
  15. const x = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  16. c.ggml_set_param(ctx0, x);
  17. const a = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  18. const b = c.ggml_mul(ctx0, x, x);
  19. const f = c.ggml_mul(ctx0, b, a);
  20. // a*x^2
  21. // 2*a*x
  22. c.ggml_print_objects(ctx0);
  23. const gf = c.ggml_build_forward(f);
  24. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  25. _ = c.ggml_set_f32(x, 2.0);
  26. _ = c.ggml_set_f32(a, 3.0);
  27. c.ggml_graph_reset(@constCast(&gf));
  28. _ = c.ggml_set_f32(f.*.grad, 1.0);
  29. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  30. std.debug.print("f = {d:.6}\n", .{c.ggml_get_f32_1d(f, 0)});
  31. std.debug.print("df/dx = {d:.6}\n", .{c.ggml_get_f32_1d(x.*.grad, 0)});
  32. try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 12.0);
  33. try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 12.0);
  34. _ = c.ggml_set_f32(x, 3.0);
  35. c.ggml_graph_reset(@constCast(&gf));
  36. _ = c.ggml_set_f32(f.*.grad, 1.0);
  37. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  38. std.debug.print("f = {d:.6}\n", .{c.ggml_get_f32_1d(f, 0)});
  39. std.debug.print("df/dx = {d:.6}\n", .{c.ggml_get_f32_1d(x.*.grad, 0)});
  40. try std.testing.expect(c.ggml_get_f32_1d(f, 0) == 27.0);
  41. try std.testing.expect(c.ggml_get_f32_1d(x.*.grad, 0) == 18.0);
  42. c.ggml_graph_dump_dot(&gf, null, "test1-1-forward.dot");
  43. c.ggml_graph_dump_dot(&gb, &gf, "test1-1-backward.dot");
  44. }
  45. /////////////////////////////////////////////////////////////
  46. {
  47. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  48. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  49. const x3 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  50. _ = c.ggml_set_f32(x1, 3.0);
  51. _ = c.ggml_set_f32(x2, 1.0);
  52. _ = c.ggml_set_f32(x3, 0.0);
  53. c.ggml_set_param(ctx0, x1);
  54. c.ggml_set_param(ctx0, x2);
  55. const y = c.ggml_add(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x1, x2));
  56. const gf = c.ggml_build_forward(y);
  57. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  58. c.ggml_graph_reset(@constCast(&gf));
  59. _ = c.ggml_set_f32(y.*.grad, 1.0);
  60. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  61. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  62. std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)});
  63. std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)});
  64. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0);
  65. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 7.0);
  66. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0);
  67. const g1 = x1.*.grad;
  68. const g2 = x2.*.grad;
  69. const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), true);
  70. c.ggml_graph_reset(@constCast(&gb));
  71. _ = c.ggml_set_f32(g1.*.grad, 1.0);
  72. _ = c.ggml_set_f32(g2.*.grad, 1.0);
  73. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gbb), n_threads);
  74. 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)});
  75. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 3.0);
  76. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0);
  77. c.ggml_graph_dump_dot(&gf, null, "test1-2-forward.dot");
  78. c.ggml_graph_dump_dot(&gb, &gf, "test1-2-backward.dot");
  79. }
  80. ///////////////////////////////////////////////////////////////
  81. {
  82. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  83. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  84. c.ggml_set_param(ctx0, x1);
  85. c.ggml_set_param(ctx0, x2);
  86. const y = c.ggml_mul(ctx0, c.ggml_add(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x1, x2)), x1);
  87. const gf = c.ggml_build_forward(y);
  88. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  89. _ = c.ggml_set_f32(x1, 3.0);
  90. _ = c.ggml_set_f32(x2, 4.0);
  91. c.ggml_graph_reset(@constCast(&gf));
  92. _ = c.ggml_set_f32(y.*.grad, 1.0);
  93. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  94. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  95. std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)});
  96. std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)});
  97. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 63.0);
  98. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 51.0);
  99. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 9.0);
  100. c.ggml_graph_dump_dot(&gf, null, "test1-3-forward.dot");
  101. c.ggml_graph_dump_dot(&gb, &gf, "test1-3-backward.dot");
  102. }
  103. ///////////////////////////////////////////////////////////////
  104. {
  105. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  106. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  107. const x3 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 1);
  108. c.ggml_set_param(ctx0, x1);
  109. c.ggml_set_param(ctx0, x2);
  110. c.ggml_set_param(ctx0, x3);
  111. const y = c.ggml_mul(ctx0, c.ggml_mul(ctx0, c.ggml_mul(ctx0, x1, x1), c.ggml_mul(ctx0, x2, x2)), x3);
  112. const gf = c.ggml_build_forward(y);
  113. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  114. _ = c.ggml_set_f32(x1, 1.0);
  115. _ = c.ggml_set_f32(x2, 2.0);
  116. _ = c.ggml_set_f32(x3, 3.0);
  117. c.ggml_graph_reset(@constCast(&gf));
  118. _ = c.ggml_set_f32(y.*.grad, 1.0);
  119. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  120. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  121. std.debug.print("df/dx1 = {d:.6}\n", .{c.ggml_get_f32_1d(x1.*.grad, 0)});
  122. std.debug.print("df/dx2 = {d:.6}\n", .{c.ggml_get_f32_1d(x2.*.grad, 0)});
  123. std.debug.print("df/dx3 = {d:.6}\n", .{c.ggml_get_f32_1d(x3.*.grad, 0)});
  124. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 12.0);
  125. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 24.0);
  126. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 12.0);
  127. try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 4.0);
  128. const g1 = x1.*.grad;
  129. const g2 = x2.*.grad;
  130. const g3 = x3.*.grad;
  131. const gbb = c.ggml_build_backward(ctx0, @constCast(&gb), true);
  132. c.ggml_graph_reset(@constCast(&gb));
  133. _ = c.ggml_set_f32(g1.*.grad, 1.0);
  134. _ = c.ggml_set_f32(g2.*.grad, 1.0);
  135. _ = c.ggml_set_f32(g3.*.grad, 1.0);
  136. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gbb), n_threads);
  137. std.debug.print("H * [1, 1, 1] = [ {d:.6} {d:.6} {d:.6}]\n",
  138. .{
  139. c.ggml_get_f32_1d(x1.*.grad, 0),
  140. c.ggml_get_f32_1d(x2.*.grad, 0),
  141. c.ggml_get_f32_1d(x3.*.grad, 0),
  142. });
  143. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 56.0);
  144. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 34.0);
  145. try std.testing.expect(c.ggml_get_f32_1d(x3.*.grad, 0) == 12.0);
  146. c.ggml_graph_dump_dot(&gf, null, "test1-4-forward.dot");
  147. c.ggml_graph_dump_dot(&gb, &gf, "test1-4-backward.dot");
  148. }
  149. ///////////////////////////////////////////////////////////////
  150. {
  151. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  152. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  153. c.ggml_set_param(ctx0, x1);
  154. c.ggml_set_param(ctx0, x2);
  155. const y = c.ggml_sum(ctx0, c.ggml_mul(ctx0, x1, x2));
  156. const gf = c.ggml_build_forward(y);
  157. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  158. _ = c.ggml_set_f32(x1, 3.0);
  159. _ = c.ggml_set_f32(x2, 5.0);
  160. c.ggml_graph_reset(@constCast(&gf));
  161. _ = c.ggml_set_f32(y.*.grad, 1.0);
  162. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  163. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  164. std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n",
  165. .{
  166. c.ggml_get_f32_1d(x1.*.grad, 0),
  167. c.ggml_get_f32_1d(x1.*.grad, 1),
  168. c.ggml_get_f32_1d(x1.*.grad, 2),
  169. });
  170. std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n",
  171. .{
  172. c.ggml_get_f32_1d(x2.*.grad, 0),
  173. c.ggml_get_f32_1d(x2.*.grad, 1),
  174. c.ggml_get_f32_1d(x2.*.grad, 2),
  175. });
  176. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 45.0);
  177. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 5.0);
  178. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0);
  179. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 5.0);
  180. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0);
  181. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 5.0);
  182. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0);
  183. c.ggml_graph_dump_dot(&gf, null, "test1-5-forward.dot");
  184. c.ggml_graph_dump_dot(&gb, &gf, "test1-5-backward.dot");
  185. }
  186. ///////////////////////////////////////////////////////////////
  187. {
  188. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  189. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  190. c.ggml_set_param(ctx0, x1);
  191. c.ggml_set_param(ctx0, x2);
  192. const y =
  193. c.ggml_sum(ctx0,
  194. c.ggml_add(ctx0,
  195. c.ggml_mul(ctx0, x1, x2),
  196. c.ggml_mul(ctx0,
  197. c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1),
  198. c.ggml_mul(ctx0, x1, x1)
  199. )
  200. )
  201. );
  202. const gf = c.ggml_build_forward(y);
  203. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  204. _ = c.ggml_set_f32(x1, 3.0);
  205. _ = c.ggml_set_f32(x2, 5.0);
  206. c.ggml_graph_reset(@constCast(&gf));
  207. _ = c.ggml_set_f32(y.*.grad, 1.0);
  208. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  209. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  210. std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n",
  211. .{
  212. c.ggml_get_f32_1d(x1.*.grad, 0),
  213. c.ggml_get_f32_1d(x1.*.grad, 1),
  214. c.ggml_get_f32_1d(x1.*.grad, 2),
  215. });
  216. std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n",
  217. .{
  218. c.ggml_get_f32_1d(x2.*.grad, 0),
  219. c.ggml_get_f32_1d(x2.*.grad, 1),
  220. c.ggml_get_f32_1d(x2.*.grad, 2),
  221. });
  222. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == -9.0);
  223. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -7.0);
  224. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -7.0);
  225. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -7.0);
  226. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0);
  227. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0);
  228. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0);
  229. c.ggml_graph_dump_dot(&gf, null, "test1-6-forward.dot");
  230. c.ggml_graph_dump_dot(&gb, &gf, "test1-6-backward.dot");
  231. }
  232. ///////////////////////////////////////////////////////////////
  233. {
  234. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  235. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  236. c.ggml_set_param(ctx0, x1);
  237. c.ggml_set_param(ctx0, x2);
  238. const y =
  239. c.ggml_sum(ctx0,
  240. c.ggml_sub(ctx0,
  241. c.ggml_mul(ctx0, x1, x2),
  242. c.ggml_mul(ctx0,
  243. c.ggml_mul(ctx0, x1, x1),
  244. c.ggml_repeat(ctx0, c.ggml_new_f32(ctx0, -2.0), x1)
  245. )
  246. )
  247. );
  248. const gf = c.ggml_build_forward(y);
  249. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  250. _ = c.ggml_set_f32(x1, 3.0);
  251. _ = c.ggml_set_f32(x2, 5.0);
  252. c.ggml_graph_reset(@constCast(&gf));
  253. _ = c.ggml_set_f32(y.*.grad, 1.0);
  254. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  255. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  256. std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n",
  257. .{
  258. c.ggml_get_f32_1d(x1.*.grad, 0),
  259. c.ggml_get_f32_1d(x1.*.grad, 1),
  260. c.ggml_get_f32_1d(x1.*.grad, 2),
  261. });
  262. std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n",
  263. .{
  264. c.ggml_get_f32_1d(x2.*.grad, 0),
  265. c.ggml_get_f32_1d(x2.*.grad, 1),
  266. c.ggml_get_f32_1d(x2.*.grad, 2),
  267. });
  268. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 99.0);
  269. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 17.0);
  270. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 17.0);
  271. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 17.0);
  272. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 3.0);
  273. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 3.0);
  274. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 3.0);
  275. c.ggml_graph_dump_dot(&gf, null, "test1-7-forward.dot");
  276. c.ggml_graph_dump_dot(&gb, &gf, "test1-7-backward.dot");
  277. }
  278. ///////////////////////////////////////////////////////////////
  279. {
  280. const x1 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  281. const x2 = c.ggml_new_tensor_1d(ctx0, c.GGML_TYPE_F32, 3);
  282. c.ggml_set_param(ctx0, x1);
  283. c.ggml_set_param(ctx0, x2);
  284. const y =
  285. c.ggml_abs(ctx0,
  286. c.ggml_sub(ctx0, x1, x2)
  287. );
  288. const gf = c.ggml_build_forward(y);
  289. const gb = c.ggml_build_backward(ctx0, @constCast(&gf), false);
  290. _ = c.ggml_set_f32(x1, 3.0);
  291. _ = c.ggml_set_f32(x2, 5.0);
  292. c.ggml_graph_reset(@constCast(&gf));
  293. _ = c.ggml_set_f32(y.*.grad, 1.0);
  294. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  295. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  296. std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n",
  297. .{
  298. c.ggml_get_f32_1d(x1.*.grad, 0),
  299. c.ggml_get_f32_1d(x1.*.grad, 1),
  300. c.ggml_get_f32_1d(x1.*.grad, 2),
  301. });
  302. std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n",
  303. .{
  304. c.ggml_get_f32_1d(x2.*.grad, 0),
  305. c.ggml_get_f32_1d(x2.*.grad, 1),
  306. c.ggml_get_f32_1d(x2.*.grad, 2),
  307. });
  308. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0);
  309. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == -1.0);
  310. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == -1.0);
  311. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == -1.0);
  312. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == 1.0);
  313. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == 1.0);
  314. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == 1.0);
  315. _ = c.ggml_set_f32(x1, 7.0);
  316. _ = c.ggml_set_f32(x2, 5.0);
  317. c.ggml_graph_reset(@constCast(&gf));
  318. _ = c.ggml_set_f32(y.*.grad, 1.0);
  319. c.ggml_graph_compute_with_ctx(ctx0, @constCast(&gb), n_threads);
  320. std.debug.print("y = {d:.6}\n", .{c.ggml_get_f32_1d(y, 0)});
  321. std.debug.print("df/dx1 = {d:.6} {d:.6} {d:.6}\n",
  322. .{
  323. c.ggml_get_f32_1d(x1.*.grad, 0),
  324. c.ggml_get_f32_1d(x1.*.grad, 1),
  325. c.ggml_get_f32_1d(x1.*.grad, 2),
  326. });
  327. std.debug.print("df/dx2 = {d:.6} {d:.6} {d:.6}\n",
  328. .{
  329. c.ggml_get_f32_1d(x2.*.grad, 0),
  330. c.ggml_get_f32_1d(x2.*.grad, 1),
  331. c.ggml_get_f32_1d(x2.*.grad, 2),
  332. });
  333. try std.testing.expect(c.ggml_get_f32_1d(y, 0) == 2.0);
  334. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 0) == 1.0);
  335. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 1) == 1.0);
  336. try std.testing.expect(c.ggml_get_f32_1d(x1.*.grad, 2) == 1.0);
  337. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 0) == -1.0);
  338. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 1) == -1.0);
  339. try std.testing.expect(c.ggml_get_f32_1d(x2.*.grad, 2) == -1.0);
  340. c.ggml_graph_dump_dot(&gf, null, "test1-8-forward.dot");
  341. c.ggml_graph_dump_dot(&gb, &gf, "test1-8-backward.dot");
  342. }
  343. _ = try std.io.getStdIn().reader().readByte();
  344. }