99except ImportError :
1010 raise ImportError ("PyTorch is required to use effectful.handlers.torch" )
1111
12- import tree
12+ import torch . utils . _pytree as pytree
1313
1414from effectful .internals .runtime import interpreter
1515from effectful .internals .tensor_utils import _desugar_tensor_index
@@ -91,7 +91,7 @@ def _partial_eval(t: Expr[torch.Tensor]) -> Expr[torch.Tensor]:
9191 isinstance (t , Term )
9292 and all (
9393 isinstance (a , torch .Tensor ) or not isinstance (a , Term ) or a .op in sized_fvs
94- for a in tree . flatten ((t .args , t .kwargs ))
94+ for a in pytree . tree_flatten ((t .args , t .kwargs ))[ 0 ]
9595 )
9696 ):
9797 return t
@@ -126,7 +126,7 @@ def reindex_flat_tensor(t):
126126 result = t .reshape (inds [0 ].shape + t .shape [1 :])
127127 return torch_getitem (result , tuple (k () for k in sized_fvs .keys ()))
128128
129- result = tree . map_structure (reindex_flat_tensor , flat_result )
129+ result = pytree . tree_map (reindex_flat_tensor , flat_result )
130130 return result
131131
132132
@@ -135,9 +135,7 @@ def reindex_flat_tensor(t):
135135def bind_dims [
136136 A ,
137137 B ,
138- HasDims : torch .Tensor
139- | torch .distributions .Distribution
140- | tree .Structure [torch .Tensor | torch .distributions .Distribution ],
138+ HasDims : pytree .PyTree | torch .Tensor | torch .distributions .Distribution ,
141139](
142140 value : Annotated [HasDims , Scoped [A | B ]],
143141 * names : Annotated [Operation [[], torch .Tensor ], Scoped [B ]],
@@ -157,8 +155,8 @@ def bind_dims[
157155 >>> bind_dims(t[a(), b()], b, a).shape
158156 torch.Size([3, 2])
159157 """
160- if tree . is_nested (value ):
161- return tree . map_structure (lambda v : bind_dims (v , * names ), value )
158+ if not pytree . tree_is_leaf (value ):
159+ return pytree . tree_map (lambda v : bind_dims (v , * names ), value )
162160 raise NotHandled
163161
164162
@@ -209,15 +207,13 @@ def _bind_dims_tensor(
209207def unbind_dims [
210208 A ,
211209 B ,
212- HasDims : torch .Tensor
213- | torch .distributions .Distribution
214- | tree .Structure [torch .Tensor | torch .distributions .Distribution ],
210+ HasDims : pytree .PyTree | torch .Tensor | torch .distributions .Distribution ,
215211](
216212 value : Annotated [HasDims , Scoped [A | B ]],
217213 * names : Annotated [Operation [[], torch .Tensor ], Scoped [B ]],
218214) -> Annotated [HasDims , Scoped [A | B ]]:
219- if tree . is_nested (value ):
220- return tree . map_structure (lambda v : unbind_dims (v , * names ), value )
215+ if not pytree . tree_is_leaf (value ):
216+ return pytree . tree_map (lambda v : unbind_dims (v , * names ), value )
221217 raise NotHandled
222218
223219
@@ -256,9 +252,9 @@ def _torch_op(*args, **kwargs) -> torch.Tensor:
256252 # which partial_eval handles.
257253 return typing .cast (torch .Tensor , _partial_eval (tm ))
258254 elif not any (
259- tree . flatten (
260- tree . map_structure (lambda x : isinstance (x , Term ), (args , kwargs ))
261- )
255+ pytree . tree_flatten (
256+ pytree . tree_map (lambda x : isinstance (x , Term ), (args , kwargs ))
257+ )[ 0 ]
262258 ):
263259 return typing .cast (torch .Tensor , torch_fn (* args , ** kwargs ))
264260 else :
@@ -600,7 +596,7 @@ def _indexed_func_wrapper[**P, S, T](
600596 # index expressions for the result of the function
601597 indexes = None
602598
603- # hide index lists from tree.map_structure
599+ # hide index lists from pytree.tree_map
604600 class Indexes :
605601 def __init__ (self , sizes ):
606602 self .sizes = sizes
@@ -616,17 +612,17 @@ def deindex_tensor(t, i):
616612 return t_
617613
618614 ret = func (* args , ** kwargs )
619- indexes = tree . map_structure (lambda t : Indexes (sizesof (t )), ret )
620- tensors = tree . map_structure (lambda t , i : deindex_tensor (t , i ), ret , indexes )
615+ indexes = pytree . tree_map (lambda t : Indexes (sizesof (t )), ret )
616+ tensors = pytree . tree_map (lambda t , i : deindex_tensor (t , i ), ret , indexes )
621617 return tensors
622618
623619 # reapply the stored indexes to a result
624620 def reindex (ret , starting_dim = 0 ):
625621 def index_expr (i ):
626622 return (slice (None ),) * (starting_dim ) + tuple (x () for x in i .indexes )
627623
628- if tree . is_nested (ret ):
629- indexed_ret = tree . map_structure (
624+ if not pytree . tree_is_leaf (ret ):
625+ indexed_ret = pytree . tree_map (
630626 lambda t , i : torch_getitem (t , index_expr (i )), ret , indexes
631627 )
632628 else :
@@ -677,7 +673,7 @@ def jvp(func, *args, **kwargs):
677673 # hide deindexed_func from _register_torch_op
678674 jvp_func = functools .partial (torch .func .jvp , deindexed_func )
679675 ret = _register_torch_op (jvp_func )(* args , ** kwargs )
680- return tree . map_structure (reindex , ret )
676+ return pytree . tree_map (reindex , ret )
681677
682678
683679@functools .wraps (torch .func .vjp )
@@ -699,15 +695,15 @@ def repack_primals(primals):
699695 def wrapper (* primals ):
700696 nonlocal indexed_result
701697 indexed_result = func (* repack_primals (primals ))
702- return tree . map_structure (
698+ return pytree . tree_map (
703699 lambda t : bind_dims (t , * list (sizesof (t ).keys ())), indexed_result
704700 )
705701
706702 unindexed_primals = [t [0 ] for t in unpacked_primals ]
707703 _ , vjpfunc = torch .func .vjp (wrapper , * unindexed_primals , ** kwargs )
708704
709705 def vjpfunc_wrapper (* tangents ):
710- unindexed_tangents = tree . map_structure (
706+ unindexed_tangents = pytree . tree_map (
711707 lambda t : bind_dims (t , * list (sizesof (t ).keys ())), tangents
712708 )
713709 grads = vjpfunc (* unindexed_tangents )
0 commit comments