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({
    'x': {
        'b': torch.as_tensor([[0, -2, 6], [5, 2, 2]], dtype=torch.long)
    },
    'a':
    torch.as_tensor([[-1, 4, 5, 5], [8, 7, 6, 0], [-3, -4, 5, 0]],
                    dtype=torch.long)
})
t_shape = Size({'x': {'b': torch.Size([2, 3])}, 'a': torch.Size([3, 4])})
t_tensor = Tensor({
    'b':
    torch.as_tensor([[
        0.9281905293464661, 0.2766069769859314, 0.7084696888923645,
        0.8880126476287842
    ],
                     [
                         -0.22929298877716064, -1.8321301937103271,
                         -1.3588366508483887, 1.2337677478790283
                     ],
                     [
                         1.296514630317688, 0.9464328289031982,
                         0.6441329121589661, 0.21820521354675293
                     ]],
                    dtype=torch.float32),
    'a':
    torch.as_tensor(
        [[-0.1312296837568283, -0.06642651557922363, -1.8783984184265137],
         [0.43429601192474365, 0.3594830632209778, -1.3621876239776611]],
        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