tinycodinglm / ai /ai-0010.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
1.18 kB
opencode trace
System You are an expert software engineer.

You are an expert software engineer.

User

How does PyTorch's operator dispatch work?

Assistant

PyTorch uses a layered dispatch system to route operator calls to backend-specific implementations.

DispatchKey enum (bit flags): CPU, CUDA, MPS, Meta, Autograd, Batched, Sparse, etc.
DispatchKeySet: computed from tensor metadata + TLS (thread-local state).

c10::Dispatcher: central registry mapping (operator, DispatchKeySet) -> KernelFunction.
KernelFunction: unified boxed/unboxed kernel interface.

Call flow:

  1. User calls torch.add(x, y)
  2. Dispatcher computes DispatchKeySet from x and y
  3. Finds best matching kernel (most specific key wins)
  4. Invokes KernelFunction with arguments

Registration: torch.library.define/impl/register_fake for custom ops.
ATen operators defined in aten/src/ATen/native/native_functions.yaml.
torchgen generates C++ dispatch tables per backend.