Skip to content

Commit 0478f68

Browse files
committed
ggml: update ggml submodule
1 parent 3e087d6 commit 0478f68

File tree

10 files changed

+76
-19
lines changed

10 files changed

+76
-19
lines changed

fundamentals/ggml/Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ TARGETS_CPP := $(patsubst $(SRCDIR)/%.cpp, %, $(SOURCES_CPP))
3737
ifeq ($(OS),Linux)
3838
CFLAGS += -fopenmp
3939
CXXFLAGS += -fopenmp
40-
WHOLE_ARCHIVE = -Wl,--whole-archive ggml/build/src/libggml.a -Wl,--no-whole-archive
40+
WHOLE_ARCHIVE = -Wl,--whole-archive ggml/build/src/libggml.a -Wl,--no-whole-archive
41+
WHOLE_ARCHIVE += -Wl,--whole-archive ggml/build/src/libggml-base.a -Wl,--no-whole-archive
42+
WHOLE_ARCHIVE += -Wl,--whole-archive ggml/build/src/ggml-cpu/libggml-cpu.a -Wl,--no-whole-archive
4143
else ifeq ($(OS),Darwin)
4244
CFLAGS += -framework Metal -framework Foundation -framework MetalKit -framework Accelerate
4345
CXXFLAGS += -framework Metal -framework Foundation -framework MetalKit -framework Accelerate
44-
WHOLE_ARCHIVE = -Wl,-force_load,ggml/build/src/libggml.a
46+
WHOLE_ARCHIVE = -Wl,-force_load,ggml/build/src/libggml.a
47+
WHOLE_ARCHIVE += -Wl,-force_load,ggml/build/src/libggml-base.a
48+
WHOLE_ARCHIVE += -Wl,-force_load,ggml/build/src/libggml-cpu.a
4549
endif
4650

4751
.PHONY: all clean

fundamentals/ggml/ggml

Submodule ggml updated from 8a3d799 to 2884dd7

fundamentals/ggml/src/backprop.c

+14-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int main(int argc, char **argv) {
1818
.mem_buffer = NULL,
1919
};
2020
struct ggml_context* ctx = ggml_init(params);
21+
struct ggml_context* static_ctx = ggml_init(params);
2122

2223
// 'a' represents a parameter in the graph/neural network
2324
struct ggml_tensor* a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
@@ -29,8 +30,8 @@ int main(int argc, char **argv) {
2930

3031
printf("Parameter a:\n");
3132
printf("a: %f\n", ggml_get_f32_1d(a, 0));
32-
printf("a->grad: %s\n", a->grad->name);
33-
printf("a->grad: %f\n", ggml_get_f32_1d(a->grad, 0));
33+
//printf("a->grad: %s\n", ggml_graph_get_grad(b_graph, a->grad->name));
34+
//printf("a->grad: %f\n", ggml_get_f32_1d(a->grad, 0));
3435
printf("\n");
3536

3637
// 'b' represents another parameter in the graph/neural network
@@ -41,8 +42,8 @@ int main(int argc, char **argv) {
4142
ggml_set_param(ctx, b);
4243
printf("Parameter b:\n");
4344
printf("b: %f\n", ggml_get_f32_1d(b, 0));
44-
printf("b->grad: %s\n", b->grad->name);
45-
printf("b->grad: %f\n", ggml_get_f32_1d(b->grad, 0));
45+
//printf("b->grad: %s\n", b->grad->name);
46+
//printf("b->grad: %f\n", ggml_get_f32_1d(b->grad, 0));
4647
printf("\n");
4748

4849
printf("Operation/Output tensor mul:\n");
@@ -51,7 +52,7 @@ int main(int argc, char **argv) {
5152
printf("mul->op: %s\n", ggml_op_name(mul->op));
5253
printf("mul->src0: %s\n", mul->src[0]->name);
5354
printf("mul->src1: %s\n", mul->src[1]->name);
54-
printf("mul->grad: %s\n", mul->grad->name);
55+
//printf("mul->grad: %s\n", mul->grad->name);
5556
printf("\n");
5657

5758
struct ggml_cgraph* f_graph = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
@@ -74,25 +75,27 @@ int main(int argc, char **argv) {
7475
ggml_graph_reset(f_graph);
7576

7677
struct ggml_cgraph* b_graph = ggml_graph_dup(ctx, f_graph);
77-
ggml_build_backward_expand(ctx, f_graph, b_graph, /*accumulate*/ false);
78+
ggml_build_backward_expand(static_ctx, ctx, b_graph, /*accumulate*/ false);
7879
ggml_graph_print(b_graph);
7980

8081
// Set the gradient of the output tensor (mul) which would be the value of
8182
// the loss function.
82-
ggml_set_f32(mul->grad, 2.0f);
83+
const ggml_tensor* a_grad = ggml_graph_get_grad(b_graph, a);
84+
printf("a->grad: %f\n", ggml_get_f32_1d(a_grad, 0));
85+
ggml_set_f32(mul, 2.0f);
8386
// Compute the gradients
8487
printf("[Perform backward pass]\n\n");
8588
ggml_graph_compute_with_ctx(ctx, b_graph, 1);
8689

8790
printf("Updated gradients:\n");
88-
printf("a->grad: %f\n", ggml_get_f32_1d(a->grad, 0));
89-
printf("b->grad: %f\n", ggml_get_f32_1d(b->grad, 0));
91+
//printf("a->grad: %f\n", ggml_get_f32_1d(static_ctx, ggml_graph_get_grad(b_graph, a)));
92+
//printf("b->grad: %f\n", ggml_get_f32_1d(b->grad, 0));
9093
printf("\n");
9194

9295
// Now, a and b values would be updated using the gradients computed above.
9396
float learning_rate = 0.01;
94-
ggml_set_f32_1d(a, 0, ggml_get_f32_1d(a, 0) - learning_rate * ggml_get_f32_1d(a->grad, 0));
95-
ggml_set_f32_1d(b, 0, ggml_get_f32_1d(b, 0) - learning_rate * ggml_get_f32_1d(b->grad, 0));
97+
//ggml_set_f32_1d(a, 0, ggml_get_f32_1d(a, 0) - learning_rate * ggml_get_f32_1d(a->grad, 0));
98+
//ggml_set_f32_1d(b, 0, ggml_get_f32_1d(b, 0) - learning_rate * ggml_get_f32_1d(b->grad, 0));
9699

97100
printf("Updated parameters a and b:\n");
98101
printf("a: %f\n", ggml_get_f32_1d(a, 0));

fundamentals/ggml/src/device.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
3+
#include "ggml.h"
4+
#include "ggml-alloc.h"
5+
#include "ggml-backend.h"
6+
7+
void print_backend_info(ggml_backend_buffer_t buffer, struct ggml_context* ctx) {
8+
printf("------- device info -------\n");
9+
printf("buffer name: %s\n", ggml_backend_buffer_name(buffer));
10+
printf("buffer size: %ld\n", ggml_backend_buffer_get_size(buffer));
11+
printf("buffer alignment: %ld\n", ggml_backend_buffer_get_alignment(buffer));
12+
printf("buffer max size: %ld\n", ggml_backend_buffer_get_max_size(buffer));
13+
printf("buffer is host: %d\n", ggml_backend_buffer_is_host(buffer));
14+
15+
ggml_backend_buffer_type_t buffer_type = ggml_backend_buffer_get_type(buffer);
16+
printf("buffer type name: %s\n", ggml_backend_buft_name(buffer_type));
17+
printf("buffer type alignment: %ld\n", ggml_backend_buft_get_alignment(buffer_type));
18+
printf("buffer type max size: %ld\n", ggml_backend_buft_get_max_size(buffer_type));
19+
printf("buffer type is host: %d\n", ggml_backend_buft_is_host(buffer_type));
20+
}
21+
22+
int main(int argc, char **argv) {
23+
printf("GGML device examples\n");
24+
size_t device_count = ggml_backend_dev_count();
25+
printf("device count: %ld\n", device_count);
26+
27+
ggml_backend_dev_t device = ggml_backend_dev_get(0);
28+
printf("device name: %s\n", ggml_backend_dev_name(device));
29+
printf("device description: %s\n", ggml_backend_dev_description(device));
30+
31+
enum ggml_backend_dev_type type = ggml_backend_dev_type(device);
32+
ggml_backend_t backend = ggml_backend_init_by_type(type, NULL);
33+
printf("backend name: %s\n", ggml_backend_name(backend));
34+
if (type == GGML_BACKEND_DEVICE_TYPE_CPU) {
35+
printf("backend type: GGML_BACKEND_DEVICE_TYPE_CPU\n");
36+
}
37+
38+
ggml_backend_buffer_type_t buf_type = ggml_backend_get_default_buffer_type(backend);
39+
printf("buffer type name: %s\n", ggml_backend_buft_name(buf_type));
40+
41+
ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buf_type, 100);
42+
printf("buffer name: %s\n", ggml_backend_buffer_name(buffer));
43+
44+
return 0;
45+
}

fundamentals/ggml/src/ggml-quants.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int main(int argc, char **argv) {
4545
printf("ggml type trait is_quantized: %s\n", q4_0->is_quantized ? "true" : "false");
4646
// The type trait contains function pointers to the quantize and dequantize
4747
// functions
48-
q4_0->from_float(data, &block_q4_0, 32);
48+
q4_0->from_float_ref(data, &block_q4_0, 32);
4949
for (int i = 0; i < QK4_0/2; i++) {
5050
printf("block_q4_0.qs[%d]: %d\n", i, block_q4_0.qs[i]);
5151
}

fundamentals/ggml/src/opt-adam.c

+4
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

@@ -13,6 +14,8 @@ int main(int argc, char **argv) {
1314
};
1415
struct ggml_context* ctx = ggml_init(params);
1516

17+
/* TODO: revisit this code after reading up on the new optimizer API
18+
1619
struct ggml_opt_params opts = ggml_opt_default_params(GGML_OPT_TYPE_ADAM);
1720
struct ggml_tensor* a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
1821
ggml_set_param(ctx, a);
@@ -25,6 +28,7 @@ int main(int argc, char **argv) {
2528
ggml_graph_compute_with_ctx(ctx, cgraph, 1);
2629
2730
printf("a: n_elements: %ld\n", ggml_nelements(a));
31+
*/
2832

2933
ggml_free(ctx);
3034
return 0;

fundamentals/ggml/src/opt-lbfgs.c

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ int main(int argc, char **argv) {
1313
};
1414
struct ggml_context* ctx = ggml_init(params);
1515

16+
/* TODO: revisit this code after reading up on the new optimizer API
17+
1618
// Simulate a sequence of 6 tokens with en embedding size of 4096 and a
1719
// context length of 512.
1820
int n_ctx_orig = 4096;
@@ -119,6 +121,7 @@ int main(int argc, char **argv) {
119121
ggml_graph_compute_with_ctx(ctx, cgraph, 1);
120122
121123
//printf("a: n_elements: %ld\n", ggml_nelements(s));
124+
*/
122125

123126
ggml_free(ctx);
124127
return 0;

fundamentals/ggml/src/outer-product.c

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ int main(int argc, char **argv) {
2929
ggml_set_name(out, "out_prod");
3030
printf("out->src0: %s\n", out->src[0]->name);
3131
printf("out->src1: %s\n", out->src[1]->name);
32-
printf("out->grad: %s\n", out->grad->name);
3332
printf("out->ne[0]: %ld\n", out->ne[0]);
3433
printf("out->ne[1]: %ld\n", out->ne[1]);
3534
printf("\n");

fundamentals/ggml/src/softmax.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ggml.h"
66
#include "ggml-alloc.h"
77
#include "ggml-backend.h"
8+
#include "ggml-cpu.h"
89

910
int main(int argc, char **argv) {
1011
printf("GGML softmax example\n");

fundamentals/ggml/src/tensor.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ int main(int argc, char **argv) {
3030
// created by a + b = c, c being the tensor then the op would be GGML_OP_ADD.
3131
printf("x tensor operation: %s, %s\n", ggml_op_name(x->op), ggml_op_symbol(x->op));
3232
// ggml_tensor's are used as the base unit values in the library, similar to
33-
// the Value struct in the LLM zero-to-hero tutorial. These values support
34-
// automatic differentiation, so they have a grad field.
35-
printf("x tensor grad: %p\n", x->grad);
33+
// the Value struct in the LLM zero-to-hero tutorial.
3634
// src are the values that were used to create the tensor, for example if the
3735
// tensor was created by a + b = c, then the src would be a and b.
3836
printf("x tensor src: %p\n", x->src);

0 commit comments

Comments
 (0)