|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +#include "ggml.h" |
| 4 | +#include "ggml-alloc.h" |
| 5 | +#include "ggml-backend.h" |
| 6 | + |
| 7 | +int main(int argc, char **argv) { |
| 8 | + printf("GGML LBFGS example\n"); |
| 9 | + |
| 10 | + struct ggml_init_params params = { |
| 11 | + .mem_size = 16*1024*1024, |
| 12 | + .mem_buffer = NULL, |
| 13 | + }; |
| 14 | + struct ggml_context* ctx = ggml_init(params); |
| 15 | + |
| 16 | + // Simulate a sequence of 6 tokens with en embedding size of 4096 and a |
| 17 | + // context length of 512. |
| 18 | + int n_ctx_orig = 4096; |
| 19 | + int embd_dim = 128; |
| 20 | + int n_head = 32; |
| 21 | + int n_tokens = 6; |
| 22 | + |
| 23 | + // The Query matrix in this case can hold 512 tokens each with a dimension |
| 24 | + // of 4096. |
| 25 | + struct ggml_tensor* query = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ctx_orig, n_tokens); |
| 26 | + // Will trigger: GGML_ASSERT(false && "backwards pass not implemented") failed |
| 27 | + ggml_set_param(ctx, query); |
| 28 | + |
| 29 | + // We reshape the query matrix embedding dimensions to account for the number |
| 30 | + // of heads (32) each which will have a dimension of 128 (128 * 32 = 4096). |
| 31 | + struct ggml_tensor* a = ggml_reshape_3d(ctx, query, embd_dim, n_head, n_tokens); |
| 32 | + ggml_set_name(a, "a"); |
| 33 | + // Will trigger: GGML_ASSERT(false && "backwards pass not implemented") failed |
| 34 | + //ggml_set_param(ctx, a); |
| 35 | + |
| 36 | + // These are the positions |
| 37 | + struct ggml_tensor* pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_tokens); |
| 38 | + ggml_set_name(pos, "pos"); |
| 39 | + ggml_set_param(ctx, pos); |
| 40 | + |
| 41 | + // Set some made up values for the tensor to be rotated. |
| 42 | + for (int i = 0; i < a->ne[2]; i++) { |
| 43 | + // Loop over the embedding heads (32) |
| 44 | + for (int j = 0; j < a->ne[1]; j++) { |
| 45 | + // Loop over the embedding dimensions (128) |
| 46 | + for (int k = 0; k < a->ne[0]; k++) { |
| 47 | + float value = 0.0f + k; |
| 48 | + ggml_set_f32_nd(a, k, j, i, 0, value); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Print a few of the first dimensions so we can see that there is a rotation |
| 54 | + // being performed. In this case we are printing the first 10 embeddings for |
| 55 | + // the 2nd token. I'm not using token 0 as this will have a cosine value of 10 |
| 56 | + // and since value of 0 which will not perform any rotations for the position |
| 57 | + // embeddings for that dimension. |
| 58 | + for (int i = 0; i < 10; i++) { |
| 59 | + printf("embedding for token 1, embedding dim %d: %f\n", i, ggml_get_f32_nd(a, i, 0, 1, 0)); |
| 60 | + } |
| 61 | + |
| 62 | + // Set the positions manually (the b tensor parameter to ggml_rope_ext). |
| 63 | + for (int i = 0; i < pos->ne[0]; i++) { |
| 64 | + ggml_set_i32_1d(pos, i, i); |
| 65 | + } |
| 66 | + |
| 67 | + int mode = 0; // rote type 0 = Normal |
| 68 | + |
| 69 | + // The RoPE base frequency |
| 70 | + // ↓ |
| 71 | + // (10000^(-2j/d). |
| 72 | + float freq_base = 10000.0f; |
| 73 | + |
| 74 | + // The RoPE frequency scale. |
| 75 | + float freq_scale = 1.0f; |
| 76 | + |
| 77 | + // TODO: What is this? It looks like this is mscale (magnituce scale) |
| 78 | + float attn_factor = 1.0f; |
| 79 | + |
| 80 | + // Extrapolation factor. If this is 0.0 then the beta_fast and beta_slow |
| 81 | + // are not used. |
| 82 | + float ext_factor = 1.0f; |
| 83 | + |
| 84 | + // This is a YaRN parameter is named α (alpha) in the YaRN paper. This |
| 85 | + // specifies that hen the number of rotations is 32 this is the position |
| 86 | + // embedding dimension that should be used for the for |
| 87 | + float beta_fast = 32.0f; |
| 88 | + |
| 89 | + // This is a YaRN parameter which I think is named β in the YaRN paper. |
| 90 | + float beta_slow = 1.0f; |
| 91 | + |
| 92 | + // LongRope Frequency factors (freq_factors/rope_scaling) are used with |
| 93 | + // certain models like Phi-3-mini-128k-instruct |
| 94 | + // (https://huggingface.co/microsoft/Phi-3-mini-128k-instruct/blob/main/config.json#L27). |
| 95 | + struct ggml_tensor* freq_factors = NULL; |
| 96 | + |
| 97 | + struct ggml_tensor* s = ggml_rope_ext(ctx, |
| 98 | + a, |
| 99 | + pos, |
| 100 | + freq_factors, |
| 101 | + embd_dim, |
| 102 | + mode, |
| 103 | + n_ctx_orig, |
| 104 | + freq_base, |
| 105 | + freq_scale, |
| 106 | + ext_factor, |
| 107 | + attn_factor, |
| 108 | + beta_fast, |
| 109 | + beta_slow); |
| 110 | + |
| 111 | + struct ggml_opt_params opts = ggml_opt_default_params(GGML_OPT_TYPE_LBFGS); |
| 112 | + ggml_set_param(ctx, s); |
| 113 | + |
| 114 | + struct ggml_cgraph * cgraph = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true); |
| 115 | + ggml_build_forward_expand(cgraph, s); |
| 116 | + |
| 117 | + ggml_opt(ctx, opts, s); |
| 118 | + |
| 119 | + ggml_graph_compute_with_ctx(ctx, cgraph, 1); |
| 120 | + |
| 121 | + //printf("a: n_elements: %ld\n", ggml_nelements(s)); |
| 122 | + |
| 123 | + ggml_free(ctx); |
| 124 | + return 0; |
| 125 | +} |
0 commit comments