Repository navigation
fix: datasets broken import due to HF package and folder name collision - #1730
Conversation
tianyu-l
left a comment
There was a problem hiding this comment.
however, it causes a collision with HF datasets Python package called
datasets. So when we try to import theDatasetclass, 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?
|
Did you launch command inside the torchtitan folder instead of at the repo root folder? Is there any reason you're not launching from the root command, where the |
Good point @lkhphuc , I doubled checked and I'm running from the root directory, however, I'm using the |
|
@idoh |
|
Directly calling something like the following also does not run into the issue.
The problem occurs in distributed environments like torchrun, mpirun etc. |
|
@tianyu-l the
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. |
|
Can someone please merge this ? This is stopping all our works. In my view should not rely on launch magics. |
tianyu-l
left a comment
There was a problem hiding this comment.
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. |
|
@tianyu-l I applied the fix according to option (2). I think this issue deserves a bigger discussion if we want edit: I see that you submitted an RFC which I missed as I already applied the changes in my new commit. |
tianyu-l
left a comment
There was a problem hiding this comment.
Agree that we should unblock torchrun users and discuss a longer term solution.
|
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. |
|
The other victim of the -m torchtitan.train fix is -m pdb ! |
…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.
…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.
This PR resolves the
datasetsimport error that were introduced in #1712. The error causes the following error message:Why is this happening? It is because #1712 added an
__init__.pyfile to thedatasetsfolder. On the surface the PR looks fine, however, it causes a collision with HF datasets Python package calleddatasets. So when we try to import theDatasetclass, we actually want the HF datasets package and not the localdatasetsfolder. This, in turn, causes an import error. The solution is simple, change the__init__.pyname to something else, in our case I changed it tocommon.pyas I found this name most fitting.