You can do this with the toeplitz matrix.
You will find some code in PyTorch forum: https://discuss.pytorch.org/t/obtaining-toeplitz-matrix-for-convolution/52968
Or here: https://gist.github.com/vvolhejn/e265665c65d3df37e381316bf57b8421