test-rel-pos.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "ggml/ggml.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct ggml_context* make_ctx(void) {
  6. struct ggml_init_params params = {
  7. .mem_size = 2 * 1024 * 1024,
  8. };
  9. return ggml_init(params);
  10. }
  11. void check_tensor(struct ggml_tensor * t, float * expected_t_d, int ne0, int ne1, int ne2) {
  12. GGML_ASSERT(t->type == GGML_TYPE_F32);
  13. GGML_ASSERT(t->ne[0] == ne0);
  14. GGML_ASSERT(t->ne[1] == ne1);
  15. GGML_ASSERT(t->ne[2] == ne2);
  16. for (int i2 = 0; i2 < ne2; ++i2) {
  17. for (int i1 = 0; i1 < ne1; ++i1) {
  18. for (int i0 = 0; i0 < ne0; ++i0) {
  19. float expected = *(expected_t_d + i2 * ne1 * ne0 + i1 * ne0 + i0);
  20. float actual = ggml_get_data_f32(t)[i2 * ne1 * ne0 + i1 * ne0 + i0];
  21. GGML_ASSERT(expected == actual);
  22. }
  23. }
  24. }
  25. }
  26. int main(int argc, const char** argv) {
  27. ggml_fp16_t buf_f16[1024];
  28. for (int i = 0; i < 1024; ++i) {
  29. buf_f16[i] = ggml_fp32_to_fp16((float)i);
  30. }
  31. float expected_out[4][9] = {
  32. { 8.0, 9.0, 10.0, 9.0, 10.0, 11.0, 10.0, 11.0, 12.0 },
  33. { 2.0, 3.0, 4.0, 3.0, 4.0, 5.0, 4.0, 5.0, 6.0 },
  34. { 14.0, 15.0, 16.0, 15.0, 16.0, 17.0, 16.0, 17.0, 18.0 },
  35. { 8.0, 9.0, 10.0, 9.0, 10.0, 11.0, 10.0, 11.0, 12.0 },
  36. };
  37. {
  38. struct ggml_context * ctx = make_ctx();
  39. struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F16, 3, 3);
  40. ggml_fp16_t* t_d = (ggml_fp16_t*)t->data;
  41. memcpy(t_d, buf_f16, ggml_nbytes(t));
  42. struct ggml_tensor * t_2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F16, 3, 3);
  43. ggml_fp16_t* t_d_2 = (ggml_fp16_t*)t_2->data;
  44. memcpy(t_d_2, buf_f16 + 1, ggml_nbytes(t_2));
  45. struct ggml_tensor * rw = ggml_get_rel_pos(ctx, t, 2, 2);
  46. struct ggml_tensor * rh = ggml_get_rel_pos(ctx, t_2, 2, 2);
  47. struct ggml_tensor * rw_f32 = ggml_cpy(ctx, rw, ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 3, 2, 2));
  48. struct ggml_tensor * rh_f32 = ggml_cpy(ctx, rh, ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 3, 2, 2));
  49. struct ggml_tensor * in = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 9, 4);
  50. struct ggml_tensor * out_inplace = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 9, 4);
  51. float * in_d = (float*)in->data;
  52. float * out_inplace_d = (float*)out_inplace->data;
  53. for (int i = 0; i < ggml_nelements(in); ++i) {
  54. in_d[i] = 1.f;
  55. out_inplace_d[i] = 1.f;
  56. }
  57. struct ggml_tensor * out = ggml_add_rel_pos(ctx, in, rw_f32, rh_f32);
  58. struct ggml_cgraph gf = ggml_build_forward(out);
  59. ggml_graph_compute_with_ctx(ctx, &gf, 1);
  60. out_inplace = ggml_add_rel_pos_inplace(ctx, out_inplace, rw_f32, rh_f32);
  61. struct ggml_cgraph gf_2 = ggml_build_forward(out_inplace);
  62. ggml_graph_compute_with_ctx(ctx, &gf_2, 1);
  63. check_tensor(out, (float*)expected_out, 9, 4, 1);
  64. check_tensor(out_inplace, (float*)expected_out, 9, 4, 1);
  65. }
  66. return 0;
  67. }