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

[cache] add a test to confirm we can use cache at train time #35709

Merged
merged 4 commits into from
Jan 16, 2025

Conversation

gante
Copy link
Member

@gante gante commented Jan 15, 2025

What does this PR do?

In #35648, the idea of always disabling the cache at train-time was floated. However, @BenjaminBossan pointed to me that some fine-tuning scripts need cache at train time (e.g. prefix tuning in PEFT).

I couldn't find any test that ensured the survival of this property, so this PR adds a test that will fail if we decide to disable it :)

@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.

@BenjaminBossan
Copy link
Member

Thanks a lot for adding this test to prevent possible regressions with PEFT methods like prefix tuning. I wonder if we can also try injecting the cache and checking if that works, basically like a mini prefix tuning:

        # simulate injecting virtual tokens like in prefix tuning
        num_virtual_tokens = 3
        past_key_values = [torch.randn(2, 1, 2, num_virtual_tokens, 8)] * 2
        past_key_values = DynamicCache.from_legacy_cache(past_key_values)
        model_inputs["attention_mask"] = torch.cat((
            model_inputs["attention_mask"],
            torch.ones(1, num_virtual_tokens).to(model_inputs["attention_mask"].device)
        ), dim=1)
        model_outputs = model(**model_inputs, past_key_values=past_key_values, use_cache=True)

Maybe this is overkill, LMK what you think.

@gante
Copy link
Member Author

gante commented Jan 15, 2025

@BenjaminBossan added, thank you for the suggestion 👍 (the extra check is nearly free, and may save some pain down the road)

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks so much for taking of ensuring that this works, LGTM.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Okay! I think we did break that once or twice, good addition! 🤗

tests/utils/test_modeling_utils.py Outdated Show resolved Hide resolved
@gante gante merged commit aeeceb9 into huggingface:main Jan 16, 2025
11 checks passed
@gante gante deleted the train_cache_test branch January 16, 2025 17:02
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.

4 participants