{"id":18508858,"url":"https://github.com/flaport/torch_sparse_solve","last_synced_at":"2025-04-09T20:51:01.591Z","repository":{"id":48268164,"uuid":"275158790","full_name":"flaport/torch_sparse_solve","owner":"flaport","description":"A sparse KLU solver for PyTorch.","archived":false,"fork":false,"pushed_at":"2022-01-10T16:32:50.000Z","size":50,"stargazers_count":62,"open_issues_count":6,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T14:49:00.059Z","etag":null,"topics":["klu-algorithm","python","pytorch","sparse","sparse-linear-systems","sparse-matrices","sparse-matrix","suitesparse"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/torch-sparse-solve","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flaport.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-26T13:09:52.000Z","updated_at":"2024-11-11T07:36:23.000Z","dependencies_parsed_at":"2022-08-24T10:51:27.092Z","dependency_job_id":null,"html_url":"https://github.com/flaport/torch_sparse_solve","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaport%2Ftorch_sparse_solve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaport%2Ftorch_sparse_solve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaport%2Ftorch_sparse_solve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaport%2Ftorch_sparse_solve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flaport","download_url":"https://codeload.github.com/flaport/torch_sparse_solve/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248111957,"owners_count":21049576,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["klu-algorithm","python","pytorch","sparse","sparse-linear-systems","sparse-matrices","sparse-matrix","suitesparse"],"created_at":"2024-11-06T15:15:38.857Z","updated_at":"2025-04-09T20:51:01.564Z","avatar_url":"https://github.com/flaport.png","language":"Python","readme":"# Torch Sparse Solve\n\nAn alternative to `torch.solve` for sparse PyTorch CPU tensors using\nthe efficient\n[KLU algorithm](https://ufdcimages.uflib.ufl.edu/UF/E0/01/17/21/00001/palamadai_e.pdf).\n\n## CPU tensors only\n\nThis library is a wrapper around the\n[SuiteSparse](https://github.com/DrTimothyAldenDavis/SuiteSparse) KLU\nalgorithms. This means the algorithm is only implemented for\nC-arrays and hence is only available for PyTorch CPU\ntensors. However, for large, sparse enough tensors, it might still be\nworth doing the GPU→CPU conversion.\n\n## Usage\n\nThe `torch_sparse_solve` library provides a single function `solve(A, b)`, which solves for `x` in the **batched matrix × batched matrix**\nsystem `Ax=b` for `torch.float64` tensors (notice the different API in comparison to `torch.solve`):\n\n```python\nimport torch\nfrom torch_sparse_solve import solve\ntorch.manual_seed(42)\nmask = torch.tensor([[[1,0,0],[1,1,0],[0,0,1]]], dtype=torch.float64)\nA = (mask * torch.randn(4, 3, 3, dtype=torch.float64)).to_sparse()\nb = torch.randn(4, 3, 2, dtype=torch.float64)\nx = solve(A, b)\n\n# compare to torch.solve:\nA = A.to_dense()\nprint( (x - torch.solve(b, A)[0] \u003c 1e-9).all() )\n```\n\n`True`\n\n## Caveats\n\nThere are two major caveats you should be aware of when using\n`torch_sparse_solve.solve(A, b)`:\n\n- `A` should be 'dense' in the first dimension, i.e. the batch dimension\n  should contain as many elements as the batch size.\n\n- `A` should have the same sparsity pattern for every element in the batch.\n  If this is not the case, you have two options:\n\n  1. Create a new sparse matrix with the same sparsity pattern for\n     every element in the batch by adding zeros to the sparse\n     representation.\n  2. **OR** loop over the batch dimension and solve sequentially, i.e.\n     with shapes `(1, m, m)` and `(1, m, n)` for each element in `A` and `b`\n     respectively.\n\n- `solve` is differentiable, but only for the non-zero elements of `A` (which in most\n  cases is what you want anyway).\n\n## Installation\n\nThe library can be installed with `pip`:\n\n```bash\npip install torch_sparse_solve\n```\n\nPlease note that no pre-built wheels exist. This means that `pip` will\nattempt to install the library from source. Make sure you have the\nnecessary dependencies installed for your OS.\n\n## Dependencies\n\n### Linux\n\nOn Linux, having PyTorch, scipy and suitesparse installed is often\nenough to be able install the library (along with the typical\ndeveloper tools for your distribution). Run the following inside a\nconda environment:\n\n```bash\nconda install suitesparse scipy\nconda install pytorch -c pytorch\npip install torch_sparse_solve\n```\n\n### Windows\n\nOn Windows, the installation process is a bit more involved as\ntypically the build dependencies are not installed. To install those,\ndownload **Visual Studio Community 2017** from\n[here](https://my.visualstudio.com/Downloads?q=visual%20studio%202017\u0026wt.mc_id=o~msft~vscom~older-downloads).\nDuring installation, go to **Workloads** and select the following\nworkloads:\n\n- Desktop development with C++\n- Python development\n\nThen go to **Individual Components** and select the following\nadditional items:\n\n- C++/CLI support\n- VC++ 2015.3 v14.00 (v140) toolset for desktop\n\nThen, download and install Microsoft Visual C++ Redistributable from\n[here](https://aka.ms/vs/16/release/vc_redist.x64.exe).\n\nAfter these installation steps, run the following commands _inside_ a\n**x64 Native Tools Command Prompt for VS 2017**, after activating your\nconda environment:\n\n```bash\nset DISTUTILS_USE_SDK=1\nconda install suitesparse scipy\nconda install pytorch -c pytorch\npip install torch_sparse_solve\n```\n\n## License \u0026 Credits\n\n© Floris Laporte 2020, LGPL-2.1\n\nThis library was partly based on:\n\n- [SuiteSparse](https://github.com/DrTimothyAldenDavis/SuiteSparse), LGPL-2.1\n- [kagami-c/PyKLU](https://github.com/kagami-c/PyKLU), LGPL-2.1\n- [scipy.sparse](https://github.com/scipy/scipy/tree/master/scipy/sparse), BSD-3\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaport%2Ftorch_sparse_solve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflaport%2Ftorch_sparse_solve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaport%2Ftorch_sparse_solve/lists"}