Skip to content
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

Fixed datatype related issues in DataCollatorForLanguageModeling #36457

Conversation

capemox
Copy link
Contributor

@capemox capemox commented Feb 27, 2025

What does this PR do?

Fixes two issues regarding the test tests/trainer/test_data_collator.py::TFDataCollatorIntegrationTest::test_all_mask_replacement:

  1. I got the error RuntimeError: "bernoulli_tensor_cpu_p_" not implemented for 'Long'. This is because the mask_replacement_prob=1 and torch.bernoulli doesn't accept this type (which would be a torch.long dtype instead). I fixed this by manually casting the probability arguments in the __post_init__ function of DataCollatorForLanguageModeling.
  2. I also got the error
    tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute Equal as input #1(zero-based) was expected to be a int64 tensor but is a int32 tensor [Op:Equal]
    due to the line
    tf.reduce_all((batch["input_ids"] == inputs) | (batch["input_ids"] == tokenizer.mask_token_id))
    in test_data_collator.py. This occurs because the type of the inputs variable is tf.int32. Solved this by manually casting it to tf.int64 in the test, as the expected return type of batch["input_ids"] is tf.int64.

These changes were done on Python 3.12.8. The dependencies installed were as pip install -e ".[dev]" along with:

  • torch==2.6.0
  • tensorflow==2.18.0
  • tf-keras==2.18.0

Motivation: I wanted to make some contributions to DataCollatorForLanguageModeling, unfortunately though the tests were failing on the existing code itself. I thought I'll fix these bugs before moving forward with that.

Fixes # (issue)

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 or the forum? 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, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@Rocketknight1 should be able to review this!

@github-actions github-actions bot marked this pull request as draft February 27, 2025 16:41
Copy link

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. When it is ready for review, please click the Ready for review button (at the bottom of the PR page).

@capemox capemox changed the title Fixed datatype related issues in tests/trainer/test_data_collator.py::TFDataCollatorIntegrationTest::test_all_mask_replacement and DataCollatorForLanguageModeling Fixed datatype related issues in DataCollatorForLanguageModeling Feb 28, 2025
@capemox capemox marked this pull request as ready for review February 28, 2025 10:02
@Rocketknight1 Rocketknight1 force-pushed the fix-data-collator-for-language-modeling-long-error branch from e51086e to 00366e0 Compare February 28, 2025 14:43
@capemox
Copy link
Contributor Author

capemox commented Mar 6, 2025

Hey @Rocketknight1! I saw the force push on the branch, but not really sure what to make of it. Hope the PR works as expected, and that there aren't any issues

@Rocketknight1
Copy link
Member

Hi @capemox, the force push is caused by me rebasing with the "Update branch" tool in the Github interface, don't worry about it! I often do this with PRs before merging to fix any CI issues. You can git pull or git reset locally to catch up to it.

…CollatorIntegrationTest::test_all_mask_replacement`:

1. I got the error `RuntimeError: "bernoulli_tensor_cpu_p_" not implemented for 'Long'`. This is because the `mask_replacement_prob=1` and `torch.bernoulli` doesn't accept this type (which would be a `torch.long` dtype instead. I fixed this by manually casting the probability arguments in the `__post_init__` function of `DataCollatorForLanguageModeling`.
2. I also got the error `tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute Equal as input huggingface#1(zero-based) was expected to be a int64 tensor but is a int32 tensor [Op:Equal]` due to the line `tf.reduce_all((batch["input_ids"] == inputs) | (batch["input_ids"] == tokenizer.mask_token_id))` in `test_data_collator.py`. This occurs because the type of the `inputs` variable is `tf.int32`. Solved this by manually casting it to `tf.int64` in the test, as the expected return type of `batch["input_ids"]` is `tf.int64`.
@Rocketknight1 Rocketknight1 force-pushed the fix-data-collator-for-language-modeling-long-error branch from 00366e0 to 6d15651 Compare March 7, 2025 13:26
Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Sorry again for the confusion with the rebase, but now that the CI is passing this looks good, thank you!

@Rocketknight1 Rocketknight1 merged commit a1cf9f3 into huggingface:main Mar 7, 2025
21 checks passed
@capemox
Copy link
Contributor Author

capemox commented Mar 7, 2025

Thanks a lot for the clarification @Rocketknight1! Feels great being able to contribute!

Not wanting to be too selfish, but could you also take a look at this PR:
#36497

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.

2 participants