Skip to content

fix: don't import datasets on the inference path#41

Open
emiliano-go wants to merge 1 commit into
cactus-compute:mainfrom
emiliano-go:fix/remove-datasets-inference-import
Open

fix: don't import datasets on the inference path#41
emiliano-go wants to merge 1 commit into
cactus-compute:mainfrom
emiliano-go:fix/remove-datasets-inference-import

Conversation

@emiliano-go

Copy link
Copy Markdown

import needle pulled in HuggingFace datasets (and its pyarrow/pandas/fsspec tree) even though nothing on the inference path uses it. The tokenizer symbols that run.py and __init__.py need are defined in needle/dataset/tokenizer.py but were being imported through needle/dataset/dataset.py, which imports datasets at module level.

This repoints those imports at the module where the symbols are actually defined. Same objects, no API or dependency changes.

Closes #40

Changes

File Change
needle/init.py:14 dataset.dataset → dataset.tokenizer
needle/model/run.py:11 dataset.dataset → dataset.tokenizer
needle/dataset/init.py:2 removed from .dataset import *

The third change is required, not cleanup. needle/dataset/__init__.py executes on any needle.dataset.* import, so it triggers datasets before tokenizer.py is ever reached. Without it, the first two changes have no effect.

Verification

python -c "import sys; import needle; print('datasets' in sys.modules)"
# before: True
# after:  False
  • needle.get_tokenizer, needle.generate, needle.load_checkpoint all still resolve
  • get_tokenizer is the same object as before (gt1 is gt2)
  • from needle.dataset.dataset import ... still works; datasets loads lazily when training code actually runs

Safety

No internal consumer relies on the star re-export. All existing imports are either submodule imports (from ..dataset import dataset as data_mod, from ..dataset import generate as gd) or submodule-qualified (from .dataset import ...), none of which are affected by removing it.

Repoint two imports and package __init__.py so that 'import needle' and
needle.model.run do not pull in the HuggingFace datasets library.

All four symbols (get_tokenizer, to_snake_case, DEFAULT_MAX_ENC_LEN,
DEFAULT_MAX_GEN_LEN) are defined in needle/dataset/tokenizer.py and were
merely re-exported through needle/dataset/dataset.py.  Changing the import
source removes the unconditional from datasets import Dataset, load_from_disk
at module level of dataset.py, which was the only thing tying the inference
path to datasets (and its pyarrow/pandas/fsspec dependency tree).

Needle/dataset/__init__.py also re-exported dataset.py via wildcard;
removing that line is required because importing needle.dataset.tokenizer
triggers package initialization, which previously loaded dataset.py.

Signed-off-by: Emiliano Gandini Outeda <emiliano.gandini@protonmail.com>
@emiliano-go
emiliano-go force-pushed the fix/remove-datasets-inference-import branch from 4eee41b to b0b7926 Compare July 17, 2026 17:22
@emiliano-go

Copy link
Copy Markdown
Author

Removing from .dataset import * from needle/dataset/__init__.py means external users

from needle.dataset import SomeTrainingClass

will now get an AttributeError. Internal imports are all safe (submodule-qualified), but for any public downstream consumers, this is a breaking change. Happy to adjust based on your versioning policy, just wanted to flag it explicitly.

Also, to make sure this heavy datasets import never slips back in, I'd recommend adding this quick check to the CI workflow:

python -c "import sys; import needle; assert 'datasets' not in sys.modules, 'datasets leaked into inference path'"

It's cheap (~100ms) and ensures the inference path stays lean indefinitely. I can add it in a separate commit or a follow-up PR, just lmk.

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.

Inference path imports datasets via re-export from dataset.dataset

1 participant