| 438 | // ═══════════════════════════════════════════════════════════════════════════════ |
| 439 | |
| 440 | static void test_encode_from_preprocessed(const std::string & model_path, |
| 441 | const std::string & ref_dir, |
| 442 | int & n_pass, int & n_fail) { |
| 443 | fprintf(stderr, "\n╔══════════════════════════════════════════╗\n"); |
| 444 | fprintf(stderr, "║ Test: ViT Numerics (Python preprocess) ║\n"); |
| 445 | fprintf(stderr, "╚══════════════════════════════════════════╝\n"); |
| 446 | |
| 447 | // Load Python-preprocessed image (CHW layout, [1, 3, 1008, 1008]) |
| 448 | auto preproc = load_ref(ref_dir + "/preprocessed"); |
| 449 | if (preproc.data.empty()) { |
| 450 | fprintf(stderr, " [SKIP] No preprocessed reference data\n"); |
| 451 | return; |
| 452 | } |
| 453 | fprintf(stderr, " Loaded preprocessed image: shape=["); |
| 454 | for (size_t i = 0; i < preproc.shape.size(); ++i) { |
| 455 | if (i > 0) fprintf(stderr, ", "); |
| 456 | fprintf(stderr, "%d", preproc.shape[i]); |
| 457 | } |
| 458 | fprintf(stderr, "] numel=%d\n", preproc.numel()); |
| 459 | |
| 460 | // The preprocessed data is [1, 3, 1008, 1008] in PyTorch NCHW layout. |
| 461 | // For ggml, the input tensor is [W=1008, H=1008, C=3, B=1]. |
| 462 | // ggml flat index: x + y*W + c*W*H = same as CHW[c*H*W + y*W + x]. |
| 463 | // So the CHW data can be uploaded directly. |
| 464 | const float * chw_data = preproc.data.data(); |
| 465 | int img_size = preproc.shape[2]; // 1008 |
| 466 | |
| 467 | sam3_params params; |
| 468 | params.model_path = model_path; |
| 469 | params.use_gpu = false; |
| 470 | params.n_threads = 4; |
| 471 | |
| 472 | auto model = sam3_load_model(params); |
| 473 | if (!model) { |
| 474 | fprintf(stderr, " FATAL: Failed to load model\n"); |
| 475 | n_fail++; |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | auto state = sam3_create_state(*model, params); |
| 480 | if (!state) { |
| 481 | fprintf(stderr, " FATAL: Failed to create state\n"); |
| 482 | n_fail++; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | bool ok = sam3_encode_image_from_preprocessed(*state, *model, chw_data, img_size); |
| 487 | if (!ok) { |
| 488 | fprintf(stderr, " FATAL: sam3_encode_image_from_preprocessed failed\n"); |
| 489 | n_fail++; |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | std::string dump_dir = ref_dir + "/cpp_out_from_preproc"; |
| 494 | { |
| 495 | std::string cmd = "mkdir -p " + dump_dir; |
| 496 | (void)system(cmd.c_str()); |
| 497 | } |
no test coverage detected