{"id":19295353,"url":"https://github.com/vict0rsch/torch_simple_timing","last_synced_at":"2026-05-06T13:32:45.585Z","repository":{"id":152786279,"uuid":"624120251","full_name":"vict0rsch/torch_simple_timing","owner":"vict0rsch","description":"A simple package to time CPU/GPU/Multi-GPU ops","archived":false,"fork":false,"pushed_at":"2023-05-17T13:42:13.000Z","size":97,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-05T21:23:20.476Z","etag":null,"topics":["pip","python","pytorch","timing"],"latest_commit_sha":null,"homepage":"https://torch-simple-timing.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/vict0rsch.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,"publiccode":null,"codemeta":null}},"created_at":"2023-04-05T19:37:36.000Z","updated_at":"2024-10-29T12:52:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"af14872d-2398-4cc0-9eea-e8584ffecf68","html_url":"https://github.com/vict0rsch/torch_simple_timing","commit_stats":{"total_commits":47,"total_committers":1,"mean_commits":47.0,"dds":0.0,"last_synced_commit":"2e42c45f1650dd24bdb092e43441259f69e58eba"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vict0rsch%2Ftorch_simple_timing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vict0rsch%2Ftorch_simple_timing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vict0rsch%2Ftorch_simple_timing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vict0rsch%2Ftorch_simple_timing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vict0rsch","download_url":"https://codeload.github.com/vict0rsch/torch_simple_timing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240400290,"owners_count":19795330,"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":["pip","python","pytorch","timing"],"created_at":"2024-11-09T22:42:11.750Z","updated_at":"2026-05-06T13:32:40.566Z","avatar_url":"https://github.com/vict0rsch.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cstrong\u003e\u003ca href=\"https://github.com/vict0rsch/torch_simple_timing\" target=\"_blank\"\u003e💻\u0026nbsp;\u0026nbsp;Code\u003c/a\u003e\u003c/strong\u003e\n\u003cstrong\u003e\u0026nbsp;\u0026nbsp;•\u0026nbsp;\u0026nbsp;\u003c/strong\u003e\n\u003cstrong\u003e\u003ca href=\"https://torch-simple-timing.readthedocs.io/\" target=\"_blank\"\u003eDocs\u0026nbsp;\u0026nbsp;📑\u003c/a\u003e\u003c/strong\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca\u003e\n\t    \u003cimg src='https://img.shields.io/badge/python-3.8%2B-blue' alt='Python' /\u003e\n\t\u003c/a\u003e\n\t\u003ca href='https://torch-simple-timing.readthedocs.io/en/latest/?badge=latest'\u003e\n    \t\u003cimg src='https://readthedocs.org/projects/torch-simple-timing/badge/?version=latest' alt='Documentation Status' /\u003e\n\t\u003c/a\u003e\n    \u003ca href=\"https://github.com/psf/black\"\u003e\n\t    \u003cimg src='https://img.shields.io/badge/code%20style-black-black' /\u003e\n\t\u003c/a\u003e\n    \u003ca href=\"https://pytorch.org\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/PyTorch-%23EE4C2C.svg?logo=PyTorch\u0026logoColor=white\"/\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/torch-simple-timing/\"\u003e\n        \u003cimg src=\"https://badge.fury.io/py/torch_simple_timing.svg\" alt=\"PyPI version\" height=\"20\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\u003cbr/\u003e\n\n\n# Torch Simple Timing\n\nA simple yet versatile package to time CPU/GPU/Multi-GPU ops.\n\n1. \"*I want to time operations once*\"\n   1. That's what a `Clock` is for\n2. \"*I want to time the same operations multiple times*\"\n   1. That's what a `Timer` is for\n\nIn simple terms:\n\n* A `Clock` is an object (and context-manager) that will compute the ellapsed time between its `start()` (or `__enter__`) and `stop()` (or `__exit__`)\n* A `Timer` will internally manage clocks so that you can focus on readability and not data structures\n\n## Installation\n\n```\npip install torch_simple_timing\n```\n\n## How to use\n\n### A `Clock`\n\n```python\nfrom torch_simple_parsing import Clock\nimport torch\n\nt = torch.rand(2000, 2000)\ngpu = torch.cuda.is_available()\n\nwith Clock(gpu=gpu) as context_clock:\n    torch.inverse(t @ t.T)\n\nclock = Clock(gpu=gpu).start()\ntorch.inverse(t @ t.T)\nclock.stop()\n\nprint(context_clock.duration) # 0.29688501358032227\nprint(clock.duration)         # 0.292896032333374\n```\n\nMore examples, including bout how to easily share data structures using a `store` can be found in the [documentation](https://torch-simple-timing.readthedocs.io/en/latest/autoapi/torch_simple_timing/clock/index.html).\n\n### A `Timer`\n\n```python\nfrom torch_simple_timing import Timer\nimport torch\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nX = torch.rand(5000, 5000, device=device)\nY = torch.rand(5000, 100, device=device)\nmodel = torch.nn.Linear(5000, 100).to(device)\noptimizer = torch.optim.Adam(model.parameters())\n\ngpu = device.type == \"cuda\"\ntimer = Timer(gpu=gpu)\n\nfor epoch in range(10):\n    timer.clock(\"epoch\").start()\n    for b in range(50):\n        x = X[b*100: (b+1)*100]\n        y = Y[b*100: (b+1)*100]\n        optimizer.zero_grad()\n        with timer.clock(\"forward\", ignore=epoch\u003e0):\n            p = model(x)\n        loss = torch.nn.functional.cross_entropy(p, y)\n        with timer.clock(\"backward\", ignore=epoch\u003e0):\n            loss.backward()\n        optimizer.step()\n    timer.clock(\"epoch\").stop()\n\nstats = timer.stats()\n# use stats for display and/or logging\n# wandb.summary.update(stats)\nprint(timer.display(stats=stats, precision=5))\n```\n\n```\nepoch    : 0.25064 ± 0.02728 (n=10)\nforward  : 0.00226 ± 0.00526 (n=50)\nbackward : 0.00209 ± 0.00387 (n=50)\n```\n\n### A decorator\n\nYou can also use a decorator to time functions without much overhead in your code:\n\n```python\nfrom torch_simple_timing import timeit, get_global_timer, reset_global_timer\nimport torch\n\n# Use the function name as the timer name\n@timeit(gpu=True)\ndef train():\n    x = torch.rand(1000, 1000, device=\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    return torch.inverse(x @ x)\n\n# Use a custom name\n@timeit(\"test\")\ndef test_cpu():\n    return torch.inverse(torch.rand(1000, 1000) @ torch.rand(1000, 1000))\n\nif __name__ == \"__main__\":\n    for _ in range((epochs := 10)):\n        train()\n\n    test_cpu()\n\n    timer = get_global_timer()\n    print(timer.display())\n\n    reset_global_timer()\n```\n\nPrints:\n\n```text\ntrain : 0.045 ± 0.007 (n=10)\ntest  : 0.046         (n= 1)\n```\n\nBy default the `@timeit` decodrator takes at least a `name`, will use `gpu=False` and use the global timer (`torch_simple_timing.TIMER`). You can pass your own timer with `@timeit(name, timer=timer)`.\n\nSee [in the docs]([https://](https://torch-simple-timing.readthedocs.io/en/latest/autoapi/torch_simple_timing/index.html)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvict0rsch%2Ftorch_simple_timing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvict0rsch%2Ftorch_simple_timing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvict0rsch%2Ftorch_simple_timing/lists"}