{"id":13738546,"url":"https://github.com/teddykoker/torchsort","last_synced_at":"2026-01-17T12:25:02.029Z","repository":{"id":41180886,"uuid":"349544221","full_name":"teddykoker/torchsort","owner":"teddykoker","description":"Fast, differentiable sorting and ranking in PyTorch","archived":false,"fork":false,"pushed_at":"2025-06-09T17:14:23.000Z","size":579,"stargazers_count":846,"open_issues_count":7,"forks_count":41,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-11-28T06:47:06.494Z","etag":null,"topics":["cuda-kernel","pytorch","ranking","sort"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/torchsort/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teddykoker.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-03-19T20:12:28.000Z","updated_at":"2025-11-25T18:24:37.000Z","dependencies_parsed_at":"2023-12-29T22:26:15.871Z","dependency_job_id":"e89b5355-3ff4-42ff-baaa-cd469ab5b7ce","html_url":"https://github.com/teddykoker/torchsort","commit_stats":{"total_commits":89,"total_committers":2,"mean_commits":44.5,"dds":"0.011235955056179803","last_synced_commit":"3f81bf5209e7c65ee8ffff98ab69b259f7d793f8"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/teddykoker/torchsort","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teddykoker%2Ftorchsort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teddykoker%2Ftorchsort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teddykoker%2Ftorchsort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teddykoker%2Ftorchsort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teddykoker","download_url":"https://codeload.github.com/teddykoker/torchsort/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teddykoker%2Ftorchsort/sbom","scorecard":{"id":872450,"data":{"date":"2025-08-11","repo":{"name":"github.com/teddykoker/torchsort","commit":"75df71db3122b1e5bdef1a659ad7708562a551d1"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.1,"checks":[{"name":"Maintained","score":7,"reason":"7 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 7","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":1,"reason":"Found 1/10 approved changesets -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:107: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:109: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:139: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:151: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/release.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:152: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/test.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/teddykoker/torchsort/test.yml/main?enable=pin","Warn: pipCommand not pinned by hash: .github/workflows/release.yml:114","Warn: pipCommand not pinned by hash: .github/workflows/release.yml:121","Warn: pipCommand not pinned by hash: .github/workflows/release.yml:135","Warn: pipCommand not pinned by hash: .github/workflows/test.yml:22","Warn: pipCommand not pinned by hash: .github/workflows/test.yml:30","Info:   0 out of   6 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned","Info:   0 out of   5 pipCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: jobLevel 'contents' permission set to 'write': .github/workflows/release.yml:149","Warn: no topLevel permission defined: .github/workflows/release.yml:1","Warn: no topLevel permission defined: .github/workflows/test.yml:1"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":0,"reason":"Project has not signed or included provenance with any releases.","details":["Warn: release artifact v0.1.10 not signed: https://api.github.com/repos/teddykoker/torchsort/releases/224092795","Warn: release artifact v0.1.9 not signed: https://api.github.com/repos/teddykoker/torchsort/releases/60572012","Warn: release artifact v0.1.10 does not have provenance: https://api.github.com/repos/teddykoker/torchsort/releases/224092795","Warn: release artifact v0.1.9 does not have provenance: https://api.github.com/repos/teddykoker/torchsort/releases/60572012"],"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":2,"reason":"8 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-3749-ghw9-m3mg","Warn: Project is vulnerable to: PYSEC-2022-43015 / GHSA-47fc-vmwq-366v","Warn: Project is vulnerable to: PYSEC-2025-41 / GHSA-53q9-r3pm-6pq6","Warn: Project is vulnerable to: PYSEC-2024-252 / GHSA-5pcm-hx3q-hm94","Warn: Project is vulnerable to: GHSA-887c-mr87-cxwp","Warn: Project is vulnerable to: PYSEC-2024-251 / GHSA-pg7h-5qx3-wjr3","Warn: Project is vulnerable to: PYSEC-2024-250","Warn: Project is vulnerable to: PYSEC-2024-259"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 24 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-24T04:42:06.588Z","repository_id":41180886,"created_at":"2025-08-24T04:42:06.588Z","updated_at":"2025-08-24T04:42:06.588Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28508464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T11:50:55.898Z","status":"ssl_error","status_checked_at":"2026-01-17T11:50:55.569Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cuda-kernel","pytorch","ranking","sort"],"created_at":"2024-08-03T03:02:26.270Z","updated_at":"2026-01-17T12:25:02.022Z","avatar_url":"https://github.com/teddykoker.png","language":"Python","readme":"# Torchsort\n\n![Tests](https://github.com/teddykoker/torchsort/workflows/Tests/badge.svg)\n\nFast, differentiable sorting and ranking in PyTorch.\n\nPure PyTorch implementation of [Fast Differentiable Sorting and\nRanking](https://arxiv.org/abs/2002.08871) (Blondel et al.). Much of the code is\ncopied from the original Numpy implementation at\n[google-research/fast-soft-sort](https://github.com/google-research/fast-soft-sort),\nwith the isotonic regression solver rewritten as a PyTorch C++ and CUDA\nextension.\n\n## Install\n\n```bash\npip install torchsort\n```\n\nTo build the CUDA extension you will need the CUDA toolchain installed. If you\nwant to build in an environment without a CUDA runtime (e.g. docker), you will\nneed to export the environment variable\n`TORCH_CUDA_ARCH_LIST=\"Pascal;Volta;Turing;Ampere\"` before installing.\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eConda Installation\u003c/strong\u003e\u003c/summary\u003e\nOn some systems the package my not compile with `pip` install in conda\nenvironments. If this happens you may need to:\n    \n 1. Install g++ with `conda install -c conda-forge gxx_linux-64=9.40`\n 2. Run `export CXX=/path/to/miniconda3/envs/env_name/bin/x86_64-conda_cos6-linux-gnu-g++`\n 3. Run `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/miniconda3/lib`\n 4. `pip install --force-reinstall --no-cache-dir --no-deps torchsort`\n\nThanks to @levnikmyskin, @sachit-menon for pointing this out!\n\u003c/details\u003e\n\n### Pre-built Wheels\n\nPre-built wheels are currently available on Linux for recent Python/PyTorch/CUDA combinations:\n\n```bash\n# torchsort version, supports \u003e= 0.1.10\nexport TORCHSORT=0.1.10\n# PyTorch version, supports pt26, pt25, pt24, pt21, pt20, and pt113 for versions\n# 2.6, 2.5, 2.4, 2.1, 2.0, and 1.13 respectively\nexport TORCH=pt26\n# CUDA version, supports cpu, cu113, cu117, cu118, cu121, cu124, and cu126 for\n# CPU-only, CUDA 11.3, CUDA 11.7, CUDA 11.8, CUDA 12.1, CUDA 12.4, and CUDA 12.6\n# respectively\nexport CUDA=cu126\n# Python version, supports cp310, cp311, and cp312 for versions 3.10, 3.11, and\n# 3.12 respectively\nexport PYTHON=cp312\n\npip install https://github.com/teddykoker/torchsort/releases/download/v${TORCHSORT}/torchsort-${TORCHSORT}+${TORCH}${CUDA}-${PYTHON}-${PYTHON}-linux_x86_64.whl\n```\n\nThanks to [siddharthab](https://github.com/siddharthab) for the help creating the build action! See the [latest release](https://github.com/teddykoker/torchsort/releases/latest) for a list of supported combinations in *Assets*.\n\n## Usage\n\n`torchsort` exposes two functions: `soft_rank` and `soft_sort`, each with\nparameters `regularization` (`\"l2\"` or `\"kl\"`) and `regularization_strength` (a\nscalar value). Each will rank/sort the last dimension of a 2-d tensor, with an\naccuracy dependent upon the regularization strength:\n\n```python\nimport torch\nimport torchsort\n\nx = torch.tensor([[8, 0, 5, 3, 2, 1, 6, 7, 9]])\n\ntorchsort.soft_sort(x, regularization_strength=1.0)\n# tensor([[0.5556, 1.5556, 2.5556, 3.5556, 4.5556, 5.5556, 6.5556, 7.5556, 8.5556]])\ntorchsort.soft_sort(x, regularization_strength=0.1)\n# tensor([[-0., 1., 2., 3., 5., 6., 7., 8., 9.]])\n\ntorchsort.soft_rank(x)\n# tensor([[8., 1., 5., 4., 3., 2., 6., 7., 9.]])\n```\n\nBoth operations are fully differentiable, on CPU or GPU:\n\n```python\nx = torch.tensor([[8., 0., 5., 3., 2., 1., 6., 7., 9.]], requires_grad=True).cuda()\ny = torchsort.soft_sort(x)\n\ntorch.autograd.grad(y[0, 0], x)\n# (tensor([[0.1111, 0.1111, 0.1111, 0.1111, 0.1111, 0.1111, 0.1111, 0.1111, 0.1111]],\n#         device='cuda:0'),)\n```\n\n## Example\n\n### Spearman's Rank Coefficient\n\n[Spearman's rank\ncoefficient](https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient)\nis a very useful metric for measuring how monotonically related two variables\nare. We can use Torchsort to create a differentiable Spearman's rank coefficient\nfunction so that we can optimize a model directly for this metric:\n\n```python\nimport torch\nimport torchsort\n\ndef spearmanr(pred, target, **kw):\n    pred = torchsort.soft_rank(pred, **kw)\n    target = torchsort.soft_rank(target, **kw)\n    pred = pred - pred.mean()\n    pred = pred / pred.norm()\n    target = target - target.mean()\n    target = target / target.norm()\n    return (pred * target).sum()\n\npred = torch.tensor([[1., 2., 3., 4., 5.]], requires_grad=True)\ntarget = torch.tensor([[5., 6., 7., 8., 7.]])\nspearman = spearmanr(pred, target)\n# tensor(0.8321)\n\ntorch.autograd.grad(spearman, pred)\n# (tensor([[-5.5470e-02,  2.9802e-09,  5.5470e-02,  1.1094e-01, -1.1094e-01]]),)\n```\n\n## Benchmark\n\n![Benchmark](https://github.com/teddykoker/torchsort/raw/main/extra/benchmark.png)\n\n`torchsort` and `fast_soft_sort` each operate with a time complexity of *O(n log\nn)*, each with some additional overhead when compared to the built-in\n`torch.sort`. With a batch size of 1 (see left), the Numba JIT'd forward pass of\n`fast_soft_sort` performs about on-par with the `torchsort` CPU kernel, however\nits backward pass still relies on some Python code, which greatly penalizes its\nperformance. \n\nFurthermore, the `torchsort` kernel supports batches, and yields much better\nperformance than `fast_soft_sort` as the batch size increases.\n\n![Benchmark](https://github.com/teddykoker/torchsort/raw/main/extra/benchmark_cuda.png)\n\nThe `torchsort` CUDA kernel performs quite well with sequence lengths under\n~2000, and scales to extremely large batch sizes. In the future the\nCUDA kernel can likely be further optimized to achieve performance closer to that of the\nbuilt in `torch.sort`.\n\n\n## Reference\n\n```bibtex\n@inproceedings{blondel2020fast,\n  title={Fast differentiable sorting and ranking},\n  author={Blondel, Mathieu and Teboul, Olivier and Berthet, Quentin and Djolonga, Josip},\n  booktitle={International Conference on Machine Learning},\n  pages={950--959},\n  year={2020},\n  organization={PMLR}\n}\n```\n","funding_links":[],"categories":["Python","其他_机器学习与深度学习"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteddykoker%2Ftorchsort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteddykoker%2Ftorchsort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteddykoker%2Ftorchsort/lists"}