Skip to content

Commit cafcd4f

Browse files
authored
ggml : remove n_dims from ggml_tensor (#4469)
ggml-ci
1 parent c50e400 commit cafcd4f

File tree

9 files changed

+81
-73
lines changed

9 files changed

+81
-73
lines changed

common/train.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void free_random_uniform_distribution(struct random_uniform_distribution * rnd)
7171

7272
struct ggml_tensor * randomize_tensor_normal(struct ggml_tensor * tensor, struct random_normal_distribution * rnd) {
7373
float scale = 1.0f; // xavier
74-
switch (tensor->n_dims) {
74+
switch (ggml_n_dims(tensor)) {
7575
case 1:
7676
scale /= sqrtf((float) tensor->ne[0]);
7777
for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
@@ -119,7 +119,7 @@ struct ggml_tensor * randomize_tensor_normal(struct ggml_tensor * tensor, struct
119119
}
120120

121121
struct ggml_tensor * randomize_tensor_uniform(struct ggml_tensor * tensor, struct random_uniform_distribution * rnd) {
122-
switch (tensor->n_dims) {
122+
switch (ggml_n_dims(tensor)) {
123123
case 1:
124124
for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
125125
float * dst = (float *) ((char *) tensor->data + i0*tensor->nb[0]);
@@ -183,25 +183,27 @@ float fclamp(const float v, const float min, const float max) {
183183
}
184184

185185
void assert_shape_1d(struct ggml_tensor * tensor, int64_t ne0) {
186-
GGML_ASSERT(tensor->n_dims == 1);
187186
GGML_ASSERT(tensor->ne[0] == ne0);
187+
GGML_ASSERT(tensor->ne[1] == 1);
188+
GGML_ASSERT(tensor->ne[2] == 1);
189+
GGML_ASSERT(tensor->ne[3] == 1);
188190
}
189191

190192
void assert_shape_2d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1) {
191-
GGML_ASSERT(tensor->n_dims == 2);
192193
GGML_ASSERT(tensor->ne[0] == ne0);
193194
GGML_ASSERT(tensor->ne[1] == ne1);
195+
GGML_ASSERT(tensor->ne[2] == 1);
196+
GGML_ASSERT(tensor->ne[3] == 1);
194197
}
195198

196199
void assert_shape_3d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1, int64_t ne2) {
197-
GGML_ASSERT(tensor->n_dims == 3);
198200
GGML_ASSERT(tensor->ne[0] == ne0);
199201
GGML_ASSERT(tensor->ne[1] == ne1);
200202
GGML_ASSERT(tensor->ne[2] == ne2);
203+
GGML_ASSERT(tensor->ne[3] == 1);
201204
}
202205

203206
void assert_shape_4d(struct ggml_tensor * tensor, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
204-
GGML_ASSERT(tensor->n_dims == 4);
205207
GGML_ASSERT(tensor->ne[0] == ne0);
206208
GGML_ASSERT(tensor->ne[1] == ne1);
207209
GGML_ASSERT(tensor->ne[2] == ne2);
@@ -225,8 +227,8 @@ int64_t get_example_targets_batch(
225227
bool sample_random_offsets
226228
) {
227229
GGML_ASSERT(samples_count > 0);
228-
GGML_ASSERT(tokens_input->n_dims == 2);
229-
GGML_ASSERT(target_probs->n_dims == 3);
230+
GGML_ASSERT(ggml_is_matrix(tokens_input));
231+
GGML_ASSERT(ggml_is_3d(target_probs));
230232
int64_t n_vocab = target_probs->ne[0];
231233
int64_t n_tokens = tokens_input->ne[0];
232234
int64_t n_batch = tokens_input->ne[1];

examples/baby-llama/baby-llama.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1258,9 +1258,9 @@ static struct ggml_tensor * forward_lora(
12581258
}
12591259

12601260
static void sample_softmax(struct ggml_tensor * logits, struct ggml_tensor * probs, struct ggml_tensor * best_samples) {
1261-
assert(logits->n_dims == 2);
1262-
assert(probs->n_dims == 2);
1263-
assert(best_samples->n_dims == 1);
1261+
assert(ggml_is_matrix(logits));
1262+
assert(ggml_is_matrix(probs));
1263+
assert(ggml_is_vector(best_samples));
12641264
assert(logits->ne[1] == best_samples->ne[0]);
12651265
assert(logits->ne[0] == probs->ne[0]);
12661266
assert(logits->ne[1] == probs->ne[1]);
@@ -1292,9 +1292,9 @@ static void sample_softmax_batch(
12921292
struct ggml_context * ctx, struct ggml_tensor * logits, struct ggml_tensor * probs,
12931293
struct ggml_tensor * best_samples
12941294
) {
1295-
GGML_ASSERT(best_samples->n_dims == 2);
1296-
GGML_ASSERT(logits->n_dims == 3);
1297-
GGML_ASSERT(probs->n_dims == 3);
1295+
GGML_ASSERT(ggml_is_matrix(best_samples));
1296+
GGML_ASSERT(ggml_is_3d(logits));
1297+
GGML_ASSERT(ggml_is_3d(probs));
12981298
int n_tokens = best_samples->ne[0];
12991299
int n_batch = best_samples->ne[1];
13001300
int n_vocab = logits->ne[0];
@@ -1334,7 +1334,7 @@ static void print_row(struct ggml_tensor * probs, int i) {
13341334
}
13351335

13361336
static void print_matrix(struct ggml_tensor * probs) {
1337-
assert(probs->n_dims == 2);
1337+
assert(ggml_is_matrix(probs));
13381338
for (int i = 0; i < probs->ne[1]; ++i) {
13391339
for (int k = 0; k < probs->ne[0]; ++k) {
13401340
float p = ggml_get_f32_1d(probs, i*probs->ne[0] + k);
@@ -1386,8 +1386,8 @@ static void get_example_targets(int example_id, struct ggml_tensor * tokens_inpu
13861386
static void get_example_targets_batch(
13871387
struct ggml_context * ctx, int example_id, struct ggml_tensor * tokens_input, struct ggml_tensor * targets
13881388
) {
1389-
GGML_ASSERT(tokens_input->n_dims == 2);
1390-
GGML_ASSERT( targets->n_dims == 3);
1389+
GGML_ASSERT(ggml_is_matrix(tokens_input));
1390+
GGML_ASSERT(ggml_is_3d(targets));
13911391
int n_tokens = tokens_input->ne[0];
13921392
int n_batch = tokens_input->ne[1];
13931393
GGML_ASSERT(n_tokens == targets->ne[1]);

examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ static void print_row(struct ggml_tensor * probs, int i) {
427427
}
428428

429429
static void print_matrix(struct ggml_tensor * probs) {
430-
assert(probs->n_dims == 2);
430+
assert(ggml_is_matrix(probs));
431431
for (int i = 0; i < probs->ne[1]; ++i) {
432432
for (int k = 0; k < probs->ne[0]; ++k) {
433433
float p = get_f32_2d(probs, k, i);
@@ -639,7 +639,7 @@ static void load_vocab(const char *filename, Config *config, struct llama_vocab
639639

640640
static void convert_weights_ak_to_gg(struct ggml_tensor * gg_weights, const float * karpathy_weights) {
641641
int ct;
642-
switch (gg_weights->n_dims){
642+
switch (ggml_n_dims(gg_weights)) {
643643
case 1:
644644
ct = 0;
645645
for (int i0 = 0; i0 < gg_weights->ne[0]; i0++){

examples/finetune/finetune.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ static void write_tensor(struct llama_file * file, struct ggml_tensor * tensor,
11101110
name = ggml_get_name(tensor);
11111111
}
11121112
uint32_t name_len = strlen(name);
1113-
uint32_t nd = tensor->n_dims;
1113+
uint32_t nd = ggml_n_dims(tensor);
11141114
uint32_t ne[4] = { (uint32_t)tensor->ne[0],
11151115
(uint32_t)tensor->ne[1],
11161116
(uint32_t)tensor->ne[2],

examples/gguf/gguf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static bool gguf_ex_read_1(const std::string & fname) {
195195

196196
struct ggml_tensor * cur = ggml_get_tensor(ctx_data, name);
197197

198-
printf("%s: tensor[%d]: n_dims = %d, name = %s, data = %p\n", __func__, i, cur->n_dims, cur->name, cur->data);
198+
printf("%s: tensor[%d]: n_dims = %d, name = %s, data = %p\n", __func__, i, ggml_n_dims(cur), cur->name, cur->data);
199199

200200
// print first 10 elements
201201
const float * data = (const float *) cur->data;

examples/llava/clip.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
514514
ctx_size += padded_size;
515515
if (verbosity >= 3) {
516516
printf("%s: tensor[%d]: n_dims = %d, name = %s, tensor_size=%zu, padded_size=%zu, offset=%zu\n", __func__, i,
517-
cur->n_dims, cur->name, tensor_size, padded_size, offset);
517+
ggml_n_dims(cur), cur->name, tensor_size, padded_size, offset);
518518
}
519519
}
520520
}
@@ -962,7 +962,7 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i
962962
}
963963

964964
// quantize only 2D tensors
965-
quantize &= (cur->n_dims == 2);
965+
quantize &= (ggml_n_dims(cur) == 2);
966966

967967
if (quantize) {
968968
new_type = type;
@@ -1035,7 +1035,7 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i
10351035
fout.put(0);
10361036
}
10371037

1038-
printf("%s: n_dims = %d | quantize=%d | size = %f MB -> %f MB\n", name.c_str(), cur->n_dims, quantize,
1038+
printf("%s: n_dims = %d | quantize=%d | size = %f MB -> %f MB\n", name.c_str(), ggml_n_dims(cur), quantize,
10391039
orig_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
10401040
}
10411041

0 commit comments

Comments
 (0)