-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstack.py
More file actions
55 lines (41 loc) · 2.06 KB
/
stack.py
File metadata and controls
55 lines (41 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Iterable, Sequence
import torch
from torch import Tensor
from ._utils import _A, _KeyType, _materialize, _ValueType
from .base import Transform
from .tensor_dict import Gradients, Jacobians
class Stack(Transform[_A, Jacobians]):
def __init__(self, transforms: Sequence[Transform[_A, Gradients]]):
self.transforms = transforms
def __call__(self, input: _A) -> Jacobians:
results = [transform(input) for transform in self.transforms]
result = _stack(results)
return result
def check_keys(self, input_keys: set[Tensor]) -> set[Tensor]:
return {key for transform in self.transforms for key in transform.check_keys(input_keys)}
def _stack(gradient_dicts: list[Gradients]) -> Jacobians:
"""
Transforms a list of tensor dicts into a single dict of (stacked) tensors. The set of keys of
the resulting dict is the union of the sets of keys of the input dicts.
If a key is absent in some input dicts, the corresponding stacked tensor is filled with zeroes
at the positions corresponding to those dicts.
"""
# It is important to first remove duplicate keys before computing their associated
# stacked tensor. Otherwise, some computations would be duplicated. Therefore, we first compute
# unique_keys, and only then, we compute the stacked tensors.
unique_keys = _dicts_union(gradient_dicts).keys()
result = Jacobians({key: _stack_one_key(gradient_dicts, key) for key in unique_keys})
return result
def _stack_one_key(gradient_dicts: list[Gradients], input: Tensor) -> Tensor:
"""
Makes the stacked tensor corresponding to a given key, from a list of tensor dicts.
"""
optional_gradients = [gradients.get(input, None) for gradients in gradient_dicts]
gradients = _materialize(optional_gradients, [input] * len(optional_gradients))
jacobian = torch.stack(gradients, dim=0)
return jacobian
def _dicts_union(dicts: Iterable[dict[_KeyType, _ValueType]]) -> dict[_KeyType, _ValueType]:
result = {}
for d in dicts:
result |= d
return result