simple_registry.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from collections.abc import Callable
  2. from typing import Any, Optional
  3. from .effects import EffectHolder
  4. from .fake_impl import FakeImplHolder
  5. from .utils import RegistrationHandle
  6. __all__ = ["SimpleLibraryRegistry", "SimpleOperatorEntry", "singleton"]
  7. class SimpleLibraryRegistry:
  8. """Registry for the "simple" torch.library APIs
  9. The "simple" torch.library APIs are a higher-level API on top of the
  10. raw PyTorch DispatchKey registration APIs that includes:
  11. - fake impl
  12. Registrations for these APIs do not go into the PyTorch dispatcher's
  13. table because they may not directly involve a DispatchKey. For example,
  14. the fake impl is a Python function that gets invoked by FakeTensor.
  15. Instead, we manage them here.
  16. SimpleLibraryRegistry is a mapping from a fully qualified operator name
  17. (including the overload) to SimpleOperatorEntry.
  18. """
  19. def __init__(self) -> None:
  20. self._data: dict[str, SimpleOperatorEntry] = {}
  21. def find(self, qualname: str) -> "SimpleOperatorEntry":
  22. res = self._data.get(qualname, None)
  23. if res is None:
  24. self._data[qualname] = res = SimpleOperatorEntry(qualname)
  25. return res
  26. singleton: SimpleLibraryRegistry = SimpleLibraryRegistry()
  27. class SimpleOperatorEntry:
  28. """This is 1:1 to an operator overload.
  29. The fields of SimpleOperatorEntry are Holders where kernels can be
  30. registered to.
  31. """
  32. def __init__(self, qualname: str) -> None:
  33. self.qualname: str = qualname
  34. self.fake_impl: FakeImplHolder = FakeImplHolder(qualname)
  35. self.torch_dispatch_rules: GenericTorchDispatchRuleHolder = (
  36. GenericTorchDispatchRuleHolder(qualname)
  37. )
  38. self.effect: EffectHolder = EffectHolder(qualname)
  39. # For compatibility reasons. We can delete this soon.
  40. @property
  41. def abstract_impl(self) -> FakeImplHolder:
  42. return self.fake_impl
  43. class GenericTorchDispatchRuleHolder:
  44. def __init__(self, qualname: str) -> None:
  45. self._data: dict[type, Callable[..., Any]] = {}
  46. self.qualname: str = qualname
  47. def register(
  48. self, torch_dispatch_class: type, func: Callable[..., Any]
  49. ) -> RegistrationHandle:
  50. if self.find(torch_dispatch_class):
  51. raise RuntimeError(
  52. f"{torch_dispatch_class} already has a `__torch_dispatch__` rule registered for {self.qualname}"
  53. )
  54. self._data[torch_dispatch_class] = func
  55. def deregister() -> None:
  56. del self._data[torch_dispatch_class]
  57. return RegistrationHandle(deregister)
  58. def find(self, torch_dispatch_class: type) -> Optional[Callable[..., Any]]:
  59. return self._data.get(torch_dispatch_class, None)
  60. def find_torch_dispatch_rule(
  61. op: Any, torch_dispatch_class: type
  62. ) -> Optional[Callable[..., Any]]:
  63. return singleton.find(op.__qualname__).torch_dispatch_rules.find(
  64. torch_dispatch_class
  65. )