Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: replace treevalue with optree #247

Merged
merged 22 commits into from
Mar 20, 2023

Conversation

Benjamin-eecs
Copy link
Collaborator

@Benjamin-eecs Benjamin-eecs commented Feb 26, 2023

Description

Motivation and Context

Initial Test Results:

Test gym

TreeValue:

image

OpTree:

image

Test dmc

TreeValue:

image

OpTree:

image

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Example (update in the folder of example)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code using make format (required)
  • I have checked the code using make lint (required)
  • I have ensured make bazel-test pass. (required)

cc @XuehaiPan

@Benjamin-eecs Benjamin-eecs changed the title deps: replace treevalue with optree deps: replace treevalue with optree Feb 26, 2023
envpool/python/data.py Outdated Show resolved Hide resolved
setup.cfg Outdated Show resolved Hide resolved
Benjamin-eecs and others added 3 commits February 26, 2023 21:15
Co-authored-by: Xuehai Pan <XuehaiPan@outlook.com>
Co-authored-by: Xuehai Pan <XuehaiPan@outlook.com>
@Benjamin-eecs Benjamin-eecs requested review from XuehaiPan and removed request for XuehaiPan February 27, 2023 04:35
@HansBug
Copy link
Contributor

HansBug commented Feb 27, 2023

We are really really glad to see @XuehaiPan @Benjamin-eecs bring some more advise to treevalue. This will make treevalue better and better, because we can add all the features you come up with to treevalue in a very short time, which is faster than you can imagine. We hope to have more in-depth exchanges with the optree team in order to significantly improve the experience of using treevalue. 😄 Love U ❤️

In order to express our gratitude, we suggest that optree should take the EASE OF USE seriously, such as whether it is possible to attach additional methods to the tree data structure, and whether it can be programmed in OOP without great overhead. Treevalue can easily acheive this, and its performance is not low at all. We think this is very important for an AI developer-oriented package, because users will use actual usage to show which one is better. Ease of use can have a profound impact on a long-term maintained library.

@XuehaiPan
Copy link
Contributor

XuehaiPan commented Feb 27, 2023

we suggest that optree should take the EASE OF USE seriously, such as whether it is possible to attach additional methods to the tree data structure, and whether it can be programmed in OOP without great overhead.

@HansBug Thanks for your advice. We may consider this in a future release of optree. Currently, we think that always returning Python native types is the top priority of the optree package support.

Currently optree only supports primitive tree operations, and native types in and native types out.

In [1]: from collections import *

In [2]: import optree
   ...: from optree.typing import PyTree, T

In [3]: tree = {'a': 1, 'b': {'c': [2, (3, 4)], 'd': deque([4, 5])}, 'e': OrderedDict([('f', 6), ('g', 7)])}

In [4]: def tree_sum(tree: PyTree[T]) -> T:
   ...:     return optree.tree_reduce(lambda x, y: x + y, tree, 0)
   ...:

In [5]: tree_sum(tree)
Out[5]: 32

In [6]: def tree_add(tree: PyTree[T], other: PyTree[T]) -> PyTree[T]:
   ...:     return optree.tree_map(lambda x, y: x + y, tree, other)
   ...:

In [7]: other = optree.tree_map(lambda x: 2 * x, tree)

In [8]: other
Out[8]: {'a': 2, 'b': {'c': [4, (6, 8)], 'd': deque([8, 10])}, 'e': OrderedDict([('f', 12), ('g', 14)])}

In [9]: tree_add(tree, other)
Out[9]: {'a': 3, 'b': {'c': [6, (9, 12)], 'd': deque([12, 15])}, 'e': OrderedDict([('f', 18), ('g', 21)])}

optree can also be extended by creating OOP wrappers implemented with dunder methods (e.g., __pos__, __add__, __mul__) using tree_map. It still needs some effort to do, such as tree broadcast when the tree structures mismatch. In this scope, I think the usage of treevalue is more intuitive. FYI, you could check out pytorch-labs/tensordict, another dict-based container with similar functionality.

examples/xla_step.py Outdated Show resolved Hide resolved
@Trinkle23897 Trinkle23897 merged commit 2788eb9 into sail-sg:main Mar 20, 2023
@Benjamin-eecs Benjamin-eecs deleted the feature/optree branch March 20, 2023 07:03
@Benjamin-eecs Benjamin-eecs restored the feature/optree branch March 20, 2023 07:03
Trinkle23897 added a commit that referenced this pull request Mar 26, 2023
This fixes a bug introduced by #247
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Move away from dm-tree broke compatibility with Jax
4 participants