How to Extend Torch via it's C++ Frontend

Hi guys,
Is there any method to extend LibTorch using C++?

For PyTorch, there is a __torch_function__, then I can do something like this:

def CustomTensor:
    def __torch_function__(self, func, types, args=(), kwargs=None):
        if func is xxx: ...

x = CustomTensor()
linear = nn.Linear(1024, 32)
y = linear(x)

But seems there’s no way to create a CustomTensor by overiding or extending torch.Tensor in C++ frontend

// torch modules are defined in pimpl.h
namespace torch::nn {
TORCH_MODULE(Linear);
} // namespace torch::nn

Any suggestion? Thanks a lot

1 Like