I’m not sure if conv2d is what you’re looking for, but here’s how to adapt it to take your inputs.
Try this:
import torch
import torch.nn.functional as F
from torch.autograd import Variable
N = 4
M = 3
A = Variable(torch.randn(1, 1, N, N))
M = Variable(torch.ones(1, 1, M, M))
output = F.conv2d(A, M)

You can set A to be any N x N matrix with A[0,0,:] = A2, where A2 is N x N. Same with M[0, 0, :] = M2 where M2 is M by M.