Spaces:
Paused
Paused
Download src/misc/collation.py from timfromhcs/AnySplat: direct link, hf CLI and curl.
- Browser
- Download file 433 Bytes
-
https://huggingface.co/spaces/timfromhcs/AnySplat/resolve/main/src/misc/collation.py
- Command line
-
hf download hf://spaces/timfromhcs/AnySplat/src/misc/collation.py
-
curl -L -o collation.py https://huggingface.co/spaces/timfromhcs/AnySplat/resolve/main/src/misc/collation.py
433 Bytes
| from typing import Callable, Dict, Union | |
| from torch import Tensor | |
| Tree = Union[Dict[str, "Tree"], Tensor] | |
| def collate(trees: list[Tree], merge_fn: Callable[[list[Tensor]], Tensor]) -> Tree: | |
| """Merge nested dictionaries of tensors.""" | |
| if isinstance(trees[0], Tensor): | |
| return merge_fn(trees) | |
| else: | |
| return { | |
| key: collate([tree[key] for tree in trees], merge_fn) for key in trees[0] | |
| } | |