{"id":16750464,"url":"https://github.com/mberr/torch-max-mem","last_synced_at":"2025-07-30T23:10:24.626Z","repository":{"id":40464288,"uuid":"454398147","full_name":"mberr/torch-max-mem","owner":"mberr","description":"Decorators for maximizing memory utilization with PyTorch \u0026 CUDA","archived":false,"fork":false,"pushed_at":"2025-02-09T12:30:37.000Z","size":96,"stargazers_count":15,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-27T16:39:52.392Z","etag":null,"topics":["cuda","python","pytorch","torch"],"latest_commit_sha":null,"homepage":"https://torch-max-mem.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mberr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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,"publiccode":null,"codemeta":null}},"created_at":"2022-02-01T13:28:19.000Z","updated_at":"2025-02-09T12:24:33.000Z","dependencies_parsed_at":"2022-08-09T21:12:40.787Z","dependency_job_id":"ccbf76f0-d8ff-401e-be15-3120d06c5aff","html_url":"https://github.com/mberr/torch-max-mem","commit_stats":{"total_commits":28,"total_committers":2,"mean_commits":14.0,"dds":0.1071428571428571,"last_synced_commit":"6fd07804a270f4ebb57fc7e971bacb22f458f31b"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mberr%2Ftorch-max-mem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mberr%2Ftorch-max-mem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mberr%2Ftorch-max-mem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mberr%2Ftorch-max-mem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mberr","download_url":"https://codeload.github.com/mberr/torch-max-mem/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243836991,"owners_count":20355810,"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":["cuda","python","pytorch","torch"],"created_at":"2024-10-13T02:28:14.227Z","updated_at":"2025-07-30T23:10:24.595Z","avatar_url":"https://github.com/mberr.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/mberr/torch-max-mem/raw/main/docs/source/logo.png\" height=\"150\"\u003e\n\u003c/p\u003e\n--\u003e\n\n\u003ch1 align=\"center\"\u003e\n  torch-max-mem\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/mberr/torch-max-mem/actions/workflows/tests.yml\"\u003e\n        \u003cimg alt=\"Tests\" src=\"https://github.com/mberr/torch-max-mem/actions/workflows/tests.yml/badge.svg\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/torch_max_mem\"\u003e\n        \u003cimg alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/torch_max_mem\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/torch_max_mem\"\u003e\n        \u003cimg alt=\"PyPI - Python Version\" src=\"https://img.shields.io/pypi/pyversions/torch_max_mem\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/mberr/torch-max-mem/blob/main/LICENSE\"\u003e\n        \u003cimg alt=\"PyPI - License\" src=\"https://img.shields.io/pypi/l/torch_max_mem\" /\u003e\u003c/a\u003e\n    \u003ca href='https://torch_max_mem.readthedocs.io/en/latest/?badge=latest'\u003e\n        \u003cimg src='https://readthedocs.org/projects/torch_max_mem/badge/?version=latest' alt='Documentation Status' /\u003e\u003c/a\u003e\n    \u003ca href=\"https://codecov.io/gh/mberr/torch-max-mem/branch/main\"\u003e\n        \u003cimg src=\"https://codecov.io/gh/mberr/torch-max-mem/branch/main/graph/badge.svg\" alt=\"Codecov status\" /\u003e\u003c/a\u003e  \n    \u003ca href=\"https://github.com/cthoyt/cookiecutter-python-package\"\u003e\n        \u003cimg alt=\"Cookiecutter template from @cthoyt\" src=\"https://img.shields.io/badge/Cookiecutter-snekpack-blue\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/astral-sh/ruff\"\u003e\n        \u003cimg src=\"https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json\" alt=\"Ruff\" style=\"max-width:100%;\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/mberr/torch-max-mem/blob/main/.github/CODE_OF_CONDUCT.md\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg\" alt=\"Contributor Covenant\"/\u003e\u003c/a\u003e\n    \u003c!-- uncomment if you archive on zenodo\n    \u003ca href=\"https://zenodo.org/badge/latestdoi/XXXXXX\"\u003e\n        \u003cimg src=\"https://zenodo.org/badge/XXXXXX.svg\" alt=\"DOI\"\u003e\u003c/a\u003e\n    --\u003e\n\u003c/p\u003e\n\nThis package provides decorators for memory utilization maximization with\nPyTorch and CUDA by starting with a maximum parameter size and applying\nsuccessive halving until no more out-of-memory exception occurs.\n\n## 💪 Getting Started\n\nAssume you have a function for batched computation of nearest neighbors using\nbrute-force distance calculation.\n\n```python\nimport torch\n\ndef knn(x, y, batch_size, k: int = 3):\n    return torch.cat(\n        [\n            torch.cdist(x[start : start + batch_size], y).topk(k=k, dim=1, largest=False).indices\n            for start in range(0, x.shape[0], batch_size)\n        ],\n        dim=0,\n    )\n```\n\nWith `torch_max_mem` you can decorate this function to reduce the batch size\nuntil no more out-of-memory error occurs.\n\n```python\nimport torch\nfrom torch_max_mem import maximize_memory_utilization\n\n\n@maximize_memory_utilization()\ndef knn(x, y, batch_size, k: int = 3):\n    return torch.cat(\n        [\n            torch.cdist(x[start : start + batch_size], y).topk(k=k, dim=1, largest=False).indices\n            for start in range(0, x.shape[0], batch_size)\n        ],\n        dim=0,\n    )\n```\n\nIn the code, you can now always pass the largest sensible batch size, e.g.,\n\n```python\nx = torch.rand(100, 100, device=\"cuda\")\ny = torch.rand(200, 100, device=\"cuda\")\nknn(x, y, batch_size=x.shape[0])\n```\n\n## 🚀 Installation\n\nThe most recent release can be installed from\n[PyPI](https://pypi.org/project/torch_max_mem/) with uv:\n\n```console\nuv pip install torch_max_mem\n```\n\nor with pip:\n\n```console\npython3 -m pip install torch_max_mem\n```\n\nThe most recent code and data can be installed directly from GitHub with uv:\n\n```console\nuv pip install git+https://github.com/mberr/torch-max-mem.git\n```\n\nor with pip:\n\n```console\npython3 -m pip install git+https://github.com/mberr/torch-max-mem.git\n```\n\n## 👐 Contributing\n\nContributions, whether filing an issue, making a pull request, or forking, are\nappreciated. See\n[CONTRIBUTING.md](https://github.com/mberr/torch-max-mem/blob/master/.github/CONTRIBUTING.md)\nfor more information on getting involved.\n\n## 👋 Attribution\n\nParts of the logic have been developed with\n[Laurent Vermue](https://github.com/lvermue) for\n[PyKEEN](https://github.com/pykeen/pykeen).\n\n### ⚖️ License\n\nThe code in this package is licensed under the MIT License.\n\n### 🍪 Cookiecutter\n\nThis package was created with\n[@audreyfeldroy](https://github.com/audreyfeldroy)'s\n[cookiecutter](https://github.com/cookiecutter/cookiecutter) package using\n[@cthoyt](https://github.com/cthoyt)'s\n[cookiecutter-snekpack](https://github.com/cthoyt/cookiecutter-snekpack)\ntemplate.\n\n## 🛠️ For Developers\n\n\u003cdetails\u003e\n  \u003csummary\u003eSee developer instructions\u003c/summary\u003e\n\nThe final section of the README is for if you want to get involved by making a\ncode contribution.\n\n### Development Installation\n\nTo install in development mode, use the following:\n\n```console\ngit clone git+https://github.com/mberr/torch-max-mem.git\ncd snekpack-demo\nuv pip install -e .\n```\n\nAlternatively, install using pip:\n\n```console\npython3 -m pip install -e .\n```\n\n### Updating Package Boilerplate\n\nThis project uses `cruft` to keep boilerplate (i.e., configuration, contribution\nguidelines, documentation configuration) up-to-date with the upstream\ncookiecutter package. Install cruft with either `uv tool install cruft` or\n`python3 -m pip install cruft` then run:\n\n```console\ncruft update\n```\n\nMore info on Cruft's update command is available\n[here](https://github.com/cruft/cruft?tab=readme-ov-file#updating-a-project).\n\n### 🥼 Testing\n\nAfter cloning the repository and installing `tox` with\n`uv tool install tox --with tox-uv` or `python3 -m pip install tox tox-uv`, the\nunit tests in the `tests/` folder can be run reproducibly with:\n\n```console\ntox -e py\n```\n\nAdditionally, these tests are automatically re-run with each commit in a\n[GitHub Action](https://github.com/mberr/torch-max-mem/actions?query=workflow%3ATests).\n\n### 📖 Building the Documentation\n\nThe documentation can be built locally using the following:\n\n```console\ngit clone git+https://github.com/mberr/torch-max-mem.git\ncd snekpack-demo\ntox -e docs\nopen docs/build/html/index.html\n```\n\nThe documentation automatically installs the package as well as the `docs` extra\nspecified in the [`pyproject.toml`](pyproject.toml). `sphinx` plugins like\n`texext` can be added there. Additionally, they need to be added to the\n`extensions` list in [`docs/source/conf.py`](docs/source/conf.py).\n\nThe documentation can be deployed to [ReadTheDocs](https://readthedocs.io) using\n[this guide](https://docs.readthedocs.io/en/stable/intro/import-guide.html). The\n[`.readthedocs.yml`](.readthedocs.yml) YAML file contains all the configuration\nyou'll need. You can also set up continuous integration on GitHub to check not\nonly that Sphinx can build the documentation in an isolated environment (i.e.,\nwith `tox -e docs-test`) but also that\n[ReadTheDocs can build it too](https://docs.readthedocs.io/en/stable/pull-requests.html).\n\n\u003c/details\u003e\n\n## 🧑‍💻 For Maintainers\n\n\u003cdetails\u003e\n  \u003csummary\u003eSee maintainer instructions\u003c/summary\u003e\n\n### Initial Configuration\n\n#### Configuring ReadTheDocs\n\n[ReadTheDocs](https://readthedocs.org) is an external documentation hosting\nservice that integrates with GitHub's CI/CD. Do the following for each\nrepository:\n\n1. Log in to ReadTheDocs with your GitHub account to install the integration at\n   https://readthedocs.org/accounts/login/?next=/dashboard/\n2. Import your project by navigating to https://readthedocs.org/dashboard/import\n   then clicking the plus icon next to your repository\n3. You can rename the repository on the next screen using a more stylized name\n   (i.e., with spaces and capital letters)\n4. Click next, and you're good to go!\n\n#### Configuring Archival on Zenodo\n\n[Zenodo](https://zenodo.org) is a long-term archival system that assigns a DOI\nto each release of your package. Do the following for each repository:\n\n1. Log in to Zenodo via GitHub with this link:\n   https://zenodo.org/oauth/login/github/?next=%2F. This brings you to a page\n   that lists all of your organizations and asks you to approve installing the\n   Zenodo app on GitHub. Click \"grant\" next to any organizations you want to\n   enable the integration for, then click the big green \"approve\" button. This\n   step only needs to be done once.\n2. Navigate to https://zenodo.org/account/settings/github/, which lists all of\n   your GitHub repositories (both in your username and any organizations you\n   enabled). Click the on/off toggle for any relevant repositories. When you\n   make a new repository, you'll have to come back to this\n\nAfter these steps, you're ready to go! After you make \"release\" on GitHub (steps\nfor this are below), you can navigate to\nhttps://zenodo.org/account/settings/github/repository/mberr/torch-max-mem to see\nthe DOI for the release and link to the Zenodo record for it.\n\n#### Registering with the Python Package Index (PyPI)\n\nThe [Python Package Index (PyPI)](https://pypi.org) hosts packages so they can\nbe easily installed with `pip`, `uv`, and equivalent tools.\n\n1. Register for an account [here](https://pypi.org/account/register)\n2. Navigate to https://pypi.org/manage/account and make sure you have verified\n   your email address. A verification email might not have been sent by default,\n   so you might have to click the \"options\" dropdown next to your address to get\n   to the \"re-send verification email\" button\n3. 2-Factor authentication is required for PyPI since the end of 2023 (see this\n   [blog post from PyPI](https://blog.pypi.org/posts/2023-05-25-securing-pypi-with-2fa/)).\n   This means you have to first issue account recovery codes, then set up\n   2-factor authentication\n4. Issue an API token from https://pypi.org/manage/account/token\n\nThis only needs to be done once per developer.\n\n#### Configuring your machine's connection to PyPI\n\nThis needs to be done once per machine.\n\n```console\nuv tool install keyring\nkeyring set https://upload.pypi.org/legacy/ __token__\nkeyring set https://test.pypi.org/legacy/ __token__\n```\n\nNote that this deprecates previous workflows using `.pypirc`.\n\n### 📦 Making a Release\n\n#### Uploading to PyPI\n\nAfter installing the package in development mode and installing `tox` with\n`uv tool install tox --with tox-uv` or `python3 -m pip install tox tox-uv`, run\nthe following from the console:\n\n```console\ntox -e finish\n```\n\nThis script does the following:\n\n1. Uses [bump-my-version](https://github.com/callowayproject/bump-my-version) to\n   switch the version number in the `pyproject.toml`, `CITATION.cff`,\n   `src/torch_max_mem/version.py`, and\n   [`docs/source/conf.py`](docs/source/conf.py) to not have the `-dev` suffix\n2. Packages the code in both a tar archive and a wheel using\n   [`uv build`](https://docs.astral.sh/uv/guides/publish/#building-your-package)\n3. Uploads to PyPI using\n   [`uv publish`](https://docs.astral.sh/uv/guides/publish/#publishing-your-package).\n4. Push to GitHub. You'll need to make a release going with the commit where the\n   version was bumped.\n5. Bump the version to the next patch. If you made big changes and want to bump\n   the version by minor, you can use `tox -e bumpversion -- minor` after.\n\n#### Releasing on GitHub\n\n1. Navigate to https://github.com/mberr/torch-max-mem/releases/new to draft a\n   new release\n2. Click the \"Choose a Tag\" dropdown and select the tag corresponding to the\n   release you just made\n3. Click the \"Generate Release Notes\" button to get a quick outline of recent\n   changes. Modify the title and description as you see fit\n4. Click the big green \"Publish Release\" button\n\nThis will trigger Zenodo to assign a DOI to your release as well.\n\n### Updating Package Boilerplate\n\nThis project uses `cruft` to keep boilerplate (i.e., configuration, contribution\nguidelines, documentation configuration) up-to-date with the upstream\ncookiecutter package. Install cruft with either `uv tool install cruft` or\n`python3 -m pip install cruft` then run:\n\n```console\n$ cruft update\n```\n\nMore info on Cruft's update command is available\n[here](https://github.com/cruft/cruft?tab=readme-ov-file#updating-a-project).\n\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmberr%2Ftorch-max-mem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmberr%2Ftorch-max-mem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmberr%2Ftorch-max-mem/lists"}