There is an older but similar question here that might give some pointers: Using ForwardDiff on qr factorization