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

[Enhancement] Enhance inputs_to_half in DeepSpeedStrategy #1400

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/distributed_training_with_flexible_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def main():
initial_scale_power=15,
),
inputs_to_half=[0],
# bf16=dict(
# enabled=True,
# ),
zero_optimization=dict(
stage=3,
allgather_partitions=True,
Expand Down
10 changes: 7 additions & 3 deletions mmengine/_strategy/deepspeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from mmengine.optim import BaseOptimWrapper, _ParamScheduler
from mmengine.registry import (MODEL_WRAPPERS, OPTIM_WRAPPERS, OPTIMIZERS,
STRATEGIES)
from mmengine.utils import digit_version, get_git_hash
from mmengine.utils import apply_to, digit_version, get_git_hash
from .base import BaseStrategy


Expand Down Expand Up @@ -188,18 +188,22 @@ def _cast_inputs_half(self, inputs: Union[list, tuple, dict, None]):
if self._inputs_to_half is None:
return inputs

dtype = next(self.model.parameters()).dtype
if isinstance(inputs, (list, tuple)):
new_inputs = []
for i, v in enumerate(inputs):
if i in self._inputs_to_half:
new_inputs.append(v.half())
new_inputs.append(
apply_to(v, lambda x: hasattr(x, 'to'),
lambda x: x.to(dtype)))
else:
new_inputs.append(v)
return inputs.__class__(new_inputs)
elif isinstance(inputs, dict):
for k, v in inputs.items():
if k in self._inputs_to_half:
inputs[k] = v.half()
inputs[k] = apply_to(v, lambda x: hasattr(x, 'to'),
lambda x: x.to(dtype))
return inputs
else:
raise TypeError('inputs should be list, tuple or dict, '
Expand Down
Loading