Skip to content

Better messaging and fix for incorrect shape when collating data.#18119

Merged
sgugger merged 14 commits into
huggingface:mainfrom
CakeCrusher:lead_nesting_solution
Jul 21, 2022
Merged

Better messaging and fix for incorrect shape when collating data.#18119
sgugger merged 14 commits into
huggingface:mainfrom
CakeCrusher:lead_nesting_solution

Conversation

@CakeCrusher

Copy link
Copy Markdown
Contributor

What does this PR do?

I ran into an error related to an incorrect shape of inputs when using DataCollatorForSeq2Seq. I learned it had to do with having excessively nested inputs for my features. The error message was not particularly useful.

This PR adds an assertion checking for incorrectly shaped inputs to be collated. The assertion also provides a solution by suggesting to use remove_excess_nesting util.
remove_excess_nesting removes excessive nesting from features within a DatasetDict.

Fixes #15505

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?

@stas00

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Jul 13, 2022

Copy link
Copy Markdown

The documentation is not available anymore as the PR was closed or merged.

@LysandreJik
LysandreJik requested a review from sgugger July 19, 2022 13:18

@sgugger sgugger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for your PR. I left some comments, as some of the asserts introduced will break training at batch size 1 which people use for large models.

Comment thread src/transformers/data/data_collator.py Outdated
Comment on lines +577 to +583
np_input_ids = np.array(feature["input_ids"])
assert (
np_input_ids.shape[0] != 1
), f"""
Feature shape cannot be collated: expected shape=( > 1, ... ), found shape={str(np_input_ids.shape)}.
You may want to remove excessively nested layers from your features.
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will prevent any user from running training at batch size 1, so this should not be added.

self[key] = tensor
except: # noqa E722
if key == "overflowing_tokens":
if key == "overflowing_tokens" or key == "input_ids" or key == "attention_mask":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please do not touch this part of the test or the error raised. You can dd the information on the key in the error raised at line 736.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am trying to formulate an explanation to add at line 736 but I'm led back to saying what is already written at line 731. The reason that excessive nesting is an issue at collating is that even with padding the features are tokenized at different lengths. This is due to the additional wrapping of the actual feature list like so: [[1,2]] and [[1,2,3]]. They both share the shape (1, but after that they are different shapes so what np.array does is: [[ list ]] and [[ list ]] thus returning shape(1, 1).

That is why I added the code at 730 because it technically is the perfect explanation at this point in the process.

@sgugger sgugger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for iterating!

@sgugger
sgugger merged commit 5e2f2d7 into huggingface:main Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants