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