Comparison Between TreeValue and Tianshou Batch

In this section, we will take a look at the feature and performance of the Tianshou Batch library, which is developed by Tsinghua Machine Learning Group.

Before starting the comparison, let us define some thing.

[1]:
import torch

_TREE_DATA_1 = {'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}}
_TREE_DATA_2 = {
    'a': torch.randn(2, 3),
    'x': {
        'c': torch.randn(3, 4)
    },
}
_TREE_DATA_3 = {
    'obs': torch.randn(4, 84, 84),
    'action': torch.randint(0, 6, size=(1,)),
    'reward': torch.rand(1),
}

Read and Write Operation

Reading and writing are the two most common operations in the tree data structure based on the data model (TreeValue and Tianshou Batch both belong to this type), so this section will compare the reading and writing performance of these two libraries.

TreeValue’s Get and Set

[2]:
from treevalue import FastTreeValue

t = FastTreeValue(_TREE_DATA_2)
[3]:
t
[3]:
<FastTreeValue 0x7f43d4518e80>
├── 'a' --> tensor([[ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458]])
└── 'x' --> <FastTreeValue 0x7f43d4518e50>
    └── 'c' --> tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634]])
[4]:
t.a
[4]:
tensor([[ 1.4829, -0.9074,  0.4875],
        [-1.8786, -0.7687,  1.2458]])
[5]:
%timeit t.a
92.8 ns ± 0.181 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
[6]:
new_value = torch.randn(2, 3)
t.a = new_value

t
[6]:
<FastTreeValue 0x7f43d4518e80>
├── 'a' --> tensor([[ 0.8185, -0.6265,  0.8451],
│                   [-1.2610, -0.8764,  1.9666]])
└── 'x' --> <FastTreeValue 0x7f43d4518e50>
    └── 'c' --> tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634]])
[7]:
%timeit t.a = new_value
104 ns ± 0.187 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Tianshou Batch’s Get and Set

[8]:
from tianshou.data import Batch

b = Batch(**_TREE_DATA_2)
[9]:
b
[9]:
Batch(
    a: tensor([[ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458]]),
    x: Batch(
           c: tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634]]),
       ),
)
[10]:
b.a
[10]:
tensor([[ 1.4829, -0.9074,  0.4875],
        [-1.8786, -0.7687,  1.2458]])
[11]:
%timeit b.a
84.2 ns ± 1.62 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
[12]:
new_value = torch.randn(2, 3)
b.a = new_value

b
[12]:
Batch(
    a: tensor([[-0.3528,  0.7515,  0.6936],
               [ 0.9199,  0.0952,  1.2813]]),
    x: Batch(
           c: tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634]]),
       ),
)
[13]:
%timeit b.a = new_value
661 ns ± 3.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Initialization

TreeValue’s Initialization

[14]:
%timeit FastTreeValue(_TREE_DATA_1)
1.15 µs ± 6.89 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Tianshou Batch’s Initialization

[15]:
%timeit Batch(**_TREE_DATA_1)
12.5 µs ± 78 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Deep Copy Operation

[16]:
import copy

Deep Copy of TreeValue

[17]:
t3 = FastTreeValue(_TREE_DATA_3)
%timeit copy.deepcopy(t3)
175 µs ± 1.63 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

Deep Copy of Tianshou Batch

[18]:
b3 = Batch(**_TREE_DATA_3)
%timeit copy.deepcopy(b3)
177 µs ± 1.49 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

Stack, Concat and Split Operation

Performance of TreeValue

[19]:
trees = [FastTreeValue(_TREE_DATA_2) for _ in range(8)]
[20]:
t_stack = FastTreeValue.func(subside=True)(torch.stack)

t_stack(trees)
[20]:
<FastTreeValue 0x7f42f45a6ee0>
├── 'a' --> tensor([[[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]],
│
│                   [[ 1.4829, -0.9074,  0.4875],
│                    [-1.8786, -0.7687,  1.2458]]])
└── 'x' --> <FastTreeValue 0x7f42f45a6460>
    └── 'c' --> tensor([[[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]],

                        [[-1.1936, -0.8915, -1.4743, -0.6240],
                         [-0.5593,  0.2848,  0.0762,  0.4769],
                         [ 1.1814,  0.3933, -0.3835, -0.1634]]])
[21]:
%timeit t_stack(trees)
39.3 µs ± 185 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
[22]:
t_cat = FastTreeValue.func(subside=True)(torch.cat)

t_cat(trees)
[22]:
<FastTreeValue 0x7f42f45a6b50>
├── 'a' --> tensor([[ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458],
│                   [ 1.4829, -0.9074,  0.4875],
│                   [-1.8786, -0.7687,  1.2458]])
└── 'x' --> <FastTreeValue 0x7f42f45a6340>
    └── 'c' --> tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634],
                        [-1.1936, -0.8915, -1.4743, -0.6240],
                        [-0.5593,  0.2848,  0.0762,  0.4769],
                        [ 1.1814,  0.3933, -0.3835, -0.1634]])
[23]:
%timeit t_cat(trees)
37 µs ± 502 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
[24]:
t_split = FastTreeValue.func(rise=True)(torch.split)
tree = FastTreeValue({
    'obs': torch.randn(8, 4, 84, 84),
    'action': torch.randint(0, 6, size=(8, 1,)),
    'reward': torch.rand(8, 1),
})

%timeit t_split(tree, 1)
74.9 µs ± 682 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

Performance of Tianshou Batch

[25]:
batches = [Batch(**_TREE_DATA_2) for _ in range(8)]

Batch.stack(batches)
[25]:
Batch(
    x: Batch(
           c: tensor([[[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]],

                      [[-1.1936, -0.8915, -1.4743, -0.6240],
                       [-0.5593,  0.2848,  0.0762,  0.4769],
                       [ 1.1814,  0.3933, -0.3835, -0.1634]]]),
       ),
    a: tensor([[[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]],

               [[ 1.4829, -0.9074,  0.4875],
                [-1.8786, -0.7687,  1.2458]]]),
)
[26]:
%timeit Batch.stack(batches)
97.8 µs ± 1.75 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
[27]:
Batch.cat(batches)
[27]:
Batch(
    x: Batch(
           c: tensor([[-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634],
                      [-1.1936, -0.8915, -1.4743, -0.6240],
                      [-0.5593,  0.2848,  0.0762,  0.4769],
                      [ 1.1814,  0.3933, -0.3835, -0.1634]]),
       ),
    a: tensor([[ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458],
               [ 1.4829, -0.9074,  0.4875],
               [-1.8786, -0.7687,  1.2458]]),
)
[28]:
%timeit Batch.cat(batches)
176 µs ± 2.39 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
[29]:
batch = Batch({
    'obs': torch.randn(8, 4, 84, 84),
    'action': torch.randint(0, 6, size=(8, 1,)),
    'reward': torch.rand(8, 1)}
)

%timeit list(Batch.split(batch, 1, shuffle=False, merge_last=True))
493 µs ± 100 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
[ ]: