Avoid double caching in mappers that derive from CachedMapper#585
Merged
inducer merged 5 commits intoinducer:mainfrom Mar 10, 2025
Merged
Avoid double caching in mappers that derive from CachedMapper#585inducer merged 5 commits intoinducer:mainfrom
CachedMapper#585inducer merged 5 commits intoinducer:mainfrom
Conversation
Collaborator
Author
|
Ready for a look. |
…uper().rec usage in CachedMapAndCopyMapper Here is a sketch of what happens with super().rec vs Mapper.rec for the previous implementation of deduplicate_data_wrappers. Suppose we have 2 data wrappers a and b with the same data pointer. With super().rec: 1) map_fn maps a to itself, then mapper copies a to a'; mapper caches a -> a' (twice, once in super().rec and then again in rec), 2) map_fn maps b to a, then mapper maps (via cache in super().rec call) a to a'; mapper caches b -> a'. => Only a' in output DAG. With Mapper.rec: 1) map_fn maps a to itself, then mapper copies a to a'; caches a -> a', 2) map_fn maps b to a, then mapper copies a to a''; caches b -> a''. => Both a' and a'' in output DAG.
5e6d355 to
4ff4017
Compare
This was referenced Feb 27, 2025
Merged
inducer
reviewed
Mar 10, 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.
If a mapper derives from
CachedMapperand overridesrecin a way that implements caching, it needs to callMapper.recinstead ofsuper().recin its implementation in order to avoid executing the cache lookup/insertion logic twice. This PR intends to fix that.The naive version of this fix had the unfortunate side effect of breaking
deduplicate_data_wrappers, because it turns out thatdeduplicate_data_wrapperswas taking advantage of this behavior inCachedMapAndCopyMapperin a subtle way. Here's a sketch of what was happening in the previous implementation:Suppose we have 2 data wrappers
aandbwith the same data pointer.With
super().rec:map_fnmapsato itself, then the mapper copiesatoa'; it caches the mappinga->a'(twice, once insuper().recand then again inrec),map_fnmapsbtoa, then the mapper maps (via cache insuper().reccall)atoa'; it caches the mappingb->a'.=> Only
a'in output DAG.With
Mapper.rec:map_fnmapsato itself, then the mapper copiesatoa'; it caches the mappinga->a',map_fnmapsbtoa, then the mapper copiesatoa''; it caches the mappingb->a''.=> Both
a'anda''in output DAG.@inducer I remembered this morning that I had previously looked into this last fall (and luckily I wrote down all the details in our meeting notes). Back then I decided to set it aside and wait for #515 (after that change
map_data_wrapperis no longer creating unnecessary copies, so it avoids the issue). But this time I thought I'd take a quick stab at refactoringdeduplicate_data_wrappersanyway. Sticking the previouscached_data_wrapper_if_presentimplementation intomap_data_wrappershould prevent the issue, because it gets rid of theCopyMapperimplementation that was creating unnecessary new data wrappers.With the changes in this PR I'm seeing a small improvement in
transform_dagtimes on prediction (7% or so).