Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/attach/torch/cudapp_torch.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / attach / torch / cudapp_torch.py` 119 lines ยท 5990 bytes Raw Blame History 1 """cudapp_torch: PyTorch custom operators that run on the NVIDIA GB10 through CUDA++ (aien-dev/CUDApp). 2 3 torch.ops.cudapp.mm(a, b) a (m, k) @ b (k, n) -> (m, n) float32, omega_gpu_gemm_ex 4 torch.ops.cudapp.swiglu(gate, up) silu(gate) * up, float32, omega_gpu_swiglu_f32 5 6 Registered with torch.library.custom_op on CPU tensors: the operands are host memory, CUDA++ stages them to the 7 chip, runs kernels from Omega's own code generator (no CUDA toolkit, no NVIDIA user-space library) and copies the 8 result back. Arithmetic (CUDApp src/omega_gpu_gemm_ex.h, src/omega_gpu_elementwise_api.h): 9 mm bf16 x bf16 and f16 x f16 native HMMA with fp32 accumulation; anything with f32 the bf16x3 route 10 (not exact fp32); the k parts summed on the host in double. 11 swiglu MUFU.EX2 / MUFU.RCP (each about 2^-22 relative). 12 The library is CUDA++'s libomega_gpu.a linked into one shared object (build.sh, CUDAPP_TORCH_LIB). 13 14 MIGRATION-PENDING host glue (Python): torch.library registration is a Python/C++ ABI, which seeded Omega cannot 15 emit yet. MIGRATION_PENDING below marks it; the named successor is the omega_native.py binding that 16 omega-bridge tools/native writes (the python word of src/native.om) for an Omega unit that calls the CUDA++ entries, once Omega 17 code can call host functions. 18 """ 19 import ctypes 20 import os 21 22 import torch 23 24 MIGRATION_PENDING = True 25 26 OP_N, OP_T = 0, 1 27 DT = {torch.float32: 0, torch.bfloat16: 1, torch.float16: 2} 28 29 30 class _Info(ctypes.Structure): 31 _fields_ = [("route", ctypes.c_uint32), ("core_type", ctypes.c_uint32), 32 ("core_calls", ctypes.c_uint64), ("host_fixed", ctypes.c_uint64)] 33 34 35 class _GemmEx(ctypes.Structure): # OmegaGemmExArgs, src/omega_gpu_gemm_ex.h 36 _fields_ = [("op_a", ctypes.c_int), ("op_b", ctypes.c_int), 37 ("m", ctypes.c_uint64), ("n", ctypes.c_uint64), ("k", ctypes.c_uint64), ("batch", ctypes.c_uint64), 38 ("a_type", ctypes.c_int), ("b_type", ctypes.c_int), ("c_type", ctypes.c_int), 39 ("a", ctypes.c_void_p), ("lda", ctypes.c_uint64), ("stride_a", ctypes.c_uint64), 40 ("b", ctypes.c_void_p), ("ldb", ctypes.c_uint64), ("stride_b", ctypes.c_uint64), 41 ("c", ctypes.c_void_p), ("ldc", ctypes.c_uint64), ("stride_c", ctypes.c_uint64), 42 ("alpha", ctypes.c_float), ("beta", ctypes.c_float), 43 ("compute", ctypes.c_int), ("scale", ctypes.c_int), 44 ("scale_a", ctypes.c_void_p), ("scale_b", ctypes.c_void_p), 45 ("scale_stride_a", ctypes.c_uint64), ("scale_stride_b", ctypes.c_uint64), 46 ("block_m", ctypes.c_uint32), ("block_n", ctypes.c_uint32), ("block_k", ctypes.c_uint32), 47 ("info", ctypes.POINTER(_Info))] 48 49 50 _lib = ctypes.CDLL(os.environ.get("CUDAPP_TORCH_LIB", os.path.join(os.path.dirname(__file__), "libcudapp_attach.so"))) 51 _lib.omega_gpu_gemm_ex.argtypes = [ctypes.POINTER(_GemmEx)] 52 _lib.omega_gpu_gemm_ex.restype = ctypes.c_int 53 _lib.omega_gpu_gemm_rc_name.argtypes = [ctypes.c_int] 54 _lib.omega_gpu_gemm_rc_name.restype = ctypes.c_char_p 55 _lib.omega_gpu_swiglu_f32.argtypes = [ctypes.c_uint32, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] 56 _lib.omega_gpu_swiglu_f32.restype = ctypes.c_int 57 _lib.omega_gpu_elementwise_rc_name.argtypes = [ctypes.c_int] 58 _lib.omega_gpu_elementwise_rc_name.restype = ctypes.c_char_p 59 _lib.omega_gpu_gemm_ex_test_set_mutant.argtypes = [ctypes.c_int] 60 _lib.omega_gpu_gemm_ex_test_set_mutant.restype = None 61 62 last_route = None # OmegaGemmRoute of the last cudapp::mm (3 = BF16X3, 1 = NATIVE) 63 64 65 def set_gemm_mutant(m): 66 """Negative control: 1 drops the last k element of every product (CUDApp's own red check).""" 67 _lib.omega_gpu_gemm_ex_test_set_mutant(m) 68 69 70 @torch.library.custom_op("cudapp::mm", mutates_args=()) 71 def mm(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: 72 global last_route 73 if a.dim() != 2 or b.dim() != 2 or a.shape[1] != b.shape[0]: 74 raise ValueError(f"cudapp::mm needs (m, k) @ (k, n), got {tuple(a.shape)} @ {tuple(b.shape)}") 75 if a.dtype not in DT or b.dtype not in DT or a.device.type != "cpu" or b.device.type != "cpu": 76 raise TypeError("cudapp::mm takes CPU float32, bfloat16 or float16 tensors") 77 a, b = a.contiguous(), b.contiguous() 78 m, k = a.shape 79 n = b.shape[1] 80 c = torch.empty((m, n), dtype=torch.float32) 81 if m == 0 or n == 0: 82 return c 83 if k == 0: 84 return c.zero_() 85 info = _Info() 86 args = _GemmEx(op_a=OP_N, op_b=OP_N, m=m, n=n, k=k, batch=1, a_type=DT[a.dtype], b_type=DT[b.dtype], c_type=0, 87 a=a.data_ptr(), lda=k, b=b.data_ptr(), ldb=n, c=c.data_ptr(), ldc=n, alpha=1.0, beta=0.0, 88 info=ctypes.pointer(info)) 89 rc = _lib.omega_gpu_gemm_ex(ctypes.byref(args)) 90 if rc != 0: 91 raise RuntimeError(f"omega_gpu_gemm_ex: {_lib.omega_gpu_gemm_rc_name(rc).decode()} ({rc})") 92 last_route = info.route 93 return c 94 95 96 @mm.register_fake 97 def _(a, b): 98 return a.new_empty((a.shape[0], b.shape[1]), dtype=torch.float32) 99 100 101 @torch.library.custom_op("cudapp::swiglu", mutates_args=()) 102 def swiglu(gate: torch.Tensor, up: torch.Tensor) -> torch.Tensor: 103 if gate.shape != up.shape or gate.dtype != torch.float32 or up.dtype != torch.float32: 104 raise TypeError("cudapp::swiglu takes two float32 CPU tensors of one shape") 105 if gate.device.type != "cpu" or up.device.type != "cpu" or gate.numel() >= 2 ** 32: 106 raise TypeError("cudapp::swiglu takes CPU tensors of fewer than 2^32 elements") 107 gate, up = gate.contiguous(), up.contiguous() 108 out = torch.empty_like(gate) 109 if gate.numel() == 0: 110 return out 111 rc = _lib.omega_gpu_swiglu_f32(gate.numel(), gate.data_ptr(), up.data_ptr(), out.data_ptr(), None) 112 if rc != 0: 113 raise RuntimeError(f"omega_gpu_swiglu_f32: {_lib.omega_gpu_elementwise_rc_name(rc).decode()} ({rc})") 114 return out 115 116 117 @swiglu.register_fake 118 def _(gate, up): 119 return torch.empty_like(gate)