Skip to content

Commit

Permalink
feat: Added chunk function to paddle frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
arshPratap committed Oct 3, 2023
1 parent 0623329 commit fbb9fff
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
20 changes: 20 additions & 0 deletions ivy/functional/frontends/paddle/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ def cast(x, dtype):
return ivy.astype(x, dtype)


@with_supported_dtypes(
{"2.5.1 and below": ("bool", "int32", "int64", "float16", "float32", "float64")},
"paddle",
)
@to_ivy_arrays_and_back
def chunk(x, chunks, axis=0, name=None):
if x.shape == () and chunks > 1:
raise ValueError("Invalid value for chunks")
axis_size = ivy.shape(x)[axis]
chunk_size = axis_size // chunks
remainder = axis_size % chunks
num_or_size_splits = (
chunks
if remainder == 0
else tuple([chunk_size + remainder] + [chunk_size] * (chunks - 1))
)
num_or_size_splits = axis_size if chunk_size == 0 else num_or_size_splits
return ivy.split(x, num_or_size_splits=num_or_size_splits, axis=axis)


@with_unsupported_dtypes({"2.5.1 and below": ("int8", "int16")}, "paddle")
@to_ivy_arrays_and_back
def concat(x, axis, name=None):
Expand Down
26 changes: 26 additions & 0 deletions ivy_tests/test_ivy/helpers/testing_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
_dtype_kind_keys,
_get_type_dict,
)
from ivy_tests.test_ivy.helpers import (
dtype_and_values,
get_axis,
get_dtypes,
)
from .globals import mod_backend

cmd_line_args = (
Expand Down Expand Up @@ -925,3 +930,24 @@ def _create_transpile_report(data: dict, backend: str, file_name: str):
json_object = json.dumps(data, indent=6)
with open(file_name, "w") as outfile:
outfile.write(json_object)


@st.composite
def _chunk_helper(draw):
dtype, x, shape = draw(
dtype_and_values(
available_dtypes=get_dtypes("float"),
min_num_dims=1,
ret_shape=True,
)
)
axis = draw(get_axis(shape=shape, force_int=True))
if shape[axis] == 0:
chunks = 0
else:
factors = []
for i in range(1, shape[axis] + 1):
if shape[axis] % i == 0:
factors.append(i)
chunks = draw(st.sampled_from(factors))
return dtype, x, axis, chunks
32 changes: 32 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_paddle/test_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from ivy_tests.test_ivy.test_frontends.test_torch.test_miscellaneous_ops import (
_get_repeat_interleaves_args,
)
from ivy_tests.test_ivy.helpers.testing_helpers import (
_chunk_helper,
)


# --- Helpers --- #
Expand Down Expand Up @@ -326,6 +329,35 @@ def test_paddle_cast(
)


# chunk
@handle_frontend_test(
fn_tree="paddle.chunk",
x_dim_chunks=_chunk_helper(),
test_with_out=st.just(False),
)
def test_paddle_chunk(
*,
x_dim_chunks,
fn_tree,
on_device,
frontend,
test_flags,
backend_fw,
):
dtype, x, axis, chunks = x_dim_chunks
helpers.test_frontend_function(
input_dtypes=dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
x=x[0],
chunks=chunks,
axis=axis,
)


@handle_frontend_test(
fn_tree="paddle.concat",
xs_n_input_dtypes_n_unique_idx=_arrays_idx_n_dtypes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from ivy_tests.test_ivy.test_functional.test_core.test_manipulation import ( # noqa
_get_splits,
)
from ivy_tests.test_ivy.helpers.testing_helpers import (
_chunk_helper,
)


# --- Helpers --- #
Expand Down Expand Up @@ -197,27 +200,6 @@ def _arrays_idx_n_dtypes(draw):
return xs, input_dtypes, unique_idx


@st.composite
def _chunk_helper(draw):
dtype, x, shape = draw(
helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_num_dims=1,
ret_shape=True,
)
)
axis = draw(helpers.get_axis(shape=shape, force_int=True))
if shape[axis] == 0:
chunks = 0
else:
factors = []
for i in range(1, shape[axis] + 1):
if shape[axis] % i == 0:
factors.append(i)
chunks = draw(st.sampled_from(factors))
return dtype, x, axis, chunks


@st.composite
def _dtype_input_dim_start_length(draw):
_shape = draw(helpers.get_shape(min_num_dims=1, min_dim_size=1))
Expand Down

0 comments on commit fbb9fff

Please sign in to comment.