A flexible, generalized tree-based data structure.
Project description
TreeValue
is a generalized tree-based data structure mainly developed by OpenDILab Contributors.
Almost all the operations can be supported in the form of trees in a convenient way to simplify the structure processing when the calculation is tree-based.
Outline
Overview
When we build a complex nested structure, we need to model it as a tree structure, and the native list and dict in Python are often used to solve this problem. However, it takes a lot of codes and some complex and non-intuitive calculation logic, which is not easy to modify and extend related code and data, and parallelization is impossible.
Therefore, we need a kind of more proper data container, named TreeValue
. It is designed for solving the following problems:
- Ease of Use: When the existing operations are applied to tree structures such as dict, they will become completely unrecognizable, with really low readability and maintainability.
- Diversity of Data: In the tree structure operation, various abnormal conditions (structure mismatch, missing key-value, type mismatch, etc.) occur from time to time, and the code will be more complicated if it needs to be handled properly.
- Scalability and Parallelization: When any multivariate operation is performed, the calculation logic needs to be redesigned under the native Python code implementation, and the processing will be more complicated and confusing, and the code quality is difficult to control.
Getting Started
Prerequisite
treevalue
has been fully tested in the Linux, macOS and Windows environments and with multiple Python versions, and it works properly on all these platforms.
However, treevalue
currently does not support PyPy, so just pay attention to this when using it.
Installation
You can simply install it with pip
command line from the official PyPI site.
pip install treevalue
Or just from the source code on github
pip install git+https://github.com/opendilab/treevalue.git@main
For more information about installation, you can refer to the installation guide.
After this, you can check if the installation is processed properly with the following code
from treevalue import __version__
print('TreeValue version is', __version__)
Quick Usage
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]])
Tutorials
For more examples, explanations and further usages, take a look at:
External
We provide an official treevalue-based-wrapper for numpy and torch called DI-treetensor since the treevalue
is often used with libraries like numpy
and torch
. It will actually be helpful while working with AI fields.
Speed Performance
Here is the speed performance of all the operations in FastTreeValue
; the following table is the performance comparison result with dm-tree.
(In DM-Tree, the unflatten
operation is different from that in TreeValue, see: Comparison Between TreeValue and DM-Tree for more details.)
flatten | flatten(with path) | mapping | mapping(with path) | |
---|---|---|---|---|
treevalue | --- | 511 ns ± 6.92 ns | 3.16 µs ± 42.8 ns | 1.58 µs ± 30 ns |
flatten | flatten_with_path | map_structure | map_structure_with_path | |
dm-tree | 830 ns ± 8.53 ns | 11.9 µs ± 358 ns | 13.3 µs ± 87.2 ns | 62.9 µs ± 2.26 µs |
The following 2 tables are the performance comparison result with jax pytree.
mapping | mapping(with path) | flatten | unflatten | flatten_values | flatten_keys | |
---|---|---|---|---|---|---|
treevalue | 2.21 µs ± 32.2 ns | 2.16 µs ± 123 ns | 515 ns ± 7.53 ns | 601 ns ± 5.99 ns | 301 ns ± 12.9 ns | 451 ns ± 17.3 ns |
tree_map | (Not Implemented) | tree_flatten | tree_unflatten | tree_leaves | tree_structure | |
jax pytree | 4.67 µs ± 184 ns | --- | 1.29 µs ± 27.2 ns | 742 ns ± 5.82 ns | 1.29 µs ± 22 ns | 1.27 µs ± 16.5 ns |
flatten + all | flatten + reduce | flatten + reduce(with init) | rise(given structure) | rise(automatic structure) | |
---|---|---|---|---|---|
treevalue | 425 ns ± 9.33 ns | 702 ns ± 5.93 ns | 793 ns ± 13.4 ns | 9.14 µs ± 129 ns | 11.5 µs ± 182 ns |
tree_all | tree_reduce | tree_reduce(with init) | tree_transpose | (Not Implemented) | |
jax pytree | 1.47 µs ± 37 ns | 1.88 µs ± 27.2 ns | 1.91 µs ± 47.4 ns | 10 µs ± 117 ns | --- |
This is the comparison between dm-tree, jax-libtree and us, with flatten
and mapping
operations (lower value means less time cost and runs faster)
The following table is the performance comparison result with tianshou Batch.
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 comparison between Tianshou Batch and us, with cat
, stack
and split
operations (lower value means less time cost and runs faster)
Test benchmark code can be found here:
Change Log
Version History [click to expand]
- 2022-05-03 1.3.1: Change definition of getitem, setitem and delitem; add pop method for TreeValue class.
- 2022-03-15 1.3.0: Add getitem, setitem and delitem for adding, editing and removing items in TreeValue class.
- 2022-02-22 1.2.2: Optimize union function; add walk utility method.
- 2022-01-26 1.2.1: Update tree printing; add keys, values, items on TreeValue; add comparision to facebook nest library.
- 2022-01-04 1.2.0: Add flatten_values and flatten_keys; fix problem in mapping function; add support for potc.
- 2021-12-03 1.1.0: Add version information; fix bug of default value; add flatten and unflatten; optimization speed performance.
- 2021-10-24 1.0.0: Greatly optimize the speed performance using cython, overhead has been reduced to a negligible level.
Feedback and Contribute
Welcome to OpenDILab community - treevalue!
If you meet some problem or have some brilliant ideas, you can file an issue.
Scan the QR code and add us on Wechat:
Or just contact us with slack or email (opendilab.contact@gmail.com).
Please check Contributing Guidances.
Thanks to the following contributors!
Citation
@misc{treevalue,
title={{TreeValue} - Tree-Structure Computing Solution},
author={TreeValue Contributors},
publisher = {GitHub},
howpublished = {\url{https://github.com/opendilab/treevalue}},
year={2021},
}
License
treevalue
released under the Apache 2.0 license. See the LICENSE file for details.
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.4.10-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7af047e0ef84dc78fa7c01a6020341d0da8e8ea18c8014665d72389d567f001 |
|
MD5 | c237e0455e21d4686dc4ea9eae222b38 |
|
BLAKE2b-256 | e295e5c4b6fa1255c433207fdfc58aed140217332edd235a260d5746e7898392 |
Hashes for treevalue-1.4.10-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aeb5d775765c2b4a444439cf6bde6554e16c78bcf5c1e744c62a712fea275d46 |
|
MD5 | f4ec2e3552c6fcbbc3cd73d317cd110c |
|
BLAKE2b-256 | 74a352634848bfc9bd30067fdefa939c9485375c68eb694368cbc8eb4073c6f2 |
Hashes for treevalue-1.4.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b25d105fd4f0dc241b041030c8637106c85948aa72593cb607a5460af7855ecb |
|
MD5 | f6ccbe36d6a681e490d13f0d6515ecce |
|
BLAKE2b-256 | 7033f9a915fd30f9c243ab5709e1dd27743e964ea60d873beed591fcd8e0f4ce |
Hashes for treevalue-1.4.10-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 187e8f48fb24513c75832b79c5265a9eea8923b48ea9bc5552d2211e31f24ed3 |
|
MD5 | e8374a98b3691141e6e8a55d10db2b68 |
|
BLAKE2b-256 | d708d5710ddc0dbdbcb4d7a7f9265baa13134d2d07b37d3b5308cafe03fe0aba |
Hashes for treevalue-1.4.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 846154b77f0fe364424cac11b6cf99b78d7edfa5d67225dbf098260a8ecc2aa3 |
|
MD5 | 0bb8b30c20c0b00c0def25794e4b7b64 |
|
BLAKE2b-256 | 4a1554e6d24e8a995363c205cd671fa483e1314b2a424824d77e890736c17b90 |
Hashes for treevalue-1.4.10-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0ef32ad663527a2b2bb81487e9a4959a4728ab0344ace85506e63b818ff055f |
|
MD5 | 49aa93377cdff0ef0d6524245f55c9c7 |
|
BLAKE2b-256 | 92910c0d6144ce6c2b1755d2d9c0bd0d8d3d50c6e05349b56ac4bed550f51183 |
Hashes for treevalue-1.4.10-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fbd5d0f06afee0622ac5108a363f0d46db31ab1bcaadb76af345b051c1b3d13 |
|
MD5 | 081447e794fad15fe781f48d4705efcc |
|
BLAKE2b-256 | 2792d6e2d3b2733db9b3029837c571f309545942e6bf80fca682f7b1990e1c50 |
Hashes for treevalue-1.4.10-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c43ea5a4b6b1f97d329aec60f97b5e40211d4412f8b20ca3309fb3d5bdfa8c28 |
|
MD5 | 8cf1254ffa85e8e33fbb219cd235a6f9 |
|
BLAKE2b-256 | 6cf2a04a99af015b304b7a499ab2dbd502e689d8ea6cdb406a189bed95300e7a |
Hashes for treevalue-1.4.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62f95d6e2a153319f42ccf10e70409a4c83dbdb14aa5979185416b0fd27346ed |
|
MD5 | 6fa887cddbaa1f47a49fc4f69423824b |
|
BLAKE2b-256 | e56382e2b7251c87c2b3f8bdd06d4f1bb718d4032ead914bb06f292ddf37df4f |
Hashes for treevalue-1.4.10-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e26fe4c56d4144c5d285ef1ae3105355b3cfc9e24e2066bcda9f84856818508 |
|
MD5 | 23f20483fbd27e4dddcee03308b6f7a6 |
|
BLAKE2b-256 | 165193157a3c75a748db2a77d6710753d3c067b5e43e718dc09d3518a9cf70c2 |
Hashes for treevalue-1.4.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 200d1d21d2f9fcd5a97f1895193e5cb68dd2171c2f107df4d90a28ce2bc0714a |
|
MD5 | 890491fca54edeb582ce36e4efadf98c |
|
BLAKE2b-256 | f7c5395d18509a0f4d958e9365a69823f541c2545d5c53c3aaf07b4f404af77c |
Hashes for treevalue-1.4.10-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7c8384e3de604cfe887429a90a8ac587dc93057b2cc2d7214bf0e40beaf92f7 |
|
MD5 | 7d869d59ba88d65fd5d4c9151b784e42 |
|
BLAKE2b-256 | e6b9357dc56a3ac971588c2e9bb3cc9f7e13899ee1c521ae2241f7eda4355b32 |
Hashes for treevalue-1.4.10-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93e3961a42bdcccc93503449519865c8369b225e75cb5cab1cefdcac0ccb33d1 |
|
MD5 | 2a18ae0e7e736c5d4c08b769db48cb83 |
|
BLAKE2b-256 | bf77c5429b09387b30e70c2f2974fb30dd9c21409bd6f8f10c64d6d1873bf202 |
Hashes for treevalue-1.4.10-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d23cc2296f091b925a4745b36b140f5e0800b640eacae0cf6063a5c3c26e47 |
|
MD5 | 6ea82a484fa7325f4d512d75fb340b5a |
|
BLAKE2b-256 | 10600b062a0cb07cb4a7e9f29e7ca3f77ccc66ab1bde2f25de776b6f4b08b961 |
Hashes for treevalue-1.4.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5c8d9fef588d7b7d28eb3dc5400c1802cec516c30d9aa8f25e78858dd29b9d4 |
|
MD5 | fb06212e06658d4e41e86071a5a7ba81 |
|
BLAKE2b-256 | f654d6fd0d25900fae50c989b0bf28c73792dccf20bc31675671b832beddb8c6 |
Hashes for treevalue-1.4.10-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d09d09a761ca9bb9e81c0fdf9b34edb5a6aa36d71c34017657987ad3622c671f |
|
MD5 | b659353e8d33d6cb0126764779646fc0 |
|
BLAKE2b-256 | 759efd831e99db0ca931bd00d91e4081ccbe87fc6c922f6c831a4d58fd61b0c2 |
Hashes for treevalue-1.4.10-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 213fafce55320c90a89109677a6e87b53398662f62378671888cb4fe53a5c081 |
|
MD5 | 86ac4fb84d33edd1ae242dc0f7c16332 |
|
BLAKE2b-256 | edd3cb53c13267fd74477a4d2335a9d4ad4a1ae318825b9042653883593ec789 |
Hashes for treevalue-1.4.10-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95fbdcadb90272e5277b0b6e0ff19c16be5937a8c61cb39e6afd01ea91b88fa8 |
|
MD5 | 33da2b461aa33e73872673c04e179c3c |
|
BLAKE2b-256 | 9ffb99a8853de73e65490320a9605e0b08b2b59de05edd2777c34d6f099e28de |
Hashes for treevalue-1.4.10-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13c5252a5744b21e1f4459da982cc13bb0e1a6d941f257c7845d75c599eba8bd |
|
MD5 | 32d5f5b454a54246866af03756895127 |
|
BLAKE2b-256 | 6f41ec8b203700ca363cdc8fa3efba1e396c2998dd5bf0cbafbef40361695400 |
Hashes for treevalue-1.4.10-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2644260b6bb8fa387fa814b2e80d47eff51743dea23ceeb8067d7ea0bec8771 |
|
MD5 | a81ed97f46132d5db9679e6c66962482 |
|
BLAKE2b-256 | 49e15c868c78a785b90f009b003c96d8b6f8568606bc79afdf547e89b9651972 |
Hashes for treevalue-1.4.10-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70ef653d7f58c0a429b9af97536b1aa49a65a11b81fd079a6056d801c57af5a5 |
|
MD5 | 2608c20dee0b1192952d29c49704edf2 |
|
BLAKE2b-256 | bc7326d5564c019287993f24476a48320dc657347c42c5da28eefdfa572aa329 |
Hashes for treevalue-1.4.10-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36eb419a88197f50c92ef277a863352293fb84713f7e160841f84bfbb39be12c |
|
MD5 | 8fd8c43e1c912584a06922eb49213c83 |
|
BLAKE2b-256 | cd0915bebffaf06d0850486a1e5f286bd056a1173757686333ae36287c53388f |
Hashes for treevalue-1.4.10-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38ea6649f4bbe9d9d1dfa1bbd4a530b58b390dff90296508b3ebe2762eeac95c |
|
MD5 | ae47d2e5c04a1702eb220d5acf87ce6d |
|
BLAKE2b-256 | 501b69924711cc16b5f17f5473d1579332044d5c1423b0ff1abfadeb05f26768 |
Hashes for treevalue-1.4.10-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2676b1630a798ef7ffb1595333a49e5531e2d7bf9fad80b4795c6b8b7cbd1ba8 |
|
MD5 | ed16af772db4973aa5cd6ef52347086f |
|
BLAKE2b-256 | 675d22d4bec1a2d36e65ab234d04910f15bce16c2e93de579fd9d4079a7cf695 |
Hashes for treevalue-1.4.10-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6307c43252aca707e5d4fd08846595222ecb08a62cd1358e6dad3f94c6734f1e |
|
MD5 | dded79ea77e43994da38c3b1516c147d |
|
BLAKE2b-256 | af3119d4fee484baa655bb76e79e49e05b25746cd748eafe38a2eade5b0be003 |
Hashes for treevalue-1.4.10-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0f4ba8789d7c382300d4df019ba032a8534bab32908002c676092c9b71cdc63 |
|
MD5 | 57dbbcac0f23e48e68f786dbb661c12c |
|
BLAKE2b-256 | 1bf985662bce69238d0a6f3dc2489feeadf350db754369a3007b1be9be458073 |
Hashes for treevalue-1.4.10-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef02a843ffd0e2f77a399ff2438bdd4315434a1063a02f74f8498655f0d5427e |
|
MD5 | 10231370d53bdb3fa96ef73fddda0bb1 |
|
BLAKE2b-256 | 04fda921e6249a68350e3e46f23d4119ccc4c0e33e5526af574c0fc46f837c32 |
Hashes for treevalue-1.4.10-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 425a3db2e7022e031a34cdf7d209415bef04e248f5783b16c102b2136c6d391d |
|
MD5 | 0c1443b3b9dba16aaa10f18de30ce447 |
|
BLAKE2b-256 | 92fba342bc2ba0c3acd7cb2a1dec55d63e22eb6ef118583086b5e3422cc0c431 |
Hashes for treevalue-1.4.10-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a680f6904ff8c1e8450dba0874f006febd73b7668004ef6f336735353c7ad4b |
|
MD5 | 1a957ed61d100e7d72a2d4233a748970 |
|
BLAKE2b-256 | 131bb9652e22a8b242640e0cfa7061725afd2ce5761a469349455ccb62932886 |