Repository navigation
perf(core): avoid O(N^2) list shifts and duplicate tokenization in LongContextReorder and ChatSummaryMemoryBuffer - #23660
Open
deep1blue wants to merge 1 commit into
Conversation
…ngContextReorder and ChatSummaryMemoryBuffer Signed-off-by: Parth Vadera <parthvadera@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates$O(N^2)$ front-list insertions/deletions (
list.insert(0, ...)andlist.pop(0)) and redundant per-message tokenization inLongContextReorderandChatSummaryMemoryBuffer:LongContextReorder._postprocess_nodes(llama-index-core/llama_index/core/postprocessor/node.py):ordered_nodesand calledreordered_nodes.insert(0, node)on every even index (i % 2 == 0), shifting all preceding elements ($O(N^2)$).ordered_nodes[::2][::-1] + ordered_nodes[1::2], producing the exact same lost-in-the-middle node ordering inChatSummaryMemoryBuffer._split_messages_summary_or_full_text&_handle_assistant_and_tool_messages(llama-index-core/llama_index/core/memory/chat_summary_memory_buffer.py):_split_messages_summary_or_full_textpreviously calledself._token_count_for_messages([chat_history[-1]])twice per retained message (once in thewhilecondition and once when incrementingself._token_count), invokingtokenizer_fntwice on every message that fits withintoken_limit, and calledchat_history_full_text.insert(0, chat_history.pop())($O(M^2)$).msg_tokens = self._token_count_for_messages([chat_history[-1]])once per iteration, append tochat_history_full_text, and reverse once at the end ($O(M)$)._handle_assistant_and_tool_messages, replace repeatedchat_history_full_text.pop(0)calls with a single slice transfer (chat_history_to_be_summarized.extend(chat_history_full_text[:idx]); del chat_history_full_text[:idx]).Testing
test_long_context_reorderinllama-index-core/tests/postprocessor/test_base.py.test_split_messages_single_tokenization_per_messageinllama-index-core/tests/memory/test_chat_summary_memory_buffer.py.test_chat_summary_memory_buffer.pyandtest_base.pytests pass.Acknowledgments
Credit to @logan-markewich, @shadyelgewily-slimstock (#13155), @Junxiao-Zhao (#14563), @pablonm3 (#17845), and @xpbowler (#16127) for the design and maintenance of
LongContextReorderandChatSummaryMemoryBuffer.