MCPcopy Create free account
hub / github.com/NVlabs/RADIO / IntermediateFeatureNormalizer

Class IntermediateFeatureNormalizer

radio/feature_normalizer.py:49–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47
48
49class IntermediateFeatureNormalizer(IntermediateFeatureNormalizerBase):
50 def __init__(self, num_intermediates: int, embed_dim: int, rot_per_layer: bool = False, dtype: torch.dtype = torch.float32):
51 super().__init__()
52 self.register_buffer('alphas', torch.ones(num_intermediates, dtype=dtype))
53
54 rot = torch.eye(embed_dim, dtype=dtype)
55 if rot_per_layer:
56 rot = rot.unsqueeze(0).repeat(num_intermediates, 1, 1)
57
58 self.register_buffer('rotation', rot.contiguous())
59 self.register_buffer('means', torch.zeros(num_intermediates, embed_dim, dtype=dtype))
60
61 def forward(self, x: torch.Tensor, index: int, rot_index: int = None, skip: Optional[int] = None) -> InterFeatState:
62 if rot_index is None:
63 rot_index = index
64
65 if skip:
66 assert x.ndim == 3, f'Cannot use the `skip` parameter when the `x` tensor isn\'t 3-dimensional.'
67 prefix, x = x[:, :skip], x[:, skip:]
68
69 rotation = self._get_rotation(rot_index)
70 y = _run_kernel(x, self.means[index], rotation)
71
72 alpha = self.alphas[index]
73 if skip:
74 alpha = torch.cat([
75 torch.ones(skip, dtype=alpha.dtype, device=alpha.device),
76 alpha[None].expand(y.shape[1]),
77 ]).reshape(1, -1, 1)
78 y = torch.cat([prefix, y], dim=1)
79 else:
80 if x.ndim == 3:
81 alpha = alpha.reshape(1, 1, 1).expand(1, y.shape[1], 1)
82 elif x.ndim == 4:
83 alpha = alpha.reshape(1, 1, 1, 1).expand(1, 1, *y.shape[2:])
84 else:
85 raise ValueError(f'Unsupported input dimension: {x.ndim}')
86
87 return InterFeatState(y, alpha)
88
89 def _get_rotation(self, rot_index: int) -> torch.Tensor:
90 if self.rotation.ndim == 2:
91 return self.rotation
92 return self.rotation[rot_index]
93
94
95class NullIntermediateFeatureNormalizer(IntermediateFeatureNormalizerBase):

Callers 2

radio_modelFunction · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…