Automatic differentiation for second renormalization of tensor networks