Skip to content

Update layers.py added max_pool1d class #15444

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion ivy/stateful/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,8 +1432,55 @@ def _forward(self, inputs, initial_state=None):


# Pooling #
# --------#

class MaxPool1D(Module):
def __init__(
self,
kernel_size,
stride,
padding,
data_format="NWC",
device=None,
v=None,
dtype=None,
):
"""
Class for applying Max Pooling over a mini-batch of inputs.
Parameters
----------
kernel_size
The size of the sliding window, must be >0.
stride
The stride of the sliding window, must be > 0. Default value: kernel_size.
padding
Implicit negative infinity padding to be added on both sides, must be >=0 and <= kernel_size/2.
device
device on which to create the layer's variables 'cuda:0', 'cuda:1', 'cpu'
"""
self._kernel_size = kernel_size
self._stride = stride or kernel_size
self._padding = padding
self._data_format = data_format
Module.__init__(self, device=device, dtype=dtype)

def _forward(self, inputs):
"""
Forward pass of the layer.
Parameters
----------
inputs
The input to the layer.
Returns
-------
The output of the layer.
"""
return ivy.max_pool1d(
inputs,
self._kernel_size,
self._stride,
self._padding,
data_format=self._data_format,
)

class MaxPool2D(Module):
def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def arrays_for_pooling(
)
if array_dim == 3:
kernel = draw(st.tuples(st.integers(1, in_shape[1])))
new_kernel = kernel
new_kernel = kernel
if return_dilation:
new_kernel = []
dilations = []
Expand Down
38 changes: 38 additions & 0 deletions ivy_tests/test_ivy/test_stateful/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,45 @@ def test_sequential_layer(

# # Pooling #

@handle_method(
method_tree="MaxPool1D.__call__",
x_k_s_p=helpers.arrays_for_pooling(min_dims=3, max_dims=3, min_side=2, max_side=4),
)
def test_maxpool1d_layer(
*,
x_k_s_p,
test_gradients,
on_device,
class_name,
method_name,
ground_truth_backend,
init_flags,
method_flags,
):
input_dtype, x, kernel_size, stride, padding = x_k_s_p
padding=kernel_size[0]-1
helpers.test_method(
ground_truth_backend=ground_truth_backend,
init_flags=init_flags,
method_flags=method_flags,
init_all_as_kwargs_np={
"kernel_size": kernel_size,
"stride": stride,
"padding": padding,
"data_format": "NCW",
"device": on_device,
"dtype": input_dtype[0],
},
method_input_dtypes=input_dtype,
method_all_as_kwargs_np={"inputs": x[0]},
class_name=class_name,
method_name=method_name,
test_gradients=test_gradients,
on_device=on_device,
)



# MaxPool2D
@handle_method(
method_tree="MaxPool2D.__call__",
Expand Down