detach_

Documentation

treetensor.torch.detach_(input)[source]

In-place version of treetensor.torch.detach().

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> tt = ttorch.randn({
...     'a': (2, 3),
...     'b': {'x': (3, 4)},
... })
>>> tt.requires_grad_(True)
>>> tt
<Tensor 0x7f588133aba8>
├── a --> tensor([[-0.1631, -1.1573,  1.3109],
│                 [ 2.7277, -0.0745, -1.2577]], requires_grad=True)
└── b --> <Tensor 0x7f588133ab00>
    └── x --> tensor([[-0.5876,  0.9836,  1.9584, -0.1513],
                      [ 0.5369, -1.3986,  0.9361,  0.6765],
                      [ 0.6465, -0.2212,  1.5499, -1.2156]], requires_grad=True)

>>> ttorch.detach_(tt)
<Tensor 0x7f588133aba8>
├── a --> tensor([[-0.1631, -1.1573,  1.3109],
│                 [ 2.7277, -0.0745, -1.2577]])
└── b --> <Tensor 0x7f588133ab00>
    └── x --> tensor([[-0.5876,  0.9836,  1.9584, -0.1513],
                      [ 0.5369, -1.3986,  0.9361,  0.6765],
                      [ 0.6465, -0.2212,  1.5499, -1.2156]])

Torch Version Related

This documentation is based on torch.detach_ in torch v2.4.1+cu121. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'