Calculate pid_docid_map.values() only once in add_to_index#267
Merged
bclavie merged 3 commits intoAnswerDotAI:mainfrom Feb 11, 2025
Merged
Calculate pid_docid_map.values() only once in add_to_index#267bclavie merged 3 commits intoAnswerDotAI:mainfrom
pid_docid_map.values() only once in add_to_index#267bclavie merged 3 commits intoAnswerDotAI:mainfrom
Conversation
Currently `if new_pid_docid_map[pid] not in self.pid_docid_map.values()` calculates `.values()` each iteration of the list comprehension so I'm moving that piece out of the list comprehension and putting it in a `set`. Hoping this speeds up the `add_to_index` operation.
Removing the previous list comprehension that I commented out while testing the updated code.
Contributor
|
Very helpful, thnak you! I have no word to answer the shame I feel reading the values() call in a loop 😆 |
Contributor
|
(ignore the CI btw -- it's going away in a hot minute. It's not related to your PR but to getting rid of one of the worst ideas I've ever had, using |
bclavie
approved these changes
Feb 11, 2025
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.
I was building an index of 200k documents (from the UKPLab/DAPR's Genomics dataset) and noted that when using
add_to_index(to build the index in batches) it was hanging on a list comprehension in line 162 of colbert.py:Profiling this showed that the entire index building process took 1240 seconds and this list comprehension took 352 seconds.
The issue was that each iteration of the list comprehension was calculating
self.pid_docid_map.values(). Pulling this out of the list comprehension to calculate it once (and only referencing it in the list comprehension) brought its execution time from 352 seconds down to <0.6 seconds.This PR implements that code.
This is my first PR in RAGatouille so please let me know if additional information is needed. Thank you for maintaining such an awesome library!
For reference, here's the code I used to profile this issue in a Google Colab Pro notebook (T4 w/High-RAM instance):