This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
forked from aniruddhraghu/meta-pretraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperparam_utils.py
163 lines (122 loc) · 4.48 KB
/
hyperparam_utils.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from typing import Callable, Iterable, Tuple
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import grad
from engine.utils import get_loss_simclr
__all__ = [
"zero_hypergrad",
"store_hypergrad",
"neumann_hyperstep_preconditioner",
"gather_flat_grad",
"hyper_step",
]
# ======================== Zero Hypergrad ======================== #
def zero_hypergrad(hyper_params: Iterable) -> None:
"""Function to zero out all the hyperparameters"""
current_index: int = 0
for p in hyper_params:
p_num_params = np.prod(p.shape)
if p.grad is not None:
p.grad = p.grad * 0
current_index += p_num_params
# ======================== Zero Hypergrad ======================== #
def store_hypergrad(
hyper_params: Iterable, total_d_val_loss_d_lambda: torch.Tensor
) -> None:
current_index: int = 0
for p in hyper_params:
p_num_params = np.prod(p.shape)
p.grad = total_d_val_loss_d_lambda[
current_index : current_index + p_num_params
].view(p.shape)
current_index += p_num_params
# ======================== Gather Flat Gradients ======================== #
def gather_flat_grad(loss_grad: Tuple[torch.Tensor, ...]) -> torch.Tensor:
return torch.cat([p.reshape(-1) for p in loss_grad])
# ======================== Neumann Hyperstep Preconditioner ======================== #
def neumann_hyperstep_preconditioner(
d_val_loss_d_theta: torch.Tensor,
d_train_loss_d_w: torch.Tensor,
elementary_lr: float,
num_neumann_terms: int,
model: nn.Module,
head: nn.Module,
) -> torch.Tensor:
preconditioner: torch.Tensor = d_val_loss_d_theta.detach()
counter: torch.Tensor = preconditioner
i: int = 0
while i < num_neumann_terms:
old_counter = counter
hessian_term = gather_flat_grad(
grad(
d_train_loss_d_w,
list(model.parameters()) + list(head.parameters()),
grad_outputs=counter.view(-1),
retain_graph=True,
)
)
counter = old_counter - elementary_lr * hessian_term
preconditioner = preconditioner + counter
i += 1
return elementary_lr * preconditioner
# ======================== A "Hyper" Step ======================== #
def hyper_step(
model: nn.Module,
head: nn.Module,
teacher: nn.Module,
hyper_params: Iterable,
pretrain_loader: Iterable,
criterion: Callable,
optimizer: torch.optim.Optimizer,
d_val_loss_d_theta: torch.Tensor,
elementary_lr: float,
neum_steps: int,
device: torch.device,
) -> torch.Tensor:
# Zero out all Hyper Parameters
zero_hypergrad(hyper_params)
# Number of weights in our Model and Head
num_weights: int = sum(p.numel() for p in model.parameters()) + sum(
p.numel() for p in head.parameters()
)
d_train_loss_d_w: torch.Tensor = torch.zeros(num_weights).to(device)
# Initialize Model and Head
model.train(), model.zero_grad(), head.train(), head.zero_grad() # type: ignore
# NOTE: This should be the pretrain set: gradient of PRETRAINING loss wrt pretrain parameters.
for _, (xis, xjs) in enumerate(pretrain_loader):
# Shift Tensors to device
xis = xis.to(device)
xjs = xjs.to(device)
if teacher is not None:
xis = teacher(xis)
xjs = teacher(xjs)
# Calculate Training Loss
train_loss: torch.Tensor = get_loss_simclr(model, criterion, xis, xjs)
train_loss = (
train_loss + train_loss * head(model.logits(xis)).sum() * 0 # type: ignore
)
# Zero out the Optimizer
optimizer.zero_grad()
d_train_loss_d_w += gather_flat_grad(
grad(
train_loss,
list(model.parameters()) + list(head.parameters()),
create_graph=True,
allow_unused=True,
)
)
break
optimizer.zero_grad()
# Initialize the preconditioner and counter
preconditioner: torch.Tensor = d_val_loss_d_theta
preconditioner = neumann_hyperstep_preconditioner(
d_val_loss_d_theta, d_train_loss_d_w, elementary_lr, neum_steps, model, head
)
indirect_grad: torch.Tensor = gather_flat_grad(
grad(d_train_loss_d_w, hyper_params, grad_outputs=preconditioner.view(-1)) # type: ignore
)
hypergrad = indirect_grad
zero_hypergrad(hyper_params)
store_hypergrad(hyper_params, -hypergrad)
return hypergrad