Skip to content

Commit 7d59c7a

Browse files
author
katsu560
committed
remove gguf_get_tensor_size
1 parent 3f06cef commit 7d59c7a

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

examples/yolo/yolov3-tiny.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,19 @@ static bool load_labels(const char * filename, std::vector<std::string> & labels
157157
return true;
158158
}
159159

160-
static bool load_labels_gguf(const struct gguf_context * ctx, const char * filename, std::vector<std::string> & labels)
160+
static bool load_labels_gguf(struct ggml_context * ctx, const struct gguf_context * ctx_gguf, const char * filename, std::vector<std::string> & labels)
161161
{
162-
int tensor = gguf_find_tensor(ctx, filename);
163-
if (tensor == -1) {
162+
struct ggml_tensor * tensor = ggml_get_tensor(ctx, filename);
163+
if (tensor == NULL) {
164164
return false;
165165
}
166-
const size_t offset = gguf_get_tensor_offset(ctx, tensor);
167-
const size_t len = gguf_get_tensor_size(ctx, tensor);
168-
const char * data = (char *)gguf_get_data(ctx);
166+
int tensoridx = gguf_find_tensor(ctx_gguf, filename);
167+
if (tensoridx == -1) {
168+
return false;
169+
}
170+
const size_t len = ggml_nelements(tensor);
171+
const size_t offset = gguf_get_tensor_offset(ctx_gguf, tensoridx);
172+
const char * data = (char *)gguf_get_data(ctx_gguf);
169173
membuf buf(data + offset, data + offset + len);
170174
std::istream file_in(&buf);
171175
if (!file_in) {
@@ -195,21 +199,26 @@ static bool load_alphabet(std::vector<yolo_image> & alphabet)
195199
return true;
196200
}
197201

198-
static bool load_alphabet_gguf(const struct gguf_context * ctx, std::vector<yolo_image> & alphabet)
202+
static bool load_alphabet_gguf(struct ggml_context * ctx, const struct gguf_context * ctx_gguf, std::vector<yolo_image> & alphabet)
199203
{
200204
alphabet.resize(8 * 128);
201205
for (int j = 0; j < 8; j++) {
202206
for (int i = 32; i < 127; i++) {
203207
char fname[256];
204208
sprintf(fname, "data/labels/%d_%d.png", i, j);
205-
int tensor = gguf_find_tensor(ctx, fname);
206-
if (tensor == -1) {
209+
struct ggml_tensor * tensor = ggml_get_tensor(ctx, fname);
210+
if (tensor == NULL) {
211+
fprintf(stderr, "Cannot find '%s' in tensor\n", fname);
212+
return false;
213+
}
214+
int tensoridx = gguf_find_tensor(ctx_gguf, fname);
215+
if (tensoridx == -1) {
207216
fprintf(stderr, "Cannot find '%s' in tensor\n", fname);
208217
return false;
209218
}
210-
const size_t offset = gguf_get_tensor_offset(ctx, tensor);
211-
const size_t len = gguf_get_tensor_size(ctx, tensor);
212-
const char * data = (char *)gguf_get_data(ctx);
219+
const size_t len = ggml_nelements(tensor);
220+
const size_t offset = gguf_get_tensor_offset(ctx_gguf, tensoridx);
221+
const char * data = (char *)gguf_get_data(ctx_gguf);
213222
if (!load_image_from_memory(data + offset, len, alphabet[j*128 + i])) {
214223
fprintf(stderr, "Cannot load '%s'\n", fname);
215224
return false;
@@ -592,15 +601,15 @@ int main(int argc, char *argv[])
592601
return 1;
593602
}
594603
std::vector<std::string> labels;
595-
if (!load_labels_gguf(model.ctx_gguf, "data/coco.names", labels)) {
604+
if (!load_labels_gguf(model.ctx, model.ctx_gguf, "data/coco.names", labels)) {
596605
fprintf(stderr, "%s: skipped loading labels from 'data/coco.names' in model\n", __func__);
597606
if (!load_labels("data/coco.names", labels)) {
598607
fprintf(stderr, "%s: failed to load labels from 'data/coco.names'\n", __func__);
599608
return 1;
600609
}
601610
}
602611
std::vector<yolo_image> alphabet;
603-
if (!load_alphabet_gguf(model.ctx_gguf, alphabet)) {
612+
if (!load_alphabet_gguf(model.ctx, model.ctx_gguf, alphabet)) {
604613
fprintf(stderr, "%s: skipped loading alphabet from model\n", __func__);
605614
if (!load_alphabet(alphabet)) {
606615
fprintf(stderr, "%s: failed to load alphabet\n", __func__);

include/ggml/ggml.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,6 @@ extern "C" {
23332333
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
23342334
GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
23352335
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);
2336-
GGML_API size_t gguf_get_tensor_size (const struct gguf_context * ctx, int i);
23372336

23382337
// removes key if it exists
23392338
GGML_API void gguf_remove_key(struct gguf_context * ctx, const char * key);

src/ggml.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21941,11 +21941,6 @@ enum ggml_type gguf_get_tensor_type(const struct gguf_context * ctx, int i) {
2194121941
return ctx->infos[i].type;
2194221942
}
2194321943

21944-
size_t gguf_get_tensor_size(const struct gguf_context * ctx, int i) {
21945-
GGML_ASSERT(i >= 0 && i < gguf_get_n_tensors(ctx));
21946-
return ctx->infos[i].size;
21947-
}
21948-
2194921944
// returns the index
2195021945
static int gguf_get_or_add_key(struct gguf_context * ctx, const char * key) {
2195121946
const int idx = gguf_find_key(ctx, key);

0 commit comments

Comments
 (0)