pytreeclass

Visualize, create, and operate on pytrees in the most intuitive way possible.

APACHE-2.0 License

Downloads
2.6K
Stars
38
Committers
2

Bot releases are hidden (Show)

pytreeclass - v0.2.6

Published by ASEM000 over 1 year ago

What's Changed

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.5...v0.2.6

pytreeclass - v0.2.5

Published by ASEM000 over 1 year ago

fix unordered keys bug in registering instance variables

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.4...v0.2.5

pytreeclass - v0.2.4

Published by ASEM000 over 1 year ago

What's Changed

import pytreeclass as pytc

@pytc.treeclass
class Tree:
  a:int = 1

tree = Tree()

tree.at[...].set(is_leaf=...)   # is leaf works with ...


Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.3...v0.2.4

pytreeclass - v0.2.3

Published by ASEM000 over 1 year ago

What's Changed

  • New viz function tree_repr_with_trace in #47 (useful for debugging)
  • Shield fields map from a user by removing from the class namespace in #44.
    • This might be the case when a user tries to change a field value from within the instance. However, this change will affect all instances of the same class.
  • Raise error for mutable values in field default. previously only raised for a:list=[] but not for a:list=pytc.field(default=[])
  • Change verbose naming of default_factory -> factory ( recognized by dataclass_transform and attrs)
  • Raise error if a field is named self
  • [internal] use lru_cache to improve treeclass perf.

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.2...v0.2.3

pytreeclass - v0.2.2

Published by ASEM000 over 1 year ago

  • Add id entry for standard python datatypes and treeclass wrapped classes in the metadata path.
import pytreeclass as pytc 
tree = [1, [2, [3]]]
traces, _ = zip(*pytc.tree_leaves_with_trace(tree))
print(pytc.tree_repr([trace[3] for trace in traces]))
# [
#   ({id:4843302976}, {id:4307173616}), 
#   ({id:4843302976}, {id:4843303936}, {id:4307173648}), 
#   ({id:4843302976}, {id:4843303936}, {id:4843297408}, {id:4307173680})
# ]
  • is_tree_equal(*trees) now accepts arbitrary number of pytrees.
  • FrozenWrapper is private.
  • [internal] merge new wrapper into init wrapper

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.1...v0.2.2

pytreeclass - v0.2.1

Published by ASEM000 over 1 year ago

  • use dataclass_transform for better typing/autocomplete
  • use one time validation on user-defined traces .

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.2.0...v0.2.1

pytreeclass - v0.2.0

Published by ASEM000 over 1 year ago

🔥 Big update

Mostly rewritten from scratch.

Changes:

  • Drop the standard library dataclasses.dataclass for faulty logic treating jax.numpy as mutable object from python 3.11 see: #33

  • Implements a simpler dataclass-like version that additionally implements:

    • pos_only to mark the argument as positional only
    • callbacks to apply a set of functions (e.g., validators/converters) on input see: #37
  • Drop metadata-based approach for marking fields static. move to a transparent wrapper approach for faster, simpler, and flexible marking of leaves static see here

  • tree_summary, tree_diagram, tree_str, and tree_repr can now work on any pytree(including registered pytrees) out of the box see

  • Remove tree.summary() in favor of pytc.tree_summary(model)

  • Remove tree.tree_diagram() in favor of pytc.tree_diagram(model)

  • Remove tree.tree_box()

  • Add tree_{repr,str,diagram,summary,mermaid}(..., depth) to control cutoff depth condition for visualization.

  • Make math operations on leaves opt-in feature via treeckass(... , leafwise=True)

  • Drop filtering by metadata , types, and name. Instead, use tree_map_with_trace to access leaves' name,type,index, and metadata at each level along their path. see #35

  • Add bcmap : a function transformation for automatic broadcasting. see readme More section.

    • using bcmap with treeclass(... , leafwise=True) achieves a data model that enables applying numpy functions like numpy.where on arbitrary PyTrees without the need for tree_map.
  • Faster flatten/unflatten see benchmarks comparison with flax/equinox.

Internally, unifying indexing and viz tools by implementing custom jax-like registry to record names, types, indices, and metadata rules for common python data structures and treeclass wrapped class. You can check the readme More section for registering custom classes to work fully with the viz/indexing tools similar to Jax registration process.

pytreeclass - 0.2.0b13

Published by ASEM000 over 1 year ago

Initial beta release for v0.2

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.13...0.2.0b13

pytreeclass - v0.1.13

Published by ASEM000 almost 2 years ago

What's Changed

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.12...v0.1.13

pytreeclass - v0.1.11

Published by ASEM000 about 2 years ago

What's Changed

  • Add immutable __delattr__

Breaking changes

pytc.is_frozen_field -> pytc.is_field_frozen
pytc.is_nondiff_field -> pytc.is_field_nondiff

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.10...v0.1.11

pytreeclass - v0.1.10

Published by ASEM000 about 2 years ago

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.9...v0.1.10

Changes

  • Added subtree size and param count in tree_mermaid placed on tree arrows.
  • Add pytc.field wrapping dataclasses.field with extra nondiff, frozen arguments
  • Add pytc.fields similar to dataclasses.fields which returns dataclasses fields and the undeclared fields
  • Add pytc.is_treeclass_{frozen, leaf_bool, leaf, equal}
  • Expand container in tree_diagram and tree_summary if at least one treeclass item exist .
  • Enable setting arbitrary types for .at[].set(...)
  • Minor refactoring
pytreeclass - v0.1.9

Published by ASEM000 about 2 years ago

Changes

  • Remove undocumented array_as_leaves option in .at methods
  • Replace all dispatching with isinstance for speed ( notable speed improvements in all treeviz methods )
  • Increase arrow length in tree_mermaid.
  • Previously, any instance variable (static or dynamic) passed between flatten/unflatten had to be declared as a dataclass field. Now by default, all non-treeclass instance variables are captured as static(i.e. part of treedef) unless declared in the dataclass fields.
    for example
import pytreeclass as pytc
import jax.tree_util as jtu

@pytc.treeclass
class Test:
  def __init__(self):
    self.a=1

t = Test()

tt = jtu.tree_unflatten(*jtu.tree_flatten(t)[::-1])

# before change it will raise an error as 
# `a` was not captured since it was not declared as dataclass field
tt.a   # -> Error

# After change, it will be part  of the treedef (i.e. static/nondiff field)
tt.a  # 1

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.8...v0.1.9

pytreeclass - v0.1.8

Published by ASEM000 about 2 years ago

Changes

  • Speed improvements for .at methods ( up to 40% improvement in speed )

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.7...v0.1.8

pytreeclass - v0.1.7

Published by ASEM000 about 2 years ago

Breaking changes

  • Option to treat only fields defined in dataclass fields as parameters is removed.
    this means any treeclass wrapped class is treated as a parameter i.e. field_only=False. This will simplify reasoning about parameter management on the user end.

Other changes

  • Speed improvements, 5x for tree arithmetic, 6x for type/metadata comparisons, and 8-10x for value comparisons based on colab hardware benchmarks. speed improvements due to: (1) using instance check instead of dispatching and (2) refactoring dataclass fields comparisons function.
  • wrap __undeclared_fields__ with MappingProxyType to make it immutable.

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/0.1.6...v0.1.7

pytreeclass - v0.1.6

Published by ASEM000 about 2 years ago

What's Changed

one addition and one change in this release,

  1. filter_nondiff, unfilter_nondiff are added. see Readme for example usage.
  2. tree_freeze now marks all fields static instead of caching them. this implementation is slower but it unifies the code and remove the distinction between module and fields when marking static.

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.5...0.1.6

pytreeclass - 0.1.5

Published by ASEM000 about 2 years ago

  1. Remove mask_util.
  2. faster __str__ and __repr__.

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/0.1.4...v0.1.5

pytreeclass - v0.1.4

Published by ASEM000 about 2 years ago

Breaking changes:

  • {key:val} == tree maps {key:val} == field.metadata for each field item. Previously the operation was {key:val} in field.metadata
  • deprecate .at aux methods namely add,multiply,power,divide,max,min,reduce_sum,reduce_product,reduce_max,reduce_min.
  • Deprecate param and Container. these features will be moved to serket in the future.

Other

  • _treeBase methods : __pytree_fields__, __pytree_structure__, frozen property, is replaced with _tree_fields, _tree_structure, _is_treeclass_frozen functions in tree_util

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/0.1.3...0.1.4

pytreeclass - v0.1.3

Published by ASEM000 about 2 years ago

Breaking changes

  • Move .at[].freeze to pytreeclass.tree_util.tree_freeze
    see

Full Changelog: https://github.com/ASEM000/PyTreeClass/compare/v0.1.2...0.1.3

pytreeclass - v0.1.2

Published by ASEM000 about 2 years ago

pytreeclass - v0.1.0

Published by ASEM000 about 2 years ago

PyTreeClass first minor release

🎉🎉🎉🎉🥳🥳🥳🥳

What's changed

  1. Faster flattening/unflattening up to ~ 30%
  2. Enable composed .at for string/boolean argument
    • model.at["a"].at["b"].{get,set,apply,reduce,...} <-> model.a.b
    • model.at[model>0].at[model == "linear"].{get,set,apply,reduce,...} <-> model.at[(model>0) & (model=="linear")].{get,set,apply,reduce,...}
  3. Switch to black-like str/repr
  4. Deprecate static_value
  5. Add filter_nondiff to make non-inexact values static. and , unfilter_nondiff to revert that operation. #21
  6. Change register_node to param
  7. Lots of internal factoring, notably the implementation for overloaded equal/not equal for field type/name/metadata is simplified by using _pytree_map to operate on dataclass field and value