Skip to content

feat(init): make models and haiku dependencies lazily imported (#160) - #188

Open
reginaldalfret wants to merge 1 commit into
google-deepmind:masterfrom
reginaldalfret:fix/issue-160-optional-models-import
Open

reginaldalfret wants to merge 1 commit into
google-deepmind:masterfrom
reginaldalfret:fix/issue-160-optional-models-import

Conversation

@reginaldalfret

Copy link
Copy Markdown

Summary

Resolves #160

Currently, importing clrs unconditionally imports clrs.models, clrs._src.decoders, clrs._src.processors, and get_processor_factory, which immediately depend on dm-haiku and chex. Users who only want to utilize the CLRS benchmark algorithms, specs, samplers, or generate datasets (e.g. for Hugging Face or custom model architectures) are unable to import clrs without installing dm-haiku.

Changes

  1. Lazy Loading via __getattr__ (PEP 562) in clrs/__init__.py:
    • models, decoders, processors, and get_processor_factory are now lazily loaded when accessed.
    • Accessing them without the required model dependencies raises a clear, helpful ImportError instructing the user to install dm-haiku.
    • Core CLRS components (CLRSDataset, create_dataset, build_sampler, algorithms, specs, clrs_text, etc.) are readily available without requiring dm-haiku.
  2. Optional TensorFlow in probing.py:
    • tensorflow import in clrs/_src/probing.py is safely handled so probing utilities can be used without TensorFlow.
  3. Regression Tests:
    • Added unit test in clrs/_src/lazy_import_test.py verifying that core components are available, lazy attributes exist in dir(clrs), and invalid attributes properly raise AttributeError.

Verification

  • Ran regression test python -m unittest clrs._src.lazy_import_test in an isolated environment without dm-haiku: 3 tests passed.
  • Verified that accessing clrs.models without dm-haiku raises a clear ImportError.

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.

Remove from __init__

1 participant