| 12767 | } |
| 12768 | |
| 12769 | bool sam3_test_dump_phase5(const sam3_model& model, |
| 12770 | const sam3_state& state, |
| 12771 | const std::vector<int32_t>& token_ids, |
| 12772 | const std::string& output_dir, |
| 12773 | int n_threads) { |
| 12774 | const auto& hp = model.hparams; |
| 12775 | const int D = hp.neck_dim; |
| 12776 | const int H = hp.n_img_embd(); |
| 12777 | const int L = hp.text_ctx_len; |
| 12778 | const int NQ = hp.ddec_num_queries; |
| 12779 | |
| 12780 | if ((int)token_ids.size() != L) { |
| 12781 | fprintf(stderr, "%s: expected %d token IDs, got %zu\n", |
| 12782 | __func__, L, token_ids.size()); |
| 12783 | return false; |
| 12784 | } |
| 12785 | if (!state.neck_det[0] || !state.neck_det_pe[2]) { |
| 12786 | fprintf(stderr, "%s: encoded detector features are missing\n", __func__); |
| 12787 | return false; |
| 12788 | } |
| 12789 | |
| 12790 | const size_t buf_size = ggml_tensor_overhead() * 65536 + ggml_graph_overhead() * 2; |
| 12791 | struct ggml_init_params gparams = { |
| 12792 | /*.mem_size =*/buf_size, |
| 12793 | /*.mem_buffer =*/nullptr, |
| 12794 | /*.no_alloc =*/true, |
| 12795 | }; |
| 12796 | struct ggml_context* ctx0 = ggml_init(gparams); |
| 12797 | if (!ctx0) { |
| 12798 | fprintf(stderr, "%s: failed to init compute context\n", __func__); |
| 12799 | return false; |
| 12800 | } |
| 12801 | |
| 12802 | auto* inp_tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, L); |
| 12803 | ggml_set_name(inp_tokens, "text_token_ids"); |
| 12804 | ggml_set_input(inp_tokens); |
| 12805 | |
| 12806 | auto* text_features_2d = sam3_build_text_encoder_graph(ctx0, inp_tokens, model); |
| 12807 | auto* text_features = ggml_reshape_3d(ctx0, text_features_2d, D, L, 1); |
| 12808 | ggml_set_name(text_features, "text_features"); |
| 12809 | |
| 12810 | // Make a snapshot copy of text_features for dumping — the graph allocator |
| 12811 | // may reuse the view's underlying buffer for later ops. |
| 12812 | auto* text_features_snap = ggml_cont(ctx0, ggml_reshape_2d(ctx0, text_features_2d, D, L)); |
| 12813 | ggml_set_name(text_features_snap, "text_features_snap"); |
| 12814 | |
| 12815 | auto* img_feats = ggml_reshape_3d(ctx0, state.neck_det[2], D, H * H, 1); |
| 12816 | auto* img_pe = ggml_reshape_3d(ctx0, state.neck_det_pe[2], D, H * H, 1); |
| 12817 | |
| 12818 | auto* sine_dim_t = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, 1, 64); |
| 12819 | ggml_set_name(sine_dim_t, "sine_dim_t"); |
| 12820 | ggml_set_input(sine_dim_t); |
| 12821 | |
| 12822 | auto* rpb_coords = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, H); |
| 12823 | ggml_set_name(rpb_coords, "rpb_coords"); |
| 12824 | ggml_set_input(rpb_coords); |
| 12825 | |
| 12826 | auto* text_valid_mask = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, L, 1, 1); |