Skip to content

tensor creation error in math_interface.py #19

Open

Description

@suryabulusu

Line 45 in math_interface.py creates list of tensors as follows:

 x_var = [minitorch.tensor(x, requires_grad=True) for x in xs]

This leads to an error when x is not a list or tuple.

This error can be corrected in tensor_functions.py by adding the following code in def tensor(): before processing ls

if not isinstance(ls, (list, tuple)):
    ls = [ls]

Activity

  1. srush commented on Jan 14, 2022

    @srush
    Contributor

    Thanks! I will fix in the master repo.

  2. jchwenger commented on Nov 10, 2025

    @jchwenger

    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:

    x_var = [minitorch.tensor(x, requires_grad=True) for x in xs]

    We end up in TensorData, where shape is []:

    self.size = int(prod(shape))

    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):

    assert len(self._storage) == self.size, f"{self._storage=}, {self.size=} {shape=}"

    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=}"
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions