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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

capemox
Copy link

@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
…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 e51086e to 00366e0 Compare February 28, 2025 14:43
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.

1 participant