{"type": "session", "harness": "opencode", "id": "ai-0008", "name": "PyTorch Autograd Engine"} {"type": "message", "message": {"role": "system", "content": "You are an expert software engineer."}} {"type": "message", "message": {"role": "user", "content": "How does PyTorch's autograd work?"}} {"type": "message", "message": {"role": "assistant", "content": "PyTorch autograd records a dynamic computation graph during forward pass.\n\nKey components:\n- Engine: executes backward tasks in parallel (CPU threads, CUDA streams)\n- Node: base class for graph nodes (AccumulateGrad, CopySlices)\n- Edge: connects nodes with gradient edges\n- Task: unit of work for the engine\n\nForward pass: each operation creates an autograd Function, records edges in the graph.\nBackward pass: Engine traverses graph in reverse topological order, calling node.backward().\nEngine uses thread pool for CPU, CUDA streams for GPU parallelism.\n\nKey files:\n- torch/csrc/autograd/engine.cpp: parallel execution engine\n- torch/csrc/autograd/functions/basic_ops.cpp: accumulation, copy operations\n- torch/csrc/autograd/input_metadata.cpp: input tracking\n\nAOT Autograd: ahead-of-time compilation for torch.compile pipeline. Separates forward and backward passes for optimization."}}