|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import abc |
| 4 | +import dataclasses |
| 5 | +import functools |
| 6 | +import hashlib |
| 7 | +import logging |
| 8 | +import os |
| 9 | +from typing import TYPE_CHECKING |
| 10 | +from typing import Hashable |
| 11 | +from typing import Sequence |
| 12 | + |
| 13 | +from .._utils import counters |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from ..runtime.config import Config |
| 17 | + from ..runtime.kernel import BoundKernel |
| 18 | + from .base_search import BaseSearch |
| 19 | + |
| 20 | +log: logging.Logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +@functools.cache |
| 24 | +def helion_key() -> str: |
| 25 | + from torch._inductor.codecache import build_code_hash |
| 26 | + |
| 27 | + here = os.path.abspath(__file__) |
| 28 | + helion_path = os.path.dirname(os.path.dirname(here)) |
| 29 | + |
| 30 | + combined_hash = hashlib.sha256() |
| 31 | + build_code_hash([helion_path], "", combined_hash) |
| 32 | + return combined_hash.hexdigest() |
| 33 | + |
| 34 | + |
| 35 | +@functools.cache |
| 36 | +def torch_key_wrapper() -> str: |
| 37 | + from torch._inductor.codecache import torch_key |
| 38 | + |
| 39 | + return torch_key().hex() |
| 40 | + |
| 41 | + |
| 42 | +@functools.cache |
| 43 | +def triton_key_wrapper() -> str: |
| 44 | + from torch._inductor.runtime.triton_compat import triton_key |
| 45 | + |
| 46 | + return triton_key() |
| 47 | + |
| 48 | + |
| 49 | +class CacheKeyBase: |
| 50 | + """ |
| 51 | + Base class to provide utility functions to all cache key dataclasses |
| 52 | + """ |
| 53 | + |
| 54 | + def stable_hash(self) -> str: |
| 55 | + return hashlib.sha256(repr(self).encode("utf-8")).hexdigest() |
| 56 | + |
| 57 | + |
| 58 | +@dataclasses.dataclass(frozen=True) |
| 59 | +class BoundKernelInMemoryCacheKey(CacheKeyBase): |
| 60 | + """ |
| 61 | + Default in memory cache key. |
| 62 | +
|
| 63 | + This key includes: |
| 64 | +
|
| 65 | + specialization_key: Information about all kernel inputs. |
| 66 | + For tensors this means their device, shape, size etc. |
| 67 | + extra_results: Information regarding `hl.specialize` decisions |
| 68 | + """ |
| 69 | + |
| 70 | + specialization_key: tuple[Hashable, ...] |
| 71 | + extra_results: tuple[Hashable, ...] |
| 72 | + |
| 73 | + |
| 74 | +@dataclasses.dataclass(frozen=True) |
| 75 | +class LooseAutotuneCacheKey(BoundKernelInMemoryCacheKey): |
| 76 | + """ |
| 77 | + Autotune Cache key to use for most use cases. |
| 78 | +
|
| 79 | + This key includes (in addition to BoundKernelInMemoryCacheKey): |
| 80 | +
|
| 81 | + kernel_source_hash: Hash of source code of input Helion kernel |
| 82 | + hardware: Hardware of the input device |
| 83 | + runtime_name: Version of the cuda/rocm arch |
| 84 | + """ |
| 85 | + |
| 86 | + kernel_source_hash: str |
| 87 | + hardware: str |
| 88 | + runtime_name: str |
| 89 | + |
| 90 | + def stable_hash(self) -> str: |
| 91 | + return hashlib.sha256(repr(self).encode("utf-8")).hexdigest() |
| 92 | + |
| 93 | + |
| 94 | +@dataclasses.dataclass(frozen=True) |
| 95 | +class StrictAutotuneCacheKey(LooseAutotuneCacheKey): |
| 96 | + """ |
| 97 | + Autotune Cache key to use for utmost strictness in terms of re-autotuning |
| 98 | + when library source code changes. |
| 99 | +
|
| 100 | + This key includes (in addition to StrictAutotuneCacheKey): |
| 101 | +
|
| 102 | + helion_key: Hash of source code of Helion |
| 103 | + torch_key: Hash of source code of PyTorch |
| 104 | + triton_key: Hash of source code of Triton |
| 105 | + """ |
| 106 | + |
| 107 | + helion_key: str = dataclasses.field(default_factory=helion_key) |
| 108 | + torch_key: str = dataclasses.field(default_factory=torch_key_wrapper) |
| 109 | + triton_key: str = dataclasses.field(default_factory=triton_key_wrapper) |
| 110 | + |
| 111 | + |
| 112 | +class AutotuneCacheBase(abc.ABC): |
| 113 | + """ |
| 114 | + Abstract base class that all autotune caches need to implement. |
| 115 | + Any user defined cache will need to extend this class, and |
| 116 | + provide implementations for get and put methods. |
| 117 | + """ |
| 118 | + |
| 119 | + def __init__( |
| 120 | + self, kernel: BoundKernel, args: Sequence[object], autotuner: BaseSearch |
| 121 | + ) -> None: |
| 122 | + self.autotuner = autotuner |
| 123 | + self.kernel = kernel |
| 124 | + self.args = args |
| 125 | + |
| 126 | + @abc.abstractmethod |
| 127 | + def get(self) -> Config | None: |
| 128 | + raise NotImplementedError |
| 129 | + |
| 130 | + @abc.abstractmethod |
| 131 | + def put(self, config: Config) -> None: |
| 132 | + raise NotImplementedError |
| 133 | + |
| 134 | + def autotune(self) -> Config: |
| 135 | + if (config := self.get()) is not None: |
| 136 | + counters["autotune"]["cache_hit"] += 1 |
| 137 | + log.debug("cache hit: %s", str(config)) |
| 138 | + return config |
| 139 | + |
| 140 | + counters["autotune"]["cache_miss"] += 1 |
| 141 | + log.debug("cache miss") |
| 142 | + |
| 143 | + config = self.autotuner.autotune() |
| 144 | + |
| 145 | + self.put(config) |
| 146 | + counters["autotune"]["cache_put"] += 1 |
| 147 | + log.debug("cache put: %s", str(config)) |
| 148 | + |
| 149 | + return config |
0 commit comments