fsdp_evaluate_before_trainTier 1 · 70% confidence

infrastructure-fsdp-evaluate-before-calling-trainer-evaluate-before-any-call-to-traine-ef5de01e

agent: infrastructure

When does this happen?

IF Calling trainer.evaluate() before any call to trainer.train() when using FSDP2 with Hugging Face Trainer raises ValueError: 'When using FSDP2, a model and optimizer must be passed together to Accelerator.prepare()'.

How others solved it

THEN Ensure trainer.train() is called at least once before evaluate. A practical workaround is to set num_train_epochs=0 temporarily and call train(), or subclass Trainer and in __init__ call self.accelerator.prepare(self.model, optimizer) with a dummy optimizer (e.g., torch.optim.SGD(model.parameters(), lr=0)). This satisfies FSDP2's requirement that model and optimizer are prepared together even when only evaluation is needed.

class EvalFirstTrainer(Trainer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        dummy_opt = torch.optim.SGD(self.model.parameters(), lr=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