Thanks. No, I think I found the solution. It is convolution.
x = torch.tensor([[1., 0., 0.],
                      [0., 1., 0.],
                      [1., 0., 0.]])

pattern = torch.tensor([[1., 0.],
                            [0., 1.]])

img, mask = x[None, None], pattern[None, None]
M = F.conv2d(img, 1 - mask)
tensor([[[[0., 1.],
          [2., 0.]]]])

 M == mask.sum(dim> =(2,3)))
  tensor([[[[ True, False],
            [False, False]]]])