-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
implement groups
for conv1d and conv2d
#637
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 |
---|---|---|
|
@@ -23,6 +23,9 @@ class Conv1d(Module): | |
Default: 1. | ||
padding (int, optional): How many positions to 0-pad the input with. | ||
Default: 0. | ||
dilation (int, optional): The size of the dilation. Default: 1. | ||
groups (int, optional): The number of groups to split the input. | ||
Default: 1. | ||
bias (bool, optional): If ``True`` add a learnable bias to the output. | ||
Default: ``True`` | ||
""" | ||
|
@@ -34,6 +37,8 @@ def __init__( | |
kernel_size: int, | ||
stride: int = 1, | ||
padding: int = 0, | ||
dilation: int = 1, | ||
groups: int = 1, | ||
bias: bool = True, | ||
): | ||
super().__init__() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the shape of the self.weight doesn't change? |
||
|
@@ -49,16 +54,21 @@ def __init__( | |
|
||
self.padding = padding | ||
self.stride = stride | ||
self.dilation = dilation | ||
self.groups = groups | ||
|
||
def _extra_repr(self): | ||
return ( | ||
f"{self.weight.shape[-1]}, {self.weight.shape[0]}, " | ||
f"kernel_size={self.weight.shape[1]}, stride={self.stride}, " | ||
f"padding={self.padding}, bias={'bias' in self}" | ||
f"padding={self.padding}, dilation={self.dilation}, " | ||
f"groups={self.groups} bias={'bias' in self}" | ||
) | ||
|
||
def __call__(self, x): | ||
y = mx.conv1d(x, self.weight, self.stride, self.padding) | ||
y = mx.conv1d( | ||
x, self.weight, self.stride, self.padding, self.dilation, self.groups | ||
) | ||
if "bias" in self: | ||
y = y + self.bias | ||
return y | ||
|
@@ -81,6 +91,10 @@ class Conv2d(Module): | |
applying the filter. Default: 1. | ||
padding (int or tuple, optional): How many positions to 0-pad | ||
the input with. Default: 0. | ||
dilation (int or tuple, optional): The size of the dilation. | ||
Default: 1. | ||
groups (int, optional): The number of groups to split the input. | ||
Default: 1. | ||
bias (bool, optional): If ``True`` add a learnable bias to the | ||
output. Default: ``True`` | ||
""" | ||
|
@@ -92,13 +106,15 @@ def __init__( | |
kernel_size: Union[int, tuple], | ||
stride: Union[int, tuple] = 1, | ||
padding: Union[int, tuple] = 0, | ||
dilation: Union[int, tuple] = 1, | ||
groups: int = 1, | ||
bias: bool = True, | ||
): | ||
super().__init__() | ||
|
||
kernel_size, stride, padding = map( | ||
kernel_size, stride, padding, dilation = map( | ||
lambda x: (x, x) if isinstance(x, int) else x, | ||
(kernel_size, stride, padding), | ||
(kernel_size, stride, padding, dilation), | ||
) | ||
scale = math.sqrt(1 / (in_channels * kernel_size[0] * kernel_size[1])) | ||
self.weight = mx.random.uniform( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here, in_channel/groups |
||
|
@@ -111,16 +127,21 @@ def __init__( | |
|
||
self.padding = padding | ||
self.stride = stride | ||
self.dilation = dilation | ||
self.groups = groups | ||
|
||
def _extra_repr(self): | ||
return ( | ||
f"{self.weight.shape[-1]}, {self.weight.shape[0]}, " | ||
f"kernel_size={self.weight.shape[1:2]}, stride={self.stride}, " | ||
f"padding={self.padding}, bias={'bias' in self}" | ||
f"padding={self.padding}, dilation={self.dilation}, " | ||
f"groups={self.groups}, bias={'bias' in self}" | ||
) | ||
|
||
def __call__(self, x): | ||
y = mx.conv2d(x, self.weight, self.stride, self.padding) | ||
y = mx.conv2d( | ||
x, self.weight, self.stride, self.padding, self.dilation, self.groups | ||
) | ||
if "bias" in self: | ||
y = y + self.bias | ||
return y |
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.
If the group>1, then the last dimension of weight * group == the last dimension of input shape