ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
| 1 | #include "ggml-backend.h" |
| 2 | #include "ggml-cpu.h" |
| 3 | #include "ggml.h" |
| 4 | |
| 5 | #ifdef GGML_USE_CUDA |
| 6 | #include "ggml-cuda.h" |
| 7 | #endif |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | struct model { |
| 13 | struct ggml_context* ctx; |
| 14 | struct ggml_context* ctx0; |
| 15 | ggml_backend_t backend; |
| 16 | ggml_backend_buffer_t buffer; |
| 17 | struct ggml_cgraph* gf; |
| 18 | ggml_gallocr_t allocr; |
| 19 | uint8_t* buf; |
| 20 | }; |
| 21 | |
| 22 | struct ggml_context* make_ctx(void) { |
| 23 | struct ggml_init_params params = { |
| 24 | .mem_size = ggml_tensor_overhead() * 3, |
| 25 | .mem_buffer = NULL, |
| 26 | .no_alloc = true, |
| 27 | }; |
| 28 | return ggml_init(params); |
| 29 | } |
| 30 | |
| 31 | ggml_backend_t make_backend(void) { |
| 32 | ggml_backend_t backend = NULL; |
| 33 | |
| 34 | #ifdef GGML_USE_CUDA |
| 35 | backend = ggml_backend_cuda_init(0); |
| 36 | GGML_ASSERT(backend != NULL); |
| 37 | #endif |
| 38 | |
| 39 | if (!backend) { |
| 40 | backend = ggml_backend_cpu_init(); |
| 41 | } |
| 42 | |
| 43 | return backend; |
| 44 | } |
| 45 | |
| 46 | void model_init(struct model* m) { |
| 47 | m->ctx = make_ctx(); |
| 48 | m->backend = make_backend(); |
| 49 | |
| 50 | size_t buf_size = ggml_tensor_overhead() * GGML_DEFAULT_GRAPH_SIZE + ggml_graph_overhead(); |
| 51 | m->buf = calloc(buf_size, sizeof(uint8_t)); |
| 52 | struct ggml_init_params params0 = { |
| 53 | .mem_size = buf_size, |
| 54 | .mem_buffer = m->buf, |
| 55 | .no_alloc = true, |
| 56 | }; |
| 57 | m->ctx0 = ggml_init(params0); |
| 58 | m->gf = ggml_new_graph(m->ctx0); |
| 59 | } |
| 60 | |
| 61 | void model_alloc(struct model* m) { |
| 62 | m->buffer = ggml_backend_alloc_ctx_tensors(m->ctx, m->backend); |
| 63 | m->allocr = ggml_gallocr_new(ggml_backend_get_default_buffer_type(m->backend)); |
| 64 | } |
| 65 | |
| 66 | void model_compute(struct model* m) { |
| 67 | ggml_gallocr_alloc_graph(m->allocr, m->gf); |
| 68 | ggml_backend_graph_compute(m->backend, m->gf); |
| 69 | } |
| 70 | |
| 71 | void model_free(struct model* m) { |
| 72 | ggml_free(m->ctx0); |
| 73 | free(m->buf); |
| 74 | ggml_gallocr_free(m->allocr); |
| 75 | ggml_free(m->ctx); |
| 76 | ggml_backend_buffer_free(m->buffer); |
| 77 | ggml_backend_free(m->backend); |
| 78 | } |
| 79 | |
| 80 | void check_tensor(struct ggml_tensor* t, |
| 81 | const float* expected_t_d, |
| 82 | const int ne0, |
| 83 | const int ne1, |
| 84 | const int ne2) { |
| 85 | GGML_ASSERT(t->ne[0] == ne0); |
| 86 | GGML_ASSERT(t->ne[1] == ne1); |
| 87 | GGML_ASSERT(t->ne[2] == ne2); |
| 88 | const size_t bsize = ggml_nbytes(t); |
| 89 | if (t->type == GGML_TYPE_F32) { |
| 90 | float* buffer = malloc(bsize); |
| 91 | ggml_backend_tensor_get(t, buffer, 0, bsize); |
| 92 | for (int i = 0; i < bsize / sizeof(float); ++i) { |
| 93 | float expected = expected_t_d[i]; |
| 94 | float actual = buffer[i]; |
| 95 | if (expected != actual) { |
| 96 | printf("expected %.1f, got %.1f\n", expected, actual); |
| 97 | } |
| 98 | GGML_ASSERT(expected == actual); |
| 99 | } |
| 100 | free(buffer); |
| 101 | } else if (t->type == GGML_TYPE_F16) { |
| 102 | ggml_fp16_t* buffer = malloc(bsize); |
| 103 | ggml_backend_tensor_get(t, buffer, 0, bsize); |
| 104 | for (int i = 0; i < bsize / sizeof(ggml_fp16_t); ++i) { |
| 105 | float expected = expected_t_d[i]; |
| 106 | float actual = ggml_fp16_to_fp32(buffer[i]); |
| 107 | if (expected != actual) { |
| 108 | printf("expected %.1f, got %.1f\n", expected, actual); |
| 109 | } |
| 110 | GGML_ASSERT(expected == actual); |
| 111 | } |
| 112 | free(buffer); |
| 113 | //} else if (t->type == GGML_TYPE_BF16) { |
| 114 | // ggml_bf16_t* buffer = malloc(bsize); |
| 115 | // ggml_backend_tensor_get(t, buffer, 0, bsize); |
| 116 | // for (int i = 0; i < bsize / sizeof(ggml_bf16_t); ++i) { |
| 117 | // float expected = expected_t_d[i]; |
| 118 | // float actual = ggml_bf16_to_fp32(buffer[i]); |
| 119 | // if (expected != actual) { |
| 120 | // printf("expected %.1f, got %.1f\n", expected, actual); |
| 121 | // } |
| 122 | // GGML_ASSERT(expected == actual); |
| 123 | // } |
| 124 | // free(buffer); |
| 125 | } else { |
| 126 | GGML_ABORT("unknown type"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void test_cont(void) { |
| 131 | float buf_f32[] = {1.0, 2.0}; |
| 132 | ggml_fp16_t buf_f16[] = {ggml_fp32_to_fp16(buf_f32[0]), ggml_fp32_to_fp16(buf_f32[1])}; |
| 133 | ggml_bf16_t buf_bf16[] = {ggml_fp32_to_bf16(buf_f32[0]), ggml_fp32_to_bf16(buf_f32[1])}; |
| 134 | |
| 135 | float expected_out[] = {1.0, 2.0}; |
| 136 | |
| 137 | struct model m; |
| 138 | model_init(&m); |
| 139 | |
| 140 | struct ggml_tensor* in_1 = ggml_new_tensor_1d(m.ctx, GGML_TYPE_F32, 2); |
| 141 | struct ggml_tensor* in_2 = ggml_new_tensor_1d(m.ctx, GGML_TYPE_F16, 2); |
| 142 | //struct ggml_tensor* in_3 = ggml_new_tensor_1d(m.ctx, GGML_TYPE_BF16, 2); |
| 143 | |
| 144 | model_alloc(&m); |
| 145 | |
| 146 | ggml_backend_tensor_set(in_1, buf_f32, 0, ggml_nbytes(in_1)); |
| 147 | ggml_backend_tensor_set(in_2, buf_f16, 0, ggml_nbytes(in_2)); |
| 148 | //ggml_backend_tensor_set(in_3, buf_bf16, 0, ggml_nbytes(in_3)); |
| 149 | |
| 150 | struct ggml_tensor* out_1 = ggml_cont(m.ctx0, ggml_transpose(m.ctx0, in_1)); |
| 151 | struct ggml_tensor* out_2 = ggml_cont(m.ctx0, ggml_transpose(m.ctx0, in_2)); |
| 152 | //struct ggml_tensor* out_3 = ggml_cont(m.ctx0, ggml_transpose(m.ctx0, in_3)); |
| 153 | |
| 154 | ggml_build_forward_expand(m.gf, out_1); |
| 155 | ggml_build_forward_expand(m.gf, out_2); |
| 156 | //ggml_build_forward_expand(m.gf, out_3); |
| 157 | |
| 158 | model_compute(&m); |
| 159 | |
| 160 | check_tensor(out_1, expected_out, 1, 2, 1); |
| 161 | check_tensor(out_2, expected_out, 1, 2, 1); |
| 162 | //check_tensor(out_3, expected_out, 1, 2, 1); |
| 163 | |
| 164 | model_free(&m); |
| 165 | } |
| 166 | |
| 167 | int main(int argc, const char* argv[]) { |
| 168 | test_cont(); |
| 169 | return 0; |
| 170 | } |