{"id":17086093,"url":"https://github.com/kach/torchsaber","last_synced_at":"2025-04-12T21:37:18.819Z","repository":{"id":57476541,"uuid":"290129513","full_name":"kach/torchsaber","owner":"kach","description":"Elegant dimensions for a more civilized age","archived":false,"fork":false,"pushed_at":"2020-08-25T06:13:08.000Z","size":5,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-26T15:48:12.979Z","etag":null,"topics":["pytorch","tensors","torch"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/torchsaber/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kach.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-25T06:12:28.000Z","updated_at":"2022-07-12T03:34:09.000Z","dependencies_parsed_at":"2022-09-12T14:34:03.820Z","dependency_job_id":null,"html_url":"https://github.com/kach/torchsaber","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/kach%2Ftorchsaber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kach%2Ftorchsaber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kach%2Ftorchsaber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kach%2Ftorchsaber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kach","download_url":"https://codeload.github.com/kach/torchsaber/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637773,"owners_count":21137537,"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":["pytorch","tensors","torch"],"created_at":"2024-10-14T13:27:14.726Z","updated_at":"2025-04-12T21:37:18.798Z","avatar_url":"https://github.com/kach.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [torchsaber: Elegant Dimensions for a more Civilized Age](https://github.com/kach/torchsaber)\n\n\u003e It is a period of civil war. Rebel spaceships have won their first victory\n\u003e against the awesome `RuntimeError` ([experimental support for Named Tensors\n\u003e in\n\u003e PyTorch](https://pytorch.org/tutorials/intermediate/named_tensor_tutorial.html)).\n\n## Motivation\n\nHow often have you written or seen code like this?\n\n```python\nimages = torch.randn(10, 3, 64, 64)\nflipped = images.transpose(2, 3)\ncropped = flipped[:, :, x:x+32, y:y+32]\nflatten = cropped.view(-1, 32 * 32)\ngray = flatten.sum(dim=2) / 3.\n```\n\nor gotten a mysterious error like\n\n```python\nRuntimeError: The size of tensor x (2) must match the size of tensor y (4) at non-singleton dimension 2\n```\n\nI don't care what universe you're from, that's got to hurt your eyes. What do\nall those numbers mean? What order are the dimensions in by the end? Is\n`dim=2` the right summation to average the channels? Who knows? Unnamed\ndimensions lead to anger; anger leads to bugs; bugs lead to suffering.\n\n**But, a ray of hope!** PyTorch has\n[released](https://pytorch.org/tutorials/intermediate/named_tensor_tutorial.html)\nexperimental support for _named tensors:_ that is, tensors whose dimensions\nhave names rather than simply numeric indices. It's wonderful news, but it\nstill has some rough edges. For example, you have to refer to names using\nhardcoded string literals everywhere; a typo (e.g. `'hieght'`) can break your\ncode in unexpected ways that aren't caught at runtime.\n\n`torchsaber` is a **minimal syntactic sugar for named tensors**. Its goal is to\ngive you power (unlimited power!) by allowing all manipulation of dimensions to\nbe done _by name_ rather than by numeric indices or hardcoded string literals.\nDimensions and their manipulations are\n**[first-class](https://en.wikipedia.org/wiki/First-class_citizen) objects**\nthat interface cleanly with PyTorch's user-facing API. For example, the above\ncode snippet becomes:\n\n```python\nfrom torchsaber import dims\nbatch, channel, height, width, features =\n\tdims('batch', 'channel', 'height', 'width', 'features')\n\nimages = torch.randn | batch(10) + channel(3) + height(64) + width(64)\nflipped = images.permute(~width, ~height)\ncropped = flipped | height[:32] + width[:32]\nflatten = cropped.flatten([~height, ~width], ~features)\ngray = flatten.sum(dim=~channel) / 3.\n```\n\nBy \"minimal\" I mean the entire implementation is around 100 lines of code.\n\n## Can I use it?\n\nSure! The easiest way to learn is to read the big comment at the top of\n`torchsaber.py`. It's a literate doctest! Then `pip install torch torchvision\ntorchsaber` and `from torchsaber import dims` and enjoy. torchsaber tries to be\ncompatible with [the named tensor\ndocs](https://pytorch.org/docs/stable/named_tensor.html#named-tensors) and\nshould work with operators\n[supported](https://pytorch.org/docs/stable/name_inference.html#named-tensors-operator-coverage)\nby named tensors.\n\nHowever, because named tensors are experimental, so is torchsaber. The _real_\ngoal of the project is to provoke some discussion around human-friendly designs\nfor the tensor programs of the future.\n\n## (Some!) references\n\nHere's a non-exhaustive list of prior work (many of these have their own\nbibliographies you can follow)…\n\n- [Tensors Considered Harmful](http://nlp.seas.harvard.edu/NamedTensor) and [Part 2](http://nlp.seas.harvard.edu/NamedTensor2), which led to named tensor support in PyTorch.\n- [An older PyTorch named tensor proposal](https://github.com/pytorch/pytorch/issues/4164)\n- [_Typesafe Abstractions for Tensor Operations_ (SCALA 2017)](https://arxiv.org/pdf/1710.06892.pdf)\n- Other projects with similar goals:\n  - [datarray](https://github.com/BIDS/datarray)\n  - [xarray](http://xarray.pydata.org/en/stable/)\n  - [tsalib](https://github.com/ofnote/tsalib)\n\n## _More_ to say, have you?\n\nOpen an issue, file a PR, send me an email!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkach%2Ftorchsaber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkach%2Ftorchsaber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkach%2Ftorchsaber/lists"}