Repository: pytorch
Issue: `amp.custom_fwd` interacts poorly with custom `autograd.Function` with `setup_context` separated from `forward`
Labels: module: autograd,triaged,actionable,module: amp (automated mixed precision)

### 🐛 Describe the bug

When trying to implement a custom `autograd.Function` wrapping a custom CUDA Extensions, I ran into what I believe is a bug or a poorly documented interaction.

A minimal example would look like that, I think : 

```
import torch.autograd
from torch.amp import custom_fwd, custom_bwd

class SomeFunction(autogard.Function):
    @staticmethod
    @custom_fwd(device_type="cuda", cast_inputs=torch.float32)
    def forward(some, tensors):
        # do something with the tensors

        return output

    @staticmethod
    def setup_context(ctx, inputs, output):
        some, tensors = inputs

        ctx.save_for_backward(some, tensors)

    @staticmethod
    @custom_bwd(device_type="cuda")
    def backward(ctx, grad_output):

        # the tensors taken from ctx are not properly marken by custom_fwd
        some, tensors = ctx.saved_tensors

        # do something with the tensors from context
        ...
```

If you try to run backward with this function you will get a : `AttributeError: 'SomeFunctionBackward' object has no attribute '_fwd_used_autocast' `, because the inputs passed to `setup_context` are not properly marked by `custom_fwd`, particularly they lack the `_fwd_used_autocast` attribute, because the inputs are passed before they pass trough the decorator.

I guess the source of my confusion comes from the fact that in order to make it work I have to decorate the `setup_context` with `custom_fwd` decorator, even though it's not really part of the forward, so this feels like a :bug: . That means you have to decorate both methods with 2 similarly configured `custom_fwd`, which feels clunky.

I think a fix, or at least a way to make this more understandable would be to implement an `amp.custom_setup_context` decorator? Or have a mechanism pass marked tensors to `setup_context`. 


### Versions

```
Collecting environment information...
PyTorch version: 2.4.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.14 (main, Jun 25 2024, 11:25:45) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-45-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA RTX 2000 Ada Generation Laptop GPU
Nvidia driver version: 555.42.06
cuDNN version: Probably one of the following:
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.7
/usr/local/cuda-11.3/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.7
/usr/local/cuda-11.5/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.7
/usr/local/cuda-12.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.7
/usr/local/cuda-12.5/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.7
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      46 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             28
On-line CPU(s) list:                0-27
Vendor ID:                          GenuineIntel
Model name:                         13th Gen Intel(R) Core(TM) i7-13850HX
CPU family:                         6
Model:                              183
Thread(s) per core:                 2
Core(s) per socket:                 20
Socket(s):                          1
Stepping:                           1
CPU max MHz:                        5300,0000
CPU min MHz:                        800,0000
BogoMIPS:                           4608.00
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          768 KiB (20 instances)
L1i cache:                          1 MiB (20 instances)
L2 cache:                           28 MiB (11 instances)
L3 cache:                           30 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-27
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] efficientnet-pytorch==0.7.1
[pip3] numpy==1.26.4
[pip3] onnx==1.16.1
[pip3] pytorch-lightning==2.3.3
[pip3] segmentation-models-pytorch==0.1.0
[pip3] torch==2.4.0
[pip3] torchdata==0.7.1
[pip3] torchmetrics==1.4.0.post0
[pip3] torchvision==0.19.0
[pip3] triton==3.0.0
[conda] Could not collect
```

cc @ezyang @albanD @gqchen @pearu @nikitaved @soulitzer @Varal7 @mcarilli @ptrblck @leslie-fang-intel @jgong5