-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Description
🐛 Describe the bug
For models that use the dynamic shape of one of its tensors as part of an indexing op, torch.export complains it can't deal with it. The same model torch.compiles() with dynamic shapes with no issues at all.
Repro:
from pathlib import Path
import torch
import torch._inductor.config
from torch.export import export, Dim, save, load
import torch._dynamo.config
class ToyModel(torch.nn.Module):
def __init__(self):
super(ToyModel, self).__init__()
self.net1 = torch.nn.Linear(100, 50)
self.net2 = torch.nn.Linear(50, 100)
def forward(self, x, y):
z = self.net1(x)
z = z[:, -y.shape[0]:, :]
z = self.net2(z)
return z
toy_model = ToyModel()
torch._inductor.config.fx_graph_cache = True
print("exporting and compiling model")
export_file = Path("model.pt2")
if not export_file.is_file():
seqlen = Dim("seqlen", max=4)
exported_model = export(
toy_model,
(torch.randn(4, 5, 100), torch.randn(3)),
dynamic_shapes={"x": {}, "y": {0: seqlen}}
)
save(exported_model, export_file)
# compiling can make first inference pass slow
loaded_model = load(export_file)
print(loaded_model(torch.randn(4, 5, 50), torch.randn(3)))Error:
$ python scripts/export_repro.py
exporting and compiling model
Traceback (most recent call last):
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 1318, in serialize_graph
getattr(self, f"handle_{node.op}")(node)
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 545, in handle_call_function
raise SerializeError(f"Serializing {node.target} is not supported")
torch._export.serde.serialize.SerializeError: Serializing <built-in function neg> is not supported
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/data/aviros/foundation-model-stack/scripts/export_repro.py", line 33, in <module>
save(exported_model, export_file)
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/export/__init__.py", line 244, in save
save(ep, f, extra_files=extra_files, opset_version=opset_version)
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/__init__.py", line 246, in save
artifact: SerializedArtifact = serialize(ep, opset_version)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 2297, in serialize
serialized_program = ExportedProgramSerializer(opset_version).serialize(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 1364, in serialize
serialized_graph_module = gm_serializer.serialize(exported_program.graph_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 1336, in serialize
graph = self.serialize_graph(graph_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/aviros/conda_envs/fms-dev/lib/python3.11/site-packages/torch/_export/serde/serialize.py", line 1320, in serialize_graph
raise SerializeError(
torch._export.serde.serialize.SerializeError: Failed serializing node neg in graph: %neg : [num_users=1] = call_function[target=operator.neg](args = (%sym_size_int,), kwargs = {})
Versions
Collecting environment information...
PyTorch version: 2.5.0.dev20240619+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A
OS: Red Hat Enterprise Linux 9.2 (Plow) (x86_64)
GCC version: (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
Clang version: Could not collect
CMake version: version 3.20.2
Libc version: glibc-2.34
Python version: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.14.0-284.67.1.el9_2.x86_64-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA L40S
GPU 1: NVIDIA L40S
Nvidia driver version: 555.42.02
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.9.1.1
/usr/lib64/libcudnn_adv.so.9.1.1
/usr/lib64/libcudnn_cnn.so.9.1.1
/usr/lib64/libcudnn_engines_precompiled.so.9.1.1
/usr/lib64/libcudnn_engines_runtime_compiled.so.9.1.1
/usr/lib64/libcudnn_graph.so.9.1.1
/usr/lib64/libcudnn_heuristic.so.9.1.1
/usr/lib64/libcudnn_ops.so.9.1.1
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: 40 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 48
On-line CPU(s) list: 0-47
Vendor ID: GenuineIntel
Model name: Intel Xeon Processor (SapphireRapids)
CPU family: 6
Model: 143
Thread(s) per core: 2
Core(s) per socket: 12
Socket(s): 2
Stepping: 4
BogoMIPS: 4200.00
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 rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx 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 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx_vnni avx512_bf16 wbnoinvd arat avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b fsrm md_clear serialize tsxldtrk amx_bf16 avx512_fp16 amx_tile amx_int8 arch_capabilities
Virtualization: VT-x
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 1.5 MiB (48 instances)
L1i cache: 1.5 MiB (48 instances)
L2 cache: 96 MiB (24 instances)
L3 cache: 32 MiB (2 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-23
NUMA node1 CPU(s): 24-47
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: Unknown: No mitigations
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 IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Versions of relevant libraries:
[pip3] flake8==7.0.0
[pip3] mypy==1.10.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] pytorch-triton==3.0.0+45fff310c8
[pip3] torch==2.5.0.dev20240619+cu124
[conda] numpy 1.26.4 pypi_0 pypi
[conda] pytorch-triton 3.0.0+45fff310c8 pypi_0 pypi
[conda] torch 2.5.0.dev20240619+cu124 pypi_0 pypi
cc @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4 @penguinwu