Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions questions/1_matrix-vector-dot-product/tinygrad/solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from tinygrad.tensor import Tensor

def matrix_dot_vector_tg(a, b) -> Tensor:
def matrix_dot_vector_tg(a: Tensor, b: Tensor) -> Tensor:
"""
Compute the product of matrix `a` and vector `b` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Inputs will be tinygrad Tensors.
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
"""
if len(a[0]) != len(b):
return Tensor(-1)
a_t = Tensor(a)
b_t = Tensor(b)
return a_t.matmul(b_t)
return a @ b
11 changes: 2 additions & 9 deletions questions/1_matrix-vector-dot-product/tinygrad/starter_code.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from tinygrad.tensor import Tensor

def matrix_dot_vector_tg(a, b) -> Tensor:
def matrix_dot_vector_tg(a:Tensor, b:Tensor) -> Tensor:
"""
Compute the product of matrix `a` and vector `b` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Will be tinygrad Tensors.
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
"""
# Dimension mismatch check
if len(a[0]) != len(b):
return Tensor(-1)
# Convert to Tensor
a_t = Tensor(a)
b_t = Tensor(b)
# Your implementation here
pass
8 changes: 6 additions & 2 deletions questions/1_matrix-vector-dot-product/tinygrad/tests.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[
{
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n [[1,2,3],[2,4,5],[6,8,9]],\n [1,2,3]\n)\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1,2,3],[2,4,5],[6,8,9]]),\n Tensor([1,2,3])\n)\nprint(res.numpy().tolist())",
"expected_output": "[14.0, 25.0, 49.0]"
},
{
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n [[1,2,3],[2,4,5]],\n [1,2]\n)\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1,2,3],[2,4,5]]),\n Tensor([1,2])\n)\nprint(res.numpy().tolist())",
"expected_output": "-1"
},
{
"test": "from tinygrad.tensor import Tensor\nres = matrix_dot_vector_tg(\n Tensor([[1, 2], [2, 4]]),\n Tensor([1, 2])\n)\nprint(res.numpy().tolist())",
"expected_output": "[5, 10]"
}
]
5 changes: 2 additions & 3 deletions questions/2_transpose-of-a-matrix/tinygrad/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
def transpose_matrix_tg(a) -> Tensor:
"""
Transpose a 2D matrix `a` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Inputs are tinygrad Tensors.
Returns a transposed Tensor.
"""
a_t = Tensor(a)
return a_t.transpose(0,1)
return a.T
7 changes: 2 additions & 5 deletions questions/2_transpose-of-a-matrix/tinygrad/starter_code.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from tinygrad.tensor import Tensor

def transpose_matrix_tg(a) -> Tensor:
def transpose_matrix_tg(a:Tensor) -> Tensor:
"""
Transpose a 2D matrix `a` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Inputs are tinygrad Tensors.
Returns a transposed Tensor.
"""
# Convert to Tensor
a_t = Tensor(a)
# Your implementation here
pass
4 changes: 2 additions & 2 deletions questions/2_transpose-of-a-matrix/tinygrad/tests.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg([[1,2,3],[4,5,6]])\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg(Tensor([[1,2,3],[4,5,6]]))\nprint(res.numpy().tolist())",
"expected_output": "[[1, 4], [2, 5], [3, 6]]"
},
{
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg([[1,2],[3,4]])\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = transpose_matrix_tg(Tensor([[1,2],[3,4]]))\nprint(res.numpy().tolist())",
"expected_output": "[[1, 3], [2, 4]]"
}
]
5 changes: 2 additions & 3 deletions questions/3_reshape-matrix/tinygrad/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
def reshape_matrix_tg(a, new_shape) -> Tensor:
"""
Reshape a 2D matrix `a` to shape `new_shape` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Inputs are tinygrad Tensors.
Returns a Tensor of shape `new_shape`, or an empty Tensor on mismatch.
"""
# Dimension check
if len(a) * len(a[0]) != new_shape[0] * new_shape[1]:
return Tensor([])
a_t = Tensor(a)
return a_t.reshape(new_shape)
return a.reshape(new_shape)
10 changes: 2 additions & 8 deletions questions/3_reshape-matrix/tinygrad/starter_code.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from tinygrad.tensor import Tensor

def reshape_matrix_tg(a, new_shape) -> Tensor:
def reshape_matrix_tg(a:Tensor, new_shape:tuple) -> Tensor:
"""
Reshape a 2D matrix `a` to shape `new_shape` using tinygrad.
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
Inputs are tinygrad Tensors.
Returns a Tensor of shape `new_shape`, or an empty Tensor on mismatch.
"""
# Dimension check
if len(a) * len(a[0]) != new_shape[0] * new_shape[1]:
return Tensor([])
# Convert to Tensor and reshape
a_t = Tensor(a)
# Your implementation here
pass
4 changes: 2 additions & 2 deletions questions/3_reshape-matrix/tinygrad/tests.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n [[1,2,3],[4,5,6]],\n (3, 2)\n)\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n Tensor([[1,2,3],[4,5,6]]),\n (3, 2)\n)\nprint(res.numpy().tolist())",
"expected_output": "[[1, 2], [3, 4], [5, 6]]"
},
{
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n [[1,2],[3,4]],\n (3, 2)\n)\nprint(res.numpy().tolist())",
"test": "from tinygrad.tensor import Tensor\nres = reshape_matrix_tg(\n Tensor([[1,2],[3,4]]),\n (3, 2)\n)\nprint(res.numpy().tolist())",
"expected_output": "[]"
}
]
Loading