From 899cb84a4ffbd087ba3317df98459e64c65a1e2f Mon Sep 17 00:00:00 2001 From: Rkeramati Date: Sat, 25 Oct 2025 13:52:05 -0400 Subject: [PATCH] made task4_4 simpler --- minitorch/fast_conv.py | 17 +++++++- minitorch/nn.py | 96 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/minitorch/fast_conv.py b/minitorch/fast_conv.py index 5d002ac..9833820 100644 --- a/minitorch/fast_conv.py +++ b/minitorch/fast_conv.py @@ -61,8 +61,14 @@ def _tensor_conv1d( `batch, out_channels, width` `reverse` decides if weight is anchored left (False) or right. - (See diagrams) - + (See diagrams) + Implementation hints: + - Use nested loops over batch, output channels, output width, input channels, and kernel width + - Use prange for appropriate outer loops to enable parallelization + - Calculate input position based on output position and kernel position + - Handle reverse flag to determine kernel indexing direction + - Apply bounds checking to avoid accessing invalid memory positions + - Use provided strides to compute correct memory offsets Args: ---- out (Storage): storage for `out` tensor. @@ -187,6 +193,13 @@ def _tensor_conv2d( `Reverse` decides if weight is anchored top-left (False) or bottom-right. (See diagrams) + Implementation hints: + - Use nested loops over batch, output channels, output height, output width, input channels, kernel height, and kernel width + - Use prange for appropriate outer loops to enable parallelization + - Calculate input positions based on output position and kernel positions + - Handle reverse flag to determine kernel indexing direction for both height and width + - Apply bounds checking to avoid accessing invalid memory positions for both dimensions + - Use provided strides to compute correct memory offsets for 4D tensors Args: ---- diff --git a/minitorch/nn.py b/minitorch/nn.py index 33ffa05..349d47a 100644 --- a/minitorch/nn.py +++ b/minitorch/nn.py @@ -1,10 +1,11 @@ from typing import Tuple -from . import operators -from .autodiff import Context -from .fast_ops import FastOps from .tensor import Tensor -from .tensor_functions import Function, rand, tensor +from .tensor_functions import rand +from typing import Optional +from .tensor_functions import Function, tensor + +from .autodiff import Context # List of functions in this file: @@ -39,4 +40,89 @@ def tile(input: Tensor, kernel: Tuple[int, int]) -> Tuple[Tensor, int, int]: raise NotImplementedError("Need to implement for Task 4.3") -# TODO: Implement for Task 4.3. +def avgpool2d(input: Tensor, kernel: Tuple[int, int]) -> Tensor: + """Tiled average pooling 2D + + Args: + ---- + input: batch x channel x height x width + kernel: height x width of pooling + + Returns: + ------- + Pooled tensor + """ + # TODO: Implement for Task 4.3. + raise NotImplementedError("Need to implement for Task 4.3") + + +class Max(Function): + @staticmethod + def forward(ctx: Context, a: Tensor, dim: Tensor) -> Tensor: + """Forward of max should be max reduction""" + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + @staticmethod + def backward(ctx: Context, grad_output: Tensor) -> Tuple[Tensor, Tensor]: + """Backward of max should be argmax (see argmax function)""" + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def max(input: Tensor, dim: int) -> Tensor: + """Apply max reduction over a dimension""" + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def argmax(input: Tensor, dim: int) -> Tensor: + """Compute the argmax as a 1-hot tensor""" + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def softmax(input: Tensor, dim: int) -> Tensor: + """Compute the softmax as a tensor""" + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def logsoftmax(input: Tensor, dim: int) -> Tensor: + """Compute the log of the softmax as a tensor + See https://en.wikipedia.org/wiki/LogSumExp#log-sum-exp_trick_for_log-domain_calculations """ + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def maxpool2d(input: Tensor, kernel: Tuple[int, int]) -> Tensor: + """Tiled max pooling 2D + + Args: + ---- + input: batch x channel x height x width + kernel: height x width of pooling + + Returns: + ------- + Pooled tensor + """ + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") + + +def dropout(input: Tensor, rate: float, ignore: bool = False) -> Tensor: + """Dropout positions based on random noise + + Args: + ---- + input: input tensor + rate: probability [0, 1) of dropping out each position + ignore: skip dropout, i.e. do nothing at all + + Returns: + ------- + tensor with random positions dropped out + """ + # TODO: Implement for Task 4.4. + raise NotImplementedError("Need to implement for Task 4.4") \ No newline at end of file