Spaces:
Sleeping
Sleeping
remove phase2
Browse files
mltau/models/MultiParTau_module.py
CHANGED
|
@@ -173,64 +173,6 @@ class ParTauModule(L.LightningModule):
|
|
| 173 |
def test_step(self, batch, _batch_idx):
|
| 174 |
return self.forward(batch)[0]
|
| 175 |
|
| 176 |
-
def reinitialize_optimizer_for_phase2(self):
|
| 177 |
-
"""Freeze the shared backbone; fine-tune kinematics params + DM head.
|
| 178 |
-
|
| 179 |
-
The DM head remains trainable in Phase 2 to allow it to continue
|
| 180 |
-
adapting as the shared backbone weights might be updated by kinematics,
|
| 181 |
-
though they now use separate CLS tokens and blocks.
|
| 182 |
-
|
| 183 |
-
Both steps are required:
|
| 184 |
-
1. requires_grad_(False) on frozen params so PCGrad's `params` list
|
| 185 |
-
excludes them (no spurious zero gradients written into them).
|
| 186 |
-
2. Rebuild the AdamW optimizer with only the unfrozen params so that
|
| 187 |
-
momentum buffers are not accumulated for frozen weights.
|
| 188 |
-
"""
|
| 189 |
-
if self.phase2_initialized:
|
| 190 |
-
return
|
| 191 |
-
|
| 192 |
-
# 1. Freeze everything, then selectively unfreeze.
|
| 193 |
-
self.ParTau.requires_grad_(False)
|
| 194 |
-
kin_params = (
|
| 195 |
-
list(self.ParTau.regression_head.parameters())
|
| 196 |
-
+ list(self.ParTau.cls_blocks_kinematics.parameters())
|
| 197 |
-
+ list(self.ParTau.norm_kinematics.parameters())
|
| 198 |
-
+ [self.ParTau.cls_token_kinematics]
|
| 199 |
-
)
|
| 200 |
-
# DM head must adapt as kinematics representation changes.
|
| 201 |
-
dm_params = list(self.ParTau.classification_head.parameters())
|
| 202 |
-
for p in kin_params + dm_params:
|
| 203 |
-
p.requires_grad_(True)
|
| 204 |
-
|
| 205 |
-
base_lr = self.cfg.training.lr
|
| 206 |
-
kin_lr = base_lr * self.cfg.training.kinematics_lr_multiplier
|
| 207 |
-
new_optimizer = torch.optim.AdamW(
|
| 208 |
-
[
|
| 209 |
-
{"params": kin_params, "lr": kin_lr},
|
| 210 |
-
{"params": dm_params, "lr": base_lr},
|
| 211 |
-
],
|
| 212 |
-
weight_decay=1e-2,
|
| 213 |
-
)
|
| 214 |
-
# Use a simple CosineAnnealingLR for the fine-tuning phase.
|
| 215 |
-
# OneCycleLR is problematic when restarted mid-run as it expects to start from 0.
|
| 216 |
-
remaining = max(
|
| 217 |
-
1, self.trainer.estimated_stepping_batches - self.trainer.global_step
|
| 218 |
-
)
|
| 219 |
-
new_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
|
| 220 |
-
new_optimizer,
|
| 221 |
-
T_max=remaining,
|
| 222 |
-
eta_min=1e-7,
|
| 223 |
-
)
|
| 224 |
-
self.trainer.optimizers = [new_optimizer]
|
| 225 |
-
self.trainer.lr_scheduler_configs[0].scheduler = new_scheduler
|
| 226 |
-
n_kin = sum(p.numel() for p in kin_params)
|
| 227 |
-
n_dm = sum(p.numel() for p in dm_params)
|
| 228 |
-
print(
|
| 229 |
-
f"[Phase 2 @ epoch {self.current_epoch}] Backbone frozen. "
|
| 230 |
-
f"Trainable: {n_kin:,} kin params + {n_dm:,} DM head params"
|
| 231 |
-
)
|
| 232 |
-
self.phase2_initialized = True
|
| 233 |
-
|
| 234 |
def configure_optimizers(self):
|
| 235 |
# AdamW is generally preferred for transformer architectures.
|
| 236 |
base_lr = self.cfg.training.lr
|
|
|
|
| 173 |
def test_step(self, batch, _batch_idx):
|
| 174 |
return self.forward(batch)[0]
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def configure_optimizers(self):
|
| 177 |
# AdamW is generally preferred for transformer architectures.
|
| 178 |
base_lr = self.cfg.training.lr
|