| 52 | |
| 53 | |
| 54 | class PythonDispatcher: |
| 55 | namespace = "__test__" |
| 56 | name = "foo" |
| 57 | # fmt: off |
| 58 | runtime_keys = [ |
| 59 | "CPU", "AutogradCPU", |
| 60 | "FPGA", "AutogradOther", |
| 61 | "XLA", "AutogradXLA", |
| 62 | "Lazy", "AutogradLazy", |
| 63 | ] |
| 64 | # fmt: on |
| 65 | alias_keys = [ |
| 66 | "CompositeExplicitAutograd", |
| 67 | "Autograd", |
| 68 | "CompositeImplicitAutograd", |
| 69 | ] |
| 70 | supported_keys = runtime_keys + alias_keys |
| 71 | |
| 72 | def __init__(self): |
| 73 | C._dispatch_check_invariants(self.name) # type: ignore[attr-defined] |
| 74 | self.ref = C._dispatch_library("FRAGMENT", self.namespace, "") |
| 75 | self.ref.def_("foo(Tensor x) -> Tensor") |
| 76 | |
| 77 | """ |
| 78 | Returns a list of dispatch keys supported by PythonDispatcher. |
| 79 | You can register kernels to these keys. |
| 80 | """ |
| 81 | |
| 82 | def keys(self): |
| 83 | return self.supported_keys |
| 84 | |
| 85 | """ |
| 86 | Register kernels to the target dispatchKeys. |
| 87 | dispatchKeys(list[str]): a list of dispatch keys that you want to register |
| 88 | your own kernel. Note that you don't need to write the kernel yourself in |
| 89 | this PythonDispatcher.E.g. for CPU key, a kernel(e.g fn_CPU for CPU) is |
| 90 | automatically generated and registered. |
| 91 | """ |
| 92 | |
| 93 | def register(self, dispatchKeys): |
| 94 | # Overriden is not supported and triggers a warning in C++ dispatcher. |
| 95 | if len(set(dispatchKeys)) != len(dispatchKeys): |
| 96 | raise RuntimeError( |
| 97 | f"Overriden is not allowed but found duplicates in {dispatchKeys}." |
| 98 | ) |
| 99 | # We currently forbid this in codegen instead of C++ dispatcher. |
| 100 | if ( |
| 101 | "CompositeImplicitAutograd" in dispatchKeys |
| 102 | and "CompositeExplicitAutograd" in dispatchKeys |
| 103 | ): |
| 104 | raise RuntimeError( |
| 105 | "Registration to both CompositeImplicitAutograd and CompositeExplicitAutograd is not allowed." |
| 106 | ) |
| 107 | for key in dispatchKeys: |
| 108 | if key not in self.supported_keys: |
| 109 | raise RuntimeError( |
| 110 | f"{key} is not supported, please select a dispatch key in {self.supported_keys}." |
| 111 | ) |
no outgoing calls
searching dependent graphs…