Skip to content

Add CacheInputs class to simplify passing of expr + optional args/kwargs/key#583

Merged
inducer merged 5 commits intoinducer:mainfrom
majosm:add-cache-inputs
Mar 12, 2025
Merged

Add CacheInputs class to simplify passing of expr + optional args/kwargs/key#583
inducer merged 5 commits intoinducer:mainfrom
majosm:add-cache-inputs

Conversation

@majosm
Copy link
Collaborator

@majosm majosm commented Feb 17, 2025

Attempts to simplify the handling for mappers with/without extra arguments, and also for keys being computed up front and passed in.

Note: This PR also disables the default implementations of get_cache_key/get_function_definition_cache_key for the extra args case; the previous implementations seem ambiguous, since a given argument can be passed with/without keyword and end up in different parts of the key tuple. I need to check if this works OK with mirgecom.

Depends on #585.



class CachedMapperCache(Generic[CacheExprT, CacheResultT]):
class CacheInputs(Generic[CacheExprT, P]):
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
class CacheInputs(Generic[CacheExprT, P]):
class CacheInputWithKey(Generic[CacheExprT, P]):
Suggested change
class CacheInputs(Generic[CacheExprT, P]):
class CacheKeyable(Generic[CacheExprT, P]):

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to CacheInputsWithKey.

Copy link
Owner

@inducer inducer left a comment

Choose a reason for hiding this comment

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

Forgot to submit these yesterday. This should be good to go once these are addressed.



class CachedMapperCache(Generic[CacheExprT, CacheResultT]):
class CacheInputs(Generic[CacheExprT, P]):
Copy link
Owner

Choose a reason for hiding this comment

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

I like how clean this is, but it's also throwing around a bunch more objects than before. Do you have a sense of the timing impact?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The previous version had caused a bit of a slowdown, so I refactored it a bit. Now it performs roughly the same as the current main.

@majosm majosm force-pushed the add-cache-inputs branch 3 times, most recently from 0ce18ff to 13d5ab0 Compare February 27, 2025 04:05
This was referenced Feb 27, 2025
@majosm majosm marked this pull request as ready for review February 28, 2025 13:15
@majosm
Copy link
Collaborator Author

majosm commented Feb 28, 2025

Ready for a look after #585.

@majosm majosm requested a review from inducer February 28, 2025 13:16
return self._cache.retrieve(inputs)
except KeyError:
s = super().rec(expr)
s = Mapper.rec(self, expr)
Copy link
Owner

Choose a reason for hiding this comment

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

Add comment as in #585.

return (expr, *args, tuple(sorted(kwargs.items())))
) -> CacheKeyT:
if args or kwargs:
raise NotImplementedError(
Copy link
Owner

Choose a reason for hiding this comment

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

Explain that cache could be different based on by-kw or by-position passing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

return (expr, *args, tuple(sorted(kwargs.items())))
) -> CacheKeyT:
if args or kwargs:
raise NotImplementedError(
Copy link
Owner

Choose a reason for hiding this comment

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

Explain that cache could be different based on by-kw or by-position passing.

@majosm majosm force-pushed the add-cache-inputs branch from 13d5ab0 to d5b5305 Compare March 10, 2025 21:57
majosm added 5 commits March 10, 2025 17:01
…nition_cache_key for extra args case

ambiguous due to the fact that any arg can be specified with/without keyword
…n_definition_cache_key are not defined for general extra args/kwargs
@majosm majosm force-pushed the add-cache-inputs branch from d5b5305 to 81d300a Compare March 10, 2025 22:02
@majosm majosm requested a review from inducer March 10, 2025 22:27
@inducer inducer merged commit 1ddaaad into inducer:main Mar 12, 2025
11 checks passed
@inducer
Copy link
Owner

inducer commented Mar 12, 2025

Thx!

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.

2 participants