ejkernel.ops.config.persistent#
Persistent disk-based configuration cache for kernel optimization.
This module provides persistent storage for optimal kernel configurations, allowing optimization results to be preserved across program runs. The cache uses JSON files for storage with automatic serialization/deserialization.
- Key Features:
Atomic file operations to prevent corruption
Automatic serialization for dataclasses and Pydantic models
Custom loader/dumper support for complex types
Device and operation-specific configuration storage
Thread-safe atomic updates using temporary files
- The persistent cache complements the in-memory cache by providing:
Long-term storage of optimization results
Sharing of configurations across program runs
Reduced autotuning overhead for repeated operations
Backup storage for critical optimization data
- Example Usage:
>>> >>> cache = PersistentCache('/path/to/cache.json') >>> cache.put('gpu:0', 'matmul_v1', 'key123', my_config) >>> config = cache.get('gpu:0', 'matmul_v1', 'key123') >>> >>> >>> def custom_dumper(cfg): return cfg.to_dict() >>> def custom_loader(data): return MyConfig.from_dict(data) >>> cache = PersistentCache('/path/to/cache.json', custom_loader, custom_dumper)
- class ejkernel.ops.config.persistent.PersistentCache(opname: str, path: str | None = None, loader: collections.abc.Callable[[Any], ejkernel.ops.config.persistent.Cfg] | None = None, dumper: collections.abc.Callable[[ejkernel.ops.config.persistent.Cfg], Any] | None = None, cfg_type: type[Cfg] | None = None)[source]#
Bases:
Generic[Cfg]Persistent disk-based cache for kernel configurations.
Provides thread-safe storage and retrieval of optimization configurations using JSON files. Supports automatic serialization for common types and custom serialization via loader/dumper functions.
The cache uses atomic file operations to ensure data consistency and prevent corruption during concurrent access.
- Type Parameters:
Cfg: Configuration type to be cached
- path#
File path for the JSON cache
- loader#
Optional function to deserialize configurations
- dumper#
Optional function to serialize configurations
- get(device: str, op_id: str, call_key: str) Optional[Cfg][source]#
Retrieve cached configuration for the given coordinates.
- Parameters
device – Device fingerprint for the configuration
op_id – Operation identifier
call_key – Function call signature hash
- Returns
Cached configuration if found, None otherwise
Note
If a custom loader was provided, it will be used to deserialize the stored data. Otherwise, the raw JSON data is returned.
- put(device: str, op_id: str, call_key: str, cfg: Cfg)[source]#
Store configuration in the cache with atomic file update.
- Parameters
device – Device fingerprint for the configuration
op_id – Operation identifier
call_key – Function call signature hash
cfg – Configuration to store
Note
Uses atomic file operations (write to temporary file, then replace) to ensure data consistency. If a custom dumper was provided, it will be used for serialization. Otherwise, automatic serialization is attempted for dataclasses and Pydantic models.
- Serialization Priority:
Custom dumper function (if provided)
Dataclass.asdict() for dataclass objects
model_dump() for Pydantic v2 models
Raw value (must be JSON-serializable)