Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

TransformerEncoderLayer raise AttributeError: 'NoneType' object has no attribute 'device' when bias=False, batch_first=True #116385

Closed
xbanke opened this issue Dec 25, 2023 · 6 comments
Assignees
Labels
high priority oncall: transformer/mha Issues related to Transformers and MultiheadAttention triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@xbanke
Copy link

xbanke commented Dec 25, 2023

馃悰 Describe the bug

import torch


batch_size = 32
seqlen = 20
d_model = 64
nhead = 4

encoder_layer = torch.nn.TransformerEncoderLayer(d_model, nhead, bias=False, batch_first=True).eval()
x = torch.randn(batch_size, seqlen, d_model)

y = encoder_layer(x)  # AttributeError: 'NoneType' object has no attribute 'device'

Versions

PyTorch version: 2.1.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.11.4 (main, Jul 5 2023, 13:45:01) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-78-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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: 43 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 40
On-line CPU(s) list: 0-39
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz
CPU family: 6
Model: 79
Thread(s) per core: 1
Core(s) per socket: 8
Socket(s): 5
Stepping: 1
BogoMIPS: 4799.99
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm rdseed adx smap xsaveopt arat
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 160 KiB (5 instances)
L1i cache: 160 KiB (5 instances)
L2 cache: 1.3 MiB (5 instances)
L3 cache: 125 MiB (5 instances)
NUMA node(s): 3
NUMA node0 CPU(s): 0-15
NUMA node1 CPU(s): 16-31
NUMA node2 CPU(s): 32-39
Vulnerability Itlb multihit: KVM: Mitigation: VMX unsupported
Vulnerability L1tf: Mitigation; PTE Inversion
Vulnerability Mds: Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Retpolines, STIBP disabled, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown

Versions of relevant libraries:
[pip3] mypy==1.5.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.3
[pip3] pytorch-tabnet==4.1.0
[pip3] torch==2.1.0+cpu
[pip3] torchaudio==2.1.0+cpu
[pip3] torchsort==0.1.9
[pip3] torchvision==0.16.0+cpu
[pip3] torchviz==0.0.2
[conda] numpy 1.24.3 pypi_0 pypi
[conda] pytorch-tabnet 4.1.0 pypi_0 pypi
[conda] torch 2.1.0+cpu pypi_0 pypi
[conda] torchaudio 2.1.0+cpu pypi_0 pypi
[conda] torchsort 0.1.9 pypi_0 pypi
[conda] torchvision 0.16.0+cpu pypi_0 pypi
[conda] torchviz 0.0.2 pypi_0 pypi

cc @ezyang @gchanan @zou3519 @kadeng @jbschlosser @bhosmer @cpuhrsch @erichan1 @drisspg @mikaylagawarecki

@willtryagain
Copy link

willtryagain commented Dec 25, 2023

tensor_args = (
              src,
              self.self_attn.in_proj_weight,
              self.self_attn.in_proj_bias,
              self.self_attn.out_proj.weight,
              self.self_attn.out_proj.bias,
              self.norm1.weight,
              self.norm1.bias,
              self.norm2.weight,
              self.norm2.bias,
              self.linear1.weight,
              self.linear1.bias,
              self.linear2.weight,
              self.linear2.bias,
          )

tensor args also includes the bias variables so while evaluating, (x.device.type in _supported_device_type) for x in tensor_args
it is checking device even for the bias variables which are None in your case. It can be fixed by modifying tensor_args based on bias.

@mingfeima
Copy link
Collaborator

@Valentine233 could you please take a look at this one ?

@jbschlosser jbschlosser added oncall: transformer/mha Issues related to Transformers and MultiheadAttention triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Dec 26, 2023
@jbschlosser
Copy link
Contributor

cc @mikaylagawarecki won't it be nice when the fastpath is gone :)

@Valentine233
Copy link
Collaborator

tensor_args = (
              src,
              self.self_attn.in_proj_weight,
              self.self_attn.in_proj_bias,
              self.self_attn.out_proj.weight,
              self.self_attn.out_proj.bias,
              self.norm1.weight,
              self.norm1.bias,
              self.norm2.weight,
              self.norm2.bias,
              self.linear1.weight,
              self.linear1.bias,
              self.linear2.weight,
              self.linear2.bias,
          )

tensor args also includes the bias variables so while evaluating, (x.device.type in _supported_device_type) for x in tensor_args it is checking device even for the bias variables which are None in your case. It can be fixed by modifying tensor_args based on bias.

Agree. We should check the 6 bias in tensor_args and remove bias which equals to None. It's an issue existing for both CUDA and CPU.

@willtryagain
Copy link

should I submit PR for this?

@mikaylagawarecki
Copy link
Contributor

@willtryagain Assigning to myself as I intend to submit a fix for this today (as this is a duplicate of #116546 which is marked high priority), feel free to take a look at the issues marked actionable if you would like to contribute

@mikaylagawarecki mikaylagawarecki self-assigned this Jan 3, 2024
@Valentine233 Valentine233 removed their assignment Jan 4, 2024
mikaylagawarecki added a commit that referenced this issue Jan 4, 2024
Fixes #116385

Don't call `torch._transformer_encoder_layer_fwd` when `bias=False` (which sets biases to `None`).

This also prevents us from ever doing checks on properties of `tensor_args` in `TransformerEncoder`/`TransformerEncoderLayer` which contained the Nones and was erroring on checks like `t.requires_grad for t in tensor_args`.

Alternative fix would be to
1) Pass `torch.zeros_like({*}.weight)` to the kernel when `bias=False` and filter `tensor_args` as appropriate
2) Fix `torch._transformer_encoder_layer_fwd` to take `Optional<Tensor>` for biases

Let me know if this approach is preferable




[ghstack-poisoned]
mikaylagawarecki added a commit that referenced this issue Jan 4, 2024
Fixes #116385

Don't call `torch._transformer_encoder_layer_fwd` when `bias=False` (which sets biases to `None`).

This also prevents us from ever doing checks on properties of `tensor_args` in `TransformerEncoder`/`TransformerEncoderLayer` which contained the Nones and was erroring on checks like `t.requires_grad for t in tensor_args`.

Alternative fix would be to
1) Pass `torch.zeros_like({*}.weight)` to the kernel when `bias=False` and filter `tensor_args` as appropriate
2) Fix `torch._transformer_encoder_layer_fwd` to take `Optional<Tensor>` for biases and fix the kernels as appropriate

Let me know if these approaches is preferable




[ghstack-poisoned]
mikaylagawarecki added a commit that referenced this issue Jan 4, 2024
Fixes #116385

Don't call `torch._transformer_encoder_layer_fwd` when `bias=False` (which sets biases to `None`).

This also prevents us from ever doing checks on properties of `tensor_args` in `TransformerEncoder`/`TransformerEncoderLayer` which contained the Nones and was erroring on checks like `t.requires_grad for t in tensor_args`.

Alternative fix would be to
1) Pass `torch.zeros_like({*}.weight)` to the kernel when `bias=False` and filter `tensor_args` as appropriate
2) Fix `torch._transformer_encoder_layer_fwd` to take `Optional<Tensor>` for biases and fix the kernels as appropriate

Let me know if these approaches is preferable




[ghstack-poisoned]
mikaylagawarecki added a commit that referenced this issue Jan 4, 2024
Fixes #116385

Don't call `torch._transformer_encoder_layer_fwd` when `bias=False`

`bias=False` was not something that `torch._transformer_encoder_layer_fwd`  was meant to work with, it was my bad that this wasn't tested as I approved #101687.

`bias=False` was causing the `tensor_args` in [`TransformerEncoder`](https://github.com/pytorch/pytorch/blob/a17de2d6455e262f9b514584443ac60cf381bc85/torch/nn/modules/transformer.py#L364-L378)/[`TransformerEncoderLayer`](https://github.com/pytorch/pytorch/blob/a17de2d6455e262f9b514584443ac60cf381bc85/torch/nn/modules/transformer.py#L663-L677) to contain `None`s and error on checks for the fastpath like `t.requires_grad for t in tensor_args`.

Alternative fix would be to
1) Pass `torch.zeros_like({*}.weight)` to the kernel when `bias=False` and filter `tensor_args` as appropriate
2) Fix `torch._transformer_encoder_layer_fwd` to take `Optional<Tensor>` for biases and fix the kernels as appropriate

Let me know if these approaches are preferable




[ghstack-poisoned]
mikaylagawarecki added a commit that referenced this issue Jan 4, 2024
Fixes #116385

Don't call `torch._transformer_encoder_layer_fwd` when `bias=False`

`bias=False` was not something that `torch._transformer_encoder_layer_fwd`  was meant to work with, it was my bad that this wasn't tested as I approved #101687.

`bias=False` was causing the `tensor_args` in [`TransformerEncoder`](https://github.com/pytorch/pytorch/blob/a17de2d6455e262f9b514584443ac60cf381bc85/torch/nn/modules/transformer.py#L364-L378)/[`TransformerEncoderLayer`](https://github.com/pytorch/pytorch/blob/a17de2d6455e262f9b514584443ac60cf381bc85/torch/nn/modules/transformer.py#L663-L677) to contain `None`s and error on checks for the fastpath like `t.requires_grad for t in tensor_args`.

Alternative fix would be to
1) Pass `torch.zeros_like({*}.weight)` to the kernel when `bias=False` and filter `tensor_args` as appropriate
2) Fix `torch._transformer_encoder_layer_fwd` to take `Optional<Tensor>` for biases and fix the kernels as appropriate

Let me know if these approaches are preferable




[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority oncall: transformer/mha Issues related to Transformers and MultiheadAttention triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
Status: Done
Development

No branches or pull requests

6 participants