| 13041 | } |
| 13042 | |
| 13043 | bool sam3_test_dump_phase5_from_ref_inputs(const sam3_model& model, |
| 13044 | const std::vector<int32_t>& token_ids, |
| 13045 | const std::string& prephase_ref_dir, |
| 13046 | const std::string& phase5_ref_dir, |
| 13047 | const std::string& output_dir, |
| 13048 | int n_threads) { |
| 13049 | const auto& hp = model.hparams; |
| 13050 | const int D = hp.neck_dim; |
| 13051 | const int H = hp.n_img_embd(); |
| 13052 | const int L = hp.text_ctx_len; |
| 13053 | const int NQ = hp.ddec_num_queries; |
| 13054 | const int H1 = H * 2; |
| 13055 | const int H0 = H * 4; |
| 13056 | |
| 13057 | if ((int)token_ids.size() != L) { |
| 13058 | fprintf(stderr, "%s: expected %d token IDs, got %zu\n", |
| 13059 | __func__, L, token_ids.size()); |
| 13060 | return false; |
| 13061 | } |
| 13062 | |
| 13063 | std::vector<float> neck_det_0; |
| 13064 | std::vector<float> neck_det_1; |
| 13065 | std::vector<float> fenc_img_input_data; |
| 13066 | std::vector<float> fenc_pos_embed_data; |
| 13067 | std::vector<float> img_pe_72_data; |
| 13068 | std::vector<float> text_features_data; |
| 13069 | if (!sam3_load_ref_f32_data(prephase_ref_dir + "/neck_det_0", neck_det_0, D * H0 * H0) || |
| 13070 | !sam3_load_ref_f32_data(prephase_ref_dir + "/neck_det_1", neck_det_1, D * H1 * H1) || |
| 13071 | !sam3_load_ref_f32_data(phase5_ref_dir + "/fenc_img_input", fenc_img_input_data, D * H * H) || |
| 13072 | !sam3_load_ref_f32_data(phase5_ref_dir + "/fenc_pos_embed", fenc_pos_embed_data, D * H * H) || |
| 13073 | !sam3_load_ref_f32_data(phase5_ref_dir + "/img_pe_72", img_pe_72_data, D * H * H) || |
| 13074 | !sam3_load_ref_f32_data(phase5_ref_dir + "/text_features", text_features_data, D * L)) { |
| 13075 | return false; |
| 13076 | } |
| 13077 | |
| 13078 | neck_det_0 = sam3_reorder_nchw_to_ggml_dwh(neck_det_0, D, H0, H0); |
| 13079 | neck_det_1 = sam3_reorder_nchw_to_ggml_dwh(neck_det_1, D, H1, H1); |
| 13080 | |
| 13081 | const size_t buf_size = ggml_tensor_overhead() * 65536 + ggml_graph_overhead() * 2; |
| 13082 | struct ggml_init_params gparams = { |
| 13083 | /*.mem_size =*/buf_size, |
| 13084 | /*.mem_buffer =*/nullptr, |
| 13085 | /*.no_alloc =*/true, |
| 13086 | }; |
| 13087 | struct ggml_context* ctx0 = ggml_init(gparams); |
| 13088 | if (!ctx0) { |
| 13089 | fprintf(stderr, "%s: failed to init compute context\n", __func__); |
| 13090 | return false; |
| 13091 | } |
| 13092 | |
| 13093 | auto* text_features = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, D, L, 1); |
| 13094 | ggml_set_name(text_features, "text_features"); |
| 13095 | ggml_set_input(text_features); |
| 13096 | |
| 13097 | auto* neck0 = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, D, H0, H0, 1); |
| 13098 | ggml_set_name(neck0, "ref_neck_det_0"); |
| 13099 | ggml_set_input(neck0); |
| 13100 | auto* neck1 = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, D, H1, H1, 1); |