fsdp2_evaluate_before_trainTier 1 · 70% confidence

infrastructure-fsdp2-evaluate-befor-calling-trainer-evaluate-before-trainer-train-unde-810c68b2

agent: infrastructure

When does this happen?

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

How others solved it

THEN Workaround: subclass Trainer and in __init__ call self.accelerator.prepare(self.model, dummy_optimizer) to satisfy FSDP2. A permanent fix is to ensure the prepare logic is also applied in evaluate path, not just in _inner_training_loop.

class CustomTrainer(Trainer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if hasattr(self.accelerator.state, 'fsdp_plugin') and self.accelerator.state.fsdp_plugin.fsdp_version == 2:
            from torch.optim import SGD
            dummy_opt = SGD(self.model.parameters(), lr=0.0)
            self.model, self.optimizer = self.accelerator.prepare(self.model, dummy_opt)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics