-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add Conv2D layer implementation #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -110,3 +110,181 @@ class Dense[ctype]: | |||||||||||||||||||||||||||||||||||||||||
| def _evaluate(mpc, layer: Dense, last_output: ctype): | ||||||||||||||||||||||||||||||||||||||||||
| layer.input = last_output @ layer.weights + layer.bias | ||||||||||||||||||||||||||||||||||||||||||
| layer.output = layer.activate(mpc) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class Conv2D[ctype]: | ||||||||||||||||||||||||||||||||||||||||||
| activation: str | ||||||||||||||||||||||||||||||||||||||||||
| out_channels: int | ||||||||||||||||||||||||||||||||||||||||||
| kernel_size: tuple[int, int] | ||||||||||||||||||||||||||||||||||||||||||
| stride: tuple[int, int] | ||||||||||||||||||||||||||||||||||||||||||
| padding: str | ||||||||||||||||||||||||||||||||||||||||||
| kernel_initializer: str | ||||||||||||||||||||||||||||||||||||||||||
| bias_initializer: str | ||||||||||||||||||||||||||||||||||||||||||
| in_channels: int | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| weights: ctype | ||||||||||||||||||||||||||||||||||||||||||
| bias: ctype | ||||||||||||||||||||||||||||||||||||||||||
| input: ctype | ||||||||||||||||||||||||||||||||||||||||||
| output: ctype | ||||||||||||||||||||||||||||||||||||||||||
| last_input: ctype | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| dw: ctype | ||||||||||||||||||||||||||||||||||||||||||
| db: ctype | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| vw: ctype | ||||||||||||||||||||||||||||||||||||||||||
| vb: ctype | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, activation: str, out_channels: int, kernel_size=(3, 3), stride=(1, 1), padding: str = "valid", kernel_initializer: str = "uniform", bias_initializer: str = "uniform"): | ||||||||||||||||||||||||||||||||||||||||||
| assert activation in SUPPORTED_ACTIVATIONS, f"Conv2D: {activation} activation not supported. Supported: {SUPPORTED_ACTIVATIONS}" | ||||||||||||||||||||||||||||||||||||||||||
| assert padding == "valid", f"Conv2D: only 'valid' padding is currently supported, got {padding}" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| self.activation = activation | ||||||||||||||||||||||||||||||||||||||||||
| self.out_channels = out_channels | ||||||||||||||||||||||||||||||||||||||||||
| self.kernel_size = kernel_size if isinstance(kernel_size, tuple) else (kernel_size, kernel_size) | ||||||||||||||||||||||||||||||||||||||||||
| self.stride = stride if isinstance(stride, tuple) else (stride, stride) | ||||||||||||||||||||||||||||||||||||||||||
| self.padding = padding | ||||||||||||||||||||||||||||||||||||||||||
| self.kernel_initializer = kernel_initializer | ||||||||||||||||||||||||||||||||||||||||||
| self.bias_initializer = bias_initializer | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||
| def size(self) -> int: | ||||||||||||||||||||||||||||||||||||||||||
| if hasattr(self, 'output') and not self.output.is_empty(): | ||||||||||||||||||||||||||||||||||||||||||
| batch, H, W, C = self.output.shape | ||||||||||||||||||||||||||||||||||||||||||
| return H * W * C | ||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||
| def channels(self) -> int: | ||||||||||||||||||||||||||||||||||||||||||
| return self.out_channels | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def initialize(self, mpc, prev_size: int, *args, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||
| self.in_channels = prev_size | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| kH, kW = self.kernel_size | ||||||||||||||||||||||||||||||||||||||||||
| w_shape = (kH, kW, self.in_channels, self.out_channels) | ||||||||||||||||||||||||||||||||||||||||||
| b_shape = (1, 1, 1, self.out_channels) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| self.weights = ctype.rand(w_shape, self.kernel_initializer, mpc, *args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||
| self.bias = ctype.rand(b_shape, self.bias_initializer, mpc, *args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| self.dw = self.weights.zeros() | ||||||||||||||||||||||||||||||||||||||||||
| self.db = self.bias.zeros() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| self.vw = self.weights.zeros() | ||||||||||||||||||||||||||||||||||||||||||
| self.vb = self.bias.zeros() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def is_evaluated(self): | ||||||||||||||||||||||||||||||||||||||||||
| return not self.output.is_empty() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def evaluate(self, mpc, last_output: ctype): | ||||||||||||||||||||||||||||||||||||||||||
| Conv2D[ctype]._evaluate(mpc, self, last_output) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def activate(self, mpc) -> ctype: | ||||||||||||||||||||||||||||||||||||||||||
| return activate(mpc, self.input, self.activation) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def derive(self, mpc, prev_output: ctype, dhidden: ctype, LAYER_IDX: Static[int]) -> ctype: | ||||||||||||||||||||||||||||||||||||||||||
| dact = dactivate(mpc, self.input, self.activation) | ||||||||||||||||||||||||||||||||||||||||||
| return Conv2D[ctype]._derive(mpc, self, prev_output, dhidden, dact, LAYER_IDX=LAYER_IDX) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def update(self, mpc, step: float, momentum: float): | ||||||||||||||||||||||||||||||||||||||||||
| Conv2D[ctype]._nesterov_update(mpc, self, step, momentum) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @sequre | ||||||||||||||||||||||||||||||||||||||||||
| def _nesterov_update(mpc, layer: Conv2D, step: float, momentum: float): | ||||||||||||||||||||||||||||||||||||||||||
| vw_prev = layer.vw.copy() | ||||||||||||||||||||||||||||||||||||||||||
| layer.vw = layer.vw * momentum - layer.dw * step | ||||||||||||||||||||||||||||||||||||||||||
| layer.weights += layer.vw * (momentum + 1) - vw_prev * momentum | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| vb_prev = layer.vb.copy() | ||||||||||||||||||||||||||||||||||||||||||
| layer.vb = layer.vb * momentum - layer.db * step | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+192
to
+198
|
||||||||||||||||||||||||||||||||||||||||||
| def _nesterov_update(mpc, layer: Conv2D, step: float, momentum: float): | |
| vw_prev = layer.vw.copy() | |
| layer.vw = layer.vw * momentum - layer.dw * step | |
| layer.weights += layer.vw * (momentum + 1) - vw_prev * momentum | |
| vb_prev = layer.vb.copy() | |
| layer.vb = layer.vb * momentum - layer.db * step | |
| def _nesterov_update(mpc, layer: Conv2D, step: float, momentum: float): | |
| # Store previous velocity for weights | |
| vw_prev = layer.vw.copy() | |
| # Update the velocity for weights using Nesterov momentum | |
| layer.vw = layer.vw * momentum - layer.dw * step | |
| # Update the weights using the new and previous velocities | |
| layer.weights += layer.vw * (momentum + 1) - vw_prev * momentum | |
| # Store previous velocity for biases | |
| vb_prev = layer.vb.copy() | |
| # Update the velocity for biases using Nesterov momentum | |
| layer.vb = layer.vb * momentum - layer.db * step | |
| # Update the biases using the new and previous velocities |
sdivyanshu90 marked this conversation as resolved.
Show resolved
Hide resolved
sdivyanshu90 marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of dX using (cols * 0)[0:1, 0:1].reshape((batch, H, W, C)) is an unclear way to create a zeros tensor. Consider using a more explicit method like cols.zeros() or similar zero-initialization method that creates the correct shape directly, which would improve code clarity and maintainability.
| dX = (cols * 0)[0:1, 0:1].reshape((batch, H, W, C)) | |
| dX = cols.zeros((batch, H, W, C)) |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The layer.last_input is stored here but never used in the Conv2D implementation. The prev_output parameter is passed to the _derive method directly, making this stored value redundant. Consider removing this assignment and the last_input class attribute (line 128) to match the Dense layer's simpler design and reduce memory overhead.
| layer.last_input = last_output | |
Uh oh!
There was an error while loading. Please reload this page.