File size: 1,681 Bytes
0c6c82c
 
 
 
 
20fb354
44745f2
0c6c82c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { loadPyodide } from "https://cdn.jsdelivr.net/pyodide/v314.0.5/full/pyodide.mjs";

const MODULES = [
  "__init__.py",
  "api.py",
  "diagnostics.py",
  "disaggregated.py",
  "kv_cache.py",
  "latency.py",
  "metrics.py",
  "models.py",
  "optimizer.py",
  "profiles.py",
  "simulator.py",
  "workloads.py",
];

let pyodide;

async function init() {
  pyodide = await loadPyodide();
  pyodide.FS.mkdirTree("/home/pyodide/inferscale");
  for (const file of MODULES) {
    const url = new URL(`./py/inferscale/${file}`, self.location.href);
    const response = await fetch(url);
    if (!response.ok) throw new Error(`Failed to fetch ${file}: HTTP ${response.status}`);
    pyodide.FS.writeFile(`/home/pyodide/inferscale/${file}`, await response.text(), { encoding: "utf8" });
  }
  await pyodide.runPythonAsync(`
import sys
if "/home/pyodide" not in sys.path:
    sys.path.insert(0, "/home/pyodide")
from inferscale.api import execute
`);
  self.postMessage({ type: "ready" });
}

const readyPromise = init().catch((error) => {
  self.postMessage({ type: "fatal", error: error.message || String(error) });
  throw error;
});

self.onmessage = async (event) => {
  const { id, action, payload } = event.data;
  try {
    await readyPromise;
    pyodide.globals.set("_action", action);
    pyodide.globals.set("_payload_json", JSON.stringify(payload ?? {}));
    const output = await pyodide.runPythonAsync(`
import json
_result = execute(_action, json.loads(_payload_json))
json.dumps(_result, separators=(",", ":"))
`);
    self.postMessage({ id, result: JSON.parse(output) });
  } catch (error) {
    self.postMessage({ id, error: error.message || String(error) });
  }
};