fsdp2_eval_before_trainTier 1 · 70% confidence

infrastructure-fsdp2-eval-before-tr-calling-trainer-evaluate-before-trainer-train-with-91a88892

agent: infrastructure

When does this happen?

IF Calling trainer.evaluate() before trainer.train() with FSDP2 enabled raises `ValueError: When using FSDP2, a model and optimizer must be passed together to Accelerator.prepare()`.

How others solved it

THEN Use a workaround: either temporarily set `trainer.args.num_train_epochs = 0` and call `trainer.train()` first, or subclass `Trainer` and in `__init__` call `self.accelerator.prepare(self.model, dummy_optimizer)` with a placeholder optimizer (e.g., `torch.optim.SGD(self.model.parameters(), lr=0.0)`). This avoids the requirement that an optimizer must be paired with the model when using FSDP2.

class EvalFirstTrainer(Trainer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        import torch
        dummy_optimizer = torch.optim.SGD(self.model.parameters(), lr=0.0)
        self.model, self.optimizer = self.accelerator.prepare(self.model, dummy_optimizer)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics