[DeepSpeed] Fix evaluate()/predict() before train()#44889
Open
roycho96 wants to merge 7 commits intohuggingface:mainfrom
Open
[DeepSpeed] Fix evaluate()/predict() before train()#44889roycho96 wants to merge 7 commits intohuggingface:mainfrom
roycho96 wants to merge 7 commits intohuggingface:mainfrom
Conversation
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=44889&sha=c1f161 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Calling
trainer.evaluate()beforetrainer.train()with DeepSpeed is broken in three ways:ZeRO-3 stale state crash:
evaluate()creates an inference engine.train()starts withaccelerator.free_memory()which destroys it and setsdeepspeed_engine_wrapped = None, but Trainer'smodel_wrapped/deepspeedstill hold stale refs, causingAttributeError: 'NoneType' has no attribute 'backward'.ZeRO-3 shared config mutation:
deepspeed_init(inference=True)mutates the shared config in-place:trainer_config_finalize(num_training_steps=0)bakes scheduler"auto"values to0(causing mismatch on subsequenttrain()), anddel_config_sub_tree("optimizer")removes the optimizer config (fallback to HF optimizer).ZeRO-1/2 evaluate blocked:
evaluation_loop()unconditionally callsdeepspeed_init(inference=True)which raisesValueErrorfor non-ZeRO-3 stages. ZeRO-1/2 have full parameters on each GPU and don't need an inference engine.evaluate()beforetrain()is a valid pattern. For example, baseline metric collection, conditional training based on eval results, or data pipeline validation.eval_on_start=Truecovers some cases but doesn't help when the user needs eval metrics before deciding whether to train.Fix
Reset stale Trainer refs at start of
_prepare_for_training()whendeepspeed_engine_wrappedis alreadyNonebutmodel_wrappedstill points to a destroyed engine. Follows existing pattern in_update_auto_batch_size().In
evaluation_loop(), gatedeepspeed_init(inference=True)onis_zero3()only. For ZeRO-1/2, useprepare_model(evaluation_mode=True)to bypass DS engine creation entirely. Protect shared config with backup/restore intry/finally.eval_on_start=Trueis unaffected.Related issues
Before submitting
Who can review?
@SunMarc @3outeille