Skip to content

Commit 3100a05

Browse files
committed
ggml: reserve in gguf_writer and added const pointers as params
1 parent a1649cc commit 3100a05

File tree

7 files changed

+80
-79
lines changed

7 files changed

+80
-79
lines changed

ggml/include/ggml-alloc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ GGML_API void ggml_tallocr_alloc(struct ggml_tallocr * talloc, st
4646
typedef struct ggml_gallocr * ggml_gallocr_t;
4747

4848
GGML_API ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft);
49-
GGML_API ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs);
49+
GGML_API ggml_gallocr_t ggml_gallocr_new_n(const ggml_backend_buffer_type_t * bufts, int n_bufs);
5050
GGML_API void ggml_gallocr_free(ggml_gallocr_t galloc);
5151

5252
// pre-allocate buffers from a measure graph - does not allocate or modify the graph
5353
// call with a worst-case graph to avoid buffer reallocations
5454
// not strictly required for single buffer usage: ggml_gallocr_alloc_graph will reallocate the buffers automatically if needed
5555
// returns false if the buffer allocation failed
56-
GGML_API bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph * graph);
56+
GGML_API bool ggml_gallocr_reserve(ggml_gallocr_t galloc, const struct ggml_cgraph * graph);
5757
GGML_API bool ggml_gallocr_reserve_n(
5858
ggml_gallocr_t galloc,
59-
struct ggml_cgraph * graph,
59+
const struct ggml_cgraph * graph,
6060
const int * node_buffer_ids,
6161
const int * leaf_buffer_ids);
6262

ggml/include/ggml.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ extern "C" {
698698

699699
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
700700

701-
GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
701+
GGML_API bool ggml_get_no_alloc(const struct ggml_context * ctx);
702702
GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
703703

704704
GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
@@ -745,7 +745,7 @@ extern "C" {
745745
// Context tensor enumeration and lookup
746746
GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
747747
GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
748-
GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
748+
GGML_API struct ggml_tensor * ggml_get_tensor(const struct ggml_context * ctx, const char * name);
749749

750750
// Converts a flat index into coordinates
751751
GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
@@ -763,7 +763,7 @@ extern "C" {
763763
// Tensor flags
764764
GGML_API void ggml_set_input(struct ggml_tensor * tensor);
765765
GGML_API void ggml_set_output(struct ggml_tensor * tensor);
766-
GGML_API void ggml_set_param(struct ggml_context * ctx, struct ggml_tensor * tensor);
766+
GGML_API void ggml_set_param(const struct ggml_context * ctx, struct ggml_tensor * tensor);
767767
GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
768768

769769
//
@@ -927,13 +927,13 @@ extern "C" {
927927
GGML_API struct ggml_tensor * ggml_repeat(
928928
struct ggml_context * ctx,
929929
struct ggml_tensor * a,
930-
struct ggml_tensor * b);
930+
const struct ggml_tensor * b);
931931

932932
// sums repetitions in a into shape of b
933933
GGML_API struct ggml_tensor * ggml_repeat_back(
934934
struct ggml_context * ctx,
935935
struct ggml_tensor * a,
936-
struct ggml_tensor * b);
936+
const struct ggml_tensor * b);
937937

938938
// concat a and b along dim
939939
// used in stable-diffusion
@@ -1243,7 +1243,7 @@ extern "C" {
12431243
GGML_API struct ggml_tensor * ggml_reshape(
12441244
struct ggml_context * ctx,
12451245
struct ggml_tensor * a,
1246-
struct ggml_tensor * b);
1246+
const struct ggml_tensor * b);
12471247

12481248
// return view(a)
12491249
// TODO: when we start computing gradient, make a copy instead of view
@@ -1335,7 +1335,7 @@ extern "C" {
13351335
struct ggml_context * ctx,
13361336
struct ggml_tensor * a, // gradients of ggml_get_rows result
13371337
struct ggml_tensor * b, // row indices
1338-
struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
1338+
const struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
13391339

13401340
GGML_API struct ggml_tensor * ggml_diag(
13411341
struct ggml_context * ctx,
@@ -1563,7 +1563,7 @@ extern "C" {
15631563
struct ggml_context * ctx,
15641564
struct ggml_tensor * a, // convolution kernel
15651565
struct ggml_tensor * b, // gradient of im2col output
1566-
int64_t * ne, // shape of im2col input
1566+
const int64_t * ne, // shape of im2col input
15671567
int s0, // stride dimension 0
15681568
int s1, // stride dimension 1
15691569
int p0, // padding dimension 0
@@ -2062,15 +2062,16 @@ extern "C" {
20622062
// graph allocation in a context
20632063
GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
20642064
GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
2065-
GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
2066-
GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
2067-
GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2065+
GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, const struct ggml_cgraph * cgraph);
2066+
GGML_API void ggml_graph_cpy (const struct ggml_cgraph * src, struct ggml_cgraph * dst);
2067+
GGML_API void ggml_graph_reset (
2068+
const struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
20682069
GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
20692070

2070-
GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph);
2071-
GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2072-
GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph);
2073-
GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph);
2071+
GGML_API int ggml_graph_size (const struct ggml_cgraph * cgraph);
2072+
GGML_API struct ggml_tensor * ggml_graph_node (const struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2073+
GGML_API struct ggml_tensor ** ggml_graph_nodes (const struct ggml_cgraph * cgraph);
2074+
GGML_API int ggml_graph_n_nodes(const struct ggml_cgraph * cgraph);
20742075

20752076
GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
20762077

ggml/src/ggml-alloc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ struct ggml_gallocr {
377377
int n_leafs;
378378
};
379379

380-
ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) {
380+
ggml_gallocr_t ggml_gallocr_new_n(const ggml_backend_buffer_type_t * bufts, int n_bufs) {
381381
ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr));
382382
GGML_ASSERT(galloc != NULL);
383383

@@ -563,7 +563,7 @@ static int get_node_buffer_id(const int * node_buffer_ids, int i) {
563563
return node_buffer_ids ? node_buffer_ids[i] : 0;
564564
}
565565

566-
static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
566+
static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, const struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
567567
// clear hash tables
568568
ggml_hash_set_reset(&galloc->hash_set);
569569
memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size);
@@ -670,7 +670,7 @@ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
670670
}
671671
}
672672

673-
bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
673+
bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, const struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
674674
size_t min_hash_size = graph->n_nodes + graph->n_leafs;
675675
// add 25% margin to avoid hash collisions
676676
min_hash_size += min_hash_size / 4;
@@ -780,11 +780,11 @@ bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, c
780780
return true;
781781
}
782782

783-
bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph *graph) {
783+
bool ggml_gallocr_reserve(ggml_gallocr_t galloc, const struct ggml_cgraph *graph) {
784784
return ggml_gallocr_reserve_n(galloc, graph, NULL, NULL);
785785
}
786786

787-
static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) {
787+
static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, const struct tensor_alloc * tensor_alloc) {
788788
int buffer_id = tensor_alloc->buffer_id;
789789
assert(tensor->data || tensor->view_src || ggml_backend_buffer_get_alloc_size(galloc->buffers[buffer_id], tensor) <= tensor_alloc->size_max);
790790

@@ -813,7 +813,7 @@ static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor *
813813
}
814814
}
815815

816-
static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, struct tensor_alloc * talloc) {
816+
static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, const struct tensor_alloc * talloc) {
817817
size_t node_size = 0;
818818
if (!node->data && !node->view_src) {
819819
GGML_ASSERT(talloc->buffer_id >= 0); // prevent segfault when misusing the API
@@ -822,7 +822,7 @@ static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_t
822822
return talloc->size_max >= node_size;
823823
}
824824

825-
static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
825+
static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, const struct ggml_cgraph * graph) {
826826
if (galloc->n_nodes != graph->n_nodes) {
827827
#ifndef NDEBUG
828828
GGML_LOG_DEBUG("%s: graph has different number of nodes\n", __func__);
@@ -933,8 +933,8 @@ size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id) {
933933

934934
// utils
935935

936-
static bool alloc_tensor_range(struct ggml_context * ctx,
937-
struct ggml_tensor * first, struct ggml_tensor * last,
936+
static bool alloc_tensor_range(const struct ggml_context * ctx,
937+
struct ggml_tensor * first, const struct ggml_tensor * last,
938938
ggml_backend_buffer_type_t buft, size_t size,
939939
ggml_backend_buffer_t ** buffers, size_t * n_buffers) {
940940
ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, size);

ggml/src/ggml-backend-reg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void * dl_get_sym(dl_handle * handle, const char * name) {
124124
using dl_handle = void;
125125

126126
struct dl_handle_deleter {
127-
void operator()(void * handle) {
127+
void operator()(void * handle) const {
128128
dlclose(handle);
129129
}
130130
};

ggml/src/ggml-impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ struct ggml_hash_set {
188188
};
189189

190190
struct ggml_hash_set ggml_hash_set_new(size_t size);
191-
void ggml_hash_set_free(struct ggml_hash_set * hash_set);
191+
void ggml_hash_set_free(const struct ggml_hash_set * hash_set);
192192

193193
// returns the minimum size for a hash set that can hold min_sz elements
194194
size_t ggml_hash_size(size_t min_sz);
195195

196196
// remove all elements from the hash set
197-
void ggml_hash_set_reset(struct ggml_hash_set * hash_set);
197+
void ggml_hash_set_reset(const struct ggml_hash_set * hash_set);
198198

199199
// returns true if key is in the hash set
200200
static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
@@ -302,7 +302,7 @@ struct ggml_cgraph {
302302
// returns a slice of cgraph with nodes [i0, i1)
303303
// the slice does not have leafs or gradients
304304
// if you need the gradients, get them from the original graph
305-
struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
305+
struct ggml_cgraph ggml_graph_view(const struct ggml_cgraph * cgraph, int i0, int i1);
306306

307307
// Memory allocation
308308

0 commit comments

Comments
 (0)