statictorch


Namestatictorch JSON
Version 0.0.4 PyPI version JSON
download
home_pageNone
SummaryProvides type annotations for torch tensors.
upload_time2025-02-22 06:17:23
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/b3/de/6d4edb346febe7044a6c8960db95536091f3346e54edeb1a31fbe4d2c8d7/statictorch-0.0.4.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.4",
    "project_urls": {
        "Repository": "https://github.com/yueyinqiu/statictorch.git"
    },
    "split_keywords": [
        "torch",
        " pytorch",
        " typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f8c135547f3014af7bdea22bd0a1349cb0150de8e917a05efe93468477a62a19",
                "md5": "a08de35ba303b74e3fb71ca51edcefef",
                "sha256": "bf9ec3a5ea642c71e2133465fe6e65a4bd417dff01397e6cfdfdc14c38885914"
            },
            "downloads": -1,
            "filename": "statictorch-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a08de35ba303b74e3fb71ca51edcefef",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4184,
            "upload_time": "2025-02-22T06:17:20",
            "upload_time_iso_8601": "2025-02-22T06:17:20.653240Z",
            "url": "https://files.pythonhosted.org/packages/f8/c1/35547f3014af7bdea22bd0a1349cb0150de8e917a05efe93468477a62a19/statictorch-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3de6d4edb346febe7044a6c8960db95536091f3346e54edeb1a31fbe4d2c8d7",
                "md5": "f334597fc81012aed7c17f3d889b32a8",
                "sha256": "8f7a054c46803ddbc80a7c86f20733defb6e1101bbed808a23a12730d5a22fd7"
            },
            "downloads": -1,
            "filename": "statictorch-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "f334597fc81012aed7c17f3d889b32a8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3440,
            "upload_time": "2025-02-22T06:17:23",
            "upload_time_iso_8601": "2025-02-22T06:17:23.033735Z",
            "url": "https://files.pythonhosted.org/packages/b3/de/6d4edb346febe7044a6c8960db95536091f3346e54edeb1a31fbe4d2c8d7/statictorch-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-22 06:17:23",
    "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.45929s