Solved by tom in post #2 
              
              
                You need the clone if you want to keep M unchanged. 
def get_off_diagonal_elements(M):
    res = M.clone()
    res.diagonal(dim1=-1, dim2=-2).zero_()
    return res