How to convert convolutions to matrix-matrix multiplication?

I like to convert a 4d input tensor to a 3d tensor where different inputs to a convolution/grouped convolution layer are flattened into rows. In other words, the function that does this conversion should be able to generate different inputs to the convolutions based on kernel size, stride, padding, etc. I have used something similar to this in numpy before, but I’m not sure how I can do this in PyTorch.

I think this is in fact what is done for converting convolutions to matrix-matrix multiplication.
Can I use unfold or Im2Col to implement what I have in mind? If yes, how? If not, what is the best way of implementing this other than writing a nested for loop?

Thanks!