EigenTorch is a header-only Eigen-compatible API layer backed by libtorch tensors. It is designed for projects that want Eigen-style C++ APIs while executing calculations through PyTorch (CPU, CUDA, or Apple MPS).
- Core matrix/vector operations (
Eigen::Matrix, dense arithmetic, blocks, maps, arrays) - Dense decompositions and solvers (LU, QR, SVD, eigenvalue methods, LLT/LDLT)
- Geometry stack (
Quaternion,AngleAxis,Transform,Translation,Rotation2D,Scaling,ParametrizedLine,Hyperplane,AlignedBox) - Sparse stack (
SparseMatrix,SparseVector,SparseLU,SparseQR,SimplicialLLT,SimplicialLDLT) - Expanded unsupported modules with libtorch-backed calculations:
AdolcForwardAlignedVector3ArpackSupportAutoDiffBVHEulerAnglesFFTIterativeSolversKroneckerProductLevenbergMarquardtMatrixFunctionsMPRealSupportNNLSNonLinearOptimizationNumericalDiffOpenGLSupportPolynomialsSparseExtraSpecialFunctionsSplinesCXX11/TensorCXX11/TensorSymmetryCXX11/ThreadPool
From repository root:
cmake -S . -B build
cmake --build build -j8cmake --install buildThen from a consumer CMake project:
find_package(Torch REQUIRED)
find_package(Eigen REQUIRED)
target_link_libraries(my_app PRIVATE Eigen::Eigen)#include <Eigen/Dense>
int main() {
Eigen::MatrixXf A = Eigen::MatrixXf::Random(3, 3);
Eigen::VectorXf b = Eigen::VectorXf::Ones(3, 1);
Eigen::VectorXf x = A.partialPivLu().solve(b);
Eigen::MatrixXf C = A * A.inverse();
(void)x;
(void)C;
return 0;
}Set the global default device before constructing matrices:
#include <Eigen/Core>
int main() {
if (torch::cuda::is_available()) {
Eigen::DeviceManager::set_default_device(torch::kCUDA);
} else if (Eigen::DeviceManager::is_mps_available()) {
Eigen::DeviceManager::set_default_device(torch::kMPS);
} else {
Eigen::DeviceManager::set_default_device(torch::kCPU);
}
return 0;
}Run all tests:
cd build
ctest --output-on-failureCurrent test targets:
core_testsparse_linalg_testapi_compat_testunsupported_compat_testunsupported_full_modules_test
Sphinx sources are in docs/.
Generate docs:
cd docs
doxygen Doxyfile
make htmlMain page after build: docs/_build/html/index.html.