@superinstance/device-router 0.1.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.
- package/package.json +11 -0
- package/src/index.js +89 -0
package/package.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@superinstance/device-router",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Heterogeneous compute router — auto-detect CUDA, iGPU, CPU, NPU and route ML workloads optimally",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"keywords": ["cuda", "gpu", "device-routing", "heterogeneous-compute", "ml", "superinstance"],
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": { "type": "git", "url": "https://github.com/SuperInstance/device-router.git" },
|
|
10
|
+
"files": ["src/"]
|
|
11
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeviceRouter — heterogeneous compute routing for ML workloads.
|
|
3
|
+
* JavaScript/TypeScript version of the Python device-router package.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const RoutingStrategy = {
|
|
7
|
+
AUTO: 'auto',
|
|
8
|
+
LATENCY: 'latency',
|
|
9
|
+
THROUGHPUT: 'throughput',
|
|
10
|
+
POWER: 'power',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const SMALL_MODEL_PARAMS = 100_000;
|
|
14
|
+
const LARGE_MODEL_PARAMS = 10_000_000;
|
|
15
|
+
|
|
16
|
+
class RoutingDecision {
|
|
17
|
+
constructor({ device, deviceIndex = 0, reason = '', precision = 'fp32', strategy = RoutingStrategy.AUTO, useAmp = false, confidence = 1.0 }) {
|
|
18
|
+
this.device = device;
|
|
19
|
+
this.deviceIndex = deviceIndex;
|
|
20
|
+
this.reason = reason;
|
|
21
|
+
this.precision = precision;
|
|
22
|
+
this.strategy = strategy;
|
|
23
|
+
this.useAmp = useAmp;
|
|
24
|
+
this.confidence = confidence;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class DeviceRouter {
|
|
29
|
+
constructor() {
|
|
30
|
+
this._cuda = { available: false, devices: [] };
|
|
31
|
+
this._cpu = { available: true, arch: null, cores: null, features: {} };
|
|
32
|
+
this._directml = { available: false };
|
|
33
|
+
this._npu = { available: false };
|
|
34
|
+
this._detected = false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
detect() {
|
|
38
|
+
this._cpu = {
|
|
39
|
+
available: true,
|
|
40
|
+
arch: typeof navigator !== 'undefined' ? navigator.platform : process?.arch || 'unknown',
|
|
41
|
+
cores: typeof navigator !== 'undefined' ? navigator.hardwareConcurrency : require('os').cpus()?.length || null,
|
|
42
|
+
features: {},
|
|
43
|
+
};
|
|
44
|
+
// WebGPU detection (browser)
|
|
45
|
+
if (typeof navigator !== 'undefined' && navigator.gpu) {
|
|
46
|
+
this._cuda = { available: true, name: 'WebGPU' };
|
|
47
|
+
}
|
|
48
|
+
// Node.js: could detect via child_process calling nvidia-smi
|
|
49
|
+
this._detected = true;
|
|
50
|
+
return this.overview();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
overview() {
|
|
54
|
+
if (!this._detected) this.detect();
|
|
55
|
+
return {
|
|
56
|
+
cuda: this._cuda,
|
|
57
|
+
cpu: this._cpu,
|
|
58
|
+
igpu: { available: this._directml.available },
|
|
59
|
+
npu: this._npu,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
route({ modelSize = 0, batchSize = 1, precision = 'fp32', strategy = RoutingStrategy.AUTO, isTraining = false, isOnnx = false } = {}) {
|
|
64
|
+
if (!this._detected) this.detect();
|
|
65
|
+
|
|
66
|
+
if (isOnnx) {
|
|
67
|
+
return new RoutingDecision({ device: 'cpu', reason: 'ONNX models are optimized for CPU execution', precision, strategy });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isTraining) {
|
|
71
|
+
if (this._cuda.available) {
|
|
72
|
+
return new RoutingDecision({ device: 'cuda', reason: 'Training workloads benefit from GPU parallelism', precision: precision === 'fp32' ? 'bf16' : precision, strategy, useAmp: true });
|
|
73
|
+
}
|
|
74
|
+
return new RoutingDecision({ device: 'cpu', reason: 'No CUDA GPU available — training on CPU', precision, strategy, confidence: 0.5 });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (modelSize < SMALL_MODEL_PARAMS) {
|
|
78
|
+
return new RoutingDecision({ device: 'cpu', reason: `Small model (${modelSize.toLocaleString()} params) — CPU is sufficient`, precision, strategy });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (this._cuda.available) {
|
|
82
|
+
return new RoutingDecision({ device: 'cuda', reason: `Medium/large model (${modelSize.toLocaleString()} params) — GPU recommended`, precision: precision === 'fp32' ? 'fp16' : precision, strategy, useAmp: true });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return new RoutingDecision({ device: 'cpu', reason: 'No accelerator available — using CPU', precision, strategy, confidence: 0.6 });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { DeviceRouter, RoutingStrategy, RoutingDecision };
|