-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Allow GradientAccumulationPlugin to be configured from AcceleratorConfig #29589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4cb501d
3d1a3d5
2161e07
1860ab5
b021316
3d1f632
796a910
ff7e171
c8a0a86
968d415
4c579dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from functools import partial | ||
| from itertools import product | ||
| from pathlib import Path | ||
| from typing import Dict, List | ||
|
|
@@ -91,6 +92,7 @@ | |
| SAFE_WEIGHTS_NAME, | ||
| WEIGHTS_INDEX_NAME, | ||
| WEIGHTS_NAME, | ||
| is_accelerate_available, | ||
| is_apex_available, | ||
| is_bitsandbytes_available, | ||
| is_safetensors_available, | ||
|
|
@@ -791,6 +793,9 @@ def test_tf32(self): | |
| @require_sentencepiece | ||
| @require_tokenizers | ||
| class TrainerIntegrationTest(TestCasePlus, TrainerIntegrationCommon): | ||
| GRAD_ACCUM_KWARGS_VERSION_AVAILABLE = is_accelerate_available("0.28") | ||
| require_accelerate_version = partial(require_accelerate, min_version="0.28") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this logic, let's just use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @muellerzr sorry can I clarify this remark?
def require_accelerate(min_version: str == "0.28"):
def _require(test):
return unittest.skipUnless(is_accelerate_available(), "test requires accelerate")(test_case)
return _requireBut the issue with this is the null pattern
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just mimic what's done in require_fsdp_version = require_fsdp
if is_accelerate_available():
...
require_fsdp_version = partial(require_fsdp, min_version=FSDP_PYTORCH_VERSION)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you can then just use |
||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| args = TrainingArguments("..") | ||
|
|
@@ -2499,6 +2504,10 @@ def test_accelerator_config_empty(self): | |
| self.assertEqual(trainer.accelerator.even_batches, True) | ||
| self.assertEqual(trainer.accelerator.use_seedable_sampler, True) | ||
|
|
||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| # gradient accumulation kwargs configures gradient_state | ||
| self.assertNotIn("sync_each_batch", trainer.accelerator.gradient_state.plugin_kwargs) | ||
|
|
||
| def test_accelerator_config_from_dict(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
@@ -2507,22 +2516,29 @@ def test_accelerator_config_from_dict(self): | |
| model = RegressionPreTrainedModel(config) | ||
| eval_dataset = SampleIterableDataset() | ||
|
|
||
| accelerator_config = { | ||
| "split_batches": True, | ||
| "dispatch_batches": True, | ||
| "even_batches": False, | ||
| "use_seedable_sampler": True, | ||
| } | ||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| accelerator_config["gradient_accumulation_kwargs"] = {"sync_each_batch": True} | ||
|
|
||
| # Leaves all options as something *not* basic | ||
| args = RegressionTrainingArguments( | ||
| output_dir=tmp_dir, | ||
| accelerator_config={ | ||
| "split_batches": True, | ||
| "dispatch_batches": True, | ||
| "even_batches": False, | ||
| "use_seedable_sampler": True, | ||
| }, | ||
| accelerator_config=accelerator_config, | ||
| ) | ||
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertEqual(trainer.accelerator.split_batches, True) | ||
| self.assertEqual(trainer.accelerator.dispatch_batches, True) | ||
| self.assertEqual(trainer.accelerator.even_batches, False) | ||
| self.assertEqual(trainer.accelerator.use_seedable_sampler, True) | ||
|
|
||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["sync_each_batch"], True) | ||
|
|
||
| def test_accelerator_config_from_yaml(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
@@ -2535,6 +2551,8 @@ def test_accelerator_config_from_yaml(self): | |
| "even_batches": False, | ||
| "use_seedable_sampler": False, | ||
|
fabianlim marked this conversation as resolved.
|
||
| } | ||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| accelerator_config["gradient_accumulation_kwargs"] = {"sync_each_batch": True} | ||
| json.dump(accelerator_config, f) | ||
| config = RegressionModelConfig(a=1.5, b=2.5) | ||
| model = RegressionPreTrainedModel(config) | ||
|
|
@@ -2548,11 +2566,18 @@ def test_accelerator_config_from_yaml(self): | |
| self.assertEqual(trainer.accelerator.even_batches, False) | ||
| self.assertEqual(trainer.accelerator.use_seedable_sampler, False) | ||
|
|
||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["sync_each_batch"], True) | ||
|
|
||
| def test_accelerator_config_from_dataclass(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
||
| accelerator_config = AcceleratorConfig( | ||
| split_batches=True, dispatch_batches=True, even_batches=False, use_seedable_sampler=False | ||
| split_batches=True, | ||
| dispatch_batches=True, | ||
| even_batches=False, | ||
| use_seedable_sampler=False, | ||
| ) | ||
| config = RegressionModelConfig(a=1.5, b=2.5) | ||
| model = RegressionPreTrainedModel(config) | ||
|
|
@@ -2565,6 +2590,35 @@ def test_accelerator_config_from_dataclass(self): | |
| self.assertEqual(trainer.accelerator.even_batches, False) | ||
| self.assertEqual(trainer.accelerator.use_seedable_sampler, False) | ||
|
|
||
| @require_accelerate_version | ||
| def test_accelerate_config_from_dataclass_grad_accum(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
||
| grad_acc_kwargs = { | ||
| "num_steps": 10, | ||
| "adjust_scheduler": False, | ||
| "sync_with_dataloader": False, | ||
| "sync_each_batch": True, | ||
| } | ||
| accelerator_config = AcceleratorConfig( | ||
| split_batches=True, | ||
| dispatch_batches=True, | ||
| even_batches=False, | ||
| use_seedable_sampler=False, | ||
| gradient_accumulation_kwargs=grad_acc_kwargs, | ||
| ) | ||
| config = RegressionModelConfig(a=1.5, b=2.5) | ||
| model = RegressionPreTrainedModel(config) | ||
| eval_dataset = SampleIterableDataset() | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| args = RegressionTrainingArguments(output_dir=tmp_dir, accelerator_config=accelerator_config) | ||
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["num_steps"], 10) | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["adjust_scheduler"], False) | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["sync_with_dataloader"], False) | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["sync_each_batch"], True) | ||
|
|
||
| def test_accelerator_config_from_partial(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
@@ -2574,18 +2628,21 @@ def test_accelerator_config_from_partial(self): | |
| eval_dataset = SampleIterableDataset() | ||
|
|
||
| # Leaves one option as something *not* basic | ||
| args = RegressionTrainingArguments( | ||
| output_dir=tmp_dir, | ||
| accelerator_config={ | ||
| "split_batches": True, | ||
| }, | ||
| ) | ||
| accelerator_config = { | ||
| "split_batches": True, | ||
| } | ||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| accelerator_config["gradient_accumulation_kwargs"] = {"sync_each_batch": True} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes what the test does. Let's not do this please.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes agree, in this case I would think its best to revert this test to its previous state and not introduce Since Do you agree?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup |
||
| args = RegressionTrainingArguments(output_dir=tmp_dir, accelerator_config=accelerator_config) | ||
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertEqual(trainer.accelerator.split_batches, True) | ||
| self.assertEqual(trainer.accelerator.dispatch_batches, None) | ||
| self.assertEqual(trainer.accelerator.even_batches, True) | ||
| self.assertEqual(trainer.accelerator.use_seedable_sampler, True) | ||
|
|
||
| if self.GRAD_ACCUM_KWARGS_VERSION_AVAILABLE: | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["sync_each_batch"], True) | ||
|
|
||
| def test_accelerator_config_from_dict_with_deprecated_args(self): | ||
| # Checks that accelerator kwargs can be passed through | ||
| # and the accelerator is initialized respectively | ||
|
|
@@ -2636,6 +2693,44 @@ def test_accelerator_config_only_deprecated_args(self): | |
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertEqual(trainer.accelerator.split_batches, True) | ||
|
|
||
| @require_accelerate_version | ||
| def test_accelerator_config_from_dict_grad_accum_num_steps(self): | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| config = RegressionModelConfig(a=1.5, b=2.5) | ||
| model = RegressionPreTrainedModel(config) | ||
| eval_dataset = SampleIterableDataset() | ||
|
|
||
| # case - TrainingArguments.gradient_accumulation_steps == 1 | ||
| # - gradient_accumulation_kwargs['num_steps] == 1 | ||
| # results in grad accum set to 1 | ||
| args = RegressionTrainingArguments( | ||
| output_dir=tmp_dir, | ||
| gradient_accumulation_steps=1, | ||
| accelerator_config={ | ||
| "gradient_accumulation_kwargs": { | ||
| "num_steps": 1, | ||
| } | ||
| }, | ||
| ) | ||
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertEqual(trainer.accelerator.gradient_state.plugin_kwargs["num_steps"], 1) | ||
|
|
||
| # case - TrainingArguments.gradient_accumulation_steps > 1 | ||
| # - gradient_accumulation_kwargs['num_steps] specified | ||
| # results in exception raised | ||
| args = RegressionTrainingArguments( | ||
| output_dir=tmp_dir, | ||
| gradient_accumulation_steps=2, | ||
| accelerator_config={ | ||
| "gradient_accumulation_kwargs": { | ||
| "num_steps": 10, | ||
| } | ||
| }, | ||
| ) | ||
| with self.assertRaises(Exception) as context: | ||
| trainer = Trainer(model=model, args=args, eval_dataset=eval_dataset) | ||
| self.assertTrue("The `AcceleratorConfig`'s `num_steps` is set but" in str(context.exception)) | ||
|
|
||
|
|
||
| @require_torch | ||
| @is_staging_test | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.