|
| 1 | +import {Llama} from "../bindings/Llama.js"; |
| 2 | +import MissingNodeLlamaError from "./errors/MissingNodeLlamaError.js"; |
| 3 | +import {GGUFMetadataResponse} from "./ggufParser/GGUFParser.js"; |
| 4 | +import NotEnoughVRamError from "./errors/ModelScore/NotEnoughVRamError.js"; |
| 5 | + |
| 6 | +const PAD_AVAILABLE_VRAM = 1024 ** 2 * 500; // 500MB |
| 7 | + |
| 8 | +export type GGUFInsightsOptions = { |
| 9 | + contextCount?: number, |
| 10 | + nodeLlama?: Llama, |
| 11 | + modelSize?: number |
| 12 | +}; |
| 13 | + |
| 14 | +export default class GGUFInsights { |
| 15 | + public readonly metadataResponse: GGUFMetadataResponse; |
| 16 | + public readonly options: GGUFInsightsOptions = {}; |
| 17 | + |
| 18 | + public get metadata() { |
| 19 | + return this.metadataResponse.metadata; |
| 20 | + } |
| 21 | + |
| 22 | + public get architectureMetadata() { |
| 23 | + return this.metadata[this.metadata.general.architecture]; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * fp16 k,v matrices |
| 28 | + */ |
| 29 | + public get kvMatrices(){ |
| 30 | + // 2 bytes each * 2 key and value |
| 31 | + return ( |
| 32 | + 2 * 2 * |
| 33 | + this.architectureMetadata.context_length * |
| 34 | + this.architectureMetadata.block_count * |
| 35 | + this.architectureMetadata.embedding_length * |
| 36 | + this.architectureMetadata.attention.head_count_kv / |
| 37 | + this.architectureMetadata.attention.head_count |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * This amount is the overhead + tensors in memory |
| 43 | + */ |
| 44 | + public get graphSize() { |
| 45 | + // TODO: get this from the llama.cpp's graph calculations instead of |
| 46 | + // estimating it's 1/6 * kv_cache_size * num_gqa |
| 47 | + return ( |
| 48 | + (this.architectureMetadata.attention.head_count_kv / |
| 49 | + this.architectureMetadata.attention.head_count) * this.kvMatrices / 6 |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public get VRAMUsage(){ |
| 54 | + return this.graphSize + this.kvMatrices + this.metadataResponse.metadataSize; |
| 55 | + } |
| 56 | + |
| 57 | + protected get _availableVRam(){ |
| 58 | + if (!this.options?.nodeLlama){ |
| 59 | + throw new MissingNodeLlamaError("GGUFInsights Calculations"); |
| 60 | + } |
| 61 | + return this.options.nodeLlama.getVramState().total - PAD_AVAILABLE_VRAM; |
| 62 | + } |
| 63 | + |
| 64 | + public constructor(metadataResponse: GGUFMetadataResponse, options: GGUFInsightsOptions = {}) { |
| 65 | + this.options = options; |
| 66 | + this.metadataResponse = metadataResponse; |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + /** |
| 72 | + * The score of the model by how much it's compatible to the current system |
| 73 | + */ |
| 74 | + public modelScore(){ |
| 75 | + const vramScore = this.VRAMUsage / this._availableVRam; |
| 76 | + if (vramScore >= 1){ |
| 77 | + throw new NotEnoughVRamError(this.VRAMUsage, this._availableVRam); |
| 78 | + } |
| 79 | + |
| 80 | + return vramScore; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments