@danielsimonjr/mathts-matrix 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +390 -714
  2. package/dist/index.js +1740 -2359
  3. package/package.json +7 -6
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
+ /// <reference types="@webgpu/types" />
1
2
  import { ComputePool, ComputePoolConfig } from '@danielsimonjr/mathts-parallel';
3
+ import { GPUContextOptions, GPUCapabilities, GPUContext, BufferPool, ShaderManager } from '@danielsimonjr/mathts-gpu';
4
+ export { BufferPool, GPUCapabilities, GPUContext, GPUContextOptions, ShaderManager, destroyGlobalGPU, detectGPUCapabilities, getGlobalGPUContext, getRecommendedWorkgroupSize, hasWebGPU } from '@danielsimonjr/mathts-gpu';
2
5
  import * as typed_function from 'typed-function';
3
6
 
4
7
  /**
@@ -1276,118 +1279,51 @@ declare const wasmBackend: WASMBackend;
1276
1279
  declare function createWASMBackend(config?: WASMBackendConfig): WASMBackend;
1277
1280
 
1278
1281
  /**
1279
- * WebGPU Detection and Capability Checking
1280
- *
1281
- * Provides runtime detection of WebGPU support and adapter capabilities.
1282
- */
1283
- /**
1284
- * WebGPU adapter information
1285
- */
1286
- interface GPUAdapterInfo {
1287
- /** Adapter vendor */
1288
- vendor: string;
1289
- /** Adapter architecture */
1290
- architecture: string;
1291
- /** Device description */
1292
- device: string;
1293
- /** Driver description */
1294
- description: string;
1295
- }
1296
- /**
1297
- * WebGPU capability information
1298
- */
1299
- interface GPUCapabilities {
1300
- /** Whether WebGPU is supported */
1301
- supported: boolean;
1302
- /** Adapter information if available */
1303
- adapterInfo: GPUAdapterInfo | null;
1304
- /** Maximum buffer size in bytes */
1305
- maxBufferSize: number;
1306
- /** Maximum compute workgroup size */
1307
- maxWorkgroupSize: [number, number, number];
1308
- /** Maximum storage buffer binding size */
1309
- maxStorageBufferBindingSize: number;
1310
- /** Maximum compute invocations per workgroup */
1311
- maxComputeInvocationsPerWorkgroup: number;
1312
- /** Maximum workgroups per dimension */
1313
- maxComputeWorkgroupsPerDimension: number;
1314
- /** Whether the adapter is a fallback/software adapter */
1315
- isFallbackAdapter: boolean;
1316
- /** Supported features */
1317
- features: string[];
1318
- }
1319
- /**
1320
- * Check if WebGPU is available in the current environment
1321
- */
1322
- declare function hasWebGPU(): boolean;
1323
- /**
1324
- * Detect WebGPU capabilities
1325
- * @param preferHighPerformance - Whether to prefer high-performance GPU
1326
- */
1327
- declare function detectGPUCapabilities(preferHighPerformance?: boolean): Promise<GPUCapabilities>;
1328
- /**
1329
- * Recommended workgroup size based on GPU capabilities
1330
- */
1331
- declare function getRecommendedWorkgroupSize(capabilities: GPUCapabilities): [number, number, number];
1332
-
1333
- /**
1334
- * WebGPU Context Management
1282
+ * GPU Backend for Matrix Operations
1335
1283
  *
1336
- * Manages WebGPU device, queue, and command encoding.
1284
+ * WebGPU-accelerated matrix operations for large matrices.
1337
1285
  */
1338
1286
 
1339
1287
  /**
1340
- * Options for GPUContext initialization
1341
- */
1342
- interface GPUContextOptions {
1343
- /** Prefer high-performance GPU */
1344
- preferHighPerformance?: boolean;
1345
- /** Required features for the device */
1346
- requiredFeatures?: GPUFeatureName[];
1347
- /** Required limits for the device */
1348
- requiredLimits?: Record<string, number>;
1349
- /** Label for debugging */
1350
- label?: string;
1351
- }
1352
- /**
1353
- * Status of the GPU context
1288
+ * GPU Backend status
1354
1289
  */
1355
- type GPUContextStatus = 'uninitialized' | 'initializing' | 'ready' | 'error' | 'lost';
1290
+ type GPUBackendStatus = 'uninitialized' | 'initializing' | 'ready' | 'error' | 'unsupported';
1356
1291
  /**
1357
- * Event emitted when device is lost
1292
+ * Options for GPU backend
1358
1293
  */
1359
- interface DeviceLostEvent {
1360
- reason: GPUDeviceLostReason;
1361
- message: string;
1294
+ interface GPUBackendOptions extends GPUContextOptions {
1295
+ /** Use global GPU context instead of creating a new one */
1296
+ useGlobalContext?: boolean;
1297
+ /** Buffer pool options */
1298
+ bufferPoolOptions?: {
1299
+ maxCacheSize?: number;
1300
+ evictionTimeout?: number;
1301
+ };
1302
+ /** Threshold for using GPU (matrix size) */
1303
+ threshold?: number;
1362
1304
  }
1363
1305
  /**
1364
- * GPU Context manages the lifecycle of WebGPU resources
1306
+ * GPU Backend for accelerated matrix operations
1365
1307
  */
1366
- declare class GPUContext {
1367
- private adapter;
1368
- private device;
1308
+ declare class GPUBackend {
1309
+ private context;
1310
+ private bufferPool;
1311
+ private shaderManager;
1369
1312
  private _status;
1370
1313
  private _capabilities;
1371
1314
  private _lastError;
1372
- private deviceLostCallbacks;
1373
- private label;
1374
- constructor(options?: GPUContextOptions);
1315
+ private threshold;
1316
+ private workgroupSize;
1317
+ private useGlobalContext;
1318
+ constructor(options?: GPUBackendOptions);
1375
1319
  /**
1376
1320
  * Get the current status
1377
1321
  */
1378
- get status(): GPUContextStatus;
1322
+ get status(): GPUBackendStatus;
1379
1323
  /**
1380
- * Check if context is ready
1324
+ * Check if backend is ready
1381
1325
  */
1382
1326
  get isReady(): boolean;
1383
- /**
1384
- * Get the GPU device (throws if not initialized)
1385
- */
1386
- getDevice(): GPUDevice;
1387
- /**
1388
- * Get the GPU queue
1389
- */
1390
- getQueue(): GPUQueue;
1391
1327
  /**
1392
1328
  * Get capabilities
1393
1329
  */
@@ -1397,259 +1333,450 @@ declare class GPUContext {
1397
1333
  */
1398
1334
  get lastError(): Error | null;
1399
1335
  /**
1400
- * Initialize the GPU context
1401
- */
1402
- initialize(options?: GPUContextOptions): Promise<boolean>;
1403
- /**
1404
- * Register callback for device lost event
1405
- */
1406
- onDeviceLost(callback: (event: DeviceLostEvent) => void): void;
1407
- /**
1408
- * Create a command encoder
1409
- */
1410
- createCommandEncoder(label?: string): GPUCommandEncoder;
1411
- /**
1412
- * Create a buffer
1336
+ * Initialize the GPU backend
1413
1337
  */
1414
- createBuffer(size: number, usage: GPUBufferUsageFlags, label?: string, mappedAtCreation?: boolean): GPUBuffer;
1338
+ initialize(options?: GPUBackendOptions): Promise<boolean>;
1415
1339
  /**
1416
- * Create a storage buffer for compute operations
1340
+ * Check if GPU should be used for the given matrix size
1417
1341
  */
1418
- createStorageBuffer(size: number, label?: string, readable?: boolean, writable?: boolean): GPUBuffer;
1342
+ shouldUseGPU(rows: number, cols: number): boolean;
1419
1343
  /**
1420
- * Create a staging buffer for reading back data
1344
+ * Calculate workgroup counts for a matrix
1421
1345
  */
1422
- createStagingBuffer(size: number, label?: string): GPUBuffer;
1346
+ calculateWorkgroups(rows: number, cols: number): [number, number, number];
1423
1347
  /**
1424
- * Create a compute pipeline
1348
+ * Get the GPU context
1425
1349
  */
1426
- createComputePipeline(shaderModule: GPUShaderModule, entryPoint: string, layout?: GPUPipelineLayout | 'auto', label?: string): GPUComputePipeline;
1350
+ getContext(): GPUContext;
1427
1351
  /**
1428
- * Create a shader module from WGSL source
1352
+ * Get the buffer pool
1429
1353
  */
1430
- createShaderModule(code: string, label?: string): GPUShaderModule;
1354
+ getBufferPool(): BufferPool;
1431
1355
  /**
1432
- * Create a bind group
1356
+ * Get the shader manager
1433
1357
  */
1434
- createBindGroup(layout: GPUBindGroupLayout, entries: GPUBindGroupEntry[], label?: string): GPUBindGroup;
1358
+ getShaderManager(): ShaderManager;
1435
1359
  /**
1436
- * Submit commands to the GPU queue
1360
+ * Add two matrices element-wise
1437
1361
  */
1438
- submitCommands(commandBuffers: GPUCommandBuffer[]): void;
1362
+ add(a: Float32Array, b: Float32Array, rows: number, cols: number): Promise<Float32Array>;
1439
1363
  /**
1440
- * Write data to a buffer
1364
+ * Multiply two matrices
1441
1365
  */
1442
- writeBuffer(buffer: GPUBuffer, data: ArrayBufferView | ArrayBuffer | SharedArrayBuffer, bufferOffset?: number, dataOffset?: number, size?: number): void;
1366
+ matmul(a: Float32Array, b: Float32Array, M: number, K: number, N: number): Promise<Float32Array>;
1443
1367
  /**
1444
- * Read data from a buffer (async)
1368
+ * Transpose a matrix
1445
1369
  */
1446
- readBuffer(buffer: GPUBuffer, offset?: number, size?: number): Promise<ArrayBuffer>;
1370
+ transpose(a: Float32Array, rows: number, cols: number): Promise<Float32Array>;
1447
1371
  /**
1448
- * Dispatch a compute shader
1372
+ * Scale a matrix by a scalar
1449
1373
  */
1450
- dispatchCompute(pipeline: GPUComputePipeline, bindGroups: GPUBindGroup[], workgroupCounts: [number, number, number]): void;
1374
+ scale(a: Float32Array, scalar: number): Promise<Float32Array>;
1451
1375
  /**
1452
- * Wait for all GPU operations to complete
1376
+ * Get backend statistics
1453
1377
  */
1454
- waitForCompletion(): Promise<void>;
1378
+ getStats(): {
1379
+ status: GPUBackendStatus;
1380
+ capabilities: GPUCapabilities | null;
1381
+ bufferPool: {
1382
+ totalBuffers: number;
1383
+ inUseBuffers: number;
1384
+ cachedBuffers: number;
1385
+ } | null;
1386
+ shaders: {
1387
+ cachedShaders: number;
1388
+ cachedPipelines: number;
1389
+ } | null;
1390
+ };
1455
1391
  /**
1456
- * Destroy the context and release resources
1392
+ * Destroy the backend
1457
1393
  */
1458
1394
  destroy(): void;
1459
1395
  }
1460
1396
  /**
1461
- * Get the global GPU context
1397
+ * Get the global GPU backend
1398
+ */
1399
+ declare function getGlobalGPUBackend(): GPUBackend;
1400
+ /**
1401
+ * Initialize the global GPU backend
1462
1402
  */
1463
- declare function getGlobalGPUContext(): GPUContext;
1403
+ declare function initializeGlobalGPUBackend(options?: GPUBackendOptions): Promise<boolean>;
1464
1404
  /**
1465
- * Destroy the global GPU context
1405
+ * Destroy the global GPU backend
1466
1406
  */
1467
- declare function destroyGlobalGPU(): void;
1407
+ declare function destroyGlobalGPUBackend(): void;
1468
1408
 
1469
1409
  /**
1470
- * GPU Buffer Pool
1410
+ * GPU Matrix Backend Adapter
1411
+ *
1412
+ * Adapts GPUBackend to implement the MatrixBackend interface,
1413
+ * enabling seamless integration with the backend selection system.
1471
1414
  *
1472
- * Manages GPU buffer allocation, deallocation, and reuse.
1473
- * Reduces allocation overhead by recycling buffers.
1415
+ * @packageDocumentation
1474
1416
  */
1475
1417
 
1476
1418
  /**
1477
- * Options for buffer pool
1478
- */
1479
- interface BufferPoolOptions {
1480
- /** Maximum total memory to cache (bytes) */
1481
- maxCacheSize?: number;
1482
- /** Time after which unused buffers are evicted (ms) */
1483
- evictionTimeout?: number;
1484
- /** Whether to enable automatic eviction */
1485
- autoEvict?: boolean;
1486
- /** Interval for automatic eviction (ms) */
1487
- evictionInterval?: number;
1419
+ * Configuration for GPU Matrix Backend
1420
+ */
1421
+ interface GPUMatrixBackendConfig {
1422
+ /** Minimum elements to use GPU (default: 65536 = 256x256) */
1423
+ minElements?: number;
1424
+ /** Use global GPU backend instance */
1425
+ useGlobalBackend?: boolean;
1426
+ /** GPU backend options */
1427
+ gpuOptions?: GPUBackendOptions;
1428
+ /** Fall back to JS on GPU errors */
1429
+ fallbackOnError?: boolean;
1488
1430
  }
1489
1431
  /**
1490
- * GPU Buffer Pool for efficient buffer management
1432
+ * GPU Matrix Backend
1433
+ *
1434
+ * Implements MatrixBackend interface using WebGPU compute shaders.
1435
+ * Provides significant acceleration for large matrices.
1436
+ *
1437
+ * @example
1438
+ * ```typescript
1439
+ * const gpu = new GPUMatrixBackend();
1440
+ * await gpu.initialize();
1441
+ *
1442
+ * const result = gpu.multiply(matrixA, matrixB);
1443
+ * ```
1491
1444
  */
1492
- declare class BufferPool {
1493
- private context;
1494
- private buffers;
1495
- private maxCacheSize;
1496
- private evictionTimeout;
1497
- private evictionTimer;
1498
- private currentCacheSize;
1499
- constructor(context: GPUContext, options?: BufferPoolOptions);
1500
- /**
1501
- * Generate a key for buffer categorization
1502
- */
1503
- private getBufferKey;
1445
+ declare class GPUMatrixBackend implements MatrixBackend {
1446
+ readonly type: BackendType;
1447
+ private config;
1448
+ private backend;
1449
+ private capabilities;
1450
+ private initPromise;
1451
+ private _available;
1452
+ constructor(config?: GPUMatrixBackendConfig);
1504
1453
  /**
1505
- * Round up to nearest power of 2
1454
+ * Check if GPU is available in the current environment
1506
1455
  */
1507
- private roundUpToPowerOf2;
1456
+ isAvailable(): boolean;
1508
1457
  /**
1509
- * Acquire a buffer from the pool or create a new one
1458
+ * Initialize the GPU backend
1510
1459
  */
1511
- acquire(size: number, usage: GPUBufferUsageFlags, label?: string): GPUBuffer;
1460
+ initialize(): Promise<void>;
1461
+ private doInitialize;
1512
1462
  /**
1513
- * Release a buffer back to the pool
1463
+ * Check if operation should use GPU
1514
1464
  */
1515
- release(buffer: GPUBuffer): void;
1465
+ private shouldUseGPU;
1516
1466
  /**
1517
- * Create a storage buffer from the pool
1467
+ * Execute GPU operation with fallback
1518
1468
  */
1519
- acquireStorageBuffer(size: number, label?: string, readable?: boolean, writable?: boolean): GPUBuffer;
1469
+ private executeWithFallback;
1520
1470
  /**
1521
- * Create a staging buffer from the pool
1471
+ * Get GPU capabilities
1522
1472
  */
1523
- acquireStagingBuffer(size: number, label?: string): GPUBuffer;
1473
+ getCapabilities(): GPUCapabilities | null;
1524
1474
  /**
1525
- * Create a uniform buffer from the pool
1475
+ * Get backend statistics
1526
1476
  */
1527
- acquireUniformBuffer(size: number, label?: string): GPUBuffer;
1477
+ getStats(): ReturnType<GPUBackend['getStats']> | null;
1478
+ add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1528
1479
  /**
1529
- * Evict old unused buffers
1480
+ * Async add operation using GPU
1530
1481
  */
1531
- evictOldBuffers(): void;
1482
+ addAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
1483
+ subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1484
+ multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1485
+ divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1486
+ scale(a: DenseMatrix, scalar: number): DenseMatrix;
1532
1487
  /**
1533
- * Force eviction to reduce cache to target size
1488
+ * Async scale operation using GPU
1534
1489
  */
1535
- evictToSize(targetSize: number): void;
1490
+ scaleAsync(a: DenseMatrix, scalar: number): Promise<DenseMatrix>;
1491
+ abs(a: DenseMatrix): DenseMatrix;
1492
+ negate(a: DenseMatrix): DenseMatrix;
1493
+ multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1536
1494
  /**
1537
- * Start automatic eviction timer
1495
+ * Async matrix multiplication using GPU
1538
1496
  */
1539
- startAutoEviction(interval: number): void;
1497
+ multiplyAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
1498
+ transpose(a: DenseMatrix): DenseMatrix;
1540
1499
  /**
1541
- * Stop automatic eviction timer
1500
+ * Async transpose using GPU
1542
1501
  */
1543
- stopAutoEviction(): void;
1502
+ transposeAsync(a: DenseMatrix): Promise<DenseMatrix>;
1503
+ sum(a: DenseMatrix): number;
1504
+ sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
1505
+ norm(a: DenseMatrix): number;
1506
+ dot(a: DenseMatrix, b: DenseMatrix): number;
1544
1507
  /**
1545
- * Get pool statistics
1508
+ * Update configuration
1546
1509
  */
1547
- getStats(): {
1548
- totalBuffers: number;
1549
- inUseBuffers: number;
1550
- cachedBuffers: number;
1551
- currentCacheSize: number;
1552
- maxCacheSize: number;
1553
- };
1510
+ updateConfig(config: Partial<GPUMatrixBackendConfig>): void;
1554
1511
  /**
1555
- * Clear all buffers and reset pool
1512
+ * Get current configuration
1556
1513
  */
1557
- clear(): void;
1514
+ getConfig(): Required<GPUMatrixBackendConfig>;
1558
1515
  /**
1559
- * Destroy the pool
1516
+ * Destroy the backend
1560
1517
  */
1561
1518
  destroy(): void;
1562
1519
  }
1563
-
1564
1520
  /**
1565
- * GPU Shader Manager
1566
- *
1567
- * Manages WGSL shader loading, compilation, and caching.
1521
+ * Global GPU matrix backend instance
1522
+ */
1523
+ declare const gpuMatrixBackend: GPUMatrixBackend;
1524
+ /**
1525
+ * Create a GPU matrix backend with custom configuration
1568
1526
  */
1527
+ declare function createGPUMatrixBackend(config?: GPUMatrixBackendConfig): GPUMatrixBackend;
1569
1528
 
1570
1529
  /**
1571
- * Built-in shader library
1530
+ * MathTS Matrix Configuration
1531
+ *
1532
+ * Centralized configuration for matrix operations, backend selection,
1533
+ * and performance tuning.
1534
+ *
1535
+ * @packageDocumentation
1572
1536
  */
1573
- declare const BUILTIN_SHADERS: {
1574
- /** Matrix addition shader */
1575
- matrixAdd: string;
1576
- /** Matrix subtraction shader */
1577
- matrixSub: string;
1578
- /** Element-wise multiplication shader */
1579
- matrixMul: string;
1580
- /** Scalar multiplication shader */
1581
- scalarMul: string;
1582
- /** Matrix multiplication (naive) shader */
1583
- matmul: string;
1584
- /** Matrix transpose shader */
1585
- transpose: string;
1586
- /** Sum reduction shader (first pass) */
1587
- sumReduce: string;
1588
- };
1537
+
1589
1538
  /**
1590
- * Shader Manager for compiling and caching GPU shaders
1539
+ * Operation type hints for backend selection.
1540
+ *
1541
+ * Defined here rather than in `BackendManager.ts` so that `config.ts` (the
1542
+ * lower-level module) owns it — `BackendManager` already imports `config`, so
1543
+ * sourcing the type the other way round closed an import cycle.
1591
1544
  */
1592
- declare class ShaderManager {
1593
- private context;
1594
- private cache;
1595
- constructor(context: GPUContext);
1596
- /**
1597
- * Get or compile a shader module
1598
- */
1599
- getShaderModule(name: string, code: string): GPUShaderModule;
1600
- /**
1601
- * Get a builtin shader module
1602
- */
1603
- getBuiltinShader(name: keyof typeof BUILTIN_SHADERS): GPUShaderModule;
1604
- /**
1605
- * Get or create a compute pipeline
1606
- */
1607
- getPipeline(shaderName: string, entryPoint: string, code?: string, layout?: GPUPipelineLayout | 'auto'): GPUComputePipeline;
1608
- /**
1609
- * Get a builtin compute pipeline
1610
- */
1611
- getBuiltinPipeline(name: keyof typeof BUILTIN_SHADERS, entryPoint?: string): GPUComputePipeline;
1612
- /**
1613
- * Precompile all builtin shaders
1614
- */
1615
- precompileBuiltins(): void;
1616
- /**
1617
- * Clear shader cache
1618
- */
1619
- clearCache(): void;
1620
- /**
1621
- * Get cache statistics
1622
- */
1623
- getStats(): {
1624
- cachedShaders: number;
1625
- cachedPipelines: number;
1626
- };
1627
- }
1545
+ type OperationType = 'add' | 'subtract' | 'multiply' | 'multiplyElementwise' | 'transpose' | 'scale' | 'decomposition' | 'solve' | 'fft' | 'eig' | 'svd';
1628
1546
 
1629
1547
  /**
1630
- * GPU Batch Executor
1548
+ * Backend Manager
1631
1549
  *
1632
- * Manages batched GPU command submission for reduced overhead.
1633
- * Queues multiple operations and executes them together for efficiency.
1550
+ * Centralized management for matrix operation backends with automatic
1551
+ * selection based on matrix size, operation type, and availability.
1552
+ * Includes adaptive threshold tuning based on runtime profiling.
1634
1553
  *
1635
1554
  * @packageDocumentation
1636
1555
  */
1637
1556
 
1638
1557
  /**
1639
- * Result of batch execution
1558
+ * Extended backend hints with operation-specific thresholds
1640
1559
  */
1641
- interface BatchResult {
1642
- /** Success status */
1643
- success: boolean;
1644
- /** Number of operations executed */
1645
- operationCount: number;
1646
- /** Execution time in milliseconds */
1647
- duration: number;
1648
- /** Error message if failed */
1649
- error?: string;
1560
+ interface ExtendedBackendHints extends BackendHints {
1561
+ /** Specific thresholds by operation type */
1562
+ operationThresholds?: Partial<Record<OperationType, {
1563
+ wasm?: number;
1564
+ gpu?: number;
1565
+ }>>;
1566
+ /** Enable automatic SIMD detection for WASM */
1567
+ autoSIMD?: boolean;
1568
+ /** Fallback to JS on backend failure */
1569
+ fallbackOnError?: boolean;
1650
1570
  }
1651
1571
  /**
1652
- * Options for batch execution
1572
+ * Default extended hints
1573
+ */
1574
+ declare const DEFAULT_EXTENDED_HINTS: Required<ExtendedBackendHints>;
1575
+ /**
1576
+ * Centralized Backend Manager
1577
+ *
1578
+ * Provides a unified interface for executing matrix operations with
1579
+ * automatic backend selection based on matrix size and operation type.
1580
+ * Features adaptive threshold tuning based on runtime profiling.
1581
+ */
1582
+ declare class BackendManager {
1583
+ private hints;
1584
+ private initialized;
1585
+ private initializationPromise;
1586
+ private adaptiveState;
1587
+ private configUnsubscribe;
1588
+ constructor(hints?: ExtendedBackendHints);
1589
+ /**
1590
+ * Sync manager state with global config
1591
+ */
1592
+ private syncWithConfig;
1593
+ /**
1594
+ * Initialize all available backends
1595
+ */
1596
+ initialize(): Promise<void>;
1597
+ private doInitialize;
1598
+ /**
1599
+ * Update backend hints
1600
+ */
1601
+ setHints(hints: ExtendedBackendHints): void;
1602
+ /**
1603
+ * Get current hints
1604
+ */
1605
+ getHints(): Required<ExtendedBackendHints>;
1606
+ /**
1607
+ * Get the best backend for a given operation and matrix size.
1608
+ *
1609
+ * Selection priority:
1610
+ * 1. Preferred backend (if explicitly set)
1611
+ * 2. Elements > gpuThreshold -> GPU (if available)
1612
+ * 3. Elements > wasmThreshold -> AS WASM (if loaded)
1613
+ * 4. JS fallback
1614
+ */
1615
+ selectBackend(elementCount: number, operation?: OperationType): MatrixBackend;
1616
+ /**
1617
+ * Execute an operation with automatic backend selection
1618
+ */
1619
+ private executeWithFallback;
1620
+ /**
1621
+ * Matrix addition with auto backend selection
1622
+ */
1623
+ add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1624
+ /**
1625
+ * Matrix subtraction with auto backend selection
1626
+ */
1627
+ subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1628
+ /**
1629
+ * Element-wise multiplication with auto backend selection
1630
+ */
1631
+ multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1632
+ /**
1633
+ * Element-wise division with auto backend selection
1634
+ */
1635
+ divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1636
+ /**
1637
+ * Scalar multiplication with auto backend selection
1638
+ */
1639
+ scale(a: DenseMatrix, scalar: number): DenseMatrix;
1640
+ /**
1641
+ * Element-wise absolute value with auto backend selection
1642
+ */
1643
+ abs(a: DenseMatrix): DenseMatrix;
1644
+ /**
1645
+ * Element-wise negation with auto backend selection
1646
+ */
1647
+ negate(a: DenseMatrix): DenseMatrix;
1648
+ /**
1649
+ * Matrix multiplication with auto backend selection
1650
+ */
1651
+ multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
1652
+ /**
1653
+ * Matrix transpose with auto backend selection
1654
+ */
1655
+ transpose(a: DenseMatrix): DenseMatrix;
1656
+ /**
1657
+ * Sum of all elements with auto backend selection
1658
+ */
1659
+ sum(a: DenseMatrix): Promise<number>;
1660
+ /**
1661
+ * Sum along axis with auto backend selection
1662
+ */
1663
+ sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
1664
+ /**
1665
+ * Frobenius norm with auto backend selection
1666
+ */
1667
+ norm(a: DenseMatrix): number;
1668
+ /**
1669
+ * Dot product with auto backend selection
1670
+ */
1671
+ dot(a: DenseMatrix, b: DenseMatrix): Promise<number>;
1672
+ /**
1673
+ * Get list of available backends
1674
+ */
1675
+ getAvailableBackends(): BackendType[];
1676
+ /**
1677
+ * Check if a specific backend is available
1678
+ */
1679
+ hasBackend(type: BackendType): boolean;
1680
+ /**
1681
+ * Get current active backend for a given operation size
1682
+ */
1683
+ getActiveBackend(elementCount: number, operation?: OperationType): BackendType;
1684
+ /**
1685
+ * Force a specific backend for all operations
1686
+ */
1687
+ forceBackend(type: BackendType | null): void;
1688
+ /**
1689
+ * Record a performance sample for adaptive tuning
1690
+ */
1691
+ recordSample(operation: OperationType, elementCount: number, backend: BackendType, durationMs: number): void;
1692
+ /**
1693
+ * Adjust thresholds based on collected samples
1694
+ */
1695
+ private maybeAdjustThresholds;
1696
+ /**
1697
+ * Get current adaptive thresholds
1698
+ */
1699
+ getAdaptiveThresholds(): Map<OperationType, {
1700
+ wasm: number;
1701
+ gpu: number;
1702
+ }>;
1703
+ /**
1704
+ * Reset adaptive tuning state
1705
+ */
1706
+ resetAdaptiveState(): void;
1707
+ /**
1708
+ * Get performance statistics
1709
+ */
1710
+ getPerformanceStats(): {
1711
+ sampleCount: number;
1712
+ operationStats: Map<OperationType, {
1713
+ avgDuration: number;
1714
+ samples: number;
1715
+ backendUsage: Record<BackendType, number>;
1716
+ }>;
1717
+ };
1718
+ /**
1719
+ * Cleanup resources
1720
+ */
1721
+ destroy(): void;
1722
+ }
1723
+ /**
1724
+ * Default backend manager instance
1725
+ */
1726
+ declare const backendManager: BackendManager;
1727
+ /**
1728
+ * Create a new backend manager with custom hints
1729
+ */
1730
+ declare function createBackendManager(hints?: ExtendedBackendHints): BackendManager;
1731
+
1732
+ /**
1733
+ * Matrix-domain WGSL kernels.
1734
+ *
1735
+ * These live in matrix — the @danielsimonjr/mathts-gpu foundation ships no
1736
+ * domain kernels. GPUBackend registers them onto a ShaderManager at init.
1737
+ */
1738
+
1739
+ declare const BUILTIN_SHADERS: {
1740
+ /** Matrix addition shader */
1741
+ readonly matrixAdd: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>; // rows, cols, _, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] + b[idx];\n }\n ";
1742
+ /** Matrix subtraction shader */
1743
+ readonly matrixSub: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>;\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] - b[idx];\n }\n ";
1744
+ /** Element-wise multiplication shader */
1745
+ readonly matrixMul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>;\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] * b[idx];\n }\n ";
1746
+ /** Scalar multiplication shader */
1747
+ readonly scalarMul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read_write> result: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<f32>; // scalar, length, _, _\n\n @compute @workgroup_size(256)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let scalar = params.x;\n let length = u32(params.y);\n let idx = gid.x;\n\n if (idx >= length) { return; }\n\n result[idx] = a[idx] * scalar;\n }\n ";
1748
+ /** Matrix multiplication (naive) shader */
1749
+ readonly matmul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>; // M, N, K, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let M = params.x;\n let N = params.y;\n let K = params.z;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= M || col >= N) { return; }\n\n var sum: f32 = 0.0;\n for (var k: u32 = 0u; k < K; k = k + 1u) {\n sum = sum + a[row * K + k] * b[k * N + col];\n }\n\n result[row * N + col] = sum;\n }\n ";
1750
+ /** Matrix transpose shader */
1751
+ readonly transpose: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read_write> result: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<u32>; // rows, cols, _, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n result[col * rows + row] = a[row * cols + col];\n }\n ";
1752
+ /** Sum reduction shader (first pass) */
1753
+ readonly sumReduce: "\n @group(0) @binding(0) var<storage, read> input: array<f32>;\n @group(0) @binding(1) var<storage, read_write> output: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<u32>; // inputLength, outputLength, _, _\n\n // NOTE: 'shared' is a RESERVED KEYWORD in WGSL — naming this workgroup\n // array 'shared' made this shader fail to compile, which (because\n // GPUBackend.initialize() precompiles every registered shader) poisoned\n // backend init and silently forced ALL GPU ops onto the CPU fallback.\n var<workgroup> sdata: array<f32, 256>;\n\n @compute @workgroup_size(256)\n fn main(\n @builtin(local_invocation_id) lid: vec3<u32>,\n @builtin(workgroup_id) wid: vec3<u32>\n ) {\n let inputLength = params.x;\n let idx = wid.x * 512u + lid.x;\n\n // Load two elements and sum\n var sum: f32 = 0.0;\n if (idx < inputLength) {\n sum = input[idx];\n }\n if (idx + 256u < inputLength) {\n sum = sum + input[idx + 256u];\n }\n sdata[lid.x] = sum;\n\n workgroupBarrier();\n\n // Reduce within workgroup\n for (var s: u32 = 128u; s > 0u; s = s >> 1u) {\n if (lid.x < s) {\n sdata[lid.x] = sdata[lid.x] + sdata[lid.x + s];\n }\n workgroupBarrier();\n }\n\n // Write result\n if (lid.x == 0u) {\n output[wid.x] = sdata[0];\n }\n }\n ";
1754
+ };
1755
+
1756
+ /**
1757
+ * GPU Batch Executor
1758
+ *
1759
+ * Manages batched GPU command submission for reduced overhead.
1760
+ * Queues multiple operations and executes them together for efficiency.
1761
+ *
1762
+ * @packageDocumentation
1763
+ */
1764
+
1765
+ /**
1766
+ * Result of batch execution
1767
+ */
1768
+ interface BatchResult {
1769
+ /** Success status */
1770
+ success: boolean;
1771
+ /** Number of operations executed */
1772
+ operationCount: number;
1773
+ /** Execution time in milliseconds */
1774
+ duration: number;
1775
+ /** Error message if failed */
1776
+ error?: string;
1777
+ }
1778
+ /**
1779
+ * Options for batch execution
1653
1780
  */
1654
1781
  interface BatchOptions {
1655
1782
  /** Maximum operations per batch before auto-flush */
@@ -1934,457 +2061,6 @@ declare class SyncManager {
1934
2061
  */
1935
2062
  declare function createSyncManager(context: GPUContext, bufferPool: BufferPool, strategy?: SyncStrategy): SyncManager;
1936
2063
 
1937
- /**
1938
- * GPU Backend for Matrix Operations
1939
- *
1940
- * WebGPU-accelerated matrix operations for large matrices.
1941
- */
1942
-
1943
- /**
1944
- * GPU Backend status
1945
- */
1946
- type GPUBackendStatus = 'uninitialized' | 'initializing' | 'ready' | 'error' | 'unsupported';
1947
- /**
1948
- * Options for GPU backend
1949
- */
1950
- interface GPUBackendOptions extends GPUContextOptions {
1951
- /** Use global GPU context instead of creating a new one */
1952
- useGlobalContext?: boolean;
1953
- /** Buffer pool options */
1954
- bufferPoolOptions?: {
1955
- maxCacheSize?: number;
1956
- evictionTimeout?: number;
1957
- };
1958
- /** Threshold for using GPU (matrix size) */
1959
- threshold?: number;
1960
- }
1961
- /**
1962
- * GPU Backend for accelerated matrix operations
1963
- */
1964
- declare class GPUBackend {
1965
- private context;
1966
- private bufferPool;
1967
- private shaderManager;
1968
- private _status;
1969
- private _capabilities;
1970
- private _lastError;
1971
- private threshold;
1972
- private workgroupSize;
1973
- private useGlobalContext;
1974
- constructor(options?: GPUBackendOptions);
1975
- /**
1976
- * Get the current status
1977
- */
1978
- get status(): GPUBackendStatus;
1979
- /**
1980
- * Check if backend is ready
1981
- */
1982
- get isReady(): boolean;
1983
- /**
1984
- * Get capabilities
1985
- */
1986
- get capabilities(): GPUCapabilities | null;
1987
- /**
1988
- * Get last error
1989
- */
1990
- get lastError(): Error | null;
1991
- /**
1992
- * Initialize the GPU backend
1993
- */
1994
- initialize(options?: GPUBackendOptions): Promise<boolean>;
1995
- /**
1996
- * Check if GPU should be used for the given matrix size
1997
- */
1998
- shouldUseGPU(rows: number, cols: number): boolean;
1999
- /**
2000
- * Calculate workgroup counts for a matrix
2001
- */
2002
- calculateWorkgroups(rows: number, cols: number): [number, number, number];
2003
- /**
2004
- * Get the GPU context
2005
- */
2006
- getContext(): GPUContext;
2007
- /**
2008
- * Get the buffer pool
2009
- */
2010
- getBufferPool(): BufferPool;
2011
- /**
2012
- * Get the shader manager
2013
- */
2014
- getShaderManager(): ShaderManager;
2015
- /**
2016
- * Add two matrices element-wise
2017
- */
2018
- add(a: Float32Array, b: Float32Array, rows: number, cols: number): Promise<Float32Array>;
2019
- /**
2020
- * Multiply two matrices
2021
- */
2022
- matmul(a: Float32Array, b: Float32Array, M: number, K: number, N: number): Promise<Float32Array>;
2023
- /**
2024
- * Transpose a matrix
2025
- */
2026
- transpose(a: Float32Array, rows: number, cols: number): Promise<Float32Array>;
2027
- /**
2028
- * Scale a matrix by a scalar
2029
- */
2030
- scale(a: Float32Array, scalar: number): Promise<Float32Array>;
2031
- /**
2032
- * Get backend statistics
2033
- */
2034
- getStats(): {
2035
- status: GPUBackendStatus;
2036
- capabilities: GPUCapabilities | null;
2037
- bufferPool: {
2038
- totalBuffers: number;
2039
- inUseBuffers: number;
2040
- cachedBuffers: number;
2041
- } | null;
2042
- shaders: {
2043
- cachedShaders: number;
2044
- cachedPipelines: number;
2045
- } | null;
2046
- };
2047
- /**
2048
- * Destroy the backend
2049
- */
2050
- destroy(): void;
2051
- }
2052
- /**
2053
- * Get the global GPU backend
2054
- */
2055
- declare function getGlobalGPUBackend(): GPUBackend;
2056
- /**
2057
- * Initialize the global GPU backend
2058
- */
2059
- declare function initializeGlobalGPUBackend(options?: GPUBackendOptions): Promise<boolean>;
2060
- /**
2061
- * Destroy the global GPU backend
2062
- */
2063
- declare function destroyGlobalGPUBackend(): void;
2064
-
2065
- /**
2066
- * GPU Matrix Backend Adapter
2067
- *
2068
- * Adapts GPUBackend to implement the MatrixBackend interface,
2069
- * enabling seamless integration with the backend selection system.
2070
- *
2071
- * @packageDocumentation
2072
- */
2073
-
2074
- /**
2075
- * Configuration for GPU Matrix Backend
2076
- */
2077
- interface GPUMatrixBackendConfig {
2078
- /** Minimum elements to use GPU (default: 65536 = 256x256) */
2079
- minElements?: number;
2080
- /** Use global GPU backend instance */
2081
- useGlobalBackend?: boolean;
2082
- /** GPU backend options */
2083
- gpuOptions?: GPUBackendOptions;
2084
- /** Fall back to JS on GPU errors */
2085
- fallbackOnError?: boolean;
2086
- }
2087
- /**
2088
- * GPU Matrix Backend
2089
- *
2090
- * Implements MatrixBackend interface using WebGPU compute shaders.
2091
- * Provides significant acceleration for large matrices.
2092
- *
2093
- * @example
2094
- * ```typescript
2095
- * const gpu = new GPUMatrixBackend();
2096
- * await gpu.initialize();
2097
- *
2098
- * const result = gpu.multiply(matrixA, matrixB);
2099
- * ```
2100
- */
2101
- declare class GPUMatrixBackend implements MatrixBackend {
2102
- readonly type: BackendType;
2103
- private config;
2104
- private backend;
2105
- private capabilities;
2106
- private initPromise;
2107
- private _available;
2108
- constructor(config?: GPUMatrixBackendConfig);
2109
- /**
2110
- * Check if GPU is available in the current environment
2111
- */
2112
- isAvailable(): boolean;
2113
- /**
2114
- * Initialize the GPU backend
2115
- */
2116
- initialize(): Promise<void>;
2117
- private doInitialize;
2118
- /**
2119
- * Check if operation should use GPU
2120
- */
2121
- private shouldUseGPU;
2122
- /**
2123
- * Execute GPU operation with fallback
2124
- */
2125
- private executeWithFallback;
2126
- /**
2127
- * Get GPU capabilities
2128
- */
2129
- getCapabilities(): GPUCapabilities | null;
2130
- /**
2131
- * Get backend statistics
2132
- */
2133
- getStats(): ReturnType<GPUBackend['getStats']> | null;
2134
- add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2135
- /**
2136
- * Async add operation using GPU
2137
- */
2138
- addAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
2139
- subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2140
- multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2141
- divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2142
- scale(a: DenseMatrix, scalar: number): DenseMatrix;
2143
- /**
2144
- * Async scale operation using GPU
2145
- */
2146
- scaleAsync(a: DenseMatrix, scalar: number): Promise<DenseMatrix>;
2147
- abs(a: DenseMatrix): DenseMatrix;
2148
- negate(a: DenseMatrix): DenseMatrix;
2149
- multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2150
- /**
2151
- * Async matrix multiplication using GPU
2152
- */
2153
- multiplyAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
2154
- transpose(a: DenseMatrix): DenseMatrix;
2155
- /**
2156
- * Async transpose using GPU
2157
- */
2158
- transposeAsync(a: DenseMatrix): Promise<DenseMatrix>;
2159
- sum(a: DenseMatrix): number;
2160
- sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
2161
- norm(a: DenseMatrix): number;
2162
- dot(a: DenseMatrix, b: DenseMatrix): number;
2163
- /**
2164
- * Update configuration
2165
- */
2166
- updateConfig(config: Partial<GPUMatrixBackendConfig>): void;
2167
- /**
2168
- * Get current configuration
2169
- */
2170
- getConfig(): Required<GPUMatrixBackendConfig>;
2171
- /**
2172
- * Destroy the backend
2173
- */
2174
- destroy(): void;
2175
- }
2176
- /**
2177
- * Global GPU matrix backend instance
2178
- */
2179
- declare const gpuMatrixBackend: GPUMatrixBackend;
2180
- /**
2181
- * Create a GPU matrix backend with custom configuration
2182
- */
2183
- declare function createGPUMatrixBackend(config?: GPUMatrixBackendConfig): GPUMatrixBackend;
2184
-
2185
- /**
2186
- * MathTS Matrix Configuration
2187
- *
2188
- * Centralized configuration for matrix operations, backend selection,
2189
- * and performance tuning.
2190
- *
2191
- * @packageDocumentation
2192
- */
2193
-
2194
- /**
2195
- * Operation type hints for backend selection.
2196
- *
2197
- * Defined here rather than in `BackendManager.ts` so that `config.ts` (the
2198
- * lower-level module) owns it — `BackendManager` already imports `config`, so
2199
- * sourcing the type the other way round closed an import cycle.
2200
- */
2201
- type OperationType = 'add' | 'subtract' | 'multiply' | 'multiplyElementwise' | 'transpose' | 'scale' | 'decomposition' | 'solve' | 'fft' | 'eig' | 'svd';
2202
-
2203
- /**
2204
- * Backend Manager
2205
- *
2206
- * Centralized management for matrix operation backends with automatic
2207
- * selection based on matrix size, operation type, and availability.
2208
- * Includes adaptive threshold tuning based on runtime profiling.
2209
- *
2210
- * @packageDocumentation
2211
- */
2212
-
2213
- /**
2214
- * Extended backend hints with operation-specific thresholds
2215
- */
2216
- interface ExtendedBackendHints extends BackendHints {
2217
- /** Specific thresholds by operation type */
2218
- operationThresholds?: Partial<Record<OperationType, {
2219
- wasm?: number;
2220
- gpu?: number;
2221
- }>>;
2222
- /** Enable automatic SIMD detection for WASM */
2223
- autoSIMD?: boolean;
2224
- /** Fallback to JS on backend failure */
2225
- fallbackOnError?: boolean;
2226
- }
2227
- /**
2228
- * Default extended hints
2229
- */
2230
- declare const DEFAULT_EXTENDED_HINTS: Required<ExtendedBackendHints>;
2231
- /**
2232
- * Centralized Backend Manager
2233
- *
2234
- * Provides a unified interface for executing matrix operations with
2235
- * automatic backend selection based on matrix size and operation type.
2236
- * Features adaptive threshold tuning based on runtime profiling.
2237
- */
2238
- declare class BackendManager {
2239
- private hints;
2240
- private initialized;
2241
- private initializationPromise;
2242
- private adaptiveState;
2243
- private configUnsubscribe;
2244
- constructor(hints?: ExtendedBackendHints);
2245
- /**
2246
- * Sync manager state with global config
2247
- */
2248
- private syncWithConfig;
2249
- /**
2250
- * Initialize all available backends
2251
- */
2252
- initialize(): Promise<void>;
2253
- private doInitialize;
2254
- /**
2255
- * Update backend hints
2256
- */
2257
- setHints(hints: ExtendedBackendHints): void;
2258
- /**
2259
- * Get current hints
2260
- */
2261
- getHints(): Required<ExtendedBackendHints>;
2262
- /**
2263
- * Get the best backend for a given operation and matrix size.
2264
- *
2265
- * Selection priority:
2266
- * 1. Preferred backend (if explicitly set)
2267
- * 2. Elements > gpuThreshold -> GPU (if available)
2268
- * 3. Elements > wasmThreshold -> AS WASM (if loaded)
2269
- * 4. JS fallback
2270
- */
2271
- selectBackend(elementCount: number, operation?: OperationType): MatrixBackend;
2272
- /**
2273
- * Execute an operation with automatic backend selection
2274
- */
2275
- private executeWithFallback;
2276
- /**
2277
- * Matrix addition with auto backend selection
2278
- */
2279
- add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2280
- /**
2281
- * Matrix subtraction with auto backend selection
2282
- */
2283
- subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2284
- /**
2285
- * Element-wise multiplication with auto backend selection
2286
- */
2287
- multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2288
- /**
2289
- * Element-wise division with auto backend selection
2290
- */
2291
- divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2292
- /**
2293
- * Scalar multiplication with auto backend selection
2294
- */
2295
- scale(a: DenseMatrix, scalar: number): DenseMatrix;
2296
- /**
2297
- * Element-wise absolute value with auto backend selection
2298
- */
2299
- abs(a: DenseMatrix): DenseMatrix;
2300
- /**
2301
- * Element-wise negation with auto backend selection
2302
- */
2303
- negate(a: DenseMatrix): DenseMatrix;
2304
- /**
2305
- * Matrix multiplication with auto backend selection
2306
- */
2307
- multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
2308
- /**
2309
- * Matrix transpose with auto backend selection
2310
- */
2311
- transpose(a: DenseMatrix): DenseMatrix;
2312
- /**
2313
- * Sum of all elements with auto backend selection
2314
- */
2315
- sum(a: DenseMatrix): Promise<number>;
2316
- /**
2317
- * Sum along axis with auto backend selection
2318
- */
2319
- sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
2320
- /**
2321
- * Frobenius norm with auto backend selection
2322
- */
2323
- norm(a: DenseMatrix): number;
2324
- /**
2325
- * Dot product with auto backend selection
2326
- */
2327
- dot(a: DenseMatrix, b: DenseMatrix): Promise<number>;
2328
- /**
2329
- * Get list of available backends
2330
- */
2331
- getAvailableBackends(): BackendType[];
2332
- /**
2333
- * Check if a specific backend is available
2334
- */
2335
- hasBackend(type: BackendType): boolean;
2336
- /**
2337
- * Get current active backend for a given operation size
2338
- */
2339
- getActiveBackend(elementCount: number, operation?: OperationType): BackendType;
2340
- /**
2341
- * Force a specific backend for all operations
2342
- */
2343
- forceBackend(type: BackendType | null): void;
2344
- /**
2345
- * Record a performance sample for adaptive tuning
2346
- */
2347
- recordSample(operation: OperationType, elementCount: number, backend: BackendType, durationMs: number): void;
2348
- /**
2349
- * Adjust thresholds based on collected samples
2350
- */
2351
- private maybeAdjustThresholds;
2352
- /**
2353
- * Get current adaptive thresholds
2354
- */
2355
- getAdaptiveThresholds(): Map<OperationType, {
2356
- wasm: number;
2357
- gpu: number;
2358
- }>;
2359
- /**
2360
- * Reset adaptive tuning state
2361
- */
2362
- resetAdaptiveState(): void;
2363
- /**
2364
- * Get performance statistics
2365
- */
2366
- getPerformanceStats(): {
2367
- sampleCount: number;
2368
- operationStats: Map<OperationType, {
2369
- avgDuration: number;
2370
- samples: number;
2371
- backendUsage: Record<BackendType, number>;
2372
- }>;
2373
- };
2374
- /**
2375
- * Cleanup resources
2376
- */
2377
- destroy(): void;
2378
- }
2379
- /**
2380
- * Default backend manager instance
2381
- */
2382
- declare const backendManager: BackendManager;
2383
- /**
2384
- * Create a new backend manager with custom hints
2385
- */
2386
- declare function createBackendManager(hints?: ExtendedBackendHints): BackendManager;
2387
-
2388
2064
  /**
2389
2065
  * Eigenvalue and Eigenvector Decomposition
2390
2066
  *
@@ -3304,4 +2980,4 @@ declare function initializeParallelMatrix(): Promise<void>;
3304
2980
  */
3305
2981
  declare function terminateParallelMatrix(): Promise<void>;
3306
2982
 
3307
- export { BUILTIN_SHADERS, type BackendHints, BackendManager, BackendRegistry, type BackendType, BatchExecutor, BufferPool, type CholeskyResult, DEFAULT_BACKEND_HINTS, DEFAULT_EXTENDED_HINTS, DenseMatrix, type EigOptions, type EigResult, type ExpmOptions, type ExtendedBackendHints, GPUBackend, type GPUBackendOptions, type GPUBackendStatus, type GPUCapabilities, GPUContext, type GPUContextOptions, GPUMatrixBackend, type GPUMatrixBackendConfig, JSBackend, type LUResult, type LogmOptions, Matrix, type MatrixBackend, type MatrixDimensions, type MatrixEntry, type MatrixIndex, type MatrixType, type OperationType, ParallelBackend, type ParallelBackendConfig, type PinvOptions, type QROptions, type QRResult, type SVDOptions, type SVDResult, type SchurOptions, type SchurResult, ShaderManager, type SliceSpec, SparseMatrix, type SqrtmOptions, type SyncConfig, SyncManager, type SyncStrategy, WASMBackend, type WASMBackendConfig, type WasmFeatures, abs, add, backendManager, backendRegistry, cholesky, clearFeatureCache, column, cond, createBackendManager, createGPUMatrixBackend, createParallelBackend, createSyncManager, createWASMBackend, destroyGlobalGPU, destroyGlobalGPUBackend, detectGPUCapabilities, detectWasmFeatures, diag, diagonal, divide, dotMultiply, eig, eigWasm, eigvals, eigvalsWasm, exp, getCachedFeatures, getGlobalGPUBackend, getGlobalGPUContext, getRecommendedWorkgroupSize, gpuMatrixBackend, hasWebGPU, identity, initializeGlobalGPUBackend, initializeParallelMatrix, isAtomicsAvailable, isDenseMatrix, isMatrix, isSharedMemoryAvailable, isSparseMatrix, isWasmAvailable, jsBackend, log, lowRankApprox, lu, matrix, matrixExpm, matrixLogm, pinv as matrixPinv, matrixSchur, matrixSqrtm, max, mean, min, multiply, norm, norm2, normFro, ones, parallelBackend, parallelDiag, parallelDotMultiply, parallelIdentity, parallelMatrix, parallelMatrixAbs, parallelMatrixAdd, parallelMatrixColumn, parallelMatrixCos, parallelMatrixDiagonal, parallelMatrixDistance, parallelMatrixDivide, parallelMatrixDot, parallelMatrixExp, parallelMatrixHistogram, parallelMatrixLog, parallelMatrixMatvec, parallelMatrixMax, parallelMatrixMean, parallelMatrixMin, parallelMatrixMultiply, parallelMatrixNorm, parallelMatrixOperations, parallelMatrixOuter, parallelMatrixRow, parallelMatrixSin, parallelMatrixSize, parallelMatrixSqrt, parallelMatrixSquare, parallelMatrixStd, parallelMatrixSubset, parallelMatrixSubtract, parallelMatrixSum, parallelMatrixTan, parallelMatrixTrace, parallelMatrixTranspose, parallelMatrixVariance, parallelOnes, parallelRandom, parallelUnaryMinus, parallelZeros, pinv$1 as pinv, pow, powerIteration, qr, random, row, singularValues, size, spectralRadiusWasm, sqrt, square, subset, subtract, sum, svd, svdWasm, terminateParallelMatrix, trace, transpose, typedMatrixOperations, unaryMinus, wasmBackend, zeros };
2983
+ export { BUILTIN_SHADERS, type BackendHints, BackendManager, BackendRegistry, type BackendType, BatchExecutor, type CholeskyResult, DEFAULT_BACKEND_HINTS, DEFAULT_EXTENDED_HINTS, DenseMatrix, type EigOptions, type EigResult, type ExpmOptions, type ExtendedBackendHints, GPUBackend, type GPUBackendOptions, type GPUBackendStatus, GPUMatrixBackend, type GPUMatrixBackendConfig, JSBackend, type LUResult, type LogmOptions, Matrix, type MatrixBackend, type MatrixDimensions, type MatrixEntry, type MatrixIndex, type MatrixType, type OperationType, ParallelBackend, type ParallelBackendConfig, type PinvOptions, type QROptions, type QRResult, type SVDOptions, type SVDResult, type SchurOptions, type SchurResult, type SliceSpec, SparseMatrix, type SqrtmOptions, type SyncConfig, SyncManager, type SyncStrategy, WASMBackend, type WASMBackendConfig, type WasmFeatures, abs, add, backendManager, backendRegistry, cholesky, clearFeatureCache, column, cond, createBackendManager, createGPUMatrixBackend, createParallelBackend, createSyncManager, createWASMBackend, destroyGlobalGPUBackend, detectWasmFeatures, diag, diagonal, divide, dotMultiply, eig, eigWasm, eigvals, eigvalsWasm, exp, getCachedFeatures, getGlobalGPUBackend, gpuMatrixBackend, identity, initializeGlobalGPUBackend, initializeParallelMatrix, isAtomicsAvailable, isDenseMatrix, isMatrix, isSharedMemoryAvailable, isSparseMatrix, isWasmAvailable, jsBackend, log, lowRankApprox, lu, matrix, matrixExpm, matrixLogm, pinv as matrixPinv, matrixSchur, matrixSqrtm, max, mean, min, multiply, norm, norm2, normFro, ones, parallelBackend, parallelDiag, parallelDotMultiply, parallelIdentity, parallelMatrix, parallelMatrixAbs, parallelMatrixAdd, parallelMatrixColumn, parallelMatrixCos, parallelMatrixDiagonal, parallelMatrixDistance, parallelMatrixDivide, parallelMatrixDot, parallelMatrixExp, parallelMatrixHistogram, parallelMatrixLog, parallelMatrixMatvec, parallelMatrixMax, parallelMatrixMean, parallelMatrixMin, parallelMatrixMultiply, parallelMatrixNorm, parallelMatrixOperations, parallelMatrixOuter, parallelMatrixRow, parallelMatrixSin, parallelMatrixSize, parallelMatrixSqrt, parallelMatrixSquare, parallelMatrixStd, parallelMatrixSubset, parallelMatrixSubtract, parallelMatrixSum, parallelMatrixTan, parallelMatrixTrace, parallelMatrixTranspose, parallelMatrixVariance, parallelOnes, parallelRandom, parallelUnaryMinus, parallelZeros, pinv$1 as pinv, pow, powerIteration, qr, random, row, singularValues, size, spectralRadiusWasm, sqrt, square, subset, subtract, sum, svd, svdWasm, terminateParallelMatrix, trace, transpose, typedMatrixOperations, unaryMinus, wasmBackend, zeros };