Removing code duplication related to reverse mode AD