Skip to content

Commit 7e80f5e

Browse files
committed
ggml: update examples after submodule update
1 parent 69e91a4 commit 7e80f5e

31 files changed

+227
-4
lines changed

fundamentals/ggml/src/acc.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45

56
int main(int argc, char **argv) {
67
printf("GGML accumulate example\n");

fundamentals/ggml/src/backprop.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "ggml-cpu.h"
23

34
#include <stdio.h>
45

fundamentals/ggml/src/broadcast.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "ggml-cpu.h"
23

34
#include <stdio.h>
45

fundamentals/ggml/src/concat.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/conv2d.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33

44
#include "ggml.h"
5+
#include "ggml-cpu.h"
56
#include "ggml-alloc.h"
67
#include "ggml-backend.h"
78

fundamentals/ggml/src/copy.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/custom.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/dup.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/galloc.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "ggml-cpu.h"
23
#include "ggml-alloc.h"
34
#include "ggml-backend.h"
45

fundamentals/ggml/src/get_rows.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/graph.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/im2col.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33

44
#include "ggml.h"
5+
#include "ggml-cpu.h"
56
#include "ggml-alloc.h"
67
#include "ggml-backend.h"
78

fundamentals/ggml/src/llama-att-softmax.c

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <math.h>
44

55
#include "ggml.h"
6+
#include "ggml-cpu.h"
67
#include "ggml-alloc.h"
78
#include "ggml-backend.h"
89

fundamentals/ggml/src/matrix-mul.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/mul-mat-id.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/mul.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
3+
#include "ggml.h"
4+
#include "ggml-cpu.h"
5+
#include "ggml-alloc.h"
6+
#include "ggml-backend.h"
7+
8+
int main(int argc, char **argv) {
9+
printf("GGML mul example\n");
10+
11+
struct ggml_init_params params = {
12+
.mem_size = 16*1024*1024,
13+
.mem_buffer = NULL,
14+
};
15+
struct ggml_context* ctx = ggml_init(params);
16+
17+
struct ggml_tensor* a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 2, 3);
18+
ggml_set_name(a, "a");
19+
ggml_set_i32_nd(a, 0, 0, 0, 0, 1);
20+
ggml_set_i32_nd(a, 1, 0, 0, 0, 2);
21+
ggml_set_i32_nd(a, 0, 1, 0, 0, 3);
22+
ggml_set_i32_nd(a, 1, 1, 0, 0, 4);
23+
ggml_set_i32_nd(a, 0, 2, 0, 0, 5);
24+
ggml_set_i32_nd(a, 1, 2, 0, 0, 6);
25+
26+
printf("matrix a %ldx%ld:\n", a->ne[0], a->ne[1]);
27+
for (int y = 0; y < a->ne[1]; y++) {
28+
for (int x = 0; x < a->ne[0]; x++) {
29+
printf("%.2f ", *(float *) ((char *) a->data + y * a->nb[1] + x * a->nb[0]));
30+
}
31+
printf("\n");
32+
}
33+
printf("\n");
34+
35+
struct ggml_tensor* b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 2);
36+
ggml_set_name(b, "b");
37+
ggml_set_i32_nd(b, 0, 0, 0, 0, 2);
38+
ggml_set_i32_nd(b, 1, 0, 0, 0, 4);
39+
40+
// Note that ggml_mul_mat() transposes the second matrix b.
41+
struct ggml_tensor* result = ggml_mul(ctx, a, b);
42+
ggml_set_name(result, "result");
43+
44+
struct ggml_cgraph* c_graph = ggml_new_graph(ctx);
45+
ggml_build_forward_expand(c_graph, result);
46+
int n_threads = 4;
47+
enum ggml_status st = ggml_graph_compute_with_ctx(ctx, c_graph, n_threads);
48+
if (st != GGML_STATUS_SUCCESS) {
49+
printf("could not compute graph\n");
50+
return 1;
51+
}
52+
53+
printf("result tensor type: %s\n", ggml_type_name(result->type));
54+
printf("result dim: %d\n", ggml_n_dims(result));
55+
printf("result dim[0]: %ld\n", result->ne[0]);
56+
printf("result dim[1]: %ld\n", result->ne[1]);
57+
58+
printf("\n");
59+
printf("result matrix %ldx%ld:\n", result->ne[0], result->ne[1]);
60+
for (int y = 0; y < result->ne[1]; y++) {
61+
for (int x = 0; x < result->ne[0]; x++) {
62+
printf("%.2f ", *(float *) ((char *) result->data + y * result->nb[1] + x * result->nb[0]));
63+
}
64+
printf("\n");
65+
}
66+
printf("\n");
67+
68+
ggml_free(ctx);
69+
return 0;
70+
}

fundamentals/ggml/src/norm.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45

56
int main(int argc, char **argv) {
67
printf("GGML normalization example\n");

fundamentals/ggml/src/opt-lbfgs.c

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

fundamentals/ggml/src/outer-product.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "ggml-cpu.h"
23

34
#include <stdio.h>
45

fundamentals/ggml/src/permute.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/reshape.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/rope.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <math.h>
33

44
#include "ggml.h"
5+
#include "ggml-cpu.h"
56
#include "ggml-alloc.h"
67
#include "ggml-backend.h"
78

fundamentals/ggml/src/scale.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/simple-backend.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ggml.h"
2+
#include "ggml-cpu.h"
23
#include "ggml-alloc.h"
34
#include "ggml-backend.h"
45

fundamentals/ggml/src/softmax.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ int main(int argc, char **argv) {
1515
};
1616
struct ggml_context* ctx = ggml_init(params);
1717

18-
struct ggml_tensor* logits = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 4, 1, 1);
18+
struct ggml_tensor* logits = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 8, 1, 1);
1919
ggml_set_name(logits, "logits");
2020

21-
float tensor_data[4] = { 6, 7, 10, 9};
21+
float tensor_data[8] = { 6, 7, 10, 9, 22, 33, 44, 55};
2222
memcpy((char *)logits->data, tensor_data, ggml_nbytes(logits));
2323

24-
struct ggml_tensor* mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 4, 1);
24+
struct ggml_tensor* mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 8, 1);
2525

2626
ggml_set_name(mask, "mask");
27-
float mask_data[4] = { 0, 0, -INFINITY, 0};
27+
float mask_data[8] = { 0, 0, -INFINITY, 0, -INFINITY, -INFINITY, -INFINITY, -INFINITY};
2828
memcpy((char *)mask->data, mask_data, ggml_nbytes(mask));
2929

3030
struct ggml_tensor* result = ggml_soft_max_ext(ctx, logits, mask, 1.0f, 0.0f);

fundamentals/ggml/src/ssm_conv.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

fundamentals/ggml/src/ssm_scan.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
#include "ggml.h"
4+
#include "ggml-cpu.h"
45
#include "ggml-alloc.h"
56
#include "ggml-backend.h"
67

0 commit comments

Comments
 (0)