Skip to content

🏊 [SFT] Compatibility with padding free and iterable dataset #3053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 12, 2025

Conversation

qgallouedec
Copy link
Member

What does this PR do?

Fixes #3030

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@@ -386,8 +386,9 @@ def _func(example):
dataset = dataset.map(_func, batched=batched, **map_kwargs)

# If the dataset is prompt-completion, convert it to language modeling type
if "prompt" in dataset.column_names and "completion" in dataset.column_names:
key = "messages" if is_conversational(dataset[0]) else "text"
first_example = next(iter(dataset))
Copy link
Member Author

Choose a reason for hiding this comment

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

Iterable dataset doesn't have column_names. So we get the keys of the first example

Copy link
Member

Choose a reason for hiding this comment

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

so here instead of the conditioned loop, it just explicitly fetches the first example of the dataset? Nice!

Copy link
Member Author

Choose a reason for hiding this comment

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

You still have the condition, but when the dataset is iterable, dataset.column_name is None so you need find another way to get it

@qgallouedec
Copy link
Member Author

@loricxy0707 can you confirm that this fixes your issue?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@qgallouedec qgallouedec changed the title 🏊 Compatibility with padding free and iterable dataset 🏊 [SFT] Compatibility with padding free and iterable dataset Mar 11, 2025
result = torch.all(encoded_instance["labels"] == -100)
self.assertTrue(result, "Not all values in the tensor are -100.")

def test_padding_free(self):
tokenizer = AutoTokenizer.from_pretrained("trl-internal-testing/tiny-Qwen2ForCausalLM-2.5")
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
Copy link
Member Author

Choose a reason for hiding this comment

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

tokenizer.pad_token_id = tokenizer.eos_token_id does that

inst1 = "### System: You are a helpful assistant.\n\n### User: How much is 2+2?\n\n### Assistant: 2+2 equals 4"
inst2 = "### System: You are a honest and helpful assistant.\n\n### User: What is the answer of 22x22?\n\n### Assistant: 22x22 equals 484"

response_template = "\n\n### Assistant:"
Copy link
Member Author

Choose a reason for hiding this comment

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

the tokenizer has a token for ?\n\n, so the template won't be found. This change fixes it.

@@ -147,7 +147,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install accelerate==0.34.0
python -m pip install datasets==2.21.0
Copy link
Member Author

Choose a reason for hiding this comment

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

This test would fail with 2.21. So we bump to the next version: 3.0.0:

FAILED tests/test_sft_trainer.py::SFTTrainerTester2::test_train_with_iterable_dataset - ValueError: Batch does not contain any data (`None`). At the end of all iterable data available before expected stop iteration.

Copy link
Member

Choose a reason for hiding this comment

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

Does it work with 3.0.0?

Copy link
Member Author

@qgallouedec qgallouedec Mar 12, 2025

Choose a reason for hiding this comment

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

Locally yes, let's wait and see if the CI passes now

@@ -147,7 +147,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install accelerate==0.34.0
python -m pip install datasets==2.21.0
Copy link
Member Author

@qgallouedec qgallouedec Mar 12, 2025

Choose a reason for hiding this comment

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

This test would fail with 2.21. So we bump to the next version: 3.0.0:

FAILED tests/test_sft_trainer.py::SFTTrainerTester2::test_train_with_iterable_dataset - ValueError: Batch does not contain any data (`None`). At the end of all iterable data available before expected stop iteration.

@@ -147,7 +147,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install accelerate==0.34.0
python -m pip install datasets==2.21.0
Copy link
Member

Choose a reason for hiding this comment

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

Does it work with 3.0.0?

@@ -386,8 +386,9 @@ def _func(example):
dataset = dataset.map(_func, batched=batched, **map_kwargs)

# If the dataset is prompt-completion, convert it to language modeling type
if "prompt" in dataset.column_names and "completion" in dataset.column_names:
key = "messages" if is_conversational(dataset[0]) else "text"
first_example = next(iter(dataset))
Copy link
Member

Choose a reason for hiding this comment

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

so here instead of the conditioned loop, it just explicitly fetches the first example of the dataset? Nice!

@qgallouedec qgallouedec merged commit 4871c82 into main Mar 12, 2025
14 checks passed
@qgallouedec qgallouedec deleted the fix-sft-iterable-and-padding-free branch March 12, 2025 18:44
yxliu-TAMU pushed a commit to mincheolseong/ECEN743-GRPO-Project-Proposal that referenced this pull request Apr 20, 2025
…face#3053)

* Compatibilitywith padding free and iterable dataset

* Fix collator test

* add a test for streaming

* some cleaning

* improve and fix tests

* tiny revert

* bump datasets to 3.0.0
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.

IterableDataset is not compatible with SFTTrainer
3 participants