RuntimeError: invalid dtype for bias - should match query's dtype #144018

Hayat_Ullah: <p>I am training the X-CLIP model using a multi-GPU setup (3 GPUs). However, when I start the training process, I encounter the following error:</p>
<p>" <strong>RuntimeError: invalid dtype for bias - should match query’s dtype</strong> "</p>
<p>Here is the complete traceback of the error:</p>
<p>UserWarning: torch.utils.checkpoint.checkpoint_sequential: the use_reentrant parameter should be passed explicitly. In version 2.5 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.<br>
warnings.warn(<br>
[rank0]: Traceback (most recent call last):<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/main.py”, line 283, in<br>
[rank0]: main(config)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/main.py”, line 104, in main<br>
[rank0]: train_one_epoch(epoch, model, criterion, optimizer, lr_scheduler, train_loader, text_labels, config, mixup_fn)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/main.py”, line 149, in train_one_epoch<br>
[rank0]: output = model(images, texts)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/parallel/distributed.py”, line 1643, in forward<br>
[rank0]: else self._run_ddp_forward(*inputs, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/parallel/distributed.py”, line 1459, in _run_ddp_forward<br>
[rank0]: return self.module(*inputs, **kwargs) # type: ignore[index]<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/models/xclip.py”, line 135, in forward<br>
[rank0]: text_features = self.cache_text(text)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/models/xclip.py”, line 125, in cache_text<br>
[rank0]: self.cache_text_features = self.encode_text(text)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/models/xclip.py”, line 97, in encode_text<br>
[rank0]: x = self.transformer(x)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/clip/model.py”, line 87, in forward<br>
[rank0]: return self.resblocks(x)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/container.py”, line 250, in forward<br>
[rank0]: input = module(input)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/clip/model.py”, line 75, in forward<br>
[rank0]: x = x + self.attention(self.ln_1(x))<br>
[rank0]: File “/data/Hayat_Research_Data/VideoX/X-CLIP/clip/model.py”, line 72, in attention<br>
[rank0]: return self.attn(x, x, x, need_weights=False, attn_mask=self.attn_mask)[0]<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1736, in _wrapped_call_impl<br>
[rank0]: return self._call_impl(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1747, in _call_impl<br>
[rank0]: return forward_call(*args, **kwargs)<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/modules/activation.py”, line 1368, in forward<br>
[rank0]: attn_output, attn_output_weights = F.multi_head_attention_forward(<br>
[rank0]: File “/home/hayatullah/anaconda3/envs/VFL/lib/python3.10/site-packages/torch/nn/functional.py”, line 6278, in multi_head_attention_forward<br>
[rank0]: attn_output = scaled_dot_product_attention(<br>
[rank0]: RuntimeError: invalid dtype for bias - should match query’s dtype</p>
<h3><a name="p-462516-versions-1" class="anchor" href="#p-462516-versions-1"></a>Versions</h3>
<p>PyTorch version: 2.5.1<br>
Is debug build: False<br>
CUDA used to build PyTorch: 12.4<br>
ROCM used to build PyTorch: N/A</p>
<p>OS: Ubuntu 22.04.5 LTS (x86_64)<br>
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0<br>
Clang version: Could not collect<br>
CMake version: version 3.22.1<br>
Libc version: glibc-2.35</p>
<p>Python version: 3.10.14 (main, May 6 2024, 19:42:50) [GCC 11.2.0] (64-bit runtime)<br>
Python platform: Linux-6.8.0-45-generic-x86_64-with-glibc2.35<br>
Is CUDA available: True<br>
CUDA runtime version: 12.4.131<br>
CUDA_MODULE_LOADING set to: LAZY<br>
GPU models and configuration:<br>
GPU 0: NVIDIA RTX 6000 Ada Generation<br>
GPU 1: NVIDIA RTX 6000 Ada Generation<br>
GPU 2: NVIDIA RTX 6000 Ada Generation</p>
<p>Nvidia driver version: 550.107.02<br>
cuDNN version: Could not collect<br>
HIP runtime version: N/A<br>
MIOpen runtime version: N/A<br>
Is XNNPACK available: True</p>
<p>CPU:<br>
Architecture: x86_64<br>
CPU op-mode(s): 32-bit, 64-bit<br>
Address sizes: 48 bits physical, 48 bits virtual<br>
Byte Order: Little Endian<br>
CPU(s): 64<br>
On-line CPU(s) list: 0-63<br>
Vendor ID: AuthenticAMD<br>
Model name: AMD Ryzen Threadripper PRO 5975WX 32-Cores<br>
CPU family: 25<br>
Model: 8<br>
Thread(s) per core: 2<br>
Core(s) per socket: 32<br>
Socket(s): 1<br>
Stepping: 2<br>
Frequency boost: enabled<br>
CPU max MHz: 7006.6401<br>
CPU min MHz: 1800.0000<br>
BogoMIPS: 7186.58<br>
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin brs arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm debug_swap<br>
Virtualization: AMD-V<br>
L1d cache: 1 MiB (32 instances)<br>
L1i cache: 1 MiB (32 instances)<br>
L2 cache: 16 MiB (32 instances)<br>
L3 cache: 128 MiB (4 instances)<br>
NUMA node(s): 1<br>
NUMA node0 CPU(s): 0-63<br>
Vulnerability Gather data sampling: Not affected<br>
Vulnerability Itlb multihit: Not affected<br>
Vulnerability L1tf: Not affected<br>
Vulnerability Mds: Not affected<br>
Vulnerability Meltdown: Not affected<br>
Vulnerability Mmio stale data: Not affected<br>
Vulnerability Reg file data sampling: Not affected<br>
Vulnerability Retbleed: Not affected<br>
Vulnerability Spec rstack overflow: Vulnerable: Safe RET, no microcode<br>
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl<br>
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization<br>
Vulnerability Spectre v2: Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected<br>
Vulnerability Srbds: Not affected<br>
Vulnerability Tsx async abort: Not affected</p>
<p>Versions of relevant libraries:<br>
[pip3] flake8==7.0.0<br>
[pip3] mypy-extensions==1.0.0<br>
[pip3] numpy==1.26.4<br>
[pip3] numpydoc==1.7.0<br>
[pip3] nvidia-cublas-cu12==12.1.3.1<br>
[pip3] nvidia-cuda-cupti-cu12==12.1.105<br>
[pip3] nvidia-cuda-nvrtc-cu12==12.1.105<br>
[pip3] nvidia-cuda-runtime-cu12==12.1.105<br>
[pip3] nvidia-cudnn-cu12==9.1.0.70<br>
[pip3] nvidia-cufft-cu12==11.0.2.54<br>
[pip3] nvidia-curand-cu12==10.3.2.106<br>
[pip3] nvidia-cusolver-cu12==11.4.5.107<br>
[pip3] nvidia-cusparse-cu12==12.1.0.106<br>
[pip3] nvidia-nccl-cu12==2.20.5<br>
[pip3] nvidia-nvjitlink-cu12==12.4.127<br>
[pip3] nvidia-nvtx-cu12==12.1.105<br>
[pip3] torch==2.5.1<br>
[pip3] torchaudio==2.4.0<br>
[pip3] torchvision==0.20.1<br>
[pip3] triton==3.0.0<br>
[conda] blas 1.0 mkl<br>
[conda] cuda-cudart 12.4.127 0 nvidia<br>
[conda] cuda-cupti 12.4.127 0 nvidia<br>
[conda] cuda-libraries 12.4.1 0 nvidia<br>
[conda] cuda-nvrtc 12.4.127 0 nvidia<br>
[conda] cuda-nvtx 12.4.127 0 nvidia<br>
[conda] cuda-opencl 12.6.77 0 nvidia<br>
[conda] cuda-runtime 12.4.1 0 nvidia<br>
[conda] ffmpeg 4.3 hf484d3e_0 pytorch<br>
[conda] libcublas 12.4.5.8 0 nvidia<br>
[conda] libcufft 11.2.1.3 0 nvidia<br>
[conda] libcurand 10.3.7.77 0 nvidia<br>
[conda] libcusolver 11.6.1.9 0 nvidia<br>
[conda] libcusparse 12.3.1.170 0 nvidia<br>
[conda] libjpeg-turbo 2.0.0 h9bf148f_0 pytorch<br>
[conda] libnvjitlink 12.4.127 0 nvidia<br>
[conda] mkl 2023.1.0 h213fc3f_46344<br>
[conda] mkl-service 2.4.0 py310h5eee18b_1<br>
[conda] mkl_fft 1.3.11 py310h5eee18b_0<br>
[conda] mkl_random 1.2.8 py310h1128e8f_0<br>
[conda] numpy 1.26.4 pypi_0 pypi<br>
[conda] numpydoc 1.7.0 py310h06a4308_0<br>
[conda] nvidia-cublas-cu12 12.1.3.1 pypi_0 pypi<br>
[conda] nvidia-cuda-cupti-cu12 12.1.105 pypi_0 pypi<br>
[conda] nvidia-cuda-nvrtc-cu12 12.1.105 pypi_0 pypi<br>
[conda] nvidia-cuda-runtime-cu12 12.1.105 pypi_0 pypi<br>
[conda] nvidia-cudnn-cu12 9.1.0.70 pypi_0 pypi<br>
[conda] nvidia-cufft-cu12 11.0.2.54 pypi_0 pypi<br>
[conda] nvidia-curand-cu12 10.3.2.106 pypi_0 pypi<br>
[conda] nvidia-cusolver-cu12 11.4.5.107 pypi_0 pypi<br>
[conda] nvidia-cusparse-cu12 12.1.0.106 pypi_0 pypi<br>
[conda] nvidia-nccl-cu12 2.20.5 pypi_0 pypi<br>
[conda] nvidia-nvjitlink-cu12 12.4.127 pypi_0 pypi<br>
[conda] nvidia-nvtx-cu12 12.1.105 pypi_0 pypi<br>
[conda] pytorch 2.5.1 py3.10_cuda12.4_cudnn9.1.0_0 pytorch<br>
[conda] pytorch-cuda 12.4 hc786d27_7 pytorch<br>
[conda] pytorch-mutex 1.0 cuda pytorch<br>
[conda] torchaudio 2.4.0 pypi_0 pypi<br>
[conda] torchtriton 3.1.0 py310 pytorch<br>
[conda] torchvision 0.20.1 pypi_0 pypi<br>
[conda] triton 3.0.0 pypi_0 pypi</p>
<p>I would appreciate if anyone who has encountered and resolved this error could share their solution.</p>

ptrblck: <aside class="quote no-group" data-username="Hayat_Ullah" data-post="1" data-topic="214902">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hayat_ullah/48/74922_2.png" class="avatar"> Hayat_Ullah:</div>
<blockquote>
<p>return self.attn(x, x, x, need_weights=False, attn_mask=self.attn_mask)[0]</p>
</blockquote>
</aside>
<p>Fails raising the invalid <code>dtype</code> so check all inputs and make sure their <code>dtype</code> is a valid combination.</p>

Hayat_Ullah: <p>I have resolved the issue ( <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20">) by using explicit type casting with <strong>torch.autocast(“cuda”)</strong>. This problem appears to be a potential bug in PyTorch (I believe). When using PyTorch 2 on a CPU instead of CUDA, the error occurs because PyTorch converts the query to torch.bfloat16, which doesn’t match the dtype used for the attention_mask (torch.float32). PyTorch 2 uses a function called scaled_dot_product_attention, which cannot handle mismatched dtypes between the query and the mask.</p>