Skip to content

feat: migrate pybaum to optree#679

Open
abelaba wants to merge 19 commits intooptimagic-dev:mainfrom
abelaba:migrate-pybaum-to-optree
Open

feat: migrate pybaum to optree#679
abelaba wants to merge 19 commits intooptimagic-dev:mainfrom
abelaba:migrate-pybaum-to-optree

Conversation

@abelaba
Copy link
Copy Markdown

@abelaba abelaba commented Apr 1, 2026

Summary

  • Replace pybaum with optree for all pytree operations used in optimagic (tree_just_flatten,tree_unflatten,tree_flatten,tree_map, tree_equal, leaf_names)
  • Remove pybaum as a dependency and add optree>=0.19.0,<0.20.
  • tree_registry.py: Rewritten to use optree's register_pytree_node with a custom namespace, including support for pandas DataFrame/Series, and JAX arrays.
  • typing.py: Added shared namespace constants (optree_namespaces list and default_namespace).

Why wrapper methods instead of using optree directly

  1. Flattening: optree does not preserve insertion order of dictionaries by default. Without the wrapper, we would need to use first convert dictionaries to an OrderedDict (referenced in this doc). The wrapper ensures dict
    ordering is maintained. (see failing test screenshots below if order is not maintained for dictionary)
Screenshot 2026-04-02 at 18 26 42

In addition, ordering currently works for flattening dictionaries when a namespace is provided.

  1. Unflattening: pybaum and optree have different Tree structures. The tree_unflatten wrapper maintains backward compatibility for methods that use pybaum's tree structure.

Remaining

  • Adding type hints and docstrings for newly added methods.
  • Flattening and unflattening for torch tensors.

Test plan

  • All existing fast tests pass with the new optree backend.

@abelaba abelaba force-pushed the migrate-pybaum-to-optree branch from 4ce5903 to 94516da Compare April 2, 2026 12:30
@abelaba abelaba force-pushed the migrate-pybaum-to-optree branch from 6dfe919 to 6f03b12 Compare April 2, 2026 14:52
@abelaba abelaba changed the title Migrate pybaum to optree feat: migrate pybaum to optree Apr 2, 2026
@abelaba abelaba marked this pull request as ready for review April 2, 2026 17:33
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.

1 participant