Hi,
If you want to change the corners of a square matrix with 4 values, the most efficient way is to change the stride by hand to get the tensor you want:
import torch

# a must be 2x2
# b can be any squared matrix
# You can generalize to batch by keeping original size and stride below
a = torch.rand(2, 2)
b = torch.zeros(3, 3)
print("a")
print(a)
print("b")
print(b)


assert b.ndimension() == 2
assert b.is_contiguous()
size0, size1 = b.size()
stride0, stride1 = b.stride()
new_size = (2, 2)
new_stride = (stride0*(size0-1), stride1*(size1-1))

b_corner = b.as_strided(new_size, new_stride)
# b_corner is a "real" tensor that shares data with b

b_corner += a
print("b_corner")
print(b_corner)

print("b")
print(b)

More generally the approach would be:

If you can use it with custom stride (elements are always spaced by the same distance in a given dimension), do it this way.
If it’s really sparse use indices and put_ 27 to set the values at the given indices. If you have more than one dimension to index, you might want to use scatter_ 16.
If it’s fairly dense, then a masked approach using masked_scatter_ 12 is the way to go (similar to what you tried). The point is that the source should be a 1D Tensor containing as many values as 1s in the mask. In your case a[mask] = b.view(-1) for example but you will need to be careful in which order you put the elements in b so that they are placed where you want to in the original matrix.