| 39 | } |
| 40 | |
| 41 | static bool run_backend(const std::string & model_path, |
| 42 | const std::vector<float> & chw_data, |
| 43 | int img_size, |
| 44 | bool use_gpu, |
| 45 | int n_threads, |
| 46 | const std::vector<std::string> & checkpoints, |
| 47 | const std::string & dump_dir, |
| 48 | double & elapsed_ms) { |
| 49 | sam3_params params; |
| 50 | params.model_path = model_path; |
| 51 | params.use_gpu = use_gpu; |
| 52 | params.n_threads = n_threads; |
| 53 | |
| 54 | auto model = sam3_load_model(params); |
| 55 | if (!model) { |
| 56 | fprintf(stderr, "failed to load model for %s\n", use_gpu ? "Metal" : "CPU"); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | auto state = sam3_create_state(*model, params); |
| 61 | if (!state) { |
| 62 | fprintf(stderr, "failed to create state for %s\n", use_gpu ? "Metal" : "CPU"); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | auto t0 = std::chrono::high_resolution_clock::now(); |
| 67 | const bool ok = sam3_encode_image_from_preprocessed(*state, *model, chw_data.data(), img_size); |
| 68 | auto t1 = std::chrono::high_resolution_clock::now(); |
| 69 | elapsed_ms = std::chrono::duration<double, std::milli>(t1 - t0).count(); |
| 70 | if (!ok) { |
| 71 | fprintf(stderr, "%s encode failed\n", use_gpu ? "Metal" : "CPU"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (!dump_checkpoints(*state, checkpoints, dump_dir)) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | state.reset(); |
| 80 | sam3_free_model(*model); |
| 81 | model.reset(); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | int main(int argc, char ** argv) { |
| 86 | if (argc < 2) { |
no test coverage detected