MCPcopy Create free account
hub / github.com/PABannier/sam3.cpp / sam3_dump_tensor_to_path

Function sam3_dump_tensor_to_path

sam3.cpp:12433–12524  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12431}
12432
12433static bool sam3_dump_tensor_to_path(struct ggml_tensor* t,
12434 const std::string& tensor_name,
12435 const std::string& output_path) {
12436 if (!t) {
12437 fprintf(stderr, "%s: tensor '%s' is null\n", __func__, tensor_name.c_str());
12438 return false;
12439 }
12440
12441 int64_t numel = 1;
12442 for (int i = 0; i < GGML_MAX_DIMS; ++i) {
12443 if (t->ne[i] > 0) {
12444 numel *= t->ne[i];
12445 }
12446 }
12447
12448 std::vector<float> data(numel);
12449 if (t->type != GGML_TYPE_F16 && t->type != GGML_TYPE_F32) {
12450 fprintf(stderr, "%s: unsupported tensor type %d for '%s'\n",
12451 __func__, (int)t->type, tensor_name.c_str());
12452 return false;
12453 }
12454
12455 const int64_t ne0 = t->ne[0];
12456 const int64_t ne1 = t->ne[1];
12457 const int64_t ne2 = t->ne[2];
12458 const int64_t ne3 = t->ne[3];
12459
12460 if (ggml_is_contiguous(t)) {
12461 if (t->type == GGML_TYPE_F16) {
12462 std::vector<ggml_fp16_t> f16_data(numel);
12463 ggml_backend_tensor_get(t, f16_data.data(), 0, numel * sizeof(ggml_fp16_t));
12464 ggml_fp16_to_fp32_row(f16_data.data(), data.data(), numel);
12465 } else {
12466 ggml_backend_tensor_get(t, data.data(), 0, numel * sizeof(float));
12467 }
12468 } else if (t->nb[0] == ggml_type_size(t->type)) {
12469 // Serialize non-contiguous logical tensors in row-major ggml order.
12470 if (t->type == GGML_TYPE_F16) {
12471 std::vector<ggml_fp16_t> row(ne0);
12472 for (int64_t i3 = 0; i3 < ne3; ++i3) {
12473 for (int64_t i2 = 0; i2 < ne2; ++i2) {
12474 for (int64_t i1 = 0; i1 < ne1; ++i1) {
12475 const size_t row_idx = ((size_t) i3 * ne2 * ne1 + (size_t) i2 * ne1 + (size_t) i1) * ne0;
12476 const size_t offs = i3 * t->nb[3] + i2 * t->nb[2] + i1 * t->nb[1];
12477 ggml_backend_tensor_get(t, row.data(), offs, ne0 * sizeof(ggml_fp16_t));
12478 ggml_fp16_to_fp32_row(row.data(), data.data() + row_idx, ne0);
12479 }
12480 }
12481 }
12482 } else {
12483 for (int64_t i3 = 0; i3 < ne3; ++i3) {
12484 for (int64_t i2 = 0; i2 < ne2; ++i2) {
12485 for (int64_t i1 = 0; i1 < ne1; ++i1) {
12486 const size_t row_idx = ((size_t) i3 * ne2 * ne1 + (size_t) i2 * ne1 + (size_t) i1) * ne0;
12487 const size_t offs = i3 * t->nb[3] + i2 * t->nb[2] + i1 * t->nb[1];
12488 ggml_backend_tensor_get(t, data.data() + row_idx, offs, ne0 * sizeof(float));
12489 }
12490 }

Calls

no outgoing calls

Tested by

no test coverage detected