Skip to content

Commit

Permalink
refactor: move helper dims_and_offsets to general hypothesis helpers …
Browse files Browse the repository at this point in the history
…and remove duplications making additional functionality easier to add
  • Loading branch information
Ishticode committed Feb 17, 2024
1 parent 7ee4aae commit 6004903
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 50 deletions.
14 changes: 14 additions & 0 deletions ivy_tests/test_ivy/helpers/hypothesis_helpers/general_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,17 @@ def get_factor(n):
else shape
)
return shape_


@st.composite
def dims_and_offset(draw, shape):
shape_actual = draw(shape)
dim1 = draw(get_axis(shape=shape, force_int=True))
dim2 = draw(get_axis(shape=shape, force_int=True))
while dim1 == dim2:
dim2 = draw(get_axis(shape=shape, force_int=True))

offset = draw(
st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
)
return dim1, dim2, offset
13 changes: 1 addition & 12 deletions ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,6 @@ def _transpose_helper(draw):
return dtype, x, perm


@st.composite
def dims_and_offset(draw, shape):
shape_actual = draw(shape)
dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
offset = draw(
st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
)
return dim1, dim2, offset


# Helpers #
# ------ #

Expand Down Expand Up @@ -706,7 +695,7 @@ def test_paddle_eigvalsh(
available_dtypes=helpers.get_dtypes("valid"),
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
),
axis_and_offset=dims_and_offset(
axis_and_offset=helpers.dims_and_offset(
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,6 @@ def _reshape_helper(draw):
return dtypes, x, reshape_shape


# diagonal
@st.composite
def dims_and_offset(draw, shape):
shape_actual = draw(shape)
dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
offset = draw(
st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
)
return dim1, dim2, offset


# expand helper function
@st.composite
def dtypes_x_shape(draw):
Expand Down Expand Up @@ -2755,7 +2743,7 @@ def test_paddle_device(
available_dtypes=helpers.get_dtypes("valid"),
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
),
dims_and_offset=dims_and_offset(
dims_and_offset=helpers.dims_and_offset(
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,6 @@ def complex_strategy(
return tuple(shape)


@st.composite
def dims_and_offset(draw, shape):
shape_actual = draw(shape)
dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
offset = draw(
st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
)
return dim1, dim2, offset


# cross
@st.composite
def dtype_value1_value2_axis(
Expand Down Expand Up @@ -900,7 +889,7 @@ def test_torch_diagflat(
available_dtypes=helpers.get_dtypes("float"),
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
),
dims_and_offset=dims_and_offset(
dims_and_offset=helpers.dims_and_offset(
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
),
)
Expand Down
14 changes: 1 addition & 13 deletions ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,6 @@ def _unfold_args(draw):
return values_dtype, values, axis, size, step


# diagonal
@st.composite
def dims_and_offset(draw, shape):
shape_actual = draw(shape)
dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
offset = draw(
st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
)
return dim1, dim2, offset


# --- Main --- #
# ------------ #

Expand Down Expand Up @@ -5898,7 +5886,7 @@ def test_torch_diag(
available_dtypes=helpers.get_dtypes("valid"),
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
),
dims_and_offset=dims_and_offset(
dims_and_offset=helpers.dims_and_offset(
shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
),
)
Expand Down

0 comments on commit 6004903

Please sign in to comment.