feat(train): log AdaptiveLossBalancer weights per batch for telemetry verification
Browse files- src/pino/thermo/losses.py +2 -2
- src/pino/train.py +7 -6
src/pino/thermo/losses.py
CHANGED
|
@@ -18,7 +18,7 @@ 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]) -> torch.Tensor:
|
| 22 |
"""
|
| 23 |
Balances losses based on their progress relative to initial baselines.
|
| 24 |
|
|
@@ -40,4 +40,4 @@ class AdaptiveLossBalancer(nn.Module):
|
|
| 40 |
weights = ratios / (torch.sum(ratios) + 1e-8)
|
| 41 |
total_loss = torch.sum(losses * weights * float(len(losses)))
|
| 42 |
|
| 43 |
-
return total_loss
|
|
|
|
| 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]) -> tuple[torch.Tensor, torch.Tensor]:
|
| 22 |
"""
|
| 23 |
Balances losses based on their progress relative to initial baselines.
|
| 24 |
|
|
|
|
| 40 |
weights = ratios / (torch.sum(ratios) + 1e-8)
|
| 41 |
total_loss = torch.sum(losses * weights * float(len(losses)))
|
| 42 |
|
| 43 |
+
return total_loss, weights.detach()
|
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 = loss_balancer(loss_payload)
|
| 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 = multitask_loss(
|
| 167 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 168 |
pred["alignment"], loss_balancer,
|
| 169 |
)
|
|
@@ -176,8 +176,9 @@ def train_epoch(
|
|
| 176 |
totals[k] += comps[k]
|
| 177 |
count += 1
|
| 178 |
logger.info(
|
| 179 |
-
"Batch %d | total=%.4f | obj=%.4f | season=%.4f | wear=%.4f | alignment=%.4f",
|
| 180 |
batch_idx, comps["total"], comps["obj"], comps["season"], comps["wear"], comps["alignment"],
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
return {k: v / max(count, 1) for k, v in {**totals, "total": total_loss}.items()}
|
|
@@ -209,7 +210,7 @@ def validate(
|
|
| 209 |
}
|
| 210 |
latent = model(tokens, physics, src_key_padding_mask)
|
| 211 |
pred = heads(latent)
|
| 212 |
-
loss, comps = multitask_loss(
|
| 213 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 214 |
pred["alignment"], loss_balancer,
|
| 215 |
)
|
|
|
|
| 88 |
target_sub: dict[str, torch.Tensor],
|
| 89 |
pred_alignment: torch.Tensor,
|
| 90 |
loss_balancer: AdaptiveLossBalancer,
|
| 91 |
+
) -> tuple[torch.Tensor, dict[str, float], torch.Tensor]:
|
| 92 |
"""
|
| 93 |
Adaptive multi-task loss using the AdaptiveLossBalancer.
|
| 94 |
|
|
|
|
| 122 |
"alignment": alignment_loss,
|
| 123 |
}
|
| 124 |
|
| 125 |
+
total, weights = 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, weights
|
| 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, weights = multitask_loss(
|
| 167 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 168 |
pred["alignment"], loss_balancer,
|
| 169 |
)
|
|
|
|
| 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].item(), weights[1].item(), weights[2].item(), weights[3].item(),
|
| 182 |
)
|
| 183 |
|
| 184 |
return {k: v / max(count, 1) for k, v in {**totals, "total": total_loss}.items()}
|
|
|
|
| 210 |
}
|
| 211 |
latent = model(tokens, physics, src_key_padding_mask)
|
| 212 |
pred = heads(latent)
|
| 213 |
+
loss, comps, weights = multitask_loss(
|
| 214 |
pred["objective"], target_obj, pred["subjective"], target_sub,
|
| 215 |
pred["alignment"], loss_balancer,
|
| 216 |
)
|