/** * GGUF Model Loader for WebGPU * Loads GGUF model weights to GPU tensors with dequantization */ import { type FileHandle } from 'fs/promises'; import { Tensor } from '../tensor.js'; import type { GGUFFile, GGUFTensorInfo, GGMLType } from '../../../types/model.js'; /** * Options for loading tensors */ export interface TensorLoadOptions { /** Whether to dequantize quantized tensors to f32 */ dequantize?: boolean; /** Maximum number of elements to load (for memory limits) */ maxElements?: number; /** Labels to use for GPU tensors */ label?: string; } /** * Information about a loaded tensor */ export interface LoadedTensor { name: string; tensor: Tensor; originalType: GGMLType; shape: number[]; } /** * Calculate the total bytes for a tensor */ export declare function calculateTensorBytes(info: GGUFTensorInfo): number; /** * Calculate total number of elements in a tensor */ export declare function calculateTensorElements(info: GGUFTensorInfo): number; /** * Load a single tensor from a GGUF file * * GGUF stores tensors in column-major order (like Fortran). * For a 2D tensor with GGUF dimensions [ne0, ne1], the element at logical * position (i, j) is at memory index: i + j*ne0 * * This means in row-major terms (like C/JavaScript), the data is laid out * as a [ne1, ne0] matrix. We handle this by: * 1. Reshaping the flat data to [ne1, ne0] (which correctly interprets the column-major data) * 2. Returning both the tensor and a flag indicating if transpose is needed for matmul */ export declare function loadTensor(handle: FileHandle, tensorInfo: GGUFTensorInfo, tensorDataOffset: bigint, options?: TensorLoadOptions): Promise; /** * Load multiple tensors from a GGUF file */ export declare function loadTensors(filePath: string, ggufFile: GGUFFile, tensorNames: string[], options?: TensorLoadOptions): Promise>; /** * Load all tensors matching a pattern */ export declare function loadTensorsByPattern(filePath: string, ggufFile: GGUFFile, pattern: RegExp, options?: TensorLoadOptions): Promise>; /** * Estimate total GPU memory needed for model tensors */ export declare function estimateModelMemory(ggufFile: GGUFFile, dequantize?: boolean): number; /** * Get tensor info by name */ export declare function getTensorInfo(ggufFile: GGUFFile, name: string): GGUFTensorInfo | undefined; /** * List all tensor names in a GGUF file */ export declare function listTensorNames(ggufFile: GGUFFile): string[]; /** * Group tensors by layer * Returns a map of layer index to tensor names */ export declare function groupTensorsByLayer(ggufFile: GGUFFile): Map; /** * Model weights organizer for Llama-style models */ export interface LlamaWeights { tokenEmbedding: Tensor | null; layers: LlamaLayerWeights[]; outputNorm: Tensor | null; outputWeight: Tensor | null; } export interface LlamaLayerWeights { attnNorm: Tensor | null; attnQ: Tensor | null; attnQBias: Tensor | null; attnK: Tensor | null; attnKBias: Tensor | null; attnV: Tensor | null; attnVBias: Tensor | null; attnOutput: Tensor | null; ffnNorm: Tensor | null; ffnGate: Tensor | null; ffnUp: Tensor | null; ffnDown: Tensor | null; } /** * Load Llama-style model weights */ export declare function loadLlamaWeights(filePath: string, ggufFile: GGUFFile, options?: TensorLoadOptions): Promise; /** * Dispose of all tensors in LlamaWeights */ export declare function disposeLlamaWeights(weights: LlamaWeights): void; //# sourceMappingURL=loader.d.ts.map