Skip to content

Conversation

@gante
Copy link
Contributor

@gante gante commented Apr 23, 2025

What does this PR do?

Fixes the flakiness pattern behind a common flaky test, tests/models/kosmos2/test_modeling_kosmos2.py::Kosmos2ModelTest::test_generate_continue_from_past_key_values

flakiness pattern TL;DR:

  • on mixin tests, the models have random weights
  • different input shapes can cause slightly different outputs (see here)
  • in generate, tiny differences in the logits can lead to a different token, changing generation from that point onward
  • equivalence checks for generate shouldn't naively look at the output sequences, but rather at the output sequences up to the point where they diverge, if they diverge 👉 corrects all equivalence checks using the right pattern (this pattern was already present in most tests, but a few ones still had the old pattern)

@github-actions github-actions bot marked this pull request as draft April 23, 2025 17:47
@github-actions
Copy link
Contributor

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

@gante gante changed the title Flaky test generate continue from past key values [tests] fix flaky pattern in test_generate_continue_from_past_key_values Apr 23, 2025
@parameterized.expand([("offloaded",)]) # ("offloaded_static",) TODO: @raushan fixme in some models (eg T5)
@require_torch_accelerator
@pytest.mark.generate
def test_offloaded_cache_implementation(self, cache_implementation):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this test doesn't make sense? tests that two generate calls with the same flags have the same output

We have tests for the offloaded cache in tests/utils/test_cache_utils.py

Copy link
Member

Choose a reason for hiding this comment

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

Right, this test was supposed to only check that calling genarate with all possible cache_implementation doesn't fail. There was some issue in the past, we lost some "cache_implementation" keys when refactoring 🥲

If the tests in cache_utils cover all caches and call generate with kwargs, I guess we're fine

Copy link
Contributor Author

@gante gante Apr 29, 2025

Choose a reason for hiding this comment

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

There are slow integration tests for all caches on main, this PR adds a fast test for all caches 🤗

@gante gante marked this pull request as ready for review April 23, 2025 17:49
@gante gante requested a review from Rocketknight1 April 23, 2025 17:50
"max_new_tokens": self.max_new_tokens,
"use_cache": True,
"return_dict_in_generate": True,
"output_scores": True,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(the correct equivalence check needs the scores)

"use_cache": True,
"do_sample": False,
"return_dict_in_generate": True,
"output_scores": True,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(same, the correct equivalence check needs the scores)

@gante gante changed the title [tests] fix flaky pattern in test_generate_continue_from_past_key_values [tests] fix flaky pattern in test_generate_continue_from_past_key_values Apr 23, 2025
@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.

@gante gante requested a review from zucchini-nlp April 28, 2025 09:29
Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines +1935 to +1936
# TODO (joao, raushan): the correct line below is `if not hasattr(config.get_text_config(), "use_cache")`,
# but it breaks a few models. Fix and then apply `_check_similar_generate_outputs` pattern
Copy link
Member

Choose a reason for hiding this comment

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

prob it is same thing when we generate from inputs_embeds in VLMs and have to pop image inputs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup 👍 I've pushed the actual fix into the future, in case the test is not (much) flaky

@parameterized.expand([("offloaded",)]) # ("offloaded_static",) TODO: @raushan fixme in some models (eg T5)
@require_torch_accelerator
@pytest.mark.generate
def test_offloaded_cache_implementation(self, cache_implementation):
Copy link
Member

Choose a reason for hiding this comment

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

Right, this test was supposed to only check that calling genarate with all possible cache_implementation doesn't fail. There was some issue in the past, we lost some "cache_implementation" keys when refactoring 🥲

If the tests in cache_utils cover all caches and call generate with kwargs, I guess we're fine

@gante gante merged commit 3a1acc3 into huggingface:main Apr 29, 2025
20 checks passed
@gante gante deleted the flaky_test_generate_continue_from_past_key_values branch April 29, 2025 11:20
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request May 14, 2025
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.

3 participants