/** * WebGPU Engine * GPU-accelerated inference engine implementing the Engine interface */ import { EventEmitter } from 'events'; import type { Engine } from './index.js'; export interface WebGPUEngineOptions { numThreads?: number; debug?: boolean; } export interface WebGPULoadModelOptions { contextLength?: number; batchSize?: number; } export interface WebGPUGenerateOptions { maxTokens?: number; temperature?: number; topP?: number; topK?: number; repeatPenalty?: number; stop?: string[]; rawPrompt?: boolean; } /** * WebGPU-accelerated inference engine */ export declare class WebGPUEngine extends EventEmitter implements Engine { private gpuDevice; private initialized; private modelLoaded; private ggufFile; private architecture; private weights; private tokenizer; private vocabSize; private hiddenSize; private numLayers; private numHeads; private numKVHeads; private headDim; private intermediateSize; private contextLength; private rmsNormEps; private ropeBase; private chatTemplate; private debugMode; private kvCache; constructor(options?: WebGPUEngineOptions); /** * Initialize the WebGPU device */ init(): Promise; /** * Load a model from a GGUF file */ loadModel(modelPath: string, options?: WebGPULoadModelOptions): Promise; /** * Setup model configuration from GGUF metadata */ private setupModelConfig; /** * Load model weights to GPU tensors */ private loadWeights; /** * Load placeholder weights for testing */ private loadPlaceholderWeights; /** * Create a new empty KV cache */ private createKVCache; /** * Clear the KV cache (call when starting a new generation) */ private clearKVCache; /** * Compute debug statistics for an array */ private debugStats; /** * Forward pass through the model * @param inputIds - Token IDs to process * @param useCache - If true, use KV cache for incremental generation (only process last token) */ private forward; /** * Self-attention with Q, K, V projections * Supports Grouped Query Attention (GQA) where numKVHeads < numHeads * Supports KV caching for incremental generation * * @param x - Input tensor [processLen, hiddenSize] * @param layer - Layer weights * @param processLen - Number of tokens being processed (1 for incremental, full for prefill) * @param startPos - Starting position in sequence (0 for prefill, cached length for incremental) * @param layerIdx - Layer index for KV cache access * @param useCache - Whether to use/update KV cache */ private selfAttention; /** * Sample next token from logits using efficient top-k selection * Uses a min-heap for O(vocab) top-k selection instead of O(vocab × k) */ private sampleToken; /** * Generate text completion */ generate(prompt: string, options?: WebGPUGenerateOptions): Promise<{ text: string; tokensGenerated: number; done: boolean; }>; /** * Apply chat template to format the user prompt using Jinja2/nunjucks * Templates expect a 'messages' array with {role, content} objects */ private applyChatTemplate; /** * Generate text with streaming */ generateStream(prompt: string, options?: WebGPUGenerateOptions): AsyncGenerator; /** * Add bias to a 2D tensor (broadcast add) - GPU accelerated * input: [seqLen, dim], bias: [dim] -> output: [seqLen, dim] */ private addBias; /** * Get embeddings for text */ embed(_text: string): Promise; /** * Check if model is loaded */ isLoaded(): boolean; /** * Unload the model */ unload(): Promise; /** * Shutdown the engine */ shutdown(): Promise; /** * Get GPU capabilities */ getCapabilities(): import("./webgpu/device.js").GPUCapabilities | null; } /** * Create a WebGPU engine instance */ export declare function createWebGPUEngine(options?: WebGPUEngineOptions): WebGPUEngine; //# sourceMappingURL=webgpu-engine.d.ts.map