[pytree] Only import optree if it's used (#131478)

torch.utils._pytree imports optree if it's available. Instead, we change
it to if it gets used. The motivation for this is better isolation.

Test Plan:
- new tests
Pull Request resolved: https://github.com/pytorch/pytorch/pull/131478
Approved by: https://github.com/albanD
This commit is contained in:
rzou
2024-07-23 10:07:20 -07:00
committed by PyTorch MergeBot
parent 6850e42266
commit 480ae51f85
3 changed files with 59 additions and 5 deletions

View File

@ -32,8 +32,9 @@ from typing_extensions import deprecated
import optree
from optree import PyTreeSpec # direct import for type annotations
from torch.utils._pytree import KeyEntry
import torch.utils._pytree as _pytree
from torch.utils._pytree import KeyEntry
__all__ = [
"PyTree",
@ -999,3 +1000,8 @@ def keystr(kp: KeyPath) -> str:
def key_get(obj: Any, kp: KeyPath) -> Any:
"""Given an object and a key path, return the value at the key path."""
raise NotImplementedError("KeyPaths are not yet supported in cxx_pytree.")
_pytree._cxx_pytree_imported = True
for args, kwargs in _pytree._cxx_pytree_pending_imports:
_private_register_pytree_node(*args, **kwargs)