MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / build_vit

Method build_vit

subprojects/llama.cpp/tools/mtmd/clip.cpp:287–448  ·  view source on GitHub ↗

build vision transformer (ViT) cgraph this function should cover most of the models if your model has specific features, you should probably duplicate this function

Source from the content-addressed store, hash-verified

285// this function should cover most of the models
286// if your model has specific features, you should probably duplicate this function
287ggml_tensor * clip_graph::build_vit(
288 ggml_tensor * inp,
289 int64_t n_pos,
290 norm_type norm_t,
291 ffn_op_type ffn_t,
292 ggml_tensor * learned_pos_embd,
293 std::function<ggml_tensor *(ggml_tensor *, const clip_layer &)> add_pos
294 ) {
295 if (learned_pos_embd) {
296 inp = ggml_add(ctx0, inp, learned_pos_embd);
297 cb(inp, "pos_embed", -1);
298 }
299
300 ggml_tensor * inpL = inp;
301
302 // pre-layernorm
303 if (model.pre_ln_w) {
304 inpL = build_norm(inpL, model.pre_ln_w, model.pre_ln_b, norm_t, eps, -1);
305 cb(inpL, "pre_ln", -1);
306 }
307
308 // loop over layers
309 for (int il = 0; il < n_layer; il++) {
310 auto & layer = model.layers[il];
311 ggml_tensor * cur = inpL; // inpL = residual, cur = hidden_states
312
313 // layernorm1
314 cur = build_norm(cur, layer.ln_1_w, layer.ln_1_b, norm_t, eps, il);
315 cb(cur, "layer_inp_normed", il);
316
317 // self-attention
318 {
319 ggml_tensor * Qcur = nullptr;
320 ggml_tensor * Kcur = nullptr;
321 ggml_tensor * Vcur = nullptr;
322 if (layer.qkv_w != nullptr) {
323 // fused qkv
324 cur = ggml_mul_mat(ctx0, layer.qkv_w, cur);
325 if (layer.qkv_b != nullptr) {
326 cur = ggml_add(ctx0, cur, layer.qkv_b);
327 }
328
329 Qcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
330 /* nb1 */ ggml_row_size(cur->type, d_head),
331 /* nb2 */ cur->nb[1],
332 /* offset */ 0);
333
334 Kcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
335 /* nb1 */ ggml_row_size(cur->type, d_head),
336 /* nb2 */ cur->nb[1],
337 /* offset */ ggml_row_size(cur->type, n_embd));
338
339 Vcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
340 /* nb1 */ ggml_row_size(cur->type, d_head),
341 /* nb2 */ cur->nb[1],
342 /* offset */ ggml_row_size(cur->type, 2 * n_embd));
343
344 // TODO: q/k norm requires row size == n_embd, while here it's d_head

Callers

nothing calls this directly

Calls 10

ggml_addFunction · 0.85
ggml_mul_matFunction · 0.85
ggml_view_3dFunction · 0.85
ggml_row_sizeFunction · 0.85
ggml_reshape_3dFunction · 0.85
ggml_mulFunction · 0.85
ggml_transposeFunction · 0.85
ggml_contFunction · 0.85
ggml_pool_1dFunction · 0.85
audio_has_avgpoolMethod · 0.80

Tested by

no test coverage detected