-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Description
🐛 Describe the bug
When running a generative model with increasing sequence length, the SDPA call will specialize on the sequence length dimension iff using Flash Attention. If memory efficient or math kernels are used instead, this does not happen. I expect that all three kernels should not specialize on this dimension.
Repro script: TORCH_LOGS="dynamo,dynamic" python repro.py
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.q = nn.Linear(1024, 1024)
self.k = nn.Linear(1024, 1024)
self.v = nn.Linear(1024, 1024)
def forward(self, x):
batch_size, seq_len, _ = x.size()
queries = self.q(x).view(batch_size, seq_len, 8, 128).transpose(2, 1)
keys = self.k(x).view(batch_size, seq_len, 8, 128).transpose(2, 1)
values = self.v(x).view(batch_size, seq_len, 8, 128).transpose(2, 1)
attn = F.scaled_dot_product_attention(
queries,
keys,
values,
)
return attn
model = Model().cuda().half()
model = torch.compile(model, dynamic=True)
torch.backends.cuda.enable_flash_sdp(True)
torch.backends.cuda.enable_math_sdp(False)
torch.backends.cuda.enable_mem_efficient_sdp(False)
input1 = torch.rand(5, 512, 1024, device="cuda", dtype=torch.float16)
input2 = torch.rand(5, 513, 1024, device="cuda", dtype=torch.float16)
input3 = torch.rand(5, 514, 1024, device="cuda", dtype=torch.float16)
out1 = model(input1)
out2 = model(input2)
out3 = model(input3)
Output:
[2023-09-29 22:17:12,296] [0/0] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward /workspace/foundation-model-stack/repro.py:14
[2023-09-29 22:17:12,296] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] create_env
[2023-09-29 22:17:12,326] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s0 = 5 for L['x'].size()[0] [2, 9223372036854775806]
[2023-09-29 22:17:12,328] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s1 = 512 for L['x'].size()[1] [2, 9223372036854775806]
[2023-09-29 22:17:12,330] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s2 = 1024 for L['x'].size()[2] [2, 9223372036854775806]
[2023-09-29 22:17:12,385] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s2, 1024) [guard added] at orkspace/foundation-model-stack/repro.py:17 in forward (_meta_registrations.py:1891 in meta_mm)
[2023-09-29 22:17:12,465] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s1, 512) [guard added] at orkspace/foundation-model-stack/repro.py:21 in forward (_ops.py:499 in __call__)
[2023-09-29 22:17:12,469] [0/0] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-09-29 22:17:12,471] [0/0] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function inductor
[2023-09-29 22:17:13,697] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] eval 524288*s0 < 2147483648 [guard added] (_inductor/codegen/triton.py:2392 in can_use_32bit_indexing)
[2023-09-29 22:17:14,949] [0/0] torch._dynamo.output_graph: [INFO] Step 2: done compiler function inductor
[2023-09-29 22:17:14,999] [0/0] torch.fx.experimental.symbolic_shapes: [INFO] produce_guards
[2023-09-29 22:17:15,104] [0/1] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward /workspace/foundation-model-stack/repro.py:14
[2023-09-29 22:17:15,104] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] create_env
[2023-09-29 22:17:15,107] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s0 = 5 for L['x'].size()[0] [2, 9223372036854775806]
[2023-09-29 22:17:15,107] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s1 = 513 for L['x'].size()[1] [2, 9223372036854775806]
[2023-09-29 22:17:15,108] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s2 = 1024 for L['x'].size()[2] [2, 9223372036854775806]
[2023-09-29 22:17:15,133] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s2, 1024) [guard added] at orkspace/foundation-model-stack/repro.py:17 in forward (_meta_registrations.py:1891 in meta_mm)
[2023-09-29 22:17:15,184] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s1, 513) [guard added] at orkspace/foundation-model-stack/repro.py:21 in forward (_ops.py:499 in __call__)
[2023-09-29 22:17:15,188] [0/1] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-09-29 22:17:15,189] [0/1] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function inductor
[2023-09-29 22:17:16,057] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] eval 525312*s0 < 2147483648 [guard added] (_inductor/codegen/triton.py:2392 in can_use_32bit_indexing)
[2023-09-29 22:17:16,093] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] eval 21504*(((513*s0 + 20)//21)) < 2147483648 [guard added] (_inductor/codegen/triton.py:2392 in can_use_32bit_indexing)
[2023-09-29 22:17:16,910] [0/1] torch._dynamo.output_graph: [INFO] Step 2: done compiler function inductor
[2023-09-29 22:17:16,912] [0/1] torch.fx.experimental.symbolic_shapes: [INFO] produce_guards
[2023-09-29 22:17:16,933] [0/2] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward /workspace/foundation-model-stack/repro.py:14
[2023-09-29 22:17:16,933] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] create_env
[2023-09-29 22:17:16,935] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s0 = 5 for L['x'].size()[0] [2, 9223372036854775806]
[2023-09-29 22:17:16,935] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s1 = 514 for L['x'].size()[1] [2, 9223372036854775806]
[2023-09-29 22:17:16,936] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] create_symbol s2 = 1024 for L['x'].size()[2] [2, 9223372036854775806]
[2023-09-29 22:17:16,964] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s2, 1024) [guard added] at orkspace/foundation-model-stack/repro.py:17 in forward (_meta_registrations.py:1891 in meta_mm)
[2023-09-29 22:17:17,016] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] eval Eq(s1, 514) [guard added] at orkspace/foundation-model-stack/repro.py:21 in forward (_ops.py:499 in __call__)
[2023-09-29 22:17:17,020] [0/2] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-09-29 22:17:17,022] [0/2] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function inductor
[2023-09-29 22:17:17,891] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] eval 526336*s0 < 2147483648 [guard added] (_inductor/codegen/triton.py:2392 in can_use_32bit_indexing)
[2023-09-29 22:17:17,913] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] eval 21504*(((514*s0 + 20)//21)) < 2147483648 [guard added] (_inductor/codegen/triton.py:2392 in can_use_32bit_indexing)
[2023-09-29 22:17:18,714] [0/2] torch._dynamo.output_graph: [INFO] Step 2: done compiler function inductor
[2023-09-29 22:17:18,717] [0/2] torch.fx.experimental.symbolic_shapes: [INFO] produce_guards
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] TorchDynamo compilation metrics:
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] Function Runtimes (s)
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] ------------------------------------ --------------
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] _compile.<locals>.compile_inner 6.3319
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] OutputGraph.call_user_compiler 5.8914
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] create_aot_dispatcher_function 5.9483
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] compile_fx.<locals>.fw_compiler_base 0.3528
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] GraphLowering.run 0.2275
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] GraphLowering.compile_to_module 3.1069
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] Scheduler.__init__ 0.1304
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] Scheduler.codegen 0.3421
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] WrapperCodeGen.generate 0.0121
[2023-09-29 22:17:18,768] torch._dynamo.utils: [INFO] compile_fx.<locals>.bw_compiler 3.5375
Versions
Collecting environment information...
PyTorch version: 2.2.0.dev20230929+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A
OS: Debian GNU/Linux 11 (bullseye) (x86_64)
GCC version: (Debian 10.2.1-6) 10.2.1 20210110
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31
Python version: 3.11.4 (main, Jul 5 2023, 13:45:01) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-4.18.0-372.58.1.el8_6.x86_64-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA A100-SXM4-80GB
GPU 1: NVIDIA A100-SXM4-80GB
GPU 2: NVIDIA A100-SXM4-80GB
GPU 3: NVIDIA A100-SXM4-80GB
GPU 4: NVIDIA A100-SXM4-80GB
GPU 5: NVIDIA A100-SXM4-80GB
GPU 6: NVIDIA A100-SXM4-80GB
GPU 7: NVIDIA A100-SXM4-80GB
Nvidia driver version: 525.105.17
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): 96
On-line CPU(s) list: 0-95
Thread(s) per core: 2
Core(s) per socket: 24
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz
Stepping: 7
CPU MHz: 2999.998
BogoMIPS: 5999.99
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 1.5 MiB
L1i cache: 1.5 MiB
L2 cache: 48 MiB
L3 cache: 71.5 MiB
NUMA node0 CPU(s): 0-23,48-71
NUMA node1 CPU(s): 24-47,72-95
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: Vulnerable
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: Not affected
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves ida arat pku ospke
Versions of relevant libraries:
[pip3] numpy==1.26.0
[pip3] pytorch-triton==2.1.0+6e4932cda8
[pip3] torch==2.2.0.dev20230929+cu118
[pip3] torchvision==0.17.0.dev20230929+cu118
[conda] numpy 1.26.0 pypi_0 pypi
[conda] pytorch-triton 2.1.0+6e4932cda8 pypi_0 pypi
[conda] torch 2.2.0.dev20230929+cu118 pypi_0 pypi
[conda] torchvision 0.17.0.dev20230929+cu118 pypi_0 pypi
cc @jbschlosser @bhosmer @cpuhrsch @erichan1 @drisspg @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @chenyang78 @aakhundov @kadeng