MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / sam_encode_image

Function sam_encode_image

external/ggml/examples/sam/sam.cpp:1190–1409  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1188}
1189
1190struct ggml_cgraph * sam_encode_image(
1191 const sam_model & model,
1192 sam_state & state,
1193 const sam_image_f32 & img) {
1194
1195 const auto & hparams = model.hparams;
1196 const auto & enc = model.enc_img;
1197
1198 const int32_t n_enc_state = hparams.n_enc_state;
1199 const int32_t n_enc_layer = hparams.n_enc_layer;
1200 const int32_t n_enc_head = hparams.n_enc_head;
1201 const int32_t n_enc_head_dim = hparams.n_enc_head_dim();
1202 const int32_t n_enc_out_chans = hparams.n_enc_out_chans;
1203 const int32_t n_img_size = hparams.n_img_size();
1204 const int32_t n_window_size = hparams.n_window_size();
1205
1206 struct ggml_init_params ggml_params = {
1207 /*.mem_size =*/ state.buf_compute_img_enc.size(),
1208 /*.mem_buffer =*/ state.buf_compute_img_enc.data(),
1209 /*.no_alloc =*/ true, // skip allocating as we use ggml_alloc to allocate exact memory requirements
1210 };
1211
1212 struct ggml_context * ctx0 = ggml_init(ggml_params);
1213 struct ggml_cgraph * gf = ggml_new_graph(ctx0);
1214
1215 struct ggml_tensor * inp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_img_size, n_img_size, 3, 1);
1216 ggml_set_name(inp, "inp");
1217 ggml_set_input(inp);
1218
1219 // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L392
1220 struct ggml_tensor * cur = ggml_conv_2d_sk_p0(ctx0, enc.proj_w, inp);
1221 cur = ggml_add_inplace(ctx0,
1222 cur,
1223 ggml_repeat(ctx0, enc.proj_b, cur));
1224
1225 // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L394
1226 // keep in F32
1227 cur = ggml_cont(ctx0,
1228 ggml_permute(ctx0, cur, 1, 2, 0, 3));
1229
1230 // convert to F16
1231 //cur = ggml_cpy(ctx0,
1232 // ggml_permute(ctx0, cur, 1, 2, 0, 3),
1233 // ggml_new_tensor_3d(ctx0, GGML_TYPE_F16, n_enc_state, n_img_embd, n_img_embd));
1234
1235 // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L108-L109
1236 cur = ggml_add_inplace(ctx0, cur, enc.pe);
1237
1238 struct ggml_tensor * inpL = cur;
1239
1240 for (int il = 0; il < n_enc_layer; ++il) {
1241 const auto & layer = enc.layers[il];
1242
1243 // norm
1244 // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L168
1245 {
1246 cur = ggml_norm(ctx0, inpL, hparams.eps);
1247

Callers 1

mainFunction · 0.85

Calls 15

ggml_initFunction · 0.85
ggml_new_graphFunction · 0.85
ggml_new_tensor_4dFunction · 0.85
ggml_set_nameFunction · 0.85
ggml_set_inputFunction · 0.85
ggml_conv_2d_sk_p0Function · 0.85
ggml_add_inplaceFunction · 0.85
ggml_repeatFunction · 0.85
ggml_contFunction · 0.85
ggml_permuteFunction · 0.85
ggml_normFunction · 0.85
ggml_mulFunction · 0.85

Tested by

no test coverage detected