Repository: ggerganov/llama.cpp
File: src/models/modern-bert.cpp (C++)

Code before:
    const int64_t n_embd_head     = hparams.n_embd_head_v;
    const int64_t n_embd_gqa      = hparams.n_embd_v_gqa();
    const float rope_theta_global = hparams.rope_freq_base_train;
    const float rope_theta_local  = hparams.rope_freq_base_train_swa;
    const uint32_t swa_period  = hparams.swa_period;

    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);

    ggml_tensor * cur = nullptr;
    ggml_tensor * inpL = nullptr;
    ggml_tensor * inp_pos = build_inp_pos();

    // construct input embeddings (token, type, position)
    inpL = build_inp_embd(model.tok_embd);
    cb(inpL, "inp_embd", -1);

    // embed layer norm
    inpL = build_norm(inpL, model.tok_norm, nullptr, LLM_NORM, -1);
    cb(inpL, "inp_norm", -1);

    ggml_tensor * inp_out_ids = build_inp_out_ids();

    auto * inp_attn = build_attn_inp_no_cache();

    for (int il = 0; il < n_layer; ++il) {
        ggml_tensor * cur = inpL;

        ggml_tensor * Qcur = nullptr;
        ggml_tensor * Kcur = nullptr;
        ggml_tensor * Vcur = nullptr;

        const float rope_theta = (il % swa_period == 0) ? rope_theta_global : rope_theta_local;

        // attention layer norm
        if (model.layers[il].attn_norm) {
            cur = build_norm(inpL,
                    model.layers[il].attn_norm, NULL,
                    LLM_NORM, il);
            cb(cur, "attn_norm", il);
        }

        // self attention
        cur = build_lora_mm(model.layers[il].wqkv, cur);
        cb(cur, "wqkv", il);

        const size_t type_size = ggml_type_size(cur->type);

        Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head,    n_tokens, n_embd_head*type_size, cur->nb[1], 0*type_size*(n_embd));
        Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*type_size, cur->nb[1], 1*type_size*(n_embd));
        Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*type_size, cur->nb[1], 1*type_size*(n_embd + n_embd_gqa));


Review comment (suggestion):
```suggestion
        float freq_base_l  = 0.0f;

        if constexpr (iswa) {
            freq_base_l = model.get_rope_freq_base(cparams, il);
        } else {
            freq_base_l = freq_base;
        }

        ggml_tensor * cur = inpL;
```

Code after:
    const int64_t n_embd_head = hparams.n_embd_head_v;
    const int64_t n_embd_gqa  = hparams.n_embd_v_gqa();

    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);

    ggml_tensor * cur;
    ggml_tensor * inpL;
    ggml_tensor * inp_pos = build_inp_pos();

    // construct input embeddings (token, type, position)
    inpL = build_inp_embd(model.tok_embd);
    cb(inpL, "inp_embd", -1);

    // embed layer norm
    inpL = build_norm(inpL, model.tok_norm, nullptr, LLM_NORM, -1);
    cb(inpL, "inp_norm", -1);

    ggml_tensor * inp_out_ids = build_inp_out_ids();

    auto * inp_attn = build_attn_inp_no_cache();

    for (int il = 0; il < n_layer; ++il) {
        float freq_base_l  = 0.0f;

        if constexpr (iswa) {
            freq_base_l = model.get_rope_freq_base(cparams, il);
        } else {
            freq_base_l = freq_base;
        }

        cur = inpL;

        // attention layer norm
        if (model.layers[il].attn_norm) {
            cur = build_norm(inpL,
                    model.layers[il].attn_norm, NULL,
                    LLM_NORM, il);
            cb(cur, "attn_norm", il);
        }

        // self attention
        cur = build_lora_mm(model.layers[il].wqkv, cur);
        cb(cur, "wqkv", il);

        const size_t type_size = ggml_type_size(cur->type);

        ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head,    n_tokens, n_embd_head*type_size, cur->nb[1], 0*type_size*(n_embd));
        ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*type_size, cur->nb[1], 1*type_size*(n_embd));
        ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*type_size, cur->nb[1], 1*type_size*(n_embd + n_embd_gqa));

        // RoPE