Download runtime/src/pixelone/model.py from KangHui2200/PixelOne: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/KangHui2200/PixelOne/resolve/main/runtime/src/pixelone/model.py
- Command line
-
hf download hf://KangHui2200/PixelOne/runtime/src/pixelone/model.py
-
curl -L -o model.py https://huggingface.co/KangHui2200/PixelOne/resolve/main/runtime/src/pixelone/model.py
3.07 kB
| import torch | |
| from torch import nn | |
| from pixelone.batching import forward_states | |
| class FineReadout(nn.Module): | |
| def __init__(self, hidden, width=128, subdivisions=32): | |
| super().__init__() | |
| self.norm = nn.LayerNorm(hidden) | |
| self.visual = nn.Linear(hidden, width) | |
| self.query = nn.Linear(hidden, 2 * width) | |
| self.local = nn.Conv2d(width, width, 3, padding=1, groups=width) | |
| self.mix = nn.Sequential(nn.Linear(width, width), nn.GELU(), nn.Linear(width, subdivisions**2)) | |
| def forward(self, spatial, query, layout, parents=None): | |
| if spatial.shape[0] != layout.count: | |
| raise ValueError('Lost visual spatial layout') | |
| values = self.visual(self.norm(spatial.float())) | |
| scale, bias = self.query(self.norm(query.float())).chunk(2, -1) | |
| values = values * (1 + scale.tanh()) + bias | |
| grid = values.T.reshape(1, -1, layout.rows, layout.cols) | |
| values = values + self.local(grid).reshape(values.shape[1], -1).T | |
| if parents is not None: | |
| values = values[parents] | |
| return self.mix(values) | |
| class FullWeightStudent(nn.Module): | |
| def __init__(self, native, recipe): | |
| super().__init__() | |
| self.native, self.recipe = native, recipe | |
| self.native.head.bias = nn.Parameter(torch.zeros(1)) | |
| self.spatial = FineReadout(native.head.q.in_features, recipe['readout_width'], recipe['subdivisions']) | |
| self.native.full_parameter_training = True | |
| self.native.train_vision = recipe['train_vision'] | |
| def forward(self, groups, features, layouts): | |
| if not isinstance(groups, (tuple, list)): | |
| raise ValueError('Full inference requires a batch, including for one screenshot') | |
| count = len(groups) | |
| if (not count or len(features) != count or len(layouts) != count or | |
| any(len(group['query_indices']) != 1 for group in groups)): | |
| raise ValueError('Each batch item requires one screenshot and one query') | |
| captured = [] | |
| hook = self.native.head.register_forward_pre_hook(lambda module, args: captured.append(args)) | |
| try: | |
| forward_states(self.native, groups, features) | |
| finally: | |
| hook.remove() | |
| if len(captured) != 1: | |
| raise ValueError('Expected one batched native readout') | |
| pairs, queries = captured[0] | |
| if pairs.shape[0] != count or queries.shape[0] != count: | |
| raise ValueError('Lost batched sample identities') | |
| result = [] | |
| for index, layout in enumerate(layouts): | |
| spatial, query = pairs[index, :layout.count], queries[index] | |
| if len(features[index]) != layout.count: | |
| raise ValueError('Visual grid differs from spatial layout') | |
| coarse = self.native.head(spatial[None], query[None])[0] | |
| parents = coarse.detach().argmax().reshape(1) | |
| result.append(dict(coarse=coarse, fine=self.spatial(spatial, query, layout, parents), parents=parents)) | |
| return result | |
| def close(self): | |
| pass | |