`ImportError: cannot import name 'amp' from 'apex'` on recent NVIDIA PyTorch Images (25.11)
**Description**
I encountered a hard crash when importing GLiNER inside a recent NVIDIA PyTorch Docker container (25.11-py3).
The issue is related to the hard dependency on apex.amp. While apex is installed in this environment, the legacy amp module seems to be deprecated/removed in favor of PyTorch's native AMP (torch.cuda.amp), causing the import to fail immediately.
**Environment details**
```
OS/Environment: Docker (nvcr.io/nvidia/pytorch:25.11-py3)
GLiNER version: (Latest from pip)
PyTorch version: 2.9.1
CUDA: 13.0 (As provided by the container)
```
**Steps to Reproduce**
1. Launch the container
```bash
docker run --gpus all -it --rm --ipc=host \
-v $HOME/.cache/huggingface:/root/.cache/huggingface \
-v ${PWD}:/workspace -w /workspace \
nvcr.io/nvidia/pytorch:25.11-py3
```
2. Install GLiNER
```bash
pip install gliner
```
3. Attempt import
```bash
python -c 'from gliner import GLiNER'
```
Traceback:
```
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.12/dist-packages/gliner/__init__.py", line 3, in <module>
from .model import GLiNER
File "/usr/local/lib/python3.12/dist-packages/gliner/model.py", line 37, in <module>
from .training import Trainer, TrainingArguments
File "/usr/local/lib/python3.12/dist-packages/gliner/training/__init__.py", line 1, in <module>
from .trainer import Trainer, TrainingArguments
File "/usr/local/lib/python3.12/dist-packages/gliner/training/trainer.py", line 22, in <module>
from apex import amp
ImportError: cannot import name 'amp' from 'apex' (/usr/local/lib/python3.12/dist-packages/apex/__init__.py)
```
Note, this issue also affects GLiNER2 (https://github.com/fastino-ai/GLiNER2/blob/765cab59181d39bd3c01204920799527b15f9f6d/gliner2/trainer.py#L12)
For anyone wanting to only run inference, a simple fix is to just wrap the import statement in a try/except block, replacing https://github.com/urchade/GLiNER/blob/7a065864c552823b76212e8d7d0ba68f6891cefc/gliner/training/trainer.py#L22 with
```python
try:
from apex import amp
except ImportError:
print("Failed to import amp from apex")
```
But perhaps it would be worth migrating to native `torch.amp` to resolve this?
1 条评论