Skip to content
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

Add bfloat16 support to DeepSpeedStrategy #12508

Merged
merged 21 commits into from
Apr 7, 2022
Merged

Conversation

rohitgr7
Copy link
Contributor

@rohitgr7 rohitgr7 commented Mar 29, 2022

What does this PR do?

Add bf16 support to DeepSpeedStrategy.

More info: https://www.deepspeed.ai/docs/config-json/#bfloat16-training-options

Note that bf16 support was added in deepspeed==0.6.0. Do we maintain a minimum version for deepspeed? If not, then if someone has deepspeed < 0.6.0 and tries to use bf16, it won't fail. I can add a check in that case and throw an exception if we support deepspeed < 0.6.0.

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda @SeanNaren @awaelchli @rohitgr7 @akihironitta

@rohitgr7 rohitgr7 added feature Is an improvement or enhancement strategy: deepspeed labels Mar 29, 2022
@rohitgr7 rohitgr7 added this to the 1.7 milestone Mar 29, 2022
@rohitgr7 rohitgr7 self-assigned this Mar 29, 2022
@rohitgr7 rohitgr7 changed the title Add bfloat16 support to DeepSpeedStrategy Add bfloat16 support to DeepSpeedStrategy Mar 29, 2022
@Lightning-AI Lightning-AI deleted a comment from Sunset17 Mar 29, 2022
@rohitgr7 rohitgr7 requested a review from kaushikb11 March 29, 2022 18:02
@leezu
Copy link
Contributor

leezu commented Mar 30, 2022

This PR misses implementation for model_sharded_context. You can apply the following patch to add support:

@@ -512,11 +521,12 @@ def _initialize_deepspeed_train(self, model):
     def model_sharded_context(self) -> Generator[None, None, None]:
         if self.zero_stage_3:
             assert self._config_initialized
-            dtype = (
-                torch.float16
-                if self.precision_plugin.precision in (PrecisionType.HALF, PrecisionType.MIXED)
-                else torch.float32
-            )
+            if self.precision_plugin.precision in (PrecisionType.HALF, PrecisionType.MIXED):
+                dtype = torch.float16
+            elif self.precision_plugin.precision == PrecisionType.BFLOAT:
+                dtype = torch.bfloat16
+            else:
+                dtype = torch.float32
             model_parallel_context = deepspeed.zero.Init(
                 remote_device=self.remote_device, pin_memory=True, config_dict_or_path=self.config, dtype=dtype
             )

@leezu
Copy link
Contributor

leezu commented Mar 30, 2022

If you include patch, please also add a test for ZeRO3+bf16 with model_sharded_context. Otherwise please ping me and I can open a new PR after this one is merged.

@rohitgr7
Copy link
Contributor Author

This PR misses implementation for model_sharded_context. You can apply the following patch to add support:

yep, I was reading about why it is done like that for stage3, that's why kept this draft.

@rohitgr7 rohitgr7 marked this pull request as ready for review March 30, 2022 17:54
pytorch_lightning/plugins/precision/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/precision/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/precision/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/strategies/deepspeed.py Outdated Show resolved Hide resolved
tests/strategies/test_deepspeed_strategy.py Show resolved Hide resolved
tests/strategies/test_deepspeed_strategy.py Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Apr 4, 2022
pytorch_lightning/strategies/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/strategies/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/strategies/deepspeed.py Outdated Show resolved Hide resolved
pytorch_lightning/strategies/deepspeed.py Show resolved Hide resolved
@rohitgr7 rohitgr7 enabled auto-merge (squash) April 5, 2022 16:59
@rohitgr7 rohitgr7 merged commit 02dc775 into master Apr 7, 2022
@rohitgr7 rohitgr7 deleted the feat/deepspeed_bfloat16 branch April 7, 2022 22:18
@toriving
Copy link

Can i use bf16 in the latest master branch?

@rohitgr7
Copy link
Contributor Author

Can i use bf16 in the latest master branch?

@toriving , yes

@toriving
Copy link

Can i use bf16 in the latest master branch?

@toriving , yes

Thx. I can not use the latest release version (1.6.5) but can use it in master branch.

@rohitgr7
Copy link
Contributor Author

Can i use bf16 in the latest master branch?

@toriving , yes

Thx. I can not use the latest release version (1.6.5) but can use it in the master branch.

yep... v1.6.5 is the bug fix branch and does not include any new features that were merged after v1.6 release. This is currently available only on master and will be available in v1.7 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement ready PRs ready to be merged strategy: deepspeed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants