encode a prompt - points - boxes - masks TODO: currently just encode a single point for simplicity
| 1447 | // TODO: currently just encode a single point for simplicity |
| 1448 | // |
| 1449 | prompt_encoder_result sam_encode_prompt( |
| 1450 | const sam_model & model, |
| 1451 | struct ggml_context * ctx0, |
| 1452 | struct ggml_cgraph * gf, |
| 1453 | sam_state & state, |
| 1454 | const sam_prompt & prompt) { |
| 1455 | |
| 1456 | const auto & hparams = model.hparams; |
| 1457 | const auto & enc = model.enc_prompt; |
| 1458 | |
| 1459 | struct ggml_tensor * inp = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, 2, 2); |
| 1460 | ggml_set_name(inp, "prompt_input"); |
| 1461 | ggml_set_input(inp); |
| 1462 | |
| 1463 | auto * embd_prompt_sparse = [&]() -> struct ggml_tensor * { |
| 1464 | switch (prompt.prompt_type) { |
| 1465 | case SAM_PROMPT_TYPE_POINT: { |
| 1466 | // PromptEncoder._embed_points |
| 1467 | auto * pt_embd = sam_prompt_encode_pe_encoding(enc, ctx0, gf, inp); |
| 1468 | |
| 1469 | // overwrite label == -1 with not_a_point_embed.weight |
| 1470 | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/prompt_encoder.py#L86 |
| 1471 | // TODO: extend for multiple points |
| 1472 | auto * pt_embd_not = ggml_view_2d(ctx0, pt_embd, pt_embd->ne[0], 1, pt_embd->nb[1], pt_embd->nb[1]); |
| 1473 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, enc.not_a_pt_embd_w, pt_embd_not)); |
| 1474 | |
| 1475 | // add point_embeddings[1] to label == 1 |
| 1476 | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/prompt_encoder.py#L90 |
| 1477 | auto * pt_embd1 = ggml_view_2d(ctx0, pt_embd, pt_embd->ne[0], 1, pt_embd->nb[1], 0); |
| 1478 | ggml_build_forward_expand(gf, ggml_add_inplace(ctx0, pt_embd1, enc.pt_embd[1])); |
| 1479 | |
| 1480 | return pt_embd; |
| 1481 | } break; |
| 1482 | case SAM_PROMPT_TYPE_BOX: { |
| 1483 | // PromptEncoder._embed_boxes |
| 1484 | auto * corner_embd = sam_prompt_encode_pe_encoding(enc, ctx0, gf, inp); |
| 1485 | |
| 1486 | // corner_embd[:, 0, :] += self.point_embeddings[2].weight |
| 1487 | // corner_embd[:, 1, :] += self.point_embeddings[3].weight |
| 1488 | auto * corner0 = ggml_view_2d( |
| 1489 | ctx0, corner_embd, corner_embd->ne[0], 1, corner_embd->nb[1], 0); |
| 1490 | auto * corner1 = ggml_view_2d( |
| 1491 | ctx0, corner_embd, corner_embd->ne[0], 1, corner_embd->nb[1], corner_embd->nb[1]); |
| 1492 | |
| 1493 | ggml_build_forward_expand(gf, ggml_add_inplace(ctx0, corner0, enc.pt_embd[2])); |
| 1494 | ggml_build_forward_expand(gf, ggml_add_inplace(ctx0, corner1, enc.pt_embd[3])); |
| 1495 | |
| 1496 | return corner_embd; |
| 1497 | } break; |
| 1498 | default: { |
| 1499 | fprintf(stderr, "%s: unsupported prompt type %d\n", __func__, prompt.prompt_type); |
| 1500 | return nullptr; |
| 1501 | } break; |
| 1502 | } |
| 1503 | }(); |
| 1504 | |
| 1505 | ggml_build_forward_expand(gf, embd_prompt_sparse); |
| 1506 |
no test coverage detected