{"id":13689356,"url":"https://github.com/szymonmaszke/torchfunc","last_synced_at":"2025-04-05T17:03:42.180Z","repository":{"id":53717842,"uuid":"208677026","full_name":"szymonmaszke/torchfunc","owner":"szymonmaszke","description":"PyTorch functions and utilities to make your life easier","archived":false,"fork":false,"pushed_at":"2021-03-18T10:51:48.000Z","size":1629,"stargazers_count":195,"open_issues_count":2,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-27T08:20:54.253Z","etag":null,"topics":["docker","extensions","functions","neural-network","performance","performance-analysis","pytorch","record","recording","tips","utilities","utils"],"latest_commit_sha":null,"homepage":"","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/szymonmaszke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-16T00:25:53.000Z","updated_at":"2024-04-27T08:20:54.254Z","dependencies_parsed_at":"2022-09-10T15:50:13.938Z","dependency_job_id":null,"html_url":"https://github.com/szymonmaszke/torchfunc","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szymonmaszke%2Ftorchfunc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szymonmaszke%2Ftorchfunc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szymonmaszke%2Ftorchfunc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szymonmaszke%2Ftorchfunc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szymonmaszke","download_url":"https://codeload.github.com/szymonmaszke/torchfunc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369953,"owners_count":20927928,"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":["docker","extensions","functions","neural-network","performance","performance-analysis","pytorch","record","recording","tips","utilities","utils"],"created_at":"2024-08-02T15:01:44.525Z","updated_at":"2025-04-05T17:03:42.149Z","avatar_url":"https://github.com/szymonmaszke.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cimg align=\"left\" width=\"256\" height=\"256\" src=\"https://github.com/szymonmaszke/torchfunc/blob/master/assets/logos/medium.png\"\u003e\n\n* Improve and analyse performance of your neural network (e.g. Tensor Cores compatibility)\n* Record/analyse internal state of `torch.nn.Module` as data passes through it\n* Do the above based on external conditions (using single `Callable` to specify it)\n* Day-to-day neural network related duties (model size, seeding, time measurements etc.)\n* Get information about your host operating system, `torch.nn.Module` device, CUDA\ncapabilities etc.\n\n\n| Version | Docs | Tests | Coverage | Style | PyPI | Python | PyTorch | Docker | Roadmap |\n|---------|------|-------|----------|-------|------|--------|---------|--------|---------|\n| [![Version](https://img.shields.io/static/v1?label=\u0026message=0.2.0\u0026color=377EF0\u0026style=for-the-badge)](https://github.com/szymonmaszke/torchfunc/releases) | [![Documentation](https://img.shields.io/static/v1?label=\u0026message=docs\u0026color=EE4C2C\u0026style=for-the-badge)](https://szymonmaszke.github.io/torchfunc/)  | ![Tests](https://github.com/szymonmaszke/torchfunc/workflows/test/badge.svg) | ![Coverage](https://img.shields.io/codecov/c/github/szymonmaszke/torchfunc?label=%20\u0026logo=codecov\u0026style=for-the-badge) | [![codebeat](https://img.shields.io/static/v1?label=\u0026message=CB\u0026color=27A8E0\u0026style=for-the-badge)](https://codebeat.co/projects/github-com-szymonmaszke-torchfunc-master) | [![PyPI](https://img.shields.io/static/v1?label=\u0026message=PyPI\u0026color=377EF0\u0026style=for-the-badge)](https://pypi.org/project/torchfunc/) | [![Python](https://img.shields.io/static/v1?label=\u0026message=3.6\u0026color=377EF0\u0026style=for-the-badge\u0026logo=python\u0026logoColor=F8C63D)](https://www.python.org/) | [![PyTorch](https://img.shields.io/static/v1?label=\u0026message=\u003e=1.2.0\u0026color=EE4C2C\u0026style=for-the-badge)](https://pytorch.org/) | [![Docker](https://img.shields.io/static/v1?label=\u0026message=docker\u0026color=309cef\u0026style=for-the-badge)](https://hub.docker.com/r/szymonmaszke/torchfunc) | [![Roadmap](https://img.shields.io/static/v1?label=\u0026message=roadmap\u0026color=009688\u0026style=for-the-badge)](https://github.com/szymonmaszke/torchfunc/blob/master/ROADMAP.md) |\n\n# :bulb: Examples\n\n__Check documentation here:__ [https://szymonmaszke.github.io/torchfunc](https://szymonmaszke.github.io/torchfunc)\n\n## 1. Getting performance tips\n\n- __Get instant performance tips about your module. All problems described by comments\nwill be shown by `torchfunc.performance.tips`:__\n\n```python\nclass Model(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.convolution = torch.nn.Sequential(\n            torch.nn.Conv2d(1, 32, 3),\n            torch.nn.ReLU(inplace=True),  # Inplace may harm kernel fusion\n            torch.nn.Conv2d(32, 128, 3, groups=32),  # Depthwise is slower in PyTorch\n            torch.nn.ReLU(inplace=True),  # Same as before\n            torch.nn.Conv2d(128, 250, 3),  # Wrong output size for TensorCores\n        )\n\n        self.classifier = torch.nn.Sequential(\n            torch.nn.Linear(250, 64),  # Wrong input size for TensorCores\n            torch.nn.ReLU(),  # Fine, no info about this layer\n            torch.nn.Linear(64, 10),  # Wrong output size for TensorCores\n        )\n\n    def forward(self, inputs):\n        convolved = torch.nn.AdaptiveAvgPool2d(1)(self.convolution(inputs)).flatten()\n        return self.classifier(convolved)\n\n# All you have to do\nprint(torchfunc.performance.tips(Model()))\n```\n\n## 2. Seeding, weight freezing and others\n\n- __Seed globaly (including `numpy` and `cuda`), freeze weights, check inference time and model size:__\n\n```python\n# Inb4 MNIST, you can use any module with those functions\nmodel = torch.nn.Linear(784, 10)\ntorchfunc.seed(0)\nfrozen = torchfunc.module.freeze(model, bias=False)\n\nwith torchfunc.Timer() as timer:\n  frozen(torch.randn(32, 784)\n  print(timer.checkpoint()) # Time since the beginning\n  frozen(torch.randn(128, 784)\n  print(timer.checkpoint()) # Since last checkpoint\n\nprint(f\"Overall time {timer}; Model size: {torchfunc.sizeof(frozen)}\")\n```\n\n## 3. Record `torch.nn.Module` internal state\n\n- __Record and sum per-layer activation statistics as data passes through network:__\n\n```python\n# Still MNIST but any module can be put in it's place\nmodel = torch.nn.Sequential(\n    torch.nn.Linear(784, 100),\n    torch.nn.ReLU(),\n    torch.nn.Linear(100, 50),\n    torch.nn.ReLU(),\n    torch.nn.Linear(50, 10),\n)\n# Recorder which sums all inputs to layers\nrecorder = torchfunc.hooks.recorders.ForwardPre(reduction=lambda x, y: x+y)\n# Record only for torch.nn.Linear\nrecorder.children(model, types=(torch.nn.Linear,))\n# Train your network normally (or pass data through it)\n...\n# Activations of all neurons of first layer!\nprint(recorder[1]) # You can also post-process this data easily with apply\n```\n\nFor other examples (and how to use condition), see [documentation](https://szymonmaszke.github.io/torchfunc/)\n\n# :wrench: Installation\n\n## :snake: [pip](\u003chttps://pypi.org/project/torchfunc/\u003e)\n\n### Latest release:\n\n```shell\npip install --user torchfunc\n```\n\n### Nightly:\n\n```shell\npip install --user torchfunc-nightly\n```\n\n## :whale2: [Docker](https://hub.docker.com/r/szymonmaszke/torchfunc)\n\n__CPU standalone__ and various versions of __GPU enabled__ images are available\nat [dockerhub](https://hub.docker.com/r/szymonmaszke/torchfunc/tags).\n\nFor CPU quickstart, issue:\n\n```shell\ndocker pull szymonmaszke/torchfunc:18.04\n```\n\nNightly builds are also available, just prefix tag with `nightly_`. If you are going for `GPU` image make sure you have\n[nvidia/docker](https://github.com/NVIDIA/nvidia-docker) installed and it's runtime set.\n\n# :question: Contributing\n\nIf you find any issue or you think some functionality may be useful to others and fits this library, please [open new Issue](https://help.github.com/en/articles/creating-an-issue) or [create Pull Request](https://help.github.com/en/articles/creating-a-pull-request-from-a-fork).\n\nTo get an overview of things one can do to help this project, see [Roadmap](https://github.com/szymonmaszke/torchfunc/blob/master/ROADMAP.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszymonmaszke%2Ftorchfunc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszymonmaszke%2Ftorchfunc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszymonmaszke%2Ftorchfunc/lists"}