perf(tree_attn): Add lazy attention mask creation for tree training#872
Conversation
Summary of ChangesHello @nuzant, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a performance improvement by optimizing memory usage during tree training, particularly concerning attention masks. By shifting the creation of block masks from an eager, batch-level process to a lazy, per-forward-pass approach, the system avoids holding all block masks in memory simultaneously. This change is crucial for scaling to larger models and datasets by mitigating peak memory consumption without altering the core functionality of tree attention. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization for tree attention training by deferring the creation of block masks to the forward pass. This change reduces peak memory usage, which is particularly beneficial for large batch sizes or long sequences.
The implementation is solid:
- The
build_packed_tree_batchfunction is updated to no longer create theblock_mask, instead storing thetrie_node. - A new function
build_block_mask_from_trieis added to lazily create the block mask. - Both FSDP and Megatron engines are updated to call this new function just before the forward pass and release the mask's memory immediately after.
- The necessary data plumbing (
padded_to_lengthinMicroBatchItem) is also correctly implemented.
I have one suggestion to improve code consistency between the FSDP and Megatron engines. Overall, this is a great improvement.
…real-project#872) * lazy attn mask for tree training * Raise error for padded_size.
…real-project#872) * lazy attn mask for tree training * Raise error for padded_size.
Description
Changes:
build_block_mask_from_trie()function to lazily build block masks from trie nodes just beforethe forward pass
Previously, block masks were created during
build_packed_tree_batch()and stored in memory until theforward pass. This caused unnecessary peak memory usage as all block masks for all micro-batches were
held simultaneously.
With this change:
build_packed_tree_batch()stores thetrie_nodeinstead ofblock_maskin micro-batch databuild_block_mask_from_trie()just before each forward passThis reduces peak memory footprint for tree training, especially beneficial for large batch sizes or
long sequences.
Related Issue
Fixes #(issue)
Type of Change
work as expected)
Checklist
jb build docs/gemini review)Breaking Change Details (if applicable):
Additional Context
Need help? Check the Contributing Guide or ask in
GitHub Discussions!