Skip to content

fix: improve pytorch model initialization to prevent meta tensor issues#180

Merged
nicoloboschi merged 3 commits intomainfrom
fix-api-ci
Jan 20, 2026
Merged

fix: improve pytorch model initialization to prevent meta tensor issues#180
nicoloboschi merged 3 commits intomainfrom
fix-api-ci

Conversation

@nicoloboschi
Copy link
Copy Markdown
Collaborator

@nicoloboschi nicoloboschi commented Jan 20, 2026

No description provided.

When accelerate is installed but no GPU is available, transformers can
incorrectly use lazy loading (meta tensors) which fails when
sentence-transformers tries to move the model to a device.

The fix checks hardware and installed packages to determine the right
loading strategy:
- GPU available: device=None, device_map=None (auto-detect GPU)
- No GPU + accelerate: device='cpu', device_map='cpu' (force CPU loading)
- No GPU + no accelerate: device='cpu', device_map=None (normal CPU)

🤖 Generated with [Claude Code](https://claude.com/claude-code)
When pytest-xdist runs multiple workers in parallel, they all try to
load models from the HuggingFace cache simultaneously, causing race
conditions and intermittent meta tensor errors.

Added filelock around embeddings and cross_encoder initialization in
conftest.py, similar to how pg0 database setup is serialized. Models
are now pre-initialized in the fixture before being passed to tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Extend GPU detection to include Apple MPS backend in addition to CUDA.
This ensures macOS users with Apple Silicon use MPS acceleration
instead of being incorrectly routed to the CPU fallback path.
@nicoloboschi nicoloboschi merged commit 0bf85a3 into main Jan 20, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant