|
| 1 | +import ast |
| 2 | +import inspect |
| 3 | +import ctypes |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +# ============================================================ |
| 7 | +# Native primitive kernels (precompiled shared library) |
| 8 | +# ============================================================ |
| 9 | + |
| 10 | +lib = ctypes.CDLL("./libvec.so") |
| 11 | + |
| 12 | +lib.vec_elem_mul.argtypes = lib.vec_elem_add.argtypes = [ |
| 13 | + ctypes.POINTER(ctypes.c_double), |
| 14 | + ctypes.POINTER(ctypes.c_double), |
| 15 | + ctypes.POINTER(ctypes.c_double), |
| 16 | + ctypes.c_int, |
| 17 | +] |
| 18 | +lib.vec_elem_add.restype = lib.vec_elem_mul.restype = None |
| 19 | + |
| 20 | + |
| 21 | +# ============================================================ |
| 22 | +# @kernel decorator: Python AST -> lowering -> native execution |
| 23 | +# ============================================================ |
| 24 | + |
| 25 | +def kernel(func): |
| 26 | + """ |
| 27 | + Treat the function body as an embedded DSL. |
| 28 | + The body is parsed, not executed. |
| 29 | + """ |
| 30 | + # -------- Parse Python AST -------- |
| 31 | + src = inspect.getsource(func) |
| 32 | + tree = ast.parse(src) |
| 33 | + func_def = tree.body[0] |
| 34 | + |
| 35 | + # Expect: |
| 36 | + # return <expr> |
| 37 | + return_stmt = func_def.body[0] |
| 38 | + |
| 39 | + expr = return_stmt.value |
| 40 | + arg_names = [arg.arg for arg in func_def.args.args] |
| 41 | + |
| 42 | + # -------- Lowering: AST -> primitive ops -------- |
| 43 | + def lower(node, env, n): |
| 44 | + if isinstance(node, ast.Name): |
| 45 | + return env[node.id] |
| 46 | + |
| 47 | + if isinstance(node, ast.BinOp): |
| 48 | + left = lower(node.left, env, n) |
| 49 | + right = lower(node.right, env, n) |
| 50 | + out = np.empty_like(left) |
| 51 | + |
| 52 | + if isinstance(node.op, ast.Mult): |
| 53 | + lib.vec_elem_mul( |
| 54 | + left.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 55 | + right.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 56 | + out.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 57 | + n, |
| 58 | + ) |
| 59 | + elif isinstance(node.op, ast.Add): |
| 60 | + lib.vec_elem_add( |
| 61 | + left.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 62 | + right.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 63 | + out.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), |
| 64 | + n, |
| 65 | + ) |
| 66 | + else: |
| 67 | + raise NotImplementedError("Unsupported operator") |
| 68 | + |
| 69 | + return out |
| 70 | + |
| 71 | + raise NotImplementedError("Unsupported AST node") |
| 72 | + |
| 73 | + # -------- Runtime wrapper -------- |
| 74 | + def wrapper(*args): |
| 75 | + arrays = [np.asarray(a, dtype=np.float64) for a in args] |
| 76 | + n = arrays[0].size |
| 77 | + |
| 78 | + env = dict(zip(arg_names, arrays)) |
| 79 | + result = lower(expr, env, n) |
| 80 | + return result |
| 81 | + |
| 82 | + return wrapper |
| 83 | + |
| 84 | + |
| 85 | +# ============================================================ |
| 86 | +# User-defined kernels (pure Python, no strings) |
| 87 | +# ============================================================ |
| 88 | + |
| 89 | +@kernel |
| 90 | +def vec_elem_mul(a, b): |
| 91 | + |
| 92 | + # element-wise multiplication |
| 93 | + return a * b |
| 94 | + |
| 95 | +@kernel |
| 96 | +def vec_elem_add(a, b): |
| 97 | + # element-wise addition |
| 98 | + return a + b |
| 99 | + |
| 100 | +@kernel |
| 101 | +def vec_elem_fma(a, b, c): |
| 102 | + # element-wise fused multiply-add: a * b + c |
| 103 | + return (a * b) + c |
| 104 | + |
| 105 | +# ============================================================ |
| 106 | +# Test |
| 107 | +# ============================================================ |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + a = [1.0, 2.0, 3.0] |
| 111 | + b = [4.0, 5.0, 6.0] |
| 112 | + c = [10.0, 10.0, 10.0] |
| 113 | + |
| 114 | + print("mul:", vec_elem_mul(a, b)) |
| 115 | + print("add:", vec_elem_add(a, b)) |
| 116 | + print("fma:", vec_elem_fma(a, b, c)) |
| 117 | + |
0 commit comments