Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
55657dc
synced gpus
stas00 Mar 16, 2021
05bddda
fix
stas00 Mar 16, 2021
a5adf30
fix
stas00 Mar 16, 2021
a241298
Merge remote-tracking branch 'origin/master' into ds-zero-3
stas00 Mar 16, 2021
08e43f2
need to use t5-small for quality tests
stas00 Mar 16, 2021
2cd947b
notes
stas00 Mar 17, 2021
a374e57
Merge remote-tracking branch 'origin/master' into ds-zero-3
stas00 Mar 17, 2021
d48ad17
complete merge
stas00 Mar 17, 2021
ad7605d
fix a disappearing std stream problem
stas00 Mar 18, 2021
a7fafbb
start zero3 tests
stas00 Mar 18, 2021
b725c96
Merge remote-tracking branch 'origin/master' into ds-zero-3
stas00 Mar 18, 2021
f936858
wip
stas00 Mar 19, 2021
0a5d872
tune params
stas00 Mar 19, 2021
80fbbe7
sorting out the pre-trained model loading
stas00 Mar 23, 2021
538a402
reworking generate loop wip
stas00 Mar 23, 2021
a3ce950
wip
stas00 Mar 30, 2021
050abd6
Merge remote-tracking branch 'origin/master' into ds-zero-3
stas00 Mar 30, 2021
0f58b8d
style
stas00 Mar 30, 2021
d2f1f70
fix tests
stas00 Mar 30, 2021
d585c8c
split the tests
stas00 Mar 30, 2021
399b981
refactor tests
stas00 Mar 30, 2021
dfe28b3
wip
stas00 Mar 30, 2021
2c3564e
parameterized
stas00 Mar 30, 2021
46e2368
fix
stas00 Mar 30, 2021
8f26f9a
workout the resume from non-ds checkpoint pass + test
stas00 Mar 31, 2021
47e3556
cleanup
stas00 Mar 31, 2021
1cfec3c
remove no longer needed code
stas00 Mar 31, 2021
1cde0d8
Merge remote-tracking branch 'origin/master' into ds-zero-3
stas00 Mar 31, 2021
17e0464
split getter/setter functions
stas00 Mar 31, 2021
ea44db6
complete the docs
stas00 Apr 1, 2021
aa63fc2
suggestions
stas00 Apr 1, 2021
1399f59
gpus and their compute capabilities link
stas00 Apr 1, 2021
cb0de9d
Apply suggestions from code review
stas00 Apr 1, 2021
f34a62b
style
stas00 Apr 2, 2021
376fe60
Merge branch 'ds-zero-3' of github.com:stas00/transformers into ds-ze…
stas00 Apr 2, 2021
0799eac
remove invalid paramgd
stas00 Apr 5, 2021
e365dfe
automatically configure zero3 params that rely on hidden size
stas00 Apr 6, 2021
b3137ae
make _get_resized_embeddings zero3-aware
stas00 Apr 6, 2021
1708a82
add test exercising resize_token_embeddings()
stas00 Apr 6, 2021
6ee8744
add docstring
stas00 Apr 6, 2021
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
121 changes: 120 additions & 1 deletion docs/source/main_classes/trainer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ or with bash magic, where you can write a multi-line code for the shell to run:




Configuration
=======================================================================================================================

Expand Down Expand Up @@ -736,7 +737,7 @@ which ZeRO stages you want to enable and how to configure them.
}
}

Notes:
Performance tuning:

- enabling ``cpu_offload`` should reduce GPU RAM usage (it requires ``"stage": 2``)
- ``"overlap_comm": true`` trades off increased GPU RAM usage to lower all-reduce latency. ``overlap_comm`` uses 4.5x
Expand All @@ -752,6 +753,41 @@ This section has to be configured exclusively via DeepSpeed configuration - the
no equivalent command line arguments.


ZeRO-3
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


Performance tuning: 

- ``stage3_max_live_parameters``: ``1e9``
- ``stage3_max_reuse_distance``: ``1e9``
- ``stage3_prefetch_bucket_size``: ``0.9 * hidden_size*hidden_size``
- ``stage3_param_persistence_threshold``: ``10*hidden_size``
- ``reduce_bucket_size``: ``hidden_size*hidden_size``
- ``sub_group_size``: ``1e14``

If hitting OOM reduce ``stage3_max_live_parameters`` and ``stage3_max_reuse_distance``. They should have minimal impact
on performance unless you are doing activation checkpointing. ``1e9`` would consume ~2GB. The memory is shared by
``stage3_max_live_parameters`` and ``stage3_max_reuse_distance``, so its not additive, its just 2GB total.

``stage3_max_live_parameters`` is the upper limit on how many full parameters you want to keep on the GPU at any given
time. ``reuse_distance`` is a metric we are using to figure out when will a parameter be used again in the future, and
we use the ``stage3_max_reuse_distance`` to decide whether to throw away the parameter or to keep it. If a parameter is
going to be used again in near future (less than ``stage3_max_reuse_distance``) then we keep it to reduce communication
overhead. This is super helpful when you have activation check-pointing enabled, where we do a forward recompute and
Comment thread
stas00 marked this conversation as resolved.
Outdated
backward passes a a single layer granularity and want to keep the parameter in the forward recompute till the backward


If you set ``reduce_bucket_size``, ``stage3_prefetch_bucket_size`` and ``stage3_param_persistence_threshold``: as
recommended above, they will already be fairly small so you won't have to tune those much.

Other:

``allgather_partitions``, ``allgather_bucket_size`` and ``reduce_scatter`` are not used in ZeRO-3.





Optimizer and Scheduler
=======================================================================================================================
Expand Down Expand Up @@ -963,6 +999,89 @@ Here is an example of the ``gradient_clipping`` configuration:



ZeRO 3 Nuances
=======================================================================================================================

ZeRO 3 is quite different from ZeRO 2 because of its param sharding, so the following might be of use in certain
circumstances.


Registering External Parameters
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

If layer A needs to access weights belonging to layer B, currently layer A needs to tell DeepSpeed about it. This is
done with the help of ``deepspeed.zero.register_external_parameter`` that needs to be called in ``A.__init__`` and can
be seen in the following example:

.. code-block:: python

class ModuleZ3(torch.nn.Module):
def __init__(self, *args):
super().__init__(self, *args)
self.layer1 = SomeLayer()
self.layer2 = OtherLayer()
deepspeed.zero.register_external_parameter(self, self.layer1.weight)

def forward(self, input):
x = self.layer1(input)
# self.layer1.weight is needed in ModuleZ3.forward
y = self.layer2(x, self.layer1.weight)
return y


For full details on this method please refer to `Registering External Parameters
<https://deepspeed.readthedocs.io/en/latest/zero3.html#registering-external-parameters>`__



Constructing Massive Models
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

DeepSpeed/ZeRO-3 can handle models with Trillions of parameters which may not fit onto the existing RAM. In such cases, but also if you want the initialization to happen much faster, initialize the model using `deepspeed.zero.Init()`
context manager (which is also a function decorator), like so:

.. code-block:: python

from transformers import T5ForConditionalGeneration, T5Config
import deepspeed
with deepspeed.zero.Init():
config = T5Config.from_pretrained("t5-small")
model = T5ForConditionalGeneration(config)

As you can see this gives you a randomly initialized model.

If you want to use a pretrained model, ``model_class.from_pretrained`` will activate this feature as long as ``deepspeed_is_zero3_enabled()`` returns ``True``. Therefore to enable this feature here is the required sequence:

.. code-block:: python

from transformers.integrations import deepspeed_is_zero3_enabled
deepspeed_is_zero3_enabled(True)
model = T5ForConditionalGeneration.from_pretrained("t5-small")

If you're using Trainer command line arguments which include ``--deepspeed ds_config.json`` with ZeRO3 config enabled, then you can skip ``deepspeed_is_zero3_enabled(True)`` as it will try to discover whether it'll be run under zero3 and ``from_pretrained`` will automatically activate this feature.

Note: If the fp16 weights of the model can't fit onto the memory of a single GPU this feature must be used.

For full details on this method and other related features please refer to `Constructing Massive Models
<https://deepspeed.readthedocs.io/en/latest/zero3.html#constructing-massive-models>`__.




External Parameters
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

When using ``deepspeed.zero.Init()`` discussed earlier, a layer A may need to access layer B's weights


XXX: complete
Comment thread
stas00 marked this conversation as resolved.
Outdated


For full details on this method please refer to `Gathering Parameters
<https://deepspeed.readthedocs.io/en/latest/zero3.html#manual-parameter-coordination>`__



Notes
=======================================================================================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"enabled": true,
"loss_scale": 0,
"loss_scale_window": 1000,
"initial_scale_power": 32,
"initial_scale_power": 16,
"hysteresis": 2,
"min_loss_scale": 1
},
Expand Down
48 changes: 48 additions & 0 deletions examples/tests/deepspeed/ds_config_zero3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"fp16": {
"enabled": true,
"loss_scale": 0,
"loss_scale_window": 1000,
"initial_scale_power": 16,
"hysteresis": 2,
"min_loss_scale": 1
},

"zero_optimization": {
"stage": 3,
"cpu_offload": true,
"cpu_offload_params": true,
"cpu_offload_use_pin_memory" : true,
"overlap_comm": true,
"contiguous_gradients": true,
"stage3_max_live_parameters": 1e9,
"stage3_max_reuse_distance": 1e9,
"stage3_prefetch_bucket_size": 0.94e6,
"stage3_param_persistence_threshold": 1e4,
"reduce_bucket_size": 1e6,
"prefetch_bucket_size": 3e6,
"sub_group_size": 1e14
},

"optimizer": {
"type": "AdamW",
"params": {
"lr": 3e-5,
"betas": [0.8, 0.999],
"eps": 1e-8,
"weight_decay": 3e-7
}
},

"scheduler": {
"type": "WarmupLR",
"params": {
"warmup_min_lr": 0,
"warmup_max_lr": 3e-5,
"warmup_num_steps": 500
}
},

"steps_per_print": 2000,
"wall_clock_breakdown": false
}
Loading