tinygrad icon indicating copy to clipboard operation
tinygrad copied to clipboard

`__setitem__` raises "self operand of augmented assign must be contiguous" even though it is

Open obadakhalili opened this issue 1 year ago • 5 comments

Code

N = Tensor.zeros((3, 3)).contiguous()

for i in range(3):
  for j in range(3):
    N[i][j] += 1

Expectation

N to be a 3x3 matrix of 1's.

Reality

tinygrad throws RuntimeError: self operand of augmented assign must be contiguous.

obadakhalili avatar Sep 04 '24 10:09 obadakhalili

Doesn't Tensor.zeros((3, )) just create a 1D array of size 3?

Cheapshot003 avatar Sep 04 '24 11:09 Cheapshot003

thanks for noting this typo @Cheapshot003. however, the issue persists.

obadakhalili avatar Sep 04 '24 11:09 obadakhalili

Can confirm the issue. It works when you do N[i, j] = N[i, j] + 1

Should be investigated by someone who knows the internals better

Cheapshot003 avatar Sep 04 '24 11:09 Cheapshot003

that is a nice workaround but it is very slow. I think this is a separate issue of indexing being very slow.

obadakhalili avatar Sep 05 '24 06:09 obadakhalili

This error likely stems from the fact that Tinygrad doesn't support loads and stores. My current understanding is that you are supposed to do everything with Tensor operations.

This could mean using a mask to accomplish your goal.

for i in range(3):
    for j in range(3):
        N += Tensor([[1 if m == i and n == j else 0 for n in range(3)] for m in range(3)])

An alternative / hack is to perform inplace assignments on a numpy array before converting it into a Tensor.

np_array = np.zeros((3, 3))

for i in range(3):
    for j in range(3):
        np_array[i, j] += 1

N = Tensor(np_array)

Here is a minimal example showing the lack of support for in-place operations, and using a mask to accomplish the same goal.

#DOESN'T WORK
t2 = Tensor([0,0]).contiguous()
t2[0] += 1
t2[1] += 1

#WORKS
t = Tensor([0,0])
t += Tensor([1,0])
t += Tensor([0,1])

gordbegli avatar Sep 17 '24 06:09 gordbegli