statictorch


Namestatictorch JSON
Version 0.0.3 PyPI version JSON
download
home_pageNone
SummaryProvides type annotations for torch tensors.
upload_time2025-01-18 06:40:40
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseMIT License
keywords torch pytorch typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Basic usages (only specify the count of dimensions):

```python
import torch
from statictorch import *

def f(x: Tensor2d):
    pass

f(torch.zeros([2, 3]))  # No runtime error, but static type checkers might say: "Tensor" is not assignable to "Tensor2d"
f(Tensor2d(torch.zeros([2, 3])))  # It's ok.
```

Note that `TensorNd()` directly return the given tensor. So at runtime you can't distinguish `x` and `TensorNd(x)`, and `isinstance(TensorNd(x), TensorNd)` will return `False`.

`TensorNd` is defined as generic classes, so that you could add a dimension descriptor for them:

```python
from statictorch import *

class Batch(TensorDimensionDescriptor):
    pass

class Channel(TensorDimensionDescriptor):
    pass

class Sample(TensorDimensionDescriptor):
    pass

def train_on(x: Tensor3d[Batch, Channel, Sample], y: Tensor3d[Channel, Batch, Sample]):
    ...

def load_data() -> tuple[Tensor3d[Batch, Channel, Sample], Tensor3d[Batch, Channel, Sample]]:
    ...

data_x, data_y = load_data()
train_on(data_x, data_y)  # Pylance: Argument of type "Tensor3d[Batch, Channel, Sample]" cannot be assigned to parameter "y" of type "Tensor3d[Channel, Batch, Sample]" in function "train_on"

# To solve the problem:
data_y_transposed = data_y.transpose(1, 0)
train_on(data_x, Tensor3d(data_y_transposed))

# Of course in some cases you want to force passing data_y, just simply cheating the type checker with:
train_on(data_x, Tensor3d(data_y))
```

`typing.cast` is also a good idea, especially when you want to call functions like `torch.stack` on a `list[TensorNd]`:

```python
import statictorch
import typing
from torch import Tensor

def work_on(tensors: list[Tensor]):
    ...

my_tensors: list[statictorch.Tensor0d] = []
work_on(my_tensors)  # Pylance: Argument of type "list[Tensor0d]" cannot be assigned to parameter "tensors" of type "list[Tensor]" in function "work_on"

# To solve the problem:
work_on(typing.cast(list[Tensor], my_tensors))

# If you find typing.cast too long:
work_on(statictorch.anify(my_tensors))
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "statictorch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "torch, pytorch, typing",
    "author": null,
    "author_email": "yueyinqiu <yueyinqiu@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/e0/9f9d77d4eeb118eb88a58dbd952aab0eac59c4ca68c4339de2ed53404440/statictorch-0.0.3.tar.gz",
    "platform": null,
    "description": "Basic usages (only specify the count of dimensions):\r\n\r\n```python\r\nimport torch\r\nfrom statictorch import *\r\n\r\ndef f(x: Tensor2d):\r\n    pass\r\n\r\nf(torch.zeros([2, 3]))  # No runtime error, but static type checkers might say: \"Tensor\" is not assignable to \"Tensor2d\"\r\nf(Tensor2d(torch.zeros([2, 3])))  # It's ok.\r\n```\r\n\r\nNote that `TensorNd()` directly return the given tensor. So at runtime you can't distinguish `x` and `TensorNd(x)`, and `isinstance(TensorNd(x), TensorNd)` will return `False`.\r\n\r\n`TensorNd` is defined as generic classes, so that you could add a dimension descriptor for them:\r\n\r\n```python\r\nfrom statictorch import *\r\n\r\nclass Batch(TensorDimensionDescriptor):\r\n    pass\r\n\r\nclass Channel(TensorDimensionDescriptor):\r\n    pass\r\n\r\nclass Sample(TensorDimensionDescriptor):\r\n    pass\r\n\r\ndef train_on(x: Tensor3d[Batch, Channel, Sample], y: Tensor3d[Channel, Batch, Sample]):\r\n    ...\r\n\r\ndef load_data() -> tuple[Tensor3d[Batch, Channel, Sample], Tensor3d[Batch, Channel, Sample]]:\r\n    ...\r\n\r\ndata_x, data_y = load_data()\r\ntrain_on(data_x, data_y)  # Pylance: Argument of type \"Tensor3d[Batch, Channel, Sample]\" cannot be assigned to parameter \"y\" of type \"Tensor3d[Channel, Batch, Sample]\" in function \"train_on\"\r\n\r\n# To solve the problem:\r\ndata_y_transposed = data_y.transpose(1, 0)\r\ntrain_on(data_x, Tensor3d(data_y_transposed))\r\n\r\n# Of course in some cases you want to force passing data_y, just simply cheating the type checker with:\r\ntrain_on(data_x, Tensor3d(data_y))\r\n```\r\n\r\n`typing.cast` is also a good idea, especially when you want to call functions like `torch.stack` on a `list[TensorNd]`:\r\n\r\n```python\r\nimport statictorch\r\nimport typing\r\nfrom torch import Tensor\r\n\r\ndef work_on(tensors: list[Tensor]):\r\n    ...\r\n\r\nmy_tensors: list[statictorch.Tensor0d] = []\r\nwork_on(my_tensors)  # Pylance: Argument of type \"list[Tensor0d]\" cannot be assigned to parameter \"tensors\" of type \"list[Tensor]\" in function \"work_on\"\r\n\r\n# To solve the problem:\r\nwork_on(typing.cast(list[Tensor], my_tensors))\r\n\r\n# If you find typing.cast too long:\r\nwork_on(statictorch.anify(my_tensors))\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Provides type annotations for torch tensors.",
    "version": "0.0.3",
    "project_urls": {
        "Repository": "https://github.com/yueyinqiu/statictorch.git"
    },
    "split_keywords": [
        "torch",
        " pytorch",
        " typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7a04e929bd864a53cea4e79a6158f5167887c2ebbc9059a67f976105e658e4b1",
                "md5": "2ad8dbb82c31503e219b83ab6aeab87a",
                "sha256": "5b03277260c1008546b6f375d337160e128cd5e31f92c08a5d4ad4dedf066b85"
            },
            "downloads": -1,
            "filename": "statictorch-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2ad8dbb82c31503e219b83ab6aeab87a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 3507,
            "upload_time": "2025-01-18T06:40:37",
            "upload_time_iso_8601": "2025-01-18T06:40:37.957527Z",
            "url": "https://files.pythonhosted.org/packages/7a/04/e929bd864a53cea4e79a6158f5167887c2ebbc9059a67f976105e658e4b1/statictorch-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ce09f9d77d4eeb118eb88a58dbd952aab0eac59c4ca68c4339de2ed53404440",
                "md5": "e1aa45aeec67656d0941307b687b73a5",
                "sha256": "8d8a4a6c0e4cf8bc85c7de7debea0e34d3952fbc3c204b83fd663dfa87f847ec"
            },
            "downloads": -1,
            "filename": "statictorch-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "e1aa45aeec67656d0941307b687b73a5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3373,
            "upload_time": "2025-01-18T06:40:40",
            "upload_time_iso_8601": "2025-01-18T06:40:40.493425Z",
            "url": "https://files.pythonhosted.org/packages/8c/e0/9f9d77d4eeb118eb88a58dbd952aab0eac59c4ca68c4339de2ed53404440/statictorch-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-18 06:40:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yueyinqiu",
    "github_project": "statictorch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "statictorch"
}
        
Elapsed time: 0.44940s