Skip to content

Commit c7cbb75

Browse files
Sophiex/dev/ema fix inference (ecmwf#1094)
* Save current state * Save current state * Barebone FSDP2 prototype TODO save checkpoints * First version of saving model * Fix save_model * Log everything and log to files * Remove redundant path creation * Allow for both slurm and torchrun + fewer log files * Cleaning up init_ddp * Ruff * Attempt to avoid duplicate logging * FSDP2 with mixed precision policy * Ruff * Clean up and logging * Try to get loggers to behave as we want * Makes ruff unhappy but works * Fixed ruff issue * Fixed problems with multi-node training. * Fix for interactive/non-DDP runs * No idea why, but this seems to work so far Committing simply so it is saved, obviously needs cleanup * Still works! So which is it memory or the grad scaler? * Also still works, I now strongly suspect the amp.gradscaler * This still works, I have no clue anymore why but whatever it works now.... * Enable loading model from absolute paths * Enable loading for 1 GPU only * Fix 1 GPU train continue * Appease ruff * Fix saving the model more regularly and perf logging * Fixed problem when training with 2 nodes. * Fix data loader seed * Appease ruff * Shouldn't overwrite with_fsdp like this * Potential fix for FSDP2 issue with different ranks using different model parts * Fix loss scaling and logging of dummy data loss * Clean up * Appease ruff * Start implementing EMA, works for 1 GPU * Make EMA model multi-gpu compatible * Fix linting issues * Address comments on PR * Enforce the ema model to strictly be the same as model Note this likely needs to be changed for student-teacher training * Rename variable * Fix inference * One more thing * Fixed linting * Fix problem with checkpoint saving when validate_with_ema is false or not specified --------- Co-authored-by: Christian Lessig <christian.lessig@ecmwf.int>
1 parent c17ad41 commit c7cbb75

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/weathergen/train/trainer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def inference(self, cf, devices, run_id_trained, epoch):
104104
cf = self.cf
105105
self.device_type = torch.accelerator.current_accelerator()
106106
self.device = torch.device(f"{self.device_type}:{cf.local_rank}")
107+
self.ema_model = None
107108

108109
# !! modifies config: adds config.streams[i].<stage>_source_channels
109110
# and config.streams[i].<stage>_target_channels !!
@@ -298,6 +299,7 @@ def run(self, cf, devices, run_id_contd=None, epoch_contd=None):
298299
self.model = torch.compile(self.model, dynamic=True)
299300

300301
self.validate_with_ema = cf.get("validate_with_ema", False)
302+
self.ema_model = None
301303
if self.validate_with_ema:
302304
meta_ema_model = self.init_model_and_shard(cf, devices)[0]
303305
self.ema_model = EMAModel(
@@ -642,7 +644,12 @@ def validate(self, epoch):
642644
dtype=self.mixed_precision_dtype,
643645
enabled=cf.with_mixed_precision,
644646
):
645-
preds, _ = self.ema_model.forward_eval(
647+
model_forward = (
648+
self.model.forward
649+
if self.ema_model is None
650+
else self.ema_model.forward_eval
651+
)
652+
preds, _ = model_forward(
646653
self.model_params, batch, cf.forecast_offset, forecast_steps
647654
)
648655

0 commit comments

Comments
 (0)