/** * Mixture of Experts (MoE) feed-forward implementation. * * This module handles: * - Token routing to experts via softmax + top-k * - Expert weight loading (on-demand) * - Parallel expert execution on GPU * - Scatter-add combination of expert outputs * * Supports multiple MoE architectures: * - Mixtral-style (gate/up/down per expert) * - GPT-OSS style (MXFP4 quantized fused gate_up + bias) * - Gemma-style packed F16 gate_up/down experts * * @module inference/pipelines/text/moe-impl */ import type { Tensor, TensorDtype } from '../../../gpu/tensor.js'; import type { MoERouter } from '../../moe-router.js'; import type { ExpertWeights } from './types.js'; import type { KernelPathSchema } from '../../../config/schema/index.js'; import type { ExecutionV1PoliciesSchema } from "../../../config/schema/execution/execution-v1.schema.js"; import type { WeightBuffer } from '../../../gpu/weight-buffer.js'; /** * Configuration for MoE feed-forward. */ export interface MoEConfig { activity?: import('../../../debug/activity-trace.js').ActivityTrace | null; signal?: AbortSignal; phase?: 'prefill' | 'decode'; groupedPrefill?: boolean; sharedSlot?: import('./moe/qwen-route-executor.js').QwenSharedSlot | null; /** * Fused decode post-attention norm: the MoE writes the normalized row into its input * buffer, and the router logits and shared-expert gate, in one dispatch. */ normRouter?: (import('./types.js').PendingFfnNorm & { variant: 'f32' | 'f32_single_pass'; sharedRouterWeight: GPUBuffer | import('../../../gpu/weight-buffer.js').WeightBuffer; }) | null; /** The layer's declared moe_topk variant; null lets the softmax rules select one. */ topKVariant?: string | null; activeExpertSelection?: import('../../../config/schema/moe.schema.js').ActiveExpertSelection; modelType?: string | null; hiddenSize: number; intermediateSize: number; rmsNormEps?: number; expertIntermediateSize?: number; numExperts: number; moeTopK: number; expertFormat: 'mixtral' | 'gpt-oss' | 'gemma4' | 'qwen3_5'; hiddenActivation: string; swigluLimit: number | null; activationDtype?: TensorDtype; routerInputBuffer?: GPUBuffer | null; routerInputDtype?: TensorDtype | null; kernelPath?: KernelPathSchema | null; executionPolicies?: ExecutionV1PoliciesSchema | null; } /** * Expert weights with optional GPT-OSS quantized format. */ export interface MoEExpertWeights extends ExpertWeights { expertFormat: 'mixtral' | 'gpt-oss' | 'gemma4' | 'qwen3_5'; numExperts?: number; expertIntermediateSize?: number; gateUp?: GPUBuffer; gateUpBlocks?: GPUBuffer; gateUpScales?: GPUBuffer; gateUpBias?: GPUBuffer; downBlocks?: GPUBuffer; downScales?: GPUBuffer; downBias?: GPUBuffer; } /** * Layer router weights (for models with per-layer routers like GPT-OSS). */ export interface LayerRouterWeights { weight: Float32Array | GPUBuffer | WeightBuffer; bias: Float32Array | GPUBuffer | null; scale?: Float32Array | GPUBuffer | WeightBuffer | null; perExpertScale?: Float32Array | GPUBuffer | WeightBuffer | null; } /** * Expert weight loader interface. */ export interface ExpertLoader { prepareExpertLayer?(layerIdx: number, consumerExperts: Map, recorder: import('../../../gpu/command-recorder.js').CommandRecorder | null, signal?: AbortSignal): Promise; loadExpert(layerIdx: number, expertIdx: number): Promise; /** False once the loader's LRU cache evicted (and released) `weights`. */ isExpertResident(layerIdx: number, expertIdx: number, weights: unknown): boolean; } /** * MoE feed-forward fully on GPU. */ export function moeFeedForwardGPU( inputBuffer: GPUBuffer, numTokens: number, config: MoEConfig, moeRouter: MoERouter, expertWeights: Map, expertLoader: ExpertLoader, layerIdx: number, layerRouterWeights?: Map, recorder?: import('../../../gpu/command-recorder.js').CommandRecorder | null ): Promise;