Skip to content

Comments

Avoid eager transformers/torch import in levanter#2942

Open
yonromai wants to merge 1 commit intomainfrom
fix/lazy-transformers-import
Open

Avoid eager transformers/torch import in levanter#2942
yonromai wants to merge 1 commit intomainfrom
fix/lazy-transformers-import

Conversation

@yonromai
Copy link
Contributor

Summary

Importing anything from levanter eagerly imports transformerstorch, which crashes on CPU-only environments missing CUDA libs (#2937, #2897).

Three independent chains were pulling in transformers:

  1. hf_utils.pyfrom transformers import ... at module level to define HfTokenizer. No call site uses this alias at runtime (no isinstance checks), so it's now TYPE_CHECKING-only with Any at runtime.

  2. silence_transformer_nag() — Did import transformers after setting the env var. The env var alone is sufficient.

  3. levanter.eval / eval_harness — Reach _batch_tokenizer.py which has a hard runtime dependency on transformers. Now lazy-loaded via __getattr__ in __init__.py.

Fixes #2941.

Test plan

  • from levanter.layers.rotary import Llama3RotaryEmbeddingsConfig no longer imports transformers
  • Pre-commit clean
  • Full levanter test suite passes (844 passed, 60 skipped)

🤖 Generated with Claude Code

Importing anything from levanter eagerly imported transformers, which
unconditionally imports torch and crashes on CPU-only environments
missing CUDA libs.

Three independent chains were fixed:

1. hf_utils.py: guard transformers import under TYPE_CHECKING (the
   HfTokenizer alias is only used in annotations, never at runtime)
2. silence_transformer_nag(): remove redundant `import transformers`
   after setting the env var
3. levanter/__init__.py: lazy-load eval and eval_harness, which have
   a hard runtime dependency on transformers via _batch_tokenizer.py

Fixes #2941

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Collaborator

@rjpower rjpower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine but dlwh should be the judge. Dumb question, can we get torch blow up like this? It seems like it should be easy but I couldn't figure it out before so maybe not worth a lot of time.

Copy link
Member

@dlwh dlwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the real problem is that we're installing cuda wheels on non-cuda machines but this is a fine workaround

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.

Levanter __init__.py eagerly imports torch via transformers

3 participants