Plugins

Potc support

Potc is a package that can convert any object into executable source code. For DI-treetensor, potc can support the source code transformation of treevalue objects through the installation of additional plugins. So we can execute the following installation command

pip install DI-treetensor[potc]

After this installation, you will be able to directly convert tree-nested tensors to objects without any additional operations. Such as

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from potc import transvars

import treetensor.torch as ttorch

t_tensor = ttorch.randn({'a': (2, 3), 'b': (3, 4)})
t_i_tensor = ttorch.randint(-5, 10, {'a': (3, 4), 'x': {'b': (2, 3)}})
t_shape = t_i_tensor.shape

if __name__ == '__main__':
    _code = transvars(
        {
            't_tensor': t_tensor,
            't_i_tensor': t_i_tensor,
            't_shape': t_shape,
        },
        reformat='pep8'
    )
    print(_code)

The output should be

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import torch
from treetensor import Tensor
from treetensor.torch import Size

__all__ = ['t_i_tensor', 't_shape', 't_tensor']
t_i_tensor = Tensor({
    'a':
    torch.as_tensor([[-4, 0, -3, 3], [9, 8, 1, 4], [-1, 0, -1, 3]],
                    dtype=torch.long),
    'x': {
        'b': torch.as_tensor([[-4, -4, 7], [5, 6, 0]], dtype=torch.long)
    }
})
t_shape = Size({'a': torch.Size([3, 4]), 'x': {'b': torch.Size([2, 3])}})
t_tensor = Tensor({
    'a':
    torch.as_tensor(
        [[-0.7067719101905823, -1.103571891784668, -0.3790871798992157],
         [-0.9946219325065613, 0.5986604690551758, 1.0759562253952026]],
        dtype=torch.float32),
    'b':
    torch.as_tensor([[
        -0.16045930981636047, -0.4624665081501007, -2.0878164768218994,
        -0.345730185508728
    ],
                     [
                         -0.16158010065555573, 0.8994827270507812,
                         0.09723912179470062, 0.7104199528694153
                     ],
                     [
                         -1.7983828783035278, -0.9509936571121216,
                         0.6165590286254883, -0.21629543602466583
                     ]],
                    dtype=torch.float32)
})

Also, you can use the following CLI command to get the same output results as above.

potc export -v 'test_simple.t_*'

For further information, you can refer to