# Difference between \_\_torch\_function\_\_ and \_\_torch\_dispatch\_\_

**URL:** <https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376>\
**Category:** Uncategorized\
**Created:** [July 15, 2023, 10:11am UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376 "2023-07-15T10:11:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weberxie](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/weberxie/32/1267_2.png) [@weberxie](https://dev-discuss.pytorch.org/u/weberxie)\
**Post date:** [July 15, 2023, 10:11am UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/1 "2023-07-15T10:11:48Z")

</div>

What is the difference between the two design goals?

---

<div class="post-metadata">

**Author:** ![ezyang](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/ezyang/32/12_2.png) [@ezyang](https://dev-discuss.pytorch.org/u/ezyang)\
**Post date:** [July 16, 2023, 1:54pm UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/2 "2023-07-16T13:54:54Z")

</div>

` __torch_function__ ` solves the problem of “I can overload the meaning of tensor.add() with Python duck typing, but I can’t overload the meaning of torch.add(tensor) because this isn’t a method call.” It lets you define a method which lets you overload the meaning of torch.\* API calls. It works even if you don’t subclass Tensor.

` __torch_dispatch__ ` solves the problem of “PyTorch has a big pile of C++ code which implements important subsystems like autograd, and I can’t interpose on it.” It offers a callback into Python after these subsystems have been processed. A torch dispatch Tensor subclass (these must be Tensor subclasses) can, for example, change the behavior of operations called by the autograd engine.

---

<div class="post-metadata">

**Author:** ![albanD](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/alband/32/9_2.png) [@albanD](https://dev-discuss.pytorch.org/u/albanD)\
**Post date:** [July 18, 2023, 7:54pm UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/3 "2023-07-18T19:54:42Z")

</div>

You can also see more detailed (though not 100% finished yet) documentation at [Extending PyTorch — PyTorch main documentation](https://pytorch.org/docs/main/notes/extending.html#extending-torch-python-api)

---

<div class="post-metadata">

**Author:** ![wenqibiao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/wenqibiao/32/1863_2.png) [@wenqibiao](https://dev-discuss.pytorch.org/u/wenqibiao)\
**Post date:** [March 26, 2024, 12:33pm UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/4 "2024-03-26T12:33:51Z")

</div>

what you said may wrong. The flollowing code work well:

```auto

import torch

class PyTensor(torch.Tensor):
    @classmethod
    def __torch_dispatch__ (cls, func, types, args=(), kwargs=None):
        print(f"PyTensor into {func}")

class MyTensor(torch.Tensor):  
    @classmethod
    def __torch_function__ (cls, func, types, args, kwargs=None):  
        print(f"MyTensor into {func}")
      

x = torch.randn(8)
y1 = PyTensor(x)
y2 = MyTensor(x)
y1 = torch.add(y1, y1)
y2 = torch.add(y2, y2)

```

the output is:

````auto
PyTensor into aten.add.Tensor
MyTensor into <built-in method add of type object at 0x7f2f100ea7e0>```
````

---

<div class="post-metadata">

**Author:** ![wenqibiao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/wenqibiao/32/1863_2.png) [@wenqibiao](https://dev-discuss.pytorch.org/u/wenqibiao)\
**Post date:** [March 26, 2024, 12:57pm UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/5 "2024-03-26T12:57:14Z")

</div>

Great! I got answer from link you post. Thank you! Copy to here:

> In a similar way where **torch\_function** is able to interpose on all of torch’s Python API and Tensor methods, **torch\_dispatch** is able intercepting all calls into the aten native API.

---

<div class="post-metadata">

**Author:** ![xushifang](https://avatars.discourse-cdn.com/v4/letter/x/e274bd/32.png) [@xushifang](https://dev-discuss.pytorch.org/u/xushifang)\
**Post date:** [July 3, 2024, 9:56am UTC](https://dev-discuss.pytorch.org/t/difference-between-torch-function-and-torch-dispatch/1376/6 "2024-07-03T09:56:07Z")

</div>

You can refer to this website for this question.  
[https://pytorch.org/docs/main/notes/extending.html#extending-torch-native-api](https://pytorch.org/docs/main/notes/extending.html#extending-torch-native-api)

> While ` __torch_function__ ` allows one to effectively extend PyTorch’s pure Python components’ behavior, it does not allow one to extend the parts of PyTorch implemented in C++. To that end, a `Tensor` subclass can also define ` __torch_dispatch__ ` which will be able to override the behavior at the C++ level.
