README.md 19.0 KB
Newer Older
1
<div align="center">
2
    <a href="https://opendilab.github.io/treevalue/"><img width="1000px" height="auto" src="https://github.com/opendilab/treevalue/blob/main/docs/source/_static/title-banner.png"></a>
3
</div>
HansBug's avatar
HansBug 已提交
4

5 6 7
---

[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Ftwitter.com%2Fopendilab)](https://twitter.com/opendilab)
HansBug's avatar
HansBug 已提交
8 9 10 11 12
[![PyPI](https://img.shields.io/pypi/v/treevalue)](https://pypi.org/project/treevalue/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/treevalue)
![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/ff0bc026423888cd7c4f287eaed4b3f5/raw/loc.json)
![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/ff0bc026423888cd7c4f287eaed4b3f5/raw/comments.json)

13 14
[![Docs Deploy](https://github.com/opendilab/treevalue/workflows/Docs%20Deploy/badge.svg)](https://github.com/opendilab/treevalue/actions?query=workflow%3A%22Docs+Deploy%22)
[![Code Test](https://github.com/opendilab/treevalue/workflows/Code%20Test/badge.svg)](https://github.com/opendilab/treevalue/actions?query=workflow%3A%22Code+Test%22)
HansBug's avatar
HansBug 已提交
15 16
[![Badge Creation](https://github.com/opendilab/treevalue/workflows/Badge%20Creation/badge.svg)](https://github.com/opendilab/treevalue/actions?query=workflow%3A%22Badge+Creation%22)
[![Package Release](https://github.com/opendilab/treevalue/workflows/Package%20Release/badge.svg)](https://github.com/opendilab/treevalue/actions?query=workflow%3A%22Package+Release%22)
17
[![codecov](https://codecov.io/gh/opendilab/treevalue/branch/main/graph/badge.svg?token=XJVDP4EFAT)](https://codecov.io/gh/opendilab/treevalue)
HansBug's avatar
HansBug 已提交
18

19
![GitHub Org's stars](https://img.shields.io/github/stars/opendilab)
HansBug's avatar
HansBug 已提交
20 21 22 23 24 25 26 27
[![GitHub stars](https://img.shields.io/github/stars/opendilab/treevalue)](https://github.com/opendilab/treevalue/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/opendilab/treevalue)](https://github.com/opendilab/treevalue/network)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/opendilab/treevalue)
[![GitHub issues](https://img.shields.io/github/issues/opendilab/treevalue)](https://github.com/opendilab/treevalue/issues)
[![GitHub pulls](https://img.shields.io/github/issues-pr/opendilab/treevalue)](https://github.com/opendilab/treevalue/pulls)
[![Contributors](https://img.shields.io/github/contributors/opendilab/treevalue)](https://github.com/opendilab/treevalue/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/opendilab/treevalue)](https://github.com/opendilab/treevalue/blob/master/LICENSE)

HansBug's avatar
HansBug 已提交
28
`TreeValue` is a generalized tree-based data structure mainly developed by [OpenDILab Contributors](https://github.com/opendilab).
29

30
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.
HansBug's avatar
HansBug 已提交
31

32 33
## Outline

34 35 36 37 38 39
* [Overview](#overview)
* [Getting Started](#getting-started)
    * [Prerequisite](#prerequisite)
    * [Installation](#installation)
    * [Quick Usage](#quick-usage)
    * [Tutorials](#tutorials)
40
    * [External](#external)
41 42
* [Speed Performance](#speed-performance)
* [Change Log](#change-log)
43
* [Feedback and Contribute](#feedback-and-contribute)
44 45
* [Citation](#citation)
* [License](#license)
46 47 48

## Overview

49
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.
50

51
Therefore, we need a kind of more proper data container, named `TreeValue`. It is designed for solving the following problems:
52 53

- **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.
54
- **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.
55
- **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.
56 57 58 59 60

## Getting Started

### Prerequisite

61
`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.
62

63
However, **`treevalue` currently does not support PyPy**, so just pay attention to this when using it.
64 65

### Installation
HansBug's avatar
HansBug 已提交
66 67 68 69 70 71 72

You can simply install it with `pip` command line from the official PyPI site.

```shell
pip install treevalue
```

73 74 75 76 77
Or just from the source code on github

```shell
pip install git+https://github.com/opendilab/treevalue.git@main
```
HansBug's avatar
HansBug 已提交
78

79
For more information about installation, you can refer to the [installation guide](https://opendilab.github.io/treevalue/main/tutorials/installation/index.html).
HansBug's avatar
HansBug 已提交
80

81
After this, you can check if the installation is processed properly with the following code
HansBug's avatar
HansBug 已提交
82

83 84 85 86
```python
from treevalue import __version__
print('TreeValue version is', __version__)
```
HansBug's avatar
HansBug 已提交
87

88
### Quick Usage
HansBug's avatar
HansBug 已提交
89 90 91 92 93 94 95

You can easily create a tree value object based on `FastTreeValue`.

```python
from treevalue import FastTreeValue

if __name__ == '__main__':
HansBug's avatar
HansBug 已提交
96 97 98 99 100 101 102 103 104
    t = FastTreeValue({
        'a': 1,
        'b': 2.3,
        'x': {
            'c': 'str',
            'd': [1, 2, None],
            'e': b'bytes',
        }
    })
HansBug's avatar
HansBug 已提交
105
    print(t)
HansBug's avatar
HansBug 已提交
106

HansBug's avatar
HansBug 已提交
107 108 109 110 111
```

The result should be

```text
HansBug's avatar
HansBug 已提交
112
<FastTreeValue 0x7f6c7df00160 keys: ['a', 'b', 'x']>
HansBug's avatar
HansBug 已提交
113
├── 'a' --> 1
HansBug's avatar
HansBug 已提交
114 115 116 117 118
├── 'b' --> 2.3
└── 'x' --> <FastTreeValue 0x7f6c81150860 keys: ['c', 'd', 'e']>
    ├── 'c' --> 'str'
    ├── 'd' --> [1, 2, None]
    └── 'e' --> b'bytes'
HansBug's avatar
HansBug 已提交
119 120
```

HansBug's avatar
HansBug 已提交
121 122
And `t` is structure should be like this

HansBug's avatar
HansBug 已提交
123
![](https://opendilab.github.io/treevalue/main/_images/simple_demo.dat.svg)
HansBug's avatar
HansBug 已提交
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
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

```python
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]])
```

188 189 190 191 192


### Tutorials

For more examples, explanations and further usages, take a look at:
HansBug's avatar
HansBug 已提交
193 194

* [Quick Start](https://opendilab.github.io/treevalue/main/tutorials/quick_start/index.html)
195 196
    * [Create a Simplest Tree](https://opendilab.github.io/treevalue/main/tutorials/quick_start/index.html#create-a-simplest-tree)
    * [Create a Slightly Complex Tree](https://opendilab.github.io/treevalue/main/tutorials/quick_start/index.html#create-a-slightly-complex-tree)
HansBug's avatar
HansBug 已提交
197
* [Basic Usage](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html)
198 199 200 201 202
    * [Create a Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#create-a-tree)
    * [Edit a Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#edit-the-tree)
    * [Do Index or Slice Calculation on The Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#do-index-or-slice-calculation-on-the-tree)
    * [Do Math Calculation on The Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#do-calculation-on-the-tree)
    * [Make Function Tree-Supported](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#make-function-tree-supported)
HansBug's avatar
HansBug 已提交
203
* [Advanced Usage](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html)
204 205 206 207 208 209 210 211 212 213 214 215
    * [Function Modes](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#function-modes)
    * [Inheriting on Trees](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#inheriting-on-trees)
    * [Process Missing Values](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#process-missing-values)
    * [Functional Utilities](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#functional-utilities)
    * [Structural Utilities](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#structural-utilities)
    * [Tree Utilities](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#tree-utilities)
    * [Flatten Utilities](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#flatten-utilities)
    * [IO Utilities](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#io-utilities)
    * [Object Oriented Usage](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#object-oriented-usage)
    * [Costumize My TreeValue Class](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#diy-treevalue-class)
    * [Visualization of TreeValue](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#draw-graph-for-treevalue)

216 217
### External

218
We provide an official treevalue-based-wrapper for numpy and torch called [DI-treetensor](https://github.com/opendilab/DI-treetensor) since the `treevalue` is often used with libraries like `numpy` and `torch`. It will actually be helpful while working with AI fields.
219 220 221



222 223
## Speed Performance

224
Here is the speed performance of all the operations in `FastTreeValue`; the following table is the performance comparison result with [dm-tree](https://github.com/deepmind/tree).
HansBug's avatar
HansBug 已提交
225
(In DM-Tree, the `unflatten` operation is different from that in TreeValue, see: [Comparison Between TreeValue and DM-Tree](https://opendilab.github.io/treevalue/main/comparison/dmtree.result.html) for more details.)
226

227 228 229 230 231
|                                                     |     flatten      |  flatten(with path)   |        mapping        |     mapping(with path)      |
| --------------------------------------------------- | :--------------: | :-------------------: | :-------------------: | :-------------------------: |
| [treevalue](https://github.com/opendilab/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](https://github.com/deepmind/tree)         | 830 ns ± 8.53 ns |   11.9 µs ± 358 ns    |   13.3 µs ± 87.2 ns   |      62.9 µs ± 2.26 µs      |
232

233
The following 2 tables are the performance comparison result with [jax pytree](https://github.com/google/jax).
234

235 236 237 238 239
|                                                     |        mapping        |  mapping(with path)   |       flatten        |      unflatten       |    flatten_values    |     flatten_keys     |
| --------------------------------------------------- | :-------------------: | :-------------------: | :------------------: | :------------------: | :------------------: | :------------------: |
| [treevalue](https://github.com/opendilab/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](https://github.com/google/jax)         |   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   |
240 241 242 243

|                                                     |    flatten + all     |   flatten + reduce   | flatten + reduce(with init) | rise(given structure) | rise(automatic structure) |
| --------------------------------------------------- | :------------------: | :------------------: | :-------------------------: | :-------------------: | :-----------------------: |
| [treevalue](https://github.com/opendilab/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**    |
244
|                                                     |     **tree_all**     |   **tree_reduce**    | **tree_reduce(with init)**  |  **tree_transpose**   |   **(Not Implemented)**   |
245 246
| [jax pytree](https://github.com/google/jax)         |   1.47 µs ± 37 ns    |  1.88 µs ± 27.2 ns   |      1.91 µs ± 47.4 ns      |    10 µs ± 117 ns     |            ---            |

247 248 249 250 251 252
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**)

![Time cost of flatten operation](docs/source/_static/Time%20cost%20of%20flatten%20operation.svg)

![Time cost of mapping operation](docs/source/_static/Time%20cost%20of%20mapping%20operation.svg)

253
The following table is the performance comparison result with [tianshou Batch](https://github.com/thu-ml/tianshou).
254 255 256 257 258 259

|                                                      |          get           |          set           |         init         |       deepcopy       |        stack         |          cat          |       split        |
| ---------------------------------------------------- | :--------------------: | :--------------------: | :------------------: | :------------------: | :------------------: | :-------------------: | :----------------: |
| [treevalue](https://github.com/opendilab/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](https://github.com/thu-ml/tianshou) | **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  |

260
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**)
261

262
![Time cost of cat operation](docs/source/_static/Time%20cost%20of%20cat%20operation.svg)
263

264
![Time cost of stack operation](docs/source/_static/Time%20cost%20of%20stack%20operation.svg)
265

266
![Time cost of split operation](docs/source/_static/Time%20cost%20of%20split%20operation.svg)
267

268 269
Test benchmark code can be found here:

270 271 272
* [Comparison with dm-tree](https://github.com/opendilab/treevalue/blob/main/test/compare/deepmind/test_dm_tree.py)
* [Comparison with jax-libtree](https://github.com/opendilab/treevalue/blob/main/test/compare/jax/test_jax.py)
* [Comparison with tianshou Batch](https://github.com/opendilab/treevalue/blob/main/test/compare/tianshou/test_tianshou_batch.py)
273

274
## Change Log
HansBug's avatar
HansBug 已提交
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
<details><summary><b>Version History</b> <i>[click to expand]</i></summary>
<div>

* 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.
HansBug's avatar
HansBug 已提交
293 294
    </div>
    </details>
HansBug's avatar
HansBug 已提交
295

296
## Feedback and Contribute
HansBug's avatar
HansBug 已提交
297

298
Welcome to **OpenDILab** community - treevalue!
HansBug's avatar
HansBug 已提交
299

300 301
If you meet some problem or have some brilliant ideas, you can [file an issue](https://github.com/opendilab/treevalue/issues/new/choose).

302
<b>Scan the QR code and add us on Wechat:</b>
303

304 305 306
<div align="center">
<img src='https://github.com/opendilab/DI-engine/raw/main/assets/wechat.png' width="25%" />
</div>
HansBug's avatar
HansBug 已提交
307

308
Or just contact us with [slack](https://opendilab.slack.com/join/shared_invite/zt-v9tmv4fp-nUBAQEH1_Kuyu_q4plBssQ#/shared-invite/email) or email (opendilab.contact@gmail.com).
309

310
Please check [Contributing Guidances](https://github.com/opendilab/treevalue/blob/main/CONTRIBUTING.md).
HansBug's avatar
HansBug 已提交
311

312
Thanks to the following contributors! 
HansBug's avatar
HansBug 已提交
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
<a href="https://github.com/opendilab/treevalue/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=opendilab/treevalue" />
</a>

## Citation

```
@misc{treevalue,
    title={{TreeValue} - Tree-Structure Computing Solution},
    author={TreeValue Contributors},
    publisher = {GitHub},
    howpublished = {\url{https://github.com/opendilab/treevalue}},
    year={2021},
}
```
HansBug's avatar
HansBug 已提交
329

HansBug's avatar
HansBug 已提交
330 331
## License

332
`treevalue` released under the Apache 2.0 license. See the [LICENSE](./LICENSE) file for details.