apache / apache/tvm

[Bug] Segmentation fault when importing `torchmetrics` after creating a TVM CUDA target (LLVM initialization / COFF OptTable)

Offen
#18,655 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
needs-triage type: bug
Vorherrschende Sprache
Python
Sterne
13.7k
Forks
4k
Ø Merge
1 T. 13 Std.
Gemergte PRs (30 T.)
125

Beschreibung

### 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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginnen Sie mit der bereitgestellten minimalen Python-Reproduktion, insbesondere mit target.Target("cuda -arch=sm_86") gefolgt vom Import von torchmetrics, und erfassen Sie den vollständigen dlopen/dl-init- und LLVM-Backtrace. Untersuchen Sie die Wechselwirkung zwischen der Initialisierung von TVM's LLVM 17.0.6 und der Kette dynamisch geladener Bibliotheken in der aufgeführten Linux-Umgebung. Als erledigt gilt die Aufgabe, wenn der Import nach dem Erstellen des CUDA-Targets keinen Segmentation Fault mehr verursacht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.