Skip to content
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

Reformating assert_supports_inplace #10647

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
19 changes: 17 additions & 2 deletions ivy/array/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def array_equal(self: ivy.Array, x: Union[ivy.Array, ivy.NativeArray], /) -> boo
"""
return ivy.array_equal(self, x)

def assert_supports_inplace(self: ivy.Array) -> bool:
def assert_supports_inplace(self: ivy.Array, /) -> bool:
"""
ivy.Array instance method variant of ivy.assert_supports_inplace. This method
simply wraps the function, and so the docstring for ivy.assert_supports_inplace
Expand All @@ -788,7 +788,22 @@ def assert_supports_inplace(self: ivy.Array) -> bool:
Returns
-------
ret
True if support, raises exception otherwise
True if supports, raises IvyBackendException otherwise

Examples
--------
With :class:`ivy.Array` input and default backend set as `numpy`:

>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
True

With :class:`ivy.Array` input and default backend set as `jax`:

>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations \
are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend

"""
return ivy.assert_supports_inplace(self)
Expand Down
23 changes: 21 additions & 2 deletions ivy/container/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,27 @@ def assert_supports_inplace(
Returns
-------
ret
a container of whether or not inplace operations are supported for x and
its leaves.
An ivy.Container instance of True bool values if nodes of the Container \
support in-place operations, raises IvyBackendException otherwise

Examples
--------
With :class:`ivy.Container` input and default backend set as `numpy`:

>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
{
a: True,
b: True
}

With :class:`ivy.Container` input and default backend set as `jax`:

>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations \
are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend

"""
return self.static_assert_supports_inplace(
self,
Expand Down
38 changes: 36 additions & 2 deletions ivy/functional/ivy/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,7 @@ def supports_inplace_updates(x: Union[ivy.Array, ivy.NativeArray], /) -> bool:
@handle_exceptions
@handle_array_function
def assert_supports_inplace(x: Union[ivy.Array, ivy.NativeArray], /) -> bool:
"""Asserts that inplace operations are supported for x, else raises exception.
"""Asserts that inplace operations are supported for x, else raises IvyBackendException.

Parameters
----------
Expand All @@ -2468,7 +2468,41 @@ def assert_supports_inplace(x: Union[ivy.Array, ivy.NativeArray], /) -> bool:
Returns
-------
ret
True if support, raises exception otherwise
True if supports, raises IvyBackendException otherwise

This function is *nestable*, and therefore also accepts :code:'ivy.Container'
instance in place of the argument.

Examples
--------
With :class:`ivy.Array` input and default backend set as `numpy`:

>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
True

With :class:`ivy.Array` input and default backend set as `jax`:

>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations \
are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend

With :class:`ivy.Container` input and default backend set as `numpy`:

>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
{
a: True,
b: True
}

With :class:`ivy.Container` input and default backend set as `jax`:

>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations \
are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend

"""
ivy.assertions.check_true(
Expand Down