-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwasm-features.ts
More file actions
104 lines (91 loc) · 2.88 KB
/
Copy pathwasm-features.ts
File metadata and controls
104 lines (91 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// WebAssembly Features Detection
// Detects SIMD, threads, and bulk memory support
export interface WasmFeatures {
simd: boolean;
threads: boolean;
bulkMemory: boolean;
}
/**
* Detect available WebAssembly features
* SIMD: 2-3x faster vector operations
* Threads: Parallel execution with SharedArrayBuffer
* Bulk Memory: Faster memory operations
*/
export async function detectWasmFeatures(): Promise<WasmFeatures> {
try {
// Test SIMD support
// This bytecode tests for v128 SIMD instructions
const simd = await WebAssembly.validate(
new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0,
253, 15, 253, 98, 11,
])
);
// Test threads support (requires SharedArrayBuffer)
const threads =
typeof SharedArrayBuffer !== 'undefined' &&
typeof Atomics !== 'undefined' &&
crossOriginIsolated; // Threads require COOP/COEP headers
// Test bulk memory support
const bulkMemory = await WebAssembly.validate(
new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 5, 3, 1, 0, 1, 10, 14, 1, 12,
0, 65, 0, 65, 0, 65, 0, 252, 10, 0, 11,
])
);
console.log('🔍 WASM Features detected:', {
simd: simd ? '✅' : '❌',
threads: threads ? '✅' : '❌',
bulkMemory: bulkMemory ? '✅' : '❌',
});
if (!threads && typeof SharedArrayBuffer !== 'undefined') {
console.warn(
'⚠️ SharedArrayBuffer available but crossOriginIsolated=false. ' +
'Add COOP/COEP headers to enable threads:\n' +
'Cross-Origin-Opener-Policy: same-origin\n' +
'Cross-Origin-Embedder-Policy: require-corp'
);
}
return { simd, threads, bulkMemory };
} catch (error) {
console.error('❌ Failed to detect WASM features:', error);
return { simd: false, threads: false, bulkMemory: false };
}
}
/**
* Get recommended Wllama build based on detected features
*/
export function getRecommendedWllamaBuild(features: WasmFeatures): {
path: string;
description: string;
speedMultiplier: number;
} {
if (features.threads && features.simd) {
return {
path: 'multi-thread/wllama.wasm',
description: 'Multi-threaded + SIMD (fastest)',
speedMultiplier: 3.0,
};
}
if (features.simd) {
return {
path: 'single-thread/wllama.wasm',
description: 'Single-threaded + SIMD',
speedMultiplier: 2.0,
};
}
return {
path: 'single-thread/wllama.wasm',
description: 'Basic (no SIMD)',
speedMultiplier: 1.0,
};
}
/**
* Get optimal number of threads for Wllama
*/
export function getOptimalThreadCount(features: WasmFeatures): number {
if (!features.threads) return 1;
const cores = navigator.hardwareConcurrency || 4;
// Use 75% of cores, minimum 2, maximum 8
return Math.max(2, Math.min(8, Math.floor(cores * 0.75)));
}