fix(train): keep AdaptiveLossBalancer forward signature intact, expose last_weights attribute
Browse files- src/pino/thermo/losses.py +7 -2
- src/pino/train.py +7 -6
src/pino/thermo/losses.py
CHANGED
|
@@ -18,12 +18,15 @@ class AdaptiveLossBalancer(nn.Module):
|
|
| 18 |
self.register_buffer("initial_losses", torch.zeros(num_tasks))
|
| 19 |
self.register_buffer("_initialized", torch.tensor(0, dtype=torch.bool))
|
| 20 |
|
| 21 |
-
def forward(self, loss_dict: dict[str, torch.Tensor]) ->
|
| 22 |
"""
|
| 23 |
Balances losses based on their progress relative to initial baselines.
|
| 24 |
|
| 25 |
Tasks that improve more slowly receive higher weights, preventing a single
|
| 26 |
task from dominating the backward pass.
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
losses = torch.stack(list(loss_dict.values()))
|
| 29 |
|
|
@@ -40,4 +43,6 @@ class AdaptiveLossBalancer(nn.Module):
|
|
| 40 |
weights = ratios / (torch.sum(ratios) + 1e-8)
|
| 41 |
total_loss = torch.sum(losses * weights * float(len(losses)))
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
| 18 |
self.register_buffer("initial_losses", torch.zeros(num_tasks))
|
| 19 |
self.register_buffer("_initialized", torch.tensor(0, dtype=torch.bool))
|
| 20 |
|
| 21 |
+
def forward(self, loss_dict: dict[str, torch.Tensor]) -> torch.Tensor:
|
| 22 |
"""
|
| 23 |
Balances losses based on their progress relative to initial baselines.
|
| 24 |
|
| 25 |
Tasks that improve more slowly receive higher weights, preventing a single
|
| 26 |
task from dominating the backward pass.
|
| 27 |
+
|
| 28 |
+
The computed weights are stored in ``self.last_weights`` as a detached
|
| 29 |
+
CPU list so training loops can log them without modifying the loss flow.
|
| 30 |
"""
|
| 31 |
losses = torch.stack(list(loss_dict.values()))
|
| 32 |
|
|
|
|
| 43 |
weights = ratios / (torch.sum(ratios) + 1e-8)
|
| 44 |
total_loss = torch.sum(losses * weights * float(len(losses)))
|
| 45 |
|
| 46 |
+
self.last_weights = weights.detach().cpu().tolist()
|
| 47 |
+
|
| 48 |
+
return total_loss
|
src/pino/train.py
CHANGED
|
@@ -88,7 +88,7 @@ def multitask_loss(
|
|
| 88 |
target_sub: dict[str, torch.Tensor],
|
| 89 |
pred_alignment: torch.Tensor,
|
| 90 |
loss_balancer: AdaptiveLossBalancer,
|
| 91 |
-
) -> tuple[torch.Tensor, dict[str, float]
|
| 92 |
"""
|
| 93 |
Adaptive multi-task loss using the AdaptiveLossBalancer.
|
| 94 |
|
|
@@ -122,7 +122,7 @@ def multitask_loss(
|
|
| 122 |
"alignment": alignment_loss,
|
| 123 |
}
|
| 124 |
|
| 125 |
-
total
|
| 126 |
components = {
|
| 127 |
"obj": obj_loss.item(),
|
| 128 |
"season": season_loss.item(),
|
|
@@ -130,7 +130,7 @@ def multitask_loss(
|
|
| 130 |
"alignment": alignment_loss.item(),
|
| 131 |
"total": total.item(),
|
| 132 |
}
|
| 133 |
-
return total, components
|
| 134 |
|
| 135 |
|
| 136 |
def train_epoch(
|
|
@@ -163,7 +163,7 @@ def train_epoch(
|
|
| 163 |
optimizer.zero_grad()
|
| 164 |
latent = model(tokens, physics, src_key_padding_mask) # (B, T, S, H)
|
| 165 |
pred = heads(latent)
|
| 166 |
-
loss, comps
|
| 167 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 168 |
pred["alignment"], loss_balancer,
|
| 169 |
)
|
|
@@ -175,10 +175,11 @@ def train_epoch(
|
|
| 175 |
for k in totals:
|
| 176 |
totals[k] += comps[k]
|
| 177 |
count += 1
|
|
|
|
| 178 |
logger.info(
|
| 179 |
"Batch %d | total=%.4f | obj=%.4f | season=%.4f | wear=%.4f | alignment=%.4f | weights=[%.3f, %.3f, %.3f, %.3f]",
|
| 180 |
batch_idx, comps["total"], comps["obj"], comps["season"], comps["wear"], comps["alignment"],
|
| 181 |
-
weights[0]
|
| 182 |
)
|
| 183 |
|
| 184 |
return {k: v / max(count, 1) for k, v in {**totals, "total": total_loss}.items()}
|
|
@@ -210,7 +211,7 @@ def validate(
|
|
| 210 |
}
|
| 211 |
latent = model(tokens, physics, src_key_padding_mask)
|
| 212 |
pred = heads(latent)
|
| 213 |
-
loss, comps
|
| 214 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 215 |
pred["alignment"], loss_balancer,
|
| 216 |
)
|
|
|
|
| 88 |
target_sub: dict[str, torch.Tensor],
|
| 89 |
pred_alignment: torch.Tensor,
|
| 90 |
loss_balancer: AdaptiveLossBalancer,
|
| 91 |
+
) -> tuple[torch.Tensor, dict[str, float]]:
|
| 92 |
"""
|
| 93 |
Adaptive multi-task loss using the AdaptiveLossBalancer.
|
| 94 |
|
|
|
|
| 122 |
"alignment": alignment_loss,
|
| 123 |
}
|
| 124 |
|
| 125 |
+
total = loss_balancer(loss_payload)
|
| 126 |
components = {
|
| 127 |
"obj": obj_loss.item(),
|
| 128 |
"season": season_loss.item(),
|
|
|
|
| 130 |
"alignment": alignment_loss.item(),
|
| 131 |
"total": total.item(),
|
| 132 |
}
|
| 133 |
+
return total, components
|
| 134 |
|
| 135 |
|
| 136 |
def train_epoch(
|
|
|
|
| 163 |
optimizer.zero_grad()
|
| 164 |
latent = model(tokens, physics, src_key_padding_mask) # (B, T, S, H)
|
| 165 |
pred = heads(latent)
|
| 166 |
+
loss, comps = multitask_loss(
|
| 167 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 168 |
pred["alignment"], loss_balancer,
|
| 169 |
)
|
|
|
|
| 175 |
for k in totals:
|
| 176 |
totals[k] += comps[k]
|
| 177 |
count += 1
|
| 178 |
+
weights = getattr(loss_balancer, "last_weights", [0.25, 0.25, 0.25, 0.25])
|
| 179 |
logger.info(
|
| 180 |
"Batch %d | total=%.4f | obj=%.4f | season=%.4f | wear=%.4f | alignment=%.4f | weights=[%.3f, %.3f, %.3f, %.3f]",
|
| 181 |
batch_idx, comps["total"], comps["obj"], comps["season"], comps["wear"], comps["alignment"],
|
| 182 |
+
weights[0], weights[1], weights[2], weights[3],
|
| 183 |
)
|
| 184 |
|
| 185 |
return {k: v / max(count, 1) for k, v in {**totals, "total": total_loss}.items()}
|
|
|
|
| 211 |
}
|
| 212 |
latent = model(tokens, physics, src_key_padding_mask)
|
| 213 |
pred = heads(latent)
|
| 214 |
+
loss, comps = multitask_loss(
|
| 215 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 216 |
pred["alignment"], loss_balancer,
|
| 217 |
)
|