Setuptools allows us to define an Extension for compilation. The entire torch._C extension is compiled by collecting all of the source files, header files, libraries, etc. and creating a setuptools Extension. Then setuptools handles building the extension itself. I will explore the build process more in a subsequent post.
To summarize, let's revisit our four questions:

How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?

It uses CPython's framework for extending the Python interpreter and defining new types, while taking special care to generate code for all types.

How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?

It does so by defining a new type, THPTensor, that is backed by a TH Tensor. Function calls are forwarded to this tensor via the CPython backend's conventions.

How does PyTorch cwrap work to generate code for Tensor methods?
