| 1602 | } |
| 1603 | |
| 1604 | bool sam_decode_mask( |
| 1605 | const sam_model & model, |
| 1606 | const prompt_encoder_result & prompt, |
| 1607 | struct ggml_tensor * pe_img, |
| 1608 | struct ggml_context * ctx0, |
| 1609 | struct ggml_cgraph * gf, |
| 1610 | sam_state & state, |
| 1611 | const bool multimask_output) { |
| 1612 | |
| 1613 | const auto & hparams = model.hparams; |
| 1614 | const auto & dec = model.dec; |
| 1615 | const int n_img_embd = hparams.n_img_embd(); |
| 1616 | |
| 1617 | struct ggml_tensor * tokens = {}; |
| 1618 | { |
| 1619 | // Concatenate output tokens |
| 1620 | // ref: https://github.com/facebookresearch/segment-anything/blob/6fdee8f2727f4506cfbbe553e23b895e27956588/segment_anything/modeling/mask_decoder.py#L120 |
| 1621 | const auto& sparse = prompt.embd_prompt_sparse; |
| 1622 | |
| 1623 | tokens = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, dec.iou_token_w->ne[0], dec.iou_token_w->ne[1] + dec.mask_tokens_w->ne[1] + sparse->ne[1], sparse->ne[2]); |
| 1624 | |
| 1625 | const size_t offsets[3] = { 0, dec.iou_token_w->ne[1]*tokens->nb[1], dec.iou_token_w->ne[1]*tokens->nb[1] + dec.mask_tokens_w->ne[1]*tokens->nb[1] }; |
| 1626 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, dec.iou_token_w, ggml_view_2d(ctx0, tokens, tokens->ne[0], dec.iou_token_w->ne[1], tokens->nb[1], offsets[0]))); |
| 1627 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, dec.mask_tokens_w, ggml_view_2d(ctx0, tokens, tokens->ne[0], dec.mask_tokens_w->ne[1], tokens->nb[1], offsets[1]))); |
| 1628 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, sparse, ggml_view_2d(ctx0, tokens, tokens->ne[0], sparse->ne[1], tokens->nb[1], offsets[2]))); |
| 1629 | // TODO: Sparse prompt embeddings can have more than one point |
| 1630 | } |
| 1631 | |
| 1632 | |
| 1633 | struct ggml_tensor * src = {}; |
| 1634 | struct ggml_tensor * pos_src = {}; |
| 1635 | int srcNE[4] = { 0, 0, 0, 0 }; |
| 1636 | { |
| 1637 | // Expand per-image data in the batch direction to be per-mask |
| 1638 | // ref: https://github.com/facebookresearch/segment-anything/blob/6fdee8f2727f4506cfbbe553e23b895e27956588/segment_anything/modeling/mask_decoder.py#L125 |
| 1639 | src = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, state.embd_img->ne[0], state.embd_img->ne[1], state.embd_img->ne[2], tokens->ne[2]); |
| 1640 | |
| 1641 | src = ggml_add(ctx0, |
| 1642 | ggml_repeat(ctx0, |
| 1643 | state.embd_img, |
| 1644 | src), |
| 1645 | prompt.embd_prompt_dense); |
| 1646 | |
| 1647 | srcNE[0] = src->ne[0]; |
| 1648 | srcNE[1] = src->ne[1]; |
| 1649 | srcNE[2] = src->ne[2]; |
| 1650 | srcNE[3] = src->ne[3]; |
| 1651 | |
| 1652 | // flatten & permute |
| 1653 | // ref: https://github.com/facebookresearch/segment-anything/blob/6fdee8f2727f4506cfbbe553e23b895e27956588/segment_anything/modeling/transformer.py#L83 |
| 1654 | src = ggml_cont(ctx0, ggml_permute(ctx0, |
| 1655 | ggml_view_3d(ctx0, |
| 1656 | src, |
| 1657 | src->ne[0]*src->ne[1], |
| 1658 | src->ne[2], |
| 1659 | src->ne[3], |
| 1660 | src->nb[2], |
| 1661 | src->nb[3], |
no test coverage detected