gradient 가 land 하는 법 — 그리고 누적할 때
tensor.backward() 가 backward pass 트리거 호출. scalar 에 호출 기대 — scalar 아니면 'incoming gradient' 인자 제공해서 backward 에게 각 output element 가중치 알려줘야 함. 정상 supervised learning 에선 loss 가 항상 scalar 라 이 인자 거의 안 나타나.
.grad accumulator
backward 후 gradient 가 leaf_tensor.grad 에 land. 중요한 사실: 반복 backward 호출이 기존 .grad 에 더해, overwrite 안 함. 모든 training loop 위에 optimizer.zero_grad() 가 있는 이유 — accumulator reset.
가끔 누적 행동이 유용 — gradient accumulation (여러 mini-batch 에 gradient 누적해서 큰 batch simulate) 을 trivial 하게 해줘.
Non-scalar output
non-scalar tensor 에 .backward() 호출해야 하면 같은 shape 의 tensor 를 'virtual incoming gradient' 로 넘겨야 함 — vector-Jacobian product (vJp). 실전엔 research 코드 (per-sample gradient, custom loss) 에서 정상 training 보다 자주 봐.