If I understand correctly, I can do it as follow:
import numpy as np
import torch
from torch.nn.functional import conv2d

def averaging(t):
    assert len(t.shape) == 2

    t_tensor = torch.from_numpy(t)[None, None, ...].float()
    kernel = torch.tensor([[0, 1, 0],
                          [1, 0, 1],
                          [0, 1, 0]])[None, None, ...].float()
    t_tensor = (conv2d(t_tensor * t_tensor, kernel, padding=1) / 4).squeeze().numpy()
    
    new_t = t.copy()
    new_t[1:-1, 1:-1] = t_tensor[1:-1, 1:-1]
    return new_t

> t = np.arange(12).reshape(3,4).astype(float)
> print(averaging(t))

About the replacement of np.nditer, I don’t know.