| 6239 | } |
| 6240 | |
| 6241 | bool sam3_encode_vit_from_preprocessed_selective(sam3_state & state, |
| 6242 | const sam3_model & model, |
| 6243 | const float * chw_data, |
| 6244 | int img_size, |
| 6245 | const std::vector<std::string> & output_tensors) { |
| 6246 | const auto & hp = model.hparams; |
| 6247 | |
| 6248 | if (img_size != hp.img_size) { |
| 6249 | fprintf(stderr, "%s: img_size mismatch: got %d, expected %d\n", |
| 6250 | __func__, img_size, hp.img_size); |
| 6251 | return false; |
| 6252 | } |
| 6253 | |
| 6254 | state.orig_width = img_size; |
| 6255 | state.orig_height = img_size; |
| 6256 | |
| 6257 | const size_t buf_size = ggml_tensor_overhead() * 4096 + ggml_graph_overhead() * 2; |
| 6258 | struct ggml_init_params gparams = { |
| 6259 | /*.mem_size =*/ buf_size, |
| 6260 | /*.mem_buffer =*/ nullptr, |
| 6261 | /*.no_alloc =*/ true, |
| 6262 | }; |
| 6263 | struct ggml_context * ctx0 = ggml_init(gparams); |
| 6264 | if (!ctx0) { |
| 6265 | fprintf(stderr, "%s: failed to init compute context\n", __func__); |
| 6266 | return false; |
| 6267 | } |
| 6268 | |
| 6269 | auto * inp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, img_size, img_size, 3, 1); |
| 6270 | ggml_set_name(inp, "input_image"); |
| 6271 | ggml_set_input(inp); |
| 6272 | |
| 6273 | auto * vit_out = sam3_build_vit_graph(ctx0, inp, model); |
| 6274 | ggml_set_name(vit_out, "vit_output"); |
| 6275 | ggml_set_output(vit_out); |
| 6276 | |
| 6277 | if (!sam3_mark_named_outputs(ctx0, output_tensors)) { |
| 6278 | ggml_free(ctx0); |
| 6279 | return false; |
| 6280 | } |
| 6281 | |
| 6282 | struct ggml_cgraph * graph = ggml_new_graph_custom(ctx0, 8192, false); |
| 6283 | ggml_build_forward_expand(graph, vit_out); |
| 6284 | |
| 6285 | auto * galloc = ggml_gallocr_new(ggml_backend_get_default_buffer_type(model.backend)); |
| 6286 | if (!ggml_gallocr_reserve(galloc, graph)) { |
| 6287 | fprintf(stderr, "%s: failed to reserve graph memory\n", __func__); |
| 6288 | ggml_gallocr_free(galloc); |
| 6289 | ggml_free(ctx0); |
| 6290 | return false; |
| 6291 | } |
| 6292 | |
| 6293 | if (!ggml_gallocr_alloc_graph(galloc, graph)) { |
| 6294 | fprintf(stderr, "%s: failed to allocate graph\n", __func__); |
| 6295 | ggml_gallocr_free(galloc); |
| 6296 | ggml_free(ctx0); |
| 6297 | return false; |
| 6298 | } |