Skip to main content


Installation |Description |Quick Example |StatefulComputation |Benchamrks |Acknowledgements

Tests pyver pyver codestyle Open In Colab Downloads codecov Documentation Status GitHub commit activity DOI PyPI CodeFactor

🛠️ Installation

pip install pytreeclass

Install development version

pip install git+https://github.com/ASEM000/PyTreeClass

📖 Description

PyTreeClass is a JAX-compatible class builder to create and operate on stateful JAX PyTrees in a performant and intuitive way, by building on familiar concepts found in numpy, dataclasses, and others.

See documentation and 🍳 Common recipes to check if this library is a good fit for your work. If you find the package useful consider giving it a 🌟.

⏩ Quick Example

import jax
import jax.numpy as jnp
import pytreeclass as pytc

@pytc.autoinit
class Tree(pytc.TreeClass):
    a: float = 1.0
    b: tuple[float, float] = (2.0, 3.0)
    c: jax.Array = jnp.array([4.0, 5.0, 6.0])

    def __call__(self, x):
        return self.a + self.b[0] + self.c + x


tree = Tree()
mask = jax.tree_map(lambda x: x > 5, tree)
tree = tree\
       .at["a"].set(100.0)\
       .at["b"][0].set(10.0)\
       .at[mask].set(100.0)

print(tree)
# Tree(a=100.0, b=(10.0, 3.0), c=[  4.   5. 100.])

print(pytc.tree_diagram(tree))
# Tree
# ├── .a=100.0
# ├── .b:tuple
# │   ├── [0]=10.0
# │   └── [1]=3.0
# └── .c=f32[3](μ=36.33, σ=45.02, ∈[4.00,100.00])

print(pytc.tree_summary(tree))
# ┌─────┬──────┬─────┬──────┐
# │Name │Type  │Count│Size  │
# ├─────┼──────┼─────┼──────┤
# │.a   │float │1    │      │
# ├─────┼──────┼─────┼──────┤
# │.b[0]│float │1    │      │
# ├─────┼──────┼─────┼──────┤
# │.b[1]│float │1    │      │
# ├─────┼──────┼─────┼──────┤
# │.c   │f32[3]│3    │12.00B│
# ├─────┼──────┼─────┼──────┤
# │Σ    │Tree  │6    │12.00B│
# └─────┴──────┴─────┴──────┘

# ** pass it to jax transformations **
# works with jit, grad, vmap, etc.

@jax.jit
@jax.grad
def sum_tree(tree: Tree, x):
    return sum(tree(x))

print(sum_tree(tree, 1.0))
# Tree(a=3.0, b=(3.0, 0.0), c=[1. 1. 1.])

📜 Stateful computations

Under jax.jit jax requires states to be explicit, this means that for any class instance; variables needs to be separated from the class and be passed explictly. However when using TreeClass no need to separate the instance variables ; instead the whole instance is passed as a state.

Using the following pattern,Updating state functionally can be achieved under jax.jit

import jax
import pytreeclass as pytc

class Counter(pytc.TreeClass):
    def __init__(self, calls: int = 0):
        self.calls = calls

    def increment(self):
        self.calls += 1
counter = Counter() # Counter(calls=0)

Here, we define the update function. Since the increment method mutate the internal state, thus we need to use the functional approach to update the state by using .at. To achieve this we can use .at[method_name].__call__(*args,**kwargs), this functional call will return the value of this call and a new model instance with the update state.

@jax.jit
def update(counter):
    value, new_counter = counter.at["increment"]()
    return new_counter

for i in range(10):
    counter = update(counter)

print(counter.calls) # 10

➕ Benchmarks

Benchmark flatten/unflatten compared to Flax and Equinox

Open In Colab

CPUGPU
Benchmark simple training against `flax` and `equinox`

Training simple sequential linear benchmark against flax and equinox

Num of layers Flax/PyTC time
Open In Colab
Equinox/PyTC time
Open In Colab
10 1.427 6.671
100 1.1130 2.714

📙 Acknowledgements

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytreeclass-0.8.0.tar.gz (56.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytreeclass-0.8.0-py3-none-any.whl (46.8 kB view details)

Uploaded Python 3

File details

Details for the file pytreeclass-0.8.0.tar.gz.

File metadata

  • Download URL: pytreeclass-0.8.0.tar.gz
  • Upload date:
  • Size: 56.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pytreeclass-0.8.0.tar.gz
Algorithm Hash digest
SHA256 6f4ad23551ac473ed3ab821a50c56e840d889763a46cb19cfd06b60c26b19afe
MD5 a9eb6e6fb341bcf1a214bd05c985b851
BLAKE2b-256 6c825e88e43fb71b44f6090e8b3317306aaed4aca99f38f80019a819cc7cb64e

See more details on using hashes here.

File details

Details for the file pytreeclass-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: pytreeclass-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 46.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pytreeclass-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d89cf04b99539c41aba3be32e4d373f92e5022da5448f29d7c57428f04c69e5c
MD5 a4139e72cdb97f40682dd0dfaf1e563e
BLAKE2b-256 9fb03d0931dace41d46ccd2462e91fc6ad1dbb553c7dd29979fef692734f909d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.11.1

2 files

0.11.0

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

This release

0.8.0 This release

2 files

0.7.0

2 files

0.6.0.post0

2 files

0.6.0

2 files

0.5.0.post0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.11

2 files

0.0.9.post1

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6.post2

2 files

0.0.6.post1

2 files

0.0.6.post0

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page