[Bug] Segmentation fault when importing `torchmetrics` after creating a TVM CUDA target (LLVM initialization / COFF OptTable)
- Lingua principale
- Python
- Stelle
- 13.7k
- Fork
- 4k
- Merge medio
- 1g 13h
- PR unite (30g)
- 125
Descrizione
### Summary
Creating a TVM `cuda` target and then importing an unrelated Python package (`torchmetrics`) causes an immediate segmentation fault. There is no model compilation or runtime execution involved—simply constructing a target triggers the problem.
The crash occurs inside LLVM initialization, specifically in COFF directive parser global constructors (e.g., `llvm::opt::OptTable::buildPrefixChars()` and `_GLOBAL__sub_I_COFFDirectiveParser.cpp`), during dynamic library loading (`dlopen`).
The behavior suggests a dynamic linking / multiple-LLVM interaction.
---
### Minimal Reproduction
```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tvm
from tvm import target
from torchmetrics import Accuracy # segmentation fault occurs here in my environment
def main():
print("Creating CUDA target...")
tgt = target.Target("cuda -arch=sm_86")
print("Target created:", tgt)
metric = Accuracy(task="multiclass", num_classes=10)
print("Metric created:", metric)
if __name__ == "__main__":
main()
```
### Actual Behavior
On my machine, the process prints "Creating CUDA target…" and then crashes with segmentation fault during import of `torchmetrics`. The beginning of the backtrace is:
```
!!!!!!! Segfault encountered !!!!!!!
File "", in llvm::opt::OptTable::buildPrefixChars()
File "", in COFFOptTable::COFFOptTable()
File "", in _GLOBAL__sub_I_COFFDirectiveParser.cpp
File "./elf/dl-init.c", in call_init
File "./elf/dl-open.c", in dl_open_worker
...
Segmentation fault (core dumped)
```
Full trace is long but mostly `dlopen` / `dl-init` frames followed by LLVM initialization frames.
---
### Expected Behavior
Importing `torchmetrics` after TVM target construction should not crash, especially before any compilation or runtime invocation occurs. The two libraries are unrelated and no model is passed to TVM.
---
### Notes on Repro Properties
* The issue does **not** require PyTorch, transformers, or CUDA execution.
* The critical step is:
```
tgt = target.Target("cuda -arch=sm_86")
```
followed by importing a package that triggers its own dynamic-library / symbol loading chain.
* The failure happens even if `Accuracy` is never called.
* Removing the TVM target creation avoids the crash.
### Environment
```
OS: Linux x86_64 (glibc-based)
Python: 3.10.16 (conda-forge)
NumPy: 2.2.6
PyTorch: 2.9.0+cu128
Torchmetrics: # fill here if needed
TVM: 0.22.0
LLVM: 17.0.6 (from tvm.support.libinfo())
GIT_COMMIT_HASH: 9dbf3f22ff6f44962472f9af310fda368ca85ef2
GPU: sm_86 (Ampere)
TVM target: cuda -keys=cuda,gpu -arch=sm_86 -max_num_threads=1024 -thread_warp_size=32
CUDA toolkit: likely 12.8 (based on PyTorch +cu128 build)
```
### Triage
Please refer to the list of label tags [here](https://github.com/apache/tvm/wiki/Issue-Triage-Labels) to find the relevant tags and add them below in a bullet format (example below).
* needs-triage
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia con la riproduzione Python minima fornita, in particolare target.Target("cuda -arch=sm_86") seguito dall’importazione di torchmetrics, e acquisisci il backtrace completo di dlopen/dl-init e LLVM. Esamina l’interazione tra l’inizializzazione di LLVM 17.0.6 di TVM e la catena di librerie caricate dinamicamente nell’ambiente Linux indicato. Il lavoro è completato quando l’importazione non causa più un segfault dopo la creazione del target CUDA.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100