Skip to content

Commit 68f1f00

Browse files
committed
Q1
1 parent f0dac06 commit 68f1f00

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from tinygrad.tensor import Tensor
22

3-
def matrix_dot_vector_tg(a, b) -> Tensor:
3+
def matrix_dot_vector_tg(a: Tensor, b: Tensor) -> Tensor:
44
"""
55
Compute the product of matrix `a` and vector `b` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Inputs will be tinygrad Tensors.
77
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
88
"""
99
if len(a[0]) != len(b):
1010
return Tensor(-1)
11-
a_t = Tensor(a)
12-
b_t = Tensor(b)
13-
return a_t.matmul(b_t)
11+
return a @ b
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
from tinygrad.tensor import Tensor
22

3-
def matrix_dot_vector_tg(a, b) -> Tensor:
3+
def matrix_dot_vector_tg(a:Tensor, b:Tensor) -> Tensor:
44
"""
55
Compute the product of matrix `a` and vector `b` using tinygrad.
6-
Inputs can be Python lists, NumPy arrays, or tinygrad Tensors.
6+
Will be tinygrad Tensors.
77
Returns a 1-D Tensor of length m, or Tensor(-1) if dimensions mismatch.
88
"""
9-
# Dimension mismatch check
10-
if len(a[0]) != len(b):
11-
return Tensor(-1)
12-
# Convert to Tensor
13-
a_t = Tensor(a)
14-
b_t = Tensor(b)
15-
# Your implementation here
169
pass
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
[
22
{
3-
"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())",
3+
"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())",
44
"expected_output": "[14.0, 25.0, 49.0]"
55
},
66
{
7-
"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())",
7+
"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())",
88
"expected_output": "-1"
9+
},
10+
{
11+
"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())",
12+
"expected_output": "[5, 10]"
913
}
1014
]

0 commit comments

Comments
 (0)