# Set dispatch key when m.impl("myadd", add\_func)

**URL:** <https://discuss.pytorch.org/t/set-dispatch-key-when-m-impl-myadd-add-func/190711>\
**Category:** Uncategorized\
**Created:** [October 26, 2023, 8:19am UTC](https://discuss.pytorch.org/t/set-dispatch-key-when-m-impl-myadd-add-func/190711 "2023-10-26T08:19:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiangxiaobin96](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiangxiaobin96/32/64886_2.png) [@jiangxiaobin96](https://discuss.pytorch.org/u/jiangxiaobin96)\
**Post date:** [October 26, 2023, 8:19am UTC](https://discuss.pytorch.org/t/set-dispatch-key-when-m-impl-myadd-add-func/190711/1 "2023-10-26T08:19:05Z")

</div>

where set the dispatch key when i run m.impl(“myadd”, add\_func).  
I just found code in function Library::_impl `aten/src/ATen/core/library.cpp`, but where init this dispatch\_key_?

```c++
auto dispatch_key = f.dispatch_key_.has_value() ? f.dispatch_key_ : dispatch_key_;

```

---

<div class="post-metadata">

**Author:** ![jiangxiaobin96](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiangxiaobin96/32/64886_2.png) [@jiangxiaobin96](https://discuss.pytorch.org/u/jiangxiaobin96)\
**Post date:** [October 26, 2023, 8:44am UTC](https://discuss.pytorch.org/t/set-dispatch-key-when-m-impl-myadd-add-func/190711/2 "2023-10-26T08:44:39Z")

</div>

```c++
class TorchLibraryInit final {
 private:
  using InitFn = void(Library&);
  Library lib_;

 public:
  TorchLibraryInit(
      Library::Kind kind,
      InitFn* fn,
      const char* ns,
      c10::optional<c10::DispatchKey> k,
      const char* file,
      uint32_t line)
      : lib_(kind, ns, k, file, line) { // init Library class
    fn(lib_); // k in lib_ is dispatchKey
  }
};

```
