[torch.compile] Fine-grained CustomOp enabling mechanism (#9300)

This commit is contained in:
Luka Govedič
2024-10-17 14:36:37 -04:00
committed by GitHub
parent 7871659abb
commit 0f41fbe5a3
8 changed files with 220 additions and 21 deletions

View File

@@ -17,6 +17,7 @@ import uuid
import warnings
import weakref
from asyncio import FIRST_COMPLETED, ensure_future
from collections.abc import Mapping
from functools import lru_cache, partial, wraps
from platform import uname
from typing import (Any, AsyncGenerator, Awaitable, Callable, Dict, Generic,
@@ -1442,3 +1443,24 @@ class AtomicCounter:
@property
def value(self):
return self._value
# Adapted from: https://stackoverflow.com/a/47212782/5082708
class LazyDict(Mapping, Generic[T]):
def __init__(self, factory: Dict[str, Callable[[], T]]):
self._factory = factory
self._dict: Dict[str, T] = {}
def __getitem__(self, key) -> T:
if key not in self._dict:
if key not in self._factory:
raise KeyError(key)
self._dict[key] = self._factory[key]()
return self._dict[key]
def __iter__(self):
return iter(self._factory)
def __len__(self):
return len(self._factory)