import json
import sys
import traceback
def _send(obj):
sys.stdout.write(json.dumps(obj))
sys.stdout.write("\n")
sys.stdout.flush()
def _try_import_jax():
try:
import jax
import jax.numpy as jnp
return jax, jnp, None
except Exception as e:
return None, None, str(e)
def _unpack_arg(arg):
if not isinstance(arg, dict) or len(arg) != 1:
raise ValueError(f"arg must be a single-key object, got {arg!r}")
key, value = next(iter(arg.items()))
if key in ("i32", "i64", "u32", "u64", "device_ptr"):
return int(value)
if key in ("f32", "f64"):
return float(value)
raise ValueError(f"unknown arg type {key!r}")
def main():
jax, jnp, import_err = _try_import_jax()
next_handle = 1
kernels = {}
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
req = json.loads(line)
op = req.get("op")
if op == "ping":
if jax is None:
_send({"ok": False, "error": f"jax import failed: {import_err}"})
continue
try:
devices = [str(d) for d in jax.devices()]
except Exception as e:
devices = []
err = f"jax.devices() failed: {e}"
_send({"ok": False, "error": err, "jax_version": jax.__version__})
continue
_send({
"ok": True,
"devices": devices,
"jax_version": getattr(jax, "__version__", "unknown"),
})
elif op == "register":
if jax is None:
_send({"ok": False, "error": f"jax not importable: {import_err}"})
continue
name = req.get("name")
source = req.get("source")
if not isinstance(name, str) or not isinstance(source, str):
_send({"ok": False, "error": "register requires 'name' and 'source' strings"})
continue
ns = {"jax": jax, "jnp": jnp}
exec(source, ns)
if name not in ns or not callable(ns[name]):
_send({"ok": False, "error": f"source must define a callable named {name!r}"})
continue
original = ns[name]
jit_fn = jax.jit(original, static_argnums=(0,))
handle = next_handle
next_handle += 1
kernels[handle] = (jit_fn, original)
_send({"ok": True, "handle": handle})
elif op == "dispatch":
if jax is None:
_send({"ok": False, "error": f"jax not importable: {import_err}"})
continue
handle = req.get("handle")
count = req.get("count")
args = req.get("args", [])
if handle not in kernels:
_send({"ok": False, "error": f"unknown handle {handle}"})
continue
try:
py_args = [_unpack_arg(a) for a in args]
except ValueError as e:
_send({"ok": False, "error": str(e)})
continue
jit_fn, _original = kernels[handle]
try:
result = jit_fn(int(count), *py_args)
if hasattr(result, "block_until_ready"):
result.block_until_ready()
except Exception as e:
_send({"ok": False, "error": f"jit call failed: {e}"})
continue
_send({"ok": True})
elif op == "shutdown":
_send({"ok": True, "goodbye": True})
return
else:
_send({"ok": False, "error": f"unknown op {op!r}"})
except json.JSONDecodeError as e:
_send({"ok": False, "error": f"json decode: {e}"})
except Exception:
_send({"ok": False, "error": traceback.format_exc()})
if __name__ == "__main__":
main()