Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
llama-adapter.h75 linesDownload Raw Back to src
1#pragma once2 3#include "llama.h"4 5#include "ggml-cpp.h"6 7#include <string>8#include <unordered_map>9#include <vector>10 11// TODO: pimpl12 13//14// llama_adapter_cvec15//16 17struct llama_adapter_cvec {18    struct ggml_tensor * tensor_for(int il) const;19 20    struct ggml_tensor * apply_to(struct ggml_context * ctx, struct ggml_tensor * cur, int  il) const;21 22    int32_t apply(23            const llama_model & model,24            const float * data,25            size_t len,26            int32_t n_embd,27            int32_t il_start,28            int32_t il_end);29 30private:31    bool init(const llama_model & model);32 33    int32_t layer_start = -1;34    int32_t layer_end   = -1;35 36    std::vector<ggml_context_ptr> ctxs;37    std::vector<ggml_backend_buffer_ptr> bufs;38 39    std::vector<struct ggml_tensor *> tensors; // per layer40};41 42//43// llama_adapter_lora44//45 46struct llama_adapter_lora_weight {47    struct ggml_tensor * a = nullptr;48    struct ggml_tensor * b = nullptr;49 50    // get actual scale based on rank and alpha51    float get_scale(float alpha, float adapter_scale) const {52        const float rank  = (float) b->ne[0];53        const float scale = alpha ? adapter_scale * alpha / rank : adapter_scale;54        return scale;55    }56 57    llama_adapter_lora_weight() = default;58    llama_adapter_lora_weight(struct ggml_tensor * a, struct ggml_tensor * b) : a(a), b(b) {}59};60 61struct llama_adapter_lora {62    // map tensor name to lora_a_b63    std::unordered_map<std::string, struct llama_adapter_lora_weight> ab_map;64 65    std::vector<ggml_context_ptr> ctxs;66    std::vector<ggml_backend_buffer_ptr> bufs;67 68    float alpha;69 70    llama_adapter_lora() = default;71    ~llama_adapter_lora() = default;72 73    llama_adapter_lora_weight * get_weight(struct ggml_tensor * w);74};75