File size: 1,179 Bytes
1f3a244 | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "ai-0010", "name": "PyTorch Dispatcher System"}
{"type": "message", "message": {"role": "system", "content": "You are an expert software engineer."}}
{"type": "message", "message": {"role": "user", "content": "How does PyTorch's operator dispatch work?"}}
{"type": "message", "message": {"role": "assistant", "content": "PyTorch uses a layered dispatch system to route operator calls to backend-specific implementations.\n\nDispatchKey enum (bit flags): CPU, CUDA, MPS, Meta, Autograd, Batched, Sparse, etc.\nDispatchKeySet: computed from tensor metadata + TLS (thread-local state).\n\nc10::Dispatcher: central registry mapping (operator, DispatchKeySet) -> KernelFunction.\nKernelFunction: unified boxed/unboxed kernel interface.\n\nCall flow:\n1. User calls torch.add(x, y)\n2. Dispatcher computes DispatchKeySet from x and y\n3. Finds best matching kernel (most specific key wins)\n4. Invokes KernelFunction with arguments\n\nRegistration: torch.library.define/impl/register_fake for custom ops.\nATen operators defined in aten/src/ATen/native/native_functions.yaml.\ntorchgen generates C++ dispatch tables per backend."}}
|