Skip to content

Custom Dataset refactoring + docs - #715

Merged
msaroufim merged 12 commits into
mainfrom
msaroufim/data
Dec 5, 2024
Merged

msaroufim merged 12 commits into
mainfrom
msaroufim/data

Conversation

@msaroufim

@msaroufim msaroufim commented Dec 4, 2024 •

Copy link
Copy Markdown

EDIT: removed the specific new functions in hf_datasets.py and kept most of the doc changes and will not go for a registration based API

Fixes #311

This PR describes the status quo of how new datasets should be registered today, in that there's the implicit assumption that people are installing torchtitan from source and updating hf_datasets.py to support new datasets. As an example I passed in the wikipedia dataset

The main "nice" thing about this PR is that class HuggingFaceDataset is now agnostic to the c4 dataset which makes it easier for new people to add datasets without reading the rest of the file

There's another direction this PR could have went in which was to allow custom dataset registration, the benefit is people can support new datasets without installing titan from source but registration apis can feel kinda "bureaucratic" and presumably people would need to register the dataset somewhere, probably train.py?

Not totally sure which is more in line with the repo's goals so opening this PR to discuss

def register_dataset(
    name: str,
    loader: Callable[[str, Dict[str, Any]], Any],
    processor: Callable[[Dict[str, Any]], str],
    path: Optional[str] = None,
) -> None:

    DATASET_LOADERS[name] = loader
    DATASET_TEXT_PROCESSORS[name] = processor

def wikipedia_loader(dataset_path: str, **kwargs):
    return load_dataset(
        dataset_path,
        name="20220301.en",
        split="train", 
        streaming=True,
        trust_remote_code=True,
    )

def wikipedia_processor(sample: Dict[str, Any]) -> str:
    return f"{sample['title']}\n\n{sample['text']}"

register_dataset(
    name="wikipedia",
    loader=wikipedia_loader,
    processor=wikipedia_processor,
    path="wikipedia"
)

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 4, 2024
@msaroufim msaroufim changed the title Custom Dataset docs Custom Dataset refactoring + docs Dec 4, 2024
Comment thread torchtitan/datasets/hf_datasets.py Resolved
Mark Saroufim added 2 commits December 5, 2024 10:46
@msaroufim
msaroufim requested a review from tianyu-l December 5, 2024 18:48
@msaroufim

Copy link
Copy Markdown
Author

This is ready for a review @tianyu-l

@msaroufim

Copy link
Copy Markdown
Author

The failure in CI seems like a flake with glib 2.8?

@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.

This is very cool!
I had some inline suggestions. As part of the test, would you also please share a sceenshot of the C4 dataset working properly? In CI, only c4_test is tested.

Comment thread docs/datasets.md Outdated
Comment on lines +7 to +10
1. Install TorchTitan from source:
```bash
pip install -e .
```

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.

nit: I don't think one needs to pip install to use torchtitan :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

huh interesting this wasn't obvious to me

Comment thread torchtitan/datasets/hf_datasets.py Resolved
Comment thread torchtitan/datasets/hf_datasets.py Outdated
# Force lowercase for consistent comparison
dataset_name = dataset_name.lower()

if dataset_name not in _supported_datasets:

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.

the tutorial didn't mention that we need to update the _supported_datasets with new dataset and its path, so it would fail here. I'd recommend we always let user specify the triple, including a default path (but still with the dataset_path overriding option).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I took your feedback and it was helpful to refactor the code and make it much simpler

Comment thread torchtitan/datasets/hf_datasets.py Resolved
Comment thread torchtitan/datasets/hf_datasets.py Resolved
@msaroufim

msaroufim commented Dec 5, 2024 •

Copy link
Copy Markdown
Author

Test logs

[training]
batch_size = 1
seq_len = 8192
warmup_steps = 200  # lr scheduler warm up
max_norm = 1.0  # grad norm clipping
steps = 1000
data_parallel_replicate_degree = 1
data_parallel_shard_degree = -1
tensor_parallel_degree = 1
compile = false
dataset = "c4"
rank0]:2024-12-05 13:24:36,127 - root - INFO - Starting job: Llama 3 8B training
[rank0]:2024-12-05 13:24:36,128 - root - INFO - Deterministic training off
[rank0]:2024-12-05 13:24:36,915 - root - WARNING - ENV[TORCH_NCCL_ASYNC_ERROR_HANDLING] = 1 will be overridden to 3 based on job config
[rank0]:2024-12-05 13:24:36,917 - root - INFO - CUDA capacity: NVIDIA H100with 95.00GiB memory
[rank0]:2024-12-05 13:24:36,995 - root - INFO - Peak FLOPS used for computing MFU: 9.890e+14
[rank0]:2024-12-05 13:24:36,995 - root - INFO - Building 1-D device mesh with ['dp'], [8]
[rank0]:2024-12-05 13:24:36,995 - root - INFO - Building tiktoken tokenizer locally from ./torchtitan/datasets/tokenizer/original/tokenizer.model
[rank0]:2024-12-05 13:24:37,139 - root - INFO - TikTokenizer built: #words 128256, BOS ID 128000, EOS ID 128001
[rank0]:2024-12-05 13:24:37,139 - root - INFO - Preparing c4 dataset from allenai/c4
[rank0]:2024-12-05 13:24:45,960 - root - INFO - Building llama3 8B with ModelArgs(dim=4096, n_layers=32, n_heads=32, n_kv_heads=8, vocab_size=128256, multiple_of=1024, ffn_dim_multiplier=1.3, norm_eps=1e-05, rope_theta=500000, max_seq_len=8192, depth_init=True, norm_type='rmsnorm')
[rank0]:2024-12-05 13:24:46,098 - root - INFO - Model llama3 8B size: 8,030,261,248 total parameters
[rank0]:2024-12-05 13:24:46,099 - root - INFO - Applied selective activation checkpointing to the model
[rank0]:2024-12-05 13:24:46,163 - root - INFO - Applied FSDP to the model
[rank0]:NCCL version 2.21.5+cuda12.4
[rank0]:/home/marksaroufim/.conda/envs/titan/lib/python3.10/site-packages/torch/nn/init.py:51: UserWarning: No PYTORCH_KERNEL_CACHE_PATH or HOME environment variable set! This disables kernel caching. (Triggered internally at ../aten/src/ATen/native/cuda/jit_utils.cpp:1426.)
[rank0]:  tensor.erfinv_()
[rank0]:2024-12-05 13:24:54,885 - root - INFO - CUDA memory usage for model: 3.77GiB(3.97%)
[rank0]:2024-12-05 13:24:54,894 - root - INFO - Checkpointing active. Checkpoints will be loaded from and saved to ./outputs/checkpoint
[rank0]:2024-12-05 13:24:54,895 - root - INFO - Loading the checkpoint at step 200.

@msaroufim
msaroufim requested a review from tianyu-l December 5, 2024 21:25

@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.

Looks really good. Thank you!
Please address the remaining comments before merging.

Comment thread train_configs/llama3_8b.toml Outdated
Comment on lines +45 to +48
enable_checkpoint = true
folder = "checkpoint"
interval_type = "steps"
interval = 500
interval = 10

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.

Looks accidental changes, pls revert

Comment thread docs/datasets.md Outdated
- `text_processor`: Function to process individual samples
- The loader function should return a HuggingFace dataset object
- The processor function should return a string that combines the relevant fields from your dataset
- Use streaming=True for large datasets to manage memory efficiently

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.

nit:

Suggested change
- Use streaming=True for large datasets to manage memory efficiently
- Use `streaming=True` for large datasets to manage memory efficiently

Comment thread torchtitan/datasets/hf_datasets.py Resolved
Comment thread torchtitan/datasets/hf_datasets.py Resolved
Comment thread torchtitan/datasets/hf_datasets.py Resolved
Comment thread torchtitan/datasets/hf_datasets.py Resolved
@tianyu-l

tianyu-l commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

oh also, since you have a nice datasets.md, I think it's worth updating this line to let people know https://github.com/pytorch/torchtitan/blob/main/README.md?plain=1#L61

Mark Saroufim added 2 commits December 5, 2024 15:06
@msaroufim
msaroufim merged commit 7281e0b into main Dec 5, 2024
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
EDIT: removed the specific new functions in hf_datasets.py and kept most
of the doc changes and will not go for a registration based API

Fixes pytorch#311

This PR describes the status quo of how new datasets should be
registered today, in that there's the implicit assumption that people
are installing torchtitan from source and updating hf_datasets.py to
support new datasets. As an example I passed in the wikipedia dataset

The main "nice" thing about this PR is that `class HuggingFaceDataset`
is now agnostic to the c4 dataset which makes it easier for new people
to add datasets without reading the rest of the file

There's another direction this PR could have went in which was to allow
custom dataset registration, the benefit is people can support new
datasets without installing titan from source but registration apis can
feel kinda "bureaucratic" and presumably people would need to register
the dataset somewhere, probably `train.py`?

Not totally sure which is more in line with the repo's goals so opening
this PR to discuss

```python
def register_dataset(
    name: str,
    loader: Callable[[str, Dict[str, Any]], Any],
    processor: Callable[[Dict[str, Any]], str],
    path: Optional[str] = None,
) -> None:

    DATASET_LOADERS[name] = loader
    DATASET_TEXT_PROCESSORS[name] = processor

def wikipedia_loader(dataset_path: str, **kwargs):
    return load_dataset(
        dataset_path,
        name="20220301.en",
        split="train", 
        streaming=True,
        trust_remote_code=True,
    )

def wikipedia_processor(sample: Dict[str, Any]) -> str:
    return f"{sample['title']}\n\n{sample['text']}"

register_dataset(
    name="wikipedia",
    loader=wikipedia_loader,
    processor=wikipedia_processor,
    path="wikipedia"
)
```
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
EDIT: removed the specific new functions in hf_datasets.py and kept most
of the doc changes and will not go for a registration based API

Fixes pytorch#311

This PR describes the status quo of how new datasets should be
registered today, in that there's the implicit assumption that people
are installing torchtitan from source and updating hf_datasets.py to
support new datasets. As an example I passed in the wikipedia dataset

The main "nice" thing about this PR is that `class HuggingFaceDataset`
is now agnostic to the c4 dataset which makes it easier for new people
to add datasets without reading the rest of the file

There's another direction this PR could have went in which was to allow
custom dataset registration, the benefit is people can support new
datasets without installing titan from source but registration apis can
feel kinda "bureaucratic" and presumably people would need to register
the dataset somewhere, probably `train.py`?

Not totally sure which is more in line with the repo's goals so opening
this PR to discuss

```python
def register_dataset(
    name: str,
    loader: Callable[[str, Dict[str, Any]], Any],
    processor: Callable[[Dict[str, Any]], str],
    path: Optional[str] = None,
) -> None:

    DATASET_LOADERS[name] = loader
    DATASET_TEXT_PROCESSORS[name] = processor

def wikipedia_loader(dataset_path: str, **kwargs):
    return load_dataset(
        dataset_path,
        name="20220301.en",
        split="train", 
        streaming=True,
        trust_remote_code=True,
    )

def wikipedia_processor(sample: Dict[str, Any]) -> str:
    return f"{sample['title']}\n\n{sample['text']}"

register_dataset(
    name="wikipedia",
    loader=wikipedia_loader,
    processor=wikipedia_processor,
    path="wikipedia"
)
```
shuhuayu added a commit that referenced this pull request Nov 11, 2026
## Why
The Qwen3.8-Flash-Next QSA attention (#5782) will use `gather_attn`'s
grouped-KV / separate-V Triton kernels. They first appear in
attention-gym v0.0.19, after attention-gym #678, #701, #702, #713, #714,
#715 and #703. torchtitan pins `26beb363`, which predates all of these.
This PR only bumps the pin; the Flash-Next change stays in #5782.

## What changed (all mechanical)
| | Before | After |
| --- | --- | --- |
| attn-gym pin | `26beb363` (#704 merge) | `0d0197fb` (v0.0.19 tag, #703
merge) |

- `.ci/docker/requirements.txt` (root `requirements.txt` symlinks to
it): new SHA. The TODO now says to switch to the PyPI release once
v0.0.19 is published there (PyPI's newest is 0.0.18). #704, which the
old TODO waited on, is already included in both pins.
- `torchtitan/models/common/attn_gym.py` (install hint) and
`tests/unit_tests/cpu/models/common/test_optional_imports.py` (2
asserts): same SHA.

## Why v0.0.19 rather than attention-gym `main`
`26beb363..0d0197fb` changes only `attn_gym/sparse/gather_attn` (plus
its tests and docs). I diffed the two installed `attn_gym` trees to
confirm this. GDN, KDA, short_conv, the indexer, MLA and the CuTe
backend are byte-identical, so qwen3_5, qwen3_8, kimi and the
deepseek_v4 indexer can't change. `main` would also pull in the
warpdrive rewrite and the indexer/delta-H ports (~28 `_delta_rule`
files).

## Numerics
The only `gather_attn` consumer is
`torchtitan/models/deepseek_v4/attention.py`. Old vs new pin, 10 steps,
seed 42, deterministic, loss and grad_norm read from TensorBoard via
`scripts/loss_compare.py`, on GB300:

| Config | GPUs | Result |
| --- | --- | --- |
| `deepseek_v4_debugmodel_fsdp4_ep4` (DistMuon) | 4 | bitwise identical*
|
| `deepseek_v4_debugmodel_fsdp2_tp2_ep2_pp2` with dp_shard=1 (host has 4
GPUs) | 4 | bitwise identical* |

\* With `gather_attn`'s Triton tuners pinned to the same candidate in
both runs. Without pinning, deepseek_v4 isn't run-to-run reproducible on
**either** pin when the autotune cache is cold. `_FWD_TMA` and
`_BWD_DLOCAL_KV_TMA` choose `num_warps` 4 vs 8 by timing, and the two
give different bits. Ranks and cold-cache runs can pick different
winners, and torch's deterministic flag doesn't control this. This
already happens on main. I filed it upstream as
meta-pytorch/attention-gym#777, with a single-GPU repro. Because of it,
this PR adds no deepseek_v4 golden; see follow-ups.

## Testing
- `pre-commit run --all-files` passes, including `pyrefly-check`.
- `pytest tests/unit_tests/cpu/models/common` plus the deepseek_v4,
qwen3_5, qwen3_8 and kimi_k3 CPU tests: 416 passed. The same 9 tests
fail with both the old and new pin on this aarch64 host
(`test_lora_forward` and 8 qwen3_5 tests). The one I inspected,
`test_lora_forward`, fails with "torch.compile on current platform is
not supported for CPU".

## Known issues / follow-ups
- deepseek_v4 golden numerics: add once attention-gym#777 lets us fix
the tile config. The golden then has to be exported on CI's A10G
runners, not on GB300.
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.

add doc for adding custom dataset

3 participants