Skip to content

fix: datasets broken import due to HF package and folder name collision - #1730

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
idoh:fix-datasets-import
Sep 23, 2025
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
idoh:fix-datasets-import

Conversation

@idoh

@idoh idoh commented Sep 21, 2025

Copy link
Copy Markdown
Contributor

This PR resolves the datasets import error that were introduced in #1712. The error causes the following error message:

  File "./torchtitan/train.py", line 16, in <module>
    import torchtitan.protocols.train_spec as train_spec_module
  File "./torchtitan/__init__.py", line 12, in <module>
    import torchtitan.experiments  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/__init__.py", line 7, in <module>
    import torchtitan.experiments.llama4  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/llama4/__init__.py", line 11, in <module>
    from torchtitan.datasets.hf_datasets import build_hf_dataloader
  File "./torchtitan/datasets/hf_datasets.py", line 12, in <module>
    from datasets import Dataset, load_dataset
ImportError: cannot import name 'Dataset' from 'datasets' (./torchtitan/datasets/__init__.py)

Why is this happening? It is because #1712 added an __init__.py file to the datasets folder. On the surface the PR looks fine, however, it causes a collision with HF datasets Python package called datasets. So when we try to import the Dataset class, we actually want the HF datasets package and not the local datasets folder. This, in turn, causes an import error. The solution is simple, change the __init__.py name to something else, in our case I changed it to common.py as I found this name most fitting.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 21, 2025

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

however, it causes a collision with HF datasets Python package called datasets. So when we try to import the Dataset class, we actually want the HF datasets package and not the local datasets folder. This, in turn, causes an import error.

I don't know the Python import mechanism too well. I somehow feel this is not ambiguous, and our CI tests aren't complaining. So I'm a bit puzzled, and would love to learn more before we try to fix it.

Is your use case slightly different? E.g. do you have name collisions under the HF datasets and torchtitan.datasets?

cc @fegin @lkhphuc

@lkhphuc

lkhphuc commented Sep 22, 2025 •

Copy link
Copy Markdown
Contributor

Did you launch command inside the torchtitan folder instead of at the repo root folder?
I can reproduce the error with this command cd torchtitan && CONFIG_FILE=experiments/llama4/train_configs/debug_model.toml ../run_train.sh
Since this folder has datasets folder it override the installed hf's datasets.

Is there any reason you're not launching from the root command, where the __init__.pyis only accessible under torchtitan.datasets?

@idoh

idoh commented Sep 22, 2025

Copy link
Copy Markdown
Contributor Author

Is there any reason you're not launching from the root command, where the __init__.pyis only accessible under torchtitan.datasets?

Good point @lkhphuc , I doubled checked and I'm running from the root directory, however, I'm using the multinode_trainer.slurm script and not the run_train.sh. My launch command is:

CONFIG_FILE=./torchtitan/models/llama3/train_configs/llama3_3b.toml sbatch multinode_trainer.slurm

@tianyu-l

Copy link
Copy Markdown
Contributor

@idoh
Instead of srun torchrun ./torchtitan/train.py, I wonder if srun torchrun -m torchtitan.train would work.

@githubsgi

Copy link
Copy Markdown
Contributor

Directly calling something like the following also does not run into the issue.

REPO=$HOME/torchtitan ; PYTHONPATH=$PYTHONPATH:$REPO python -m pdb $REPO/torchtitan/datasets/hf_datasets.py

The problem occurs in distributed environments like torchrun, mpirun etc.

@idoh

idoh commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor Author

@tianyu-l the -m torchtitan.train fixed it. So, the question is with which direction would we like to go forward with?

  1. Changing the name of __init__.py
  2. Changing the launch command to -m

I prefer option (2) as it applies less code changes, but it would be confusing for others that try to launch it in a straightforward way and get this error, which took a while to understand what's happening. Let me know which option you prefer going forward with.

@githubsgi

githubsgi commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Can someone please merge this ? This is stopping all our works. In my view should not rely on launch magics.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's go with option (2)

@githubsgi

githubsgi commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Let's go with option (2)

That is a problematic fix. There are many launchers beside slurm and torchrun, and may not work with all. The better and cleaner solution is to change the name of the dataset directory to say titands or ttdataset or dataset2 or tt_datasets ..

On hindsight, the reuse of the name datasets should have been flagged as a problem and hence not merged.

@idoh

idoh commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor Author

@tianyu-l I applied the fix according to option (2). I think this issue deserves a bigger discussion if we want torchtitan to be production grade. But, since this is blocking users I suggest merging this and then later discussing the proper way we want to address this and future problems like this.

edit: I see that you submitted an RFC which I missed as I already applied the changes in my new commit.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree that we should unblock torchrun users and discuss a longer term solution.

@tianyu-l
tianyu-l merged commit 686b523 into pytorch:main Sep 23, 2025
@githubsgi

githubsgi commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Looks like the init.py file was remove on 2/24/2025 and reintroduced on 9/16/2025. From the title it sounds like it is for refactoring of DataClass. Is it possible to backout this commit ?

I was able to run with mpirun .... python -m torchtitan.train . Does anyone know why direct calling does not work in this case ? From what I understand it is because PyThon cached the first import of datasets ( totrchtitan) and reused that during imports from the hf dataset.

If the decision is to go with the -m work around for now, then that should be prominently documented everywhere wherever train.py is documented.

@githubsgi

Copy link
Copy Markdown
Contributor

The other victim of the -m torchtitan.train fix is -m pdb !

xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
…on (pytorch#1730)

This PR resolves the `datasets` import error that were introduced in
pytorch#1712. The error causes the following error message:
```Traceback (most recent call last):
  File "./torchtitan/train.py", line 16, in <module>
    import torchtitan.protocols.train_spec as train_spec_module
  File "./torchtitan/__init__.py", line 12, in <module>
    import torchtitan.experiments  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/__init__.py", line 7, in <module>
    import torchtitan.experiments.llama4  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/llama4/__init__.py", line 11, in <module>
    from torchtitan.datasets.hf_datasets import build_hf_dataloader
  File "./torchtitan/datasets/hf_datasets.py", line 12, in <module>
    from datasets import Dataset, load_dataset
ImportError: cannot import name 'Dataset' from 'datasets' (./torchtitan/datasets/__init__.py)
```

Why is this happening? It is because pytorch#1712 added an `__init__.py` file
to the `datasets` folder. On the surface the PR looks fine, however, it
causes a collision with HF datasets Python package called `datasets`. So
when we try to import the `Dataset` class, we actually want the HF
datasets package and not the local `datasets` folder. This, in turn,
causes an import error. The solution is simple, change the `__init__.py`
name to something else, in our case I changed it to `common.py` as I
found this name most fitting.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
…on (pytorch#1730)

This PR resolves the `datasets` import error that were introduced in
pytorch#1712. The error causes the following error message:
```Traceback (most recent call last):
  File "./torchtitan/train.py", line 16, in <module>
    import torchtitan.protocols.train_spec as train_spec_module
  File "./torchtitan/__init__.py", line 12, in <module>
    import torchtitan.experiments  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/__init__.py", line 7, in <module>
    import torchtitan.experiments.llama4  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./torchtitan/experiments/llama4/__init__.py", line 11, in <module>
    from torchtitan.datasets.hf_datasets import build_hf_dataloader
  File "./torchtitan/datasets/hf_datasets.py", line 12, in <module>
    from datasets import Dataset, load_dataset
ImportError: cannot import name 'Dataset' from 'datasets' (./torchtitan/datasets/__init__.py)
```

Why is this happening? It is because pytorch#1712 added an `__init__.py` file
to the `datasets` folder. On the surface the PR looks fine, however, it
causes a collision with HF datasets Python package called `datasets`. So
when we try to import the `Dataset` class, we actually want the HF
datasets package and not the local `datasets` folder. This, in turn,
causes an import error. The solution is simple, change the `__init__.py`
name to something else, in our case I changed it to `common.py` as I
found this name most fitting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torchtitan/datasets and https://pypi.org/project/datasets conflict

4 participants