Repository: deepspeedai/DeepSpeed
Pull request: Update linear.py compatible with torch 2.4.0
deepspeed/runtime/zero/linear.py:67: FutureWarning: `torch.cuda.amp.custom_bwd(args...)` is deprecated. Please use `torch.amp.custom_bwd(args..., device_type='cuda')` instead.

Fixes: #5682

Problem statement:
[BUG] Logs full of FutureWarning when training with nightly PyTorch
**Describe the bug**
When training with nightly PyTorch, the logs are full of deprecation warnings like this:

```
/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/deepspeed/runtime/zero/linear.py:49: FutureWarn\
ing: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cu\
da')` instead.
[860302:0]:/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/deepspeed/runtime/zero/linear.py:67:\
 FutureWarning: `torch.cuda.amp.custom_bwd(args...)` is deprecated. Please use `torch.amp.custom_bwd(args..., devi\
ce_type='cuda')` instead.
```

**To Reproduce**
Steps to reproduce the behavior:
Fine-tune a model with Zero-3 while using nightly PyTorch.

**Expected behavior**
The deprecation warning shouldn't appear.

**ds_report output**
[2024-06-18 08:59:11,766] [INFO] [real_accelerator.py:203:get_accelerator] Setting ds_accelerator to cuda (auto detect)
/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/deepspeed/runtime/zero/linear.py:49: FutureWarning: `torch.cuda.amp.custo\
m_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
  def forward(ctx, input, weight, bias=None):
/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/deepspeed/runtime/zero/linear.py:67: FutureWarning: `torch.cuda.amp.custo\
m_bwd(args...)` is deprecated. Please use `torch.amp.custom_bwd(args..., device_type='cuda')` instead.
  def backward(ctx, grad_output):
Warning: The default cache directory for DeepSpeed Triton autotune, /home/alyssavance/.triton/autotune, appears to be on an NFS system. Whil\
e this is generally acceptable, if you experience slowdowns or hanging when DeepSpeed exits, it is recommended to set the TRITON_CACHE_DIR e\
nvironment variable to a non-NFS path.
^[[93m [WARNING] ^[[0m Please specify the CUTLASS repo directory as environment variable $CUTLASS_PATH
^[[93m [WARNING] ^[[0m sparse_attn requires a torch version >= 1.5 and < 2.0 but detected 2.4
^[[93m [WARNING] ^[[0m using untested triton version (3.0.0+45fff310c8), only 1.0.0 is known to be compatible
--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
      runtime if needed. Op compatibility means that your system
      meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. ^[[92m[OKAY]^[[0m
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
async_io ............... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
fused_adam ............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
cpu_adam ............... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
cpu_adagrad ............ ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
cpu_lion ............... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
^[[93m [WARNING] ^[[0m Please specify the CUTLASS repo directory as environment variable $CUTLASS_PATH
evoformer_attn ......... ^[[93m[NO]^[[0m ....... ^[[93m[NO]^[[0m
fp_quantizer ........... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
fused_lamb ............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
fused_lion ............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
inference_core_ops ..... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
cutlass_ops ............ ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
transformer_inference .. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
quantizer .............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
ragged_device_ops ...... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
ragged_ops ............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
random_ltd ............. ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
^[[93m [WARNING] ^[[0m sparse_attn requires a torch version >= 1.5 and < 2.0 but detected 2.4
^[[93m [WARNING] ^[[0m using untested triton version (3.0.0+45fff310c8), only 1.0.0 is known to be compatible
sparse_attn ............ ^[[93m[NO]^[[0m ....... ^[[93m[NO]^[[0m
spatial_inference ...... ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
transformer ............ ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
stochastic_transformer . ^[[93m[NO]^[[0m ....... ^[[92m[OKAY]^[[0m
--------------------------------------------------
DeepSpeed general environment info:
torch install path ............... ['/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/torch']
torch version .................... 2.4.0.dev20240609+cu124
deepspeed install path ........... ['/home/alyssavance/miniforge3/envs/brr/lib/python3.10/site-packages/deepspeed']
deepspeed info ................... 0.14.3, unknown, unknown
torch cuda version ............... 12.4
torch hip version ................ None
nvcc version ..................... 12.5
deepspeed wheel compiled w. ...... torch 2.4, cuda 12.4
shared memory (/dev/shm) size .... 1007.73 GB

**System info (please complete the following information):**
 - OS: Ubuntu 22.04
 - GPU count and types: 8x H100
 - Interconnects (if applicable): 3.2 Tbps Infiniband
 - Python version: 3.10.14
 - Any other relevant info about your setup

**Launcher context**
Are you launching your experiment with the `deepspeed` launcher, MPI, or something else?

Launching with Slurm via HuggingFace "accelerate launch"

**Docker context**
Are you using a specific docker image that you can share?

N/A

**Additional context**
Add any other context about the problem here.

N/A

Hints from issue discussion:
Thanks @rosario-purple, we will take a look at these

Commit message: Merge branch 'master' into torch2.4.0-amp-futurewarning-patch

Patch to review:
diff --git a/deepspeed/runtime/zero/linear.py b/deepspeed/runtime/zero/linear.py
index e9dd78864cde..7091371cebe0 100644
--- a/deepspeed/runtime/zero/linear.py
+++ b/deepspeed/runtime/zero/linear.py
@@ -16,6 +16,7 @@
 #when implemented outside of torch.autograd.Function
 
 import math
+import functools
 
 import torch
 from torch import Tensor
@@ -33,8 +34,8 @@ def print_rank_0(message, debug=False, force=False):
 
 
 try:
-    autocast_custom_fwd = get_accelerator().amp().custom_fwd
-    autocast_custom_bwd = get_accelerator().amp().custom_bwd
+    autocast_custom_fwd = functools.partial(torch.amp.custom_fwd, device_type=get_accelerator().device_name())
+    autocast_custom_bwd = functools.partial(torch.amp.custom_bwd, device_type=get_accelerator().device_name())
 except (ImportError, AttributeError) as exp:
     autocast_custom_fwd = noop_decorator
     autocast_custom_bwd = noop_decorator


Code review comments:
diff_hunk: @@ -33,8 +34,8 @@ def print_rank_0(message, debug=False, force=False):
 
 
 try:
-    autocast_custom_fwd = get_accelerator().amp().custom_fwd
-    autocast_custom_bwd = get_accelerator().amp().custom_bwd
+    autocast_custom_fwd = functools.partial(torch.amp.custom_fwd, device_type=get_accelerator().device_name())
path: deepspeed/runtime/zero/linear.py
text: @user3:
Thanks @user1-for-github - It looks like torch.amp is introduced in torch 1.11, @user2 thoughts on keeping support for older torch versions?

@user3:
@user1-for-github 

Could you add something like

```python
if hasattr(torch, 'amp'):
    autocast_custom_fwd = functools.partial(torch.amp.custom_fwd, device_type=get_accelerator().device_name())
    autocast_custom_bwd = functools.partial(torch.amp.custom_bwd, device_type=get_accelerator().device_name())
else:
    autocast_custom_fwd = get_accelerator().amp().custom_fwd
    autocast_custom_bwd = get_accelerator().amp().custom_bwd
```

That way we can support older torch versions too (like 1.11)

@author:
Sure! I will try it.

@author:
torch.amp.custom_f/bwd only exist in torch 2.4 even though torch.amp is introduced after torch 1.11(not included).

So what I understand is that the original code supports torch 1.11-2.3(or maybe older, I'm not sure) and my code only supports 2.4. What I need to do is fix my code so that it supports 1.11-2.4.

Am I right?

@author:
@user3 See ab7ab617c1aec835a15759ea5bf90ec0aa7d7fad .
This may not be the correct usage of `hasattr` but I think it works.

@author:
I think there is something wrong with the unit test.

@user3:
Yes, we've fixed the unit tests now, thanks!