A flexible, generalized tree-based data structure.
Project description
treevalue
TreeValue
is a generalized tree-based data structure mainly developed by OpenDILab Contributors.
Almost all the operation can be supported in form of trees in a convenient way to simplify the structure processing when the calculation is tree-based.
Installation
You can simply install it with pip
command line from the official PyPI site.
pip install treevalue
For more information about installation, you can refer to Installation.
Documentation
The detailed documentation are hosted on https://opendilab.github.io/treevalue.
Only english version is provided now, the chinese documentation is still under development.
Quick Start
You can easily create a tree value object based on FastTreeValue
.
from treevalue import FastTreeValue
if __name__ == '__main__':
t = FastTreeValue({
'a': 1,
'b': 2.3,
'x': {
'c': 'str',
'd': [1, 2, None],
'e': b'bytes',
}
})
print(t)
The result should be
<FastTreeValue 0x7f6c7df00160 keys: ['a', 'b', 'x']>
├── 'a' --> 1
├── 'b' --> 2.3
└── 'x' --> <FastTreeValue 0x7f6c81150860 keys: ['c', 'd', 'e']>
├── 'c' --> 'str'
├── 'd' --> [1, 2, None]
└── 'e' --> b'bytes'
And t
is structure should be like this
Not only a visible tree structure, but abundant operation supports is provided.
You can just put objects (such as torch.Tensor
, or any other types) here and just
call their methods, like this
import torch
from treevalue import FastTreeValue
t = FastTreeValue({
'a': torch.rand(2, 5),
'x': {
'c': torch.rand(3, 4),
}
})
print(t)
# <FastTreeValue 0x7f8c069346a0>
# ├── a --> tensor([[0.3606, 0.2583, 0.3843, 0.8611, 0.5130],
# │ [0.0717, 0.1370, 0.1724, 0.7627, 0.7871]])
# └── x --> <FastTreeValue 0x7f8ba6130f40>
# └── c --> tensor([[0.2320, 0.6050, 0.6844, 0.3609],
# [0.0084, 0.0816, 0.8740, 0.3773],
# [0.6523, 0.4417, 0.6413, 0.8965]])
print(t.shape) # property access
# <FastTreeValue 0x7f8c06934ac0>
# ├── a --> torch.Size([2, 5])
# └── x --> <FastTreeValue 0x7f8c069346d0>
# └── c --> torch.Size([3, 4])
print(t.sin()) # method call
# <FastTreeValue 0x7f8c06934b80>
# ├── a --> tensor([[0.3528, 0.2555, 0.3749, 0.7586, 0.4908],
# │ [0.0716, 0.1365, 0.1715, 0.6909, 0.7083]])
# └── x --> <FastTreeValue 0x7f8c06934b20>
# └── c --> tensor([[0.2300, 0.5688, 0.6322, 0.3531],
# [0.0084, 0.0816, 0.7669, 0.3684],
# [0.6070, 0.4275, 0.5982, 0.7812]])
print(t.reshape((2, -1))) # method with arguments
# <FastTreeValue 0x7f8c06934b80>
# ├── a --> tensor([[0.3606, 0.2583, 0.3843, 0.8611, 0.5130],
# │ [0.0717, 0.1370, 0.1724, 0.7627, 0.7871]])
# └── x --> <FastTreeValue 0x7f8c06934b20>
# └── c --> tensor([[0.2320, 0.6050, 0.6844, 0.3609, 0.0084, 0.0816],
# [0.8740, 0.3773, 0.6523, 0.4417, 0.6413, 0.8965]])
print(t[:, 1:-1]) # index operator
# <FastTreeValue 0x7f8ba5c8eca0>
# ├── a --> tensor([[0.2583, 0.3843, 0.8611],
# │ [0.1370, 0.1724, 0.7627]])
# └── x --> <FastTreeValue 0x7f8ba5c8ebe0>
# └── c --> tensor([[0.6050, 0.6844],
# [0.0816, 0.8740],
# [0.4417, 0.6413]])
print(1 + (t - 0.8) ** 2 * 1.5) # math operators
# <FastTreeValue 0x7fdfa5836b80>
# ├── a --> tensor([[1.6076, 1.0048, 1.0541, 1.3524, 1.0015],
# │ [1.0413, 1.8352, 1.2328, 1.7904, 1.0088]])
# └── x --> <FastTreeValue 0x7fdfa5836880>
# └── c --> tensor([[1.1550, 1.0963, 1.3555, 1.2030],
# [1.0575, 1.4045, 1.0041, 1.0638],
# [1.0782, 1.0037, 1.5075, 1.0658]])
For more quick start explanation and further usage, take a look at:
Speed Performance
Here is the speed performance of all the operations in FastTreeValue
flatten | flatten(with path) | map | map(with path) | |
---|---|---|---|---|
treevalue | --- | 511 ns ± 6.92 ns | 3.16 µs ± 42.8 ns | 1.58 µs ± 30 ns |
dm-tree | 830 ns ± 8.53 ns | 11.9 µs ± 358 ns | 13.3 µs ± 87.2 ns | 62.9 µs ± 2.26 µs |
get | set | init | deepcopy | stack | cat | split | |
---|---|---|---|---|---|---|---|
treevalue | 51.6 ns ± 0.609 ns | 64.4 ns ± 0.564 ns | 750 ns ± 14.2 ns | 88.9 µs ± 887 ns | 50.2 µs ± 771 ns | 40.3 µs ± 1.08 µs | 62 µs ± 1.2 µs |
tianshou Batch | 43.2 ns ± 0.698 ns | 396 ns ± 8.99 ns | 11.1 µs ± 277 ns | 89 µs ± 1.42 µs | 119 µs ± 1.1 µs | 194 µs ± 1.81 µs | 653 µs ± 17.8 µs |
And this is the comparasion between tianshou Batch and us, with cat
, stack
and split
operations
Test benchmark code can be found here:
Contribution
We appreciate all contributions to improve treevalue, both logic and system designs. Please refer to CONTRIBUTING.md for more guides.
And users can join our slack communication channel, or contact the core developer HansBug for more detailed discussion.
License
treevalue
released under the Apache 2.0 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for treevalue-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe5b8090a208315eebc28ed975dbf76f33e8a18f402d1f070dd9c001ace9f3dc |
|
MD5 | 575950b1e328606882ffb7e85b8fd49e |
|
BLAKE2b-256 | 20b2403e86ef72acf5cd3a5c76c37a8392162e991d62949a3e576d915476af22 |
Hashes for treevalue-1.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5e2f93c4e220d8443953219b9d8a74263132d9dd45d927bd9ca74f2c178cc2b |
|
MD5 | 8187725e7c05ae319a1c69f9e1172d94 |
|
BLAKE2b-256 | 0631c9bb7580ec87bed12ea64d943810d2332c66d4ee00a7c4d45929211aef03 |
Hashes for treevalue-1.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a0781319f44281aa5acb3d48a7740b09c470a7d544052e31033e7d71d39cf6c |
|
MD5 | ad17c21543d475192172228370e52ccc |
|
BLAKE2b-256 | 91a52c3a3ba0b52890c19bcd3ecac754a150dae31a28b46d2b6b1e46bb06405b |
Hashes for treevalue-1.1.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f35b89052bb086458f25c03f3cc8435a13741c3d8c27efd39eb161fb9ba75038 |
|
MD5 | 9589bf00789b106bfe70bf626e94434f |
|
BLAKE2b-256 | 45dd007ce882ec12dea237ebbb223261952a88886f875d9b350995f99b89245a |