Got error when train models with more than one param_group in torch2.0
ThisisBillhe opened this issue ยท 7 comments
๐ Describe the bug
I create optimizer in this way:
for name, module in model.named_modules():
if isinstance(module, QuantModule_int2lora) and module.ignore_reconstruction is False:
avg_delta = torch.sum(module.weight_quantizer.delta) / torch.numel(module.weight_quantizer.delta)
params = [param for name, param in module.named_parameters() if 'lora' in name]
if firstone:
optimizer = torch.optim.AdamW(params, lr=avg_delta / 300)
firstone = False
else:
optimizer.add_param_group({'params': params, 'lr': avg_delta / 300})
which will lead to an error in torch >= 2.0.0
File "/fs03/dl65/jing/Yefei/stable-diffusion-v1/./ldm/models/diffusion/plms.py", line 477, in p_sample_plms
self.optimizer.step()
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/lr_scheduler.py", line 69, in wrapper
return wrapped(*args, **kwargs)
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/optimizer.py", line 280, in wrapper
out = func(*args, **kwargs)
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/optimizer.py", line 33, in _use_grad
ret = func(self, *args, **kwargs)
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/adamw.py", line 171, in step
adamw(
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/adamw.py", line 321, in adamw
func(
File "/projects/dl65/jliu/conda_envs/stablediffusion/lib/python3.8/site-packages/torch/optim/adamw.py", line 568, in _multi_tensor_adamw
torch._foreach_addcdiv_(device_params, device_exp_avgs, denom, step_size)
TypeError: _foreach_addcdiv_() received an invalid combination of arguments - got (list, list, tuple, list), but expected one of:
* (tuple of Tensors self, tuple of Tensors tensor1, tuple of Tensors tensor2, tuple of Scalars scalars)
didn't match because some of the arguments have invalid types: (list of [Parameter, Parameter], list of [Tensor, Tensor], tuple of (Tensor, Tensor), list of [Tensor, Tensor])
* (tuple of Tensors self, tuple of Tensors tensor1, tuple of Tensors tensor2, Tensor scalars)
didn't match because some of the arguments have invalid types: (list of [Parameter, Parameter], list of [Tensor, Tensor], tuple of (Tensor, Tensor), list of [Tensor, Tensor])
* (tuple of Tensors self, tuple of Tensors tensor1, tuple of Tensors tensor2, Number value)
didn't match because some of the arguments have invalid types: (list of [Parameter, Parameter], list of [Tensor, Tensor], tupl
However, it works fine with torch==1.9.0. I wonder why ?
Versions
Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A
OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31
Python version: 3.8.16 (default, Mar 2 2023, 03:21:46) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-76-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090
GPU 3: NVIDIA GeForce RTX 3090
GPU 4: NVIDIA GeForce RTX 3090
GPU 5: NVIDIA GeForce RTX 3090
GPU 6: NVIDIA GeForce RTX 3090
GPU 7: NVIDIA GeForce RTX 3090
Nvidia driver version: 525.125.06
cuDNN version: Could not collect
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
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 72
On-line CPU(s) list: 0-71
Thread(s) per core: 2
Core(s) per socket: 18
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6140M CPU @ 2.30GHz
Stepping: 4
CPU MHz: 1000.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4600.00
Virtualization: VT-x
L1d cache: 1.1 MiB
L1i cache: 1.1 MiB
L2 cache: 36 MiB
L3 cache: 49.5 MiB
NUMA node0 CPU(s): 0-17,36-53
NUMA node1 CPU(s): 18-35,54-71
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
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 pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear flush_l1d arch_capabilities
Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.3
[pip3] pytorch-lightning==2.0.4
[pip3] pytorchcv==0.0.67
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.15.2
[conda] blas 1.0 mkl https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] ffmpeg 4.3 hf484d3e_0 pytorch
[conda] mkl 2023.1.0 h6d00ec8_46342 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl-service 2.4.0 py38h5eee18b_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl_fft 1.3.6 py38h417a72b_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl_random 1.2.2 py38h417a72b_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] numpy 1.24.3 py38hf6e8229_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] numpy-base 1.24.3 py38h060ed82_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] pytorch 2.0.1 py3.8_cuda11.7_cudnn8.5.0_0 pytorch
[conda] pytorch-cuda 11.7 h778d358_5 pytorch
[conda] pytorch-lightning 2.0.4 pypi_0 pypi
[conda] pytorch-mutex 1.0 cuda pytorch
[conda] torchaudio 2.0.2 py38_cu117 pytorch
[conda] torchmetrics 0.11.4 pypi_0 pypi
[conda] torchtriton 2.0.0 py38 pytorch
[conda] torchvision 0.15.2 py38_cu117 pytorch
Hello! Thanks for the report. I believe this is likely an issue due to the foreach optimizer, which was moved to be the default in 2.0.
To confirm that notion, could you see if you could repro with the following?
torch.optim.AdamW(params, lr=avg_delta / 300, foreach=False)
or
torch.optim.AdamW(params, lr=avg_delta / 300, fused=True)
Thanks! It works with "foreach=False".
hmm this is still worth investigating as foreach=True (the default) is much more performant.
did you also get a chance to try fused=True? (that should be even more performant and may avoid this bug as well!)
i am reopening as this is something i would like to fix for foreach=True
Another error occurs for fused=True:
/home/hyf/.conda/envs/qlora/lib/python3.8/site-packages/torch/optim/optimizer.py:33 in _use_grad โ
โ โ
โ 30 โ โ prev_grad = torch.is_grad_enabled() โ
โ 31 โ โ try: โ
โ 32 โ โ โ torch.set_grad_enabled(self.defaults['differentiable']) โ
โ โฑ 33 โ โ โ ret = func(self, *args, **kwargs) โ
โ 34 โ โ finally: โ
โ 35 โ โ โ torch.set_grad_enabled(prev_grad) โ
โ 36 โ โ return ret โ
โ โ
โ /home/hyf/.conda/envs/qlora/lib/python3.8/site-packages/torch/optim/adamw.py:171 in step โ
โ โ
โ 168 โ โ โ โ state_steps, โ
โ 169 โ โ โ ) โ
โ 170 โ โ โ โ
โ โฑ 171 โ โ โ adamw( โ
โ 172 โ โ โ โ params_with_grad, โ
โ 173 โ โ โ โ grads, โ
โ 174 โ โ โ โ exp_avgs, โ
โ โ
โ /home/hyf/.conda/envs/qlora/lib/python3.8/site-packages/torch/optim/adamw.py:321 in adamw โ
โ โ
โ 318 โ else: โ
โ 319 โ โ func = _single_tensor_adamw โ
โ 320 โ โ
โ โฑ 321 โ func( โ
โ 322 โ โ params, โ
โ 323 โ โ grads, โ
โ 324 โ โ exp_avgs, โ
โ โ
โ /home/hyf/.conda/envs/qlora/lib/python3.8/site-packages/torch/optim/adamw.py:615 in _fused_adamw โ
โ โ
โ 612 โ โ โ โ found_inf_dict[device] = found_inf.to(device, non_blocking=True) โ
โ 613 โ โ โ device_found_inf = found_inf_dict[device] โ
โ 614 โ โ torch._foreach_add_(device_state_steps, 1) โ
โ โฑ 615 โ โ torch._fused_adamw_( โ
โ 616 โ โ โ device_params, โ
โ 617 โ โ โ device_grads, โ
โ 618 โ โ โ device_exp_avgs, โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
RuntimeError: params, grads, exp_avgs, and exp_avg_sqs must have same dtype, device, and layout
Also, I do not use amp for training so dtypes should be the same.
I now know why your first error occurs (with the TypeError: _foreach_addcdiv_() received an invalid combination of arguments
). It is related to the lr being a tensor, which currently does not have support for the default AdamW without also setting capturable=True.
Hmmm for your second fused=True error, the issue may be that the params and grads have differing strides. Do you manually update the grads at any point instead of letting autograd generate them?
Interestingly, this issue disappears after first passing fused=True
and then setting fused=False
later. Is there an in-place fix when fused=True
is called?
How do you set fused=False later? The optimizer with fused=True accepts Tensor LRs, though I believe fused=False should allow tensor LRs as well...so I suppose it depends on which issue you are referring to. @danielajisafe