Explain the mechanism of automatic differentiation and how custom gradients are registered in different frameworks.