ejkernel.ops.config.cache#

Configuration caching system for ejkernel operations.

This module provides a thread-safe caching mechanism for storing and retrieving optimal kernel configurations. The cache system supports:

  • Device-specific configuration storage

  • Operation-specific caching with call signature hashing

  • Temporary cache overlays for testing and debugging

  • Context-aware configuration management

Key Components:

ConfigCache: Main cache storage with thread-safe operations overlay_cache: Context manager for temporary cache overrides _cache_overlay: Context variable for managing overlay stack

The caching system is designed to minimize recomputation of optimal configurations by storing results based on device characteristics, operation types, and input signatures. This significantly improves performance for repeated operations.

class ejkernel.ops.config.cache.ConfigCache[source]#

Bases: Generic[Cfg]

Thread-safe cache for storing kernel configurations.

The ConfigCache stores optimal configurations for kernel operations based on device characteristics, operation identifiers, and call signatures. This enables efficient reuse of previously determined optimal configurations.

The cache key consists of: - device_fingerprint: Unique identifier for the target device - op_id: Operation identifier (e.g., ‘matmul’, ‘conv’) - call_key: Hash of the call signature (args, kwargs)

Type Parameters:

Cfg: Configuration type (e.g., dict, dataclass, etc.)

_data#

Internal storage mapping cache keys to configurations

clear() None[source]#

Clear all cached configurations.

get(dev: str, op_id: str, call_key: str) Optional[Cfg][source]#

Retrieve a cached configuration.

Parameters
  • dev – Device fingerprint identifying the target device

  • op_id – Operation identifier

  • call_key – Call signature hash

Returns

Cached configuration if found, None otherwise

keys() list[tuple[str, str, str]][source]#

Get all cache keys.

Returns

List of (device_fingerprint, op_id, call_key) tuples

put(dev: str, op_id: str, call_key: str, cfg: Cfg) None[source]#

Store a configuration in the cache.

Parameters
  • dev – Device fingerprint identifying the target device

  • op_id – Operation identifier

  • call_key – Call signature hash

  • cfg – Configuration to cache

size() int[source]#

Get the number of cached configurations.

Returns

Number of entries in the cache

class ejkernel.ops.config.cache.overlay_cache(mapping: dict[tuple[str, str, str], Any])[source]#

Bases: object

Context manager for temporarily overriding cache entries.

This context manager allows temporary cache overlays that shadow the main cache during execution. Useful for testing, debugging, or providing custom configurations for specific contexts without permanently modifying the global cache.

The overlay uses a context variable stack to support nested overlays.

Parameters

mapping – Dictionary mapping cache keys to override configurations

Example

>>> cache = ConfigCache()
>>> cache.put('dev1', 'op1', 'key1', 'original_config')
>>> override = {('dev1', 'op1', 'key1'): 'override_config'}
>>> with overlay_cache(override):
...
...     config = cache.get('dev1', 'op1', 'key1')
>>>