What is the underlying implementation process of 2D convolution in libtorch?

I want to add other operations (which form is ciphertext) to convolution process. To do this, i need implement the underlying operations in 2D convolution. Such as, how to construct the data structure of input and output, how to deal with padding to get correct output?
If anybody have direct and simple method or ideas, thank you very much for your messages.

To write a manual convolution you could use nn.Unfold and apply a matrix multiplication.
The nn.Unfold docs give you an example.