Skip to content

Commit

Permalink
added adaptive_max_pool1d to frontend and ivy experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
sherySSH committed Sep 3, 2023
1 parent 3a10637 commit 08ee129
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ivy/functional/frontends/torch/nn/functional/pooling_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ def adaptive_avg_pool2d(input, output_size):
return ivy.adaptive_avg_pool2d(input, output_size)


@to_ivy_arrays_and_back
def adaptive_max_pool1d(
input,
output_size,
return_indices=False,
):
# ToDo: Add return_indices once superset is implemented
return ivy.adaptive_max_pool1d(input, output_size)


@to_ivy_arrays_and_back
def adaptive_max_pool2d(
input,
Expand Down
50 changes: 50 additions & 0 deletions ivy/functional/ivy/experimental/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,56 @@ def _mask(vals, length, range_max, dim, mask_value=0.0):
return vals, length


@handle_nestable
@inputs_to_ivy_arrays
def adaptive_max_pool1d(
input: Union[ivy.Array, ivy.NativeArray],
output_size: Union[Sequence[int], int],
):
"""
"""
squeeze = False
if input.ndim == 2:
input = ivy.expand_dims(input, axis=0)
squeeze = True
elif input.ndim != 3:
raise ivy.utils.exceptions.IvyException(
f"Got {len(input.shape)}D input, but only 2D and 3D inputs are supported.",
)

if input.shape[-1] % output_size == 0:
stride = input.shape[-1] // output_size
kernel_size = input.shape[-1] - (output_size - 1) * stride
pooled_output = ivy.max_pool1d(
input, kernel_size, stride, "VALID", data_format="NCW"
)
if squeeze:
return ivy.squeeze(pooled_output, axis=0)
return pooled_output

idxw, length_w, range_max_w, adaptive_w = _compute_idx(
input.shape[-1], output_size, input.device
)

# to numpy and back in order to bypass a slicing error in tensorflow
vals = ivy.array(input.to_numpy()[..., idxw])

if not adaptive_w:
ret = ivy.max(vals, axis=-1)
ret = ivy.squeeze(ret, axis=0) if squeeze else ret
return ret

vals, length_w = _mask(vals, length_w, range_max_w, dim=-1)

ret = []
for i in range(vals.shape[-2]):
ret.append( ivy.max( vals[:,:,i, 0:length_w[i]] ).to_scalar() )

pooled_output = ivy.array(ret)

return pooled_output


@handle_nestable
@inputs_to_ivy_arrays
def adaptive_max_pool2d(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,44 @@ def test_torch_adaptive_avg_pool2d(
)


# adaptive_max_pool1d
@handle_frontend_test(
fn_tree="torch.nn.functional.adaptive_max_pool1d",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_num_dims=2,
max_num_dims=3,
min_dim_size=5,
max_value=100,
min_value=-100,
),
output_size=helpers.ints(min_value=1, max_value=10),
test_with_out=st.just(False),
)
def test_torch_adaptive_max_pool1d(
*,
dtype_and_x,
output_size,
on_device,
frontend,
test_flags,
fn_tree,
backend_fw,
):
input_dtype, x = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
input=x[0],
output_size=output_size,
atol=1e-2,
)


# adaptive_max_pool2d
@handle_frontend_test(
fn_tree="torch.nn.functional.adaptive_max_pool2d",
Expand Down

0 comments on commit 08ee129

Please sign in to comment.