Skip to content

Commit ecda33c

Browse files
committed
Add layers implementations for Minitorch
1 parent 471d350 commit ecda33c

File tree

4 files changed

+59
-174
lines changed

4 files changed

+59
-174
lines changed

minitorch/backends/cuda_conv.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ def _tensor_conv1d_kernel(
3333
weight_strides: Strides,
3434
reverse: bool,
3535
) -> None:
36-
"""
37-
CUDA 1D Convolution implementation.
38-
"""
3936
i = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
4037
if i >= out_size:
4138
return
@@ -155,7 +152,6 @@ def _tensor_conv2d_kernel(
155152
if i >= out_size:
156153
return
157154

158-
# Deconstruct i into batch, out_channel, out_width
159155
out_index = cuda.local.array(MAX_DIMS, numba.int32)
160156
to_index(i, out_shape, out_index)
161157

minitorch/nn/layers.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from ..tensor.operators import TensorBackend
2+
from ..tensor.functions import rand, zeros
3+
from .module import Module, Parameter
4+
from ..backends import fast_conv, fast_ops
5+
6+
BACKEND = TensorBackend(fast_ops.FastOps)
7+
8+
9+
class Linear(Module):
10+
def __init__(self, in_size, out_size):
11+
super().__init__()
12+
13+
# He initialization
14+
scale = (2.0 / in_size) ** 0.5
15+
self.weights = Parameter(scale * rand((in_size, out_size), backend=BACKEND))
16+
self.bias = Parameter(zeros((out_size,), backend=BACKEND))
17+
self.out_size = out_size
18+
19+
def forward(self, x):
20+
batch, in_size = x.shape
21+
return (
22+
x.view(batch, in_size) @ self.weights.value.view(in_size, self.out_size)
23+
).view(batch, self.out_size) + self.bias.value
24+
25+
26+
class Conv1d(Module):
27+
def __init__(self, in_channels, out_channels, kernel_width):
28+
super().__init__()
29+
30+
# He initialization
31+
fan_in = in_channels * kernel_width
32+
scale = (2.0 / fan_in) ** 0.5
33+
self.weights = Parameter(
34+
scale * rand((out_channels, in_channels, kernel_width), backend=BACKEND)
35+
)
36+
self.bias = Parameter(zeros((1, out_channels, 1), backend=BACKEND))
37+
38+
def forward(self, input):
39+
out = fast_conv.conv1d(input, self.weights.value) + self.bias.value
40+
return out
41+
42+
43+
class Conv2d(Module):
44+
def __init__(self, in_channels, out_channels, kh, kw):
45+
super().__init__()
46+
47+
# He initialization
48+
fan_in = in_channels * kh * kw
49+
scale = (2.0 / fan_in) ** 0.5
50+
self.weights = Parameter(
51+
scale * rand((out_channels, in_channels, kh, kw), backend=BACKEND)
52+
)
53+
self.bias = Parameter(zeros((out_channels, 1, 1), backend=BACKEND))
54+
55+
def forward(self, input):
56+
out = fast_conv.conv2d(input, self.weights.value) + self.bias.value
57+
return out

minitorch/tensor/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def rand(
298298
requires_grad: bool = False,
299299
) -> Tensor:
300300
"""
301-
Produce a random tensor of size `shape`.
301+
Produce a standard random distribution tensor of size `shape`.
302302
303303
Args:
304304
shape : shape of tensor
@@ -308,7 +308,7 @@ def rand(
308308
Returns:
309309
:class:`Tensor` : new tensor
310310
"""
311-
vals = [random.random() for _ in range(int(common_operators.prod(shape)))]
311+
vals = [np.random.randn() for _ in range(int(common_operators.prod(shape)))]
312312
tensor = minitorch.Tensor.make(vals, shape, backend=backend)
313313
tensor.requires_grad_(requires_grad)
314314
return tensor

project/run_mnist.py

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)