You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @srush, I'm not sure if that's something coming from an error in my implementation, but I still get an issue for scalar tensors, this time because of a mismatch when checking the size attribute of TensorData and the length of the underlying array.
So, starting from the same Line 45:
In my implementation, prod returns 0.0 if len(a) == 0, but of course we do have an underlying array of length 1 (one scalar), which makes the following assert fail (I added a message):
It seems reasonable to follow the standard to have a shape of () for scalars, which is the case in PyTorch, but then I'm not sure what would be the elegant way to handle those (and not bug-inducing in other parts of the code base)? Simply avoid scalars and do x_var = [minitorch.tensor([x], requires_grad=True) for x in xs] in math_interface.py? Return 1.0 instead in prod for length-zero Iterables?
Apologies if I overlooked something really simple!
Full trace:
AssertionError: self._storage=array([-48.99999]), self.size=0 shape=()
---
File "/Users/jcw/Documents/dl/learning/srush/minitorch/.venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 556, in _run_script
exec(code, module.__dict__)
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/project/app.py", line 118, in <module>
page()
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/project/app.py", line 89, in render_m2_sandbox
return render_math_sandbox(True, True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/project/math_interface.py", line 47, in render_math_sandbox
x_var = [minitorch.tensor(x, requires_grad=True) for x in xs]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/minitorch/tensor_functions.py", line 434, in tensor
return _tensor(cur, tuple(shape2), backend=backend, requires_grad=requires_grad)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/minitorch/tensor_functions.py", line 399, in _tensor
tensor = minitorch.Tensor.make(ls, shape, backend=backend)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/minitorch/tensor.py", line 263, in make
return Tensor(TensorData(storage, shape, strides), backend=backend)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jcw/Documents/dl/learning/srush/minitorch/minitorch-module-2-jchwenger/minitorch/tensor_data.py", line 192, in __init__
assert len(self._storage) == self.size, f"{self._storage=}, {self.size=} {shape=}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Activity
srush commentedon Jan 14, 2022
Thanks! I will fix in the master repo.
jchwenger commentedon Nov 10, 2025
Hi @srush, I'm not sure if that's something coming from an error in my implementation, but I still get an issue for scalar tensors, this time because of a mismatch when checking the size attribute of
TensorDataand the length of the underlying array.So, starting from the same Line 45:
We end up in
TensorData, whereshapeis[]:In my implementation,
prodreturns0.0iflen(a) == 0, but of course we do have an underlying array of length 1 (one scalar), which makes the following assert fail (I added a message):It seems reasonable to follow the standard to have a shape of
()for scalars, which is the case in PyTorch, but then I'm not sure what would be the elegant way to handle those (and not bug-inducing in other parts of the code base)? Simply avoid scalars and dox_var = [minitorch.tensor([x], requires_grad=True) for x in xs]inmath_interface.py? Return1.0instead inprodfor length-zeroIterables?Apologies if I overlooked something really simple!
Full trace: