You want to create an extension. This tutorial 20 will get you through all the details.
Basically, there are 3 steps:

Write your extension code
Bind it to python using pybind11

Build it using setuptools and torch.utils.cpp_extension


You can see another example of pytorch extension in the first commit of this repo 9. There is only one cpp extension there, it’s maybe easier to follow.
Tell me if you need any further help.