From acb6c17ccae627ed981f1a31451ac1d97ce4aaba Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Sat, 18 May 2024 09:01:42 -0300 Subject: [PATCH] fix subtraction broadcasted access memory wrong values --- norch/__pycache__/tensor.cpython-38.pyc | Bin 15623 -> 15627 bytes norch/tensor.py | 27 ++++++++++++------------ test.py | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 6c93f168b96f7a34206c20121ae3073bdf07cbca..cc2bfbb01905a7a4280bde1fc7a2266214c3f8a8 100644 GIT binary patch delta 1281 zcmb7COKcNY6rDSsnS3S=#*Qa(LgMf-mE#1kgMg3}l0Yk{N*9fwl7YO~8IxcVJb9k* z2@_~VTiTRGq+1DgbyQVVBt)r3Ai60+LSoVKS+$ZEhy}1?SE*vheU6h_6_v_J@4Wlo z+{L&)n60$L4BQ72o15Q=Irqd=Oef$)Cml&P(rVT zTDclDs0AO+hdbOqL+t=5eA3>jq^Sf8dJx}jX;?E#2i{JwsFb+4DJ~KMx-sqz;HNDg z*K2fCXnAPm(UmZ6B45X~w_W-WemPxMQyU`iEneC1xp#^xqH?*>@_QQ=p7 zFY=+r1>PccNWz-+dX4ijE#_>cD~62e2gnU5x%f z40lEFLF_)g{d!9W-s#+-&%B{cD|ec)W;fjR(-P0+GDmK69c*$P!V*DRpZ!;HP23BI z+{XAP026L5Q7^-1)YCdFY*pZb`@_}|fbTJvh`I4?YXNSR!t>j&1N??vJ9Foj6-3P94X&7wU*@+%yg-Z4;|d;31S=R1z+tfm7S-v~h0E@j5rH zX=ws2t%{>O8D3-j>c4b*A;02$O}4Bu9|GM zr4ZIQT26gddM#Aa9Zi*THI9B*k|kC%b%WwU`ClE#g(!|zF2w(^YaPjf1)09=o>-#f zk!Y9&Y;9GyeQRL*a}~KTs}goKjK7ELPf`+fwQfzK3o7|RvfBmFp6ICrB3gp{tM^ka z0q1sPhs@CiBvZvx&EFtKPA5&RtQXFw^4Y|u?JoPqQ?qO=J5g4xk@19w-Rn@UN%P5+ zl}Q!sti`-5xVQdCXuzY55!9ll#=A&6?2I-+7N3vCA%nBgZqVINqk{m$%Z+h3hrvDZ z(0K~uZ3OEoj|qPSM;j;cMA+|phPH*5gy=06Sn{G&k{wz6IsK8kAP>nUrKCD4zeR%e zoziUof+Xa2bY?gTAvMQQs5$zkHae^Jw9z-T$$#Q0qf--fYQhiOP0+WRaN(g%pgacM zHrC|QgS=aPA$E!hK8&0C(tAHU_2q@_z7W5oQ(u&(lNp&b*m`tw#2TPgSi#Ze(-+Q3 zCB-I9#PHC#9U@KClIEz6HmdMNN3~?nY8%#8EYjl=ILfMOOCoAPUIUP(h8)FGhvfl@ zpP}4*h``kO>%wOO&I+&uqy(e|)DpCN$ajlo1a(j+tAK+oN7P~Z ziXYa;@Y|O5%{lq6#oXu3NjWf3Oh28#qt74RKC7dsBqc&|5t zH);Ppeih5WEw{1#V+BU=wz(gEa_^cZ)rEK4YO%8;1zs^Cdwz0;F%@rg{7t9VyY}Jj z_@TfXqJTC5J4Jhe^%NL$`}ben$rZtSo#~ByMu0J!7qiyLG-Gl1;(;m=cD*Ydc=N%8 zXe106bQ=>d1LUx>r^8+8z60KLhv$6NmqT zS^Ou_iaotqxPu?`mf&A(==<6n6(r*V@&cv=2*vn}fH?v40!mn!Y;Jv9xR(W75%3N{ d*`Mc=`7F2R_*FcZ?1v+GF?j&4;pfRS{{tB|a$^7h diff --git a/norch/tensor.py b/norch/tensor.py index 6548e9a..807c290 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -223,8 +223,8 @@ class Tensor: def __add__(self, other): if isinstance(other, (int, float)): other = other * self.ones_like() - - broadcasted_shape = [] + + broadcasted_shape_add = [] # Function to determine if broadcasting is needed and get the broadcasted shape def broadcast_shape(shape1, shape2): @@ -239,10 +239,10 @@ class Tensor: for dim1, dim2 in zip(shape1, shape2): if dim1 != dim2 and dim1 != 1 and dim2 != 1: raise ValueError("Shapes are not compatible for broadcasting") - broadcasted_shape.append(max(dim1, dim2)) - return broadcasted_shape, True + broadcasted_shape_add.append(max(dim1, dim2)) + return broadcasted_shape_add, True - broadcasted_shape, needs_broadcasting = broadcast_shape(self.shape, other.shape) + broadcasted_shape_add, needs_broadcasting = broadcast_shape(self.shape, other.shape) if needs_broadcasting: # Call add_broadcasted_tensor if broadcasting is needed @@ -253,8 +253,8 @@ class Tensor: result_data = Tensor() result_data.tensor = result_tensor_ptr - result_data.shape = broadcasted_shape - result_data.ndim = len(broadcasted_shape) + result_data.shape = broadcasted_shape_add.copy() + result_data.ndim = len(broadcasted_shape_add) result_data.device = self.device result_data.numel = 1 @@ -316,7 +316,7 @@ class Tensor: if isinstance(other, (int, float)): other = other * self.ones_like() - broadcasted_shape = [] + broadcasted_shape_sub = [] # Function to determine if broadcasting is needed and get the broadcasted shape def broadcast_shape(shape1, shape2): @@ -327,13 +327,14 @@ class Tensor: shape1 = [1] * (max_len - len(shape1)) + shape1 shape2 = [1] * (max_len - len(shape2)) + shape2 + for dim1, dim2 in zip(shape1, shape2): if dim1 != dim2 and dim1 != 1 and dim2 != 1: raise ValueError("Shapes are not compatible for broadcasting") - broadcasted_shape.append(max(dim1, dim2)) - return broadcasted_shape, True + broadcasted_shape_sub.append(max(dim1, dim2)) + return broadcasted_shape_sub, True - broadcasted_shape, needs_broadcasting = broadcast_shape(self.shape, other.shape) + broadcasted_shape_sub, needs_broadcasting = broadcast_shape(self.shape, other.shape) if needs_broadcasting: # Call add_broadcasted_tensor if broadcasting is needed @@ -344,8 +345,8 @@ class Tensor: result_data = Tensor() result_data.tensor = result_tensor_ptr - result_data.shape = broadcasted_shape - result_data.ndim = len(broadcasted_shape) + result_data.shape = broadcasted_shape_sub.copy() + result_data.ndim = len(broadcasted_shape_sub) result_data.device = self.device result_data.numel = self.numel # Update this to calculate the correct number of elements if broadcasting diff --git a/test.py b/test.py index 3e45c78..92d779d 100644 --- a/test.py +++ b/test.py @@ -9,7 +9,7 @@ W2 = norch.Tensor([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]], requires_ B2 = norch.Tensor([[1, 2, 3, 4, 5]], requires_grad=True) -Z1 = W1 @ X - B1 +Z1 = W1 @ X + B1 # Perform matrix multiplication