Skip to content

Commit

Permalink
Merge branch 'unifyai:master' into invert_numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
AsmaaMHadir authored Jul 10, 2023
2 parents 39b1898 + 092986b commit 6451643
Show file tree
Hide file tree
Showing 94 changed files with 1,322 additions and 3,309 deletions.
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ but this can easily be changed to your favorite framework, such as TensorFlow, o
super().__init__()
def _build(self, *args, **kwargs):
self.linear0 = ivy.Linear(input_dim, 128)
self.linear1 = ivy.Linear(128, output_dim)
self.linear0 = ivy.Linear(self.input_dim, 128)
self.linear1 = ivy.Linear(128, self.output_dim)
def _forward(self, x):
x = self.linear0(x)
Expand All @@ -285,15 +285,16 @@ but this can easily be changed to your favorite framework, such as TensorFlow, o
y = 0.2 * x ** 2 + 0.5 * x + 0.1 + noise
def loss_fn(pred, target):
def loss_fn(v, x, target):
pred = model(x, v=v)
return ivy.mean((pred - target) ** 2)
for epoch in range(40):
# forward pass
pred = model(x)
# compute loss and gradients
loss, grads = ivy.execute_with_gradients(lambda v: loss_fn(pred, y), model.v)
loss, grads = ivy.execute_with_gradients(lambda params: loss_fn(*params), (model.v, x, y), xs_grad_idxs=[[0]])
# update parameters
model.v = optimizer.step(model.v, grads)
Expand Down
2 changes: 2 additions & 0 deletions docs/partial_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@
from docs.conf import html_theme_options

html_theme_options["switcher"]["json_url"] = "https://unify.ai/docs/versions/ivy.json"

repo_name = "ivy"
29 changes: 29 additions & 0 deletions ivy/data_classes/array/experimental/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,32 @@ def frexp(
ivy.array([[0.5, 0.5, 0.75], [1, 2, 2]])
"""
return ivy.frexp(self._data, out=out)

def modf(
self: ivy.Array, /, *, out: Optional[Tuple[ivy.Array, ivy.Array]] = None
) -> Tuple[ivy.Array, ivy.Array]:
"""
ivy.Array instance method variant of ivy.modf. This method simply wraps the
function, and so the docstring for ivy.modf also applies to this method with
minimal changes.
Parameters
----------
self
Input array.
out
Alternate output arrays in which to place the result.
The default is None.
Returns
-------
ret
The fractional and integral parts of the input array.
Examples
--------
>>> x = ivy.array([1.5, 2.7, 3.9])
>>> x.modf()
(ivy.array([0.5, 0.7, 0.9]), ivy.array([1, 2, 3]))
"""
return ivy.modf(self._data, out=out)
94 changes: 94 additions & 0 deletions ivy/data_classes/container/experimental/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2745,3 +2745,97 @@ def frexp(
}
"""
return self.static_frexp(self, out=out)

@staticmethod
def static_modf(
x: Union[ivy.Array, ivy.NativeArray, ivy.Container],
/,
*,
key_chains: Optional[Union[List[str], Dict[str, str]]] = None,
to_apply: bool = True,
prune_unapplied: bool = False,
map_sequences: bool = False,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.modf. This method simply wraps the
function, and so the docstring for ivy.modf also applies to this method with
minimal changes.
Parameters
----------
x
The container whose arrays should be split into the fractional and integral parts.
key_chains
The key-chains to apply or not apply the method to. Default is ``None``.
to_apply
If True, the method will be applied to key_chains, otherwise key_chains
will be skipped. Default is ``True``.
prune_unapplied
Whether to prune key_chains for which the function was not applied.
Default is ``False``.
map_sequences
Whether to also map method to sequences (lists, tuples).
Default is ``False``.
out
optional output container, for writing the result to.
Returns
-------
ret
container including the fractional and integral parts of x.
Examples
--------
With one :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1.2, 2.7, 3.9]), b=ivy.array([-1.5, 5.3, -10.7]))
>>> ivy.Container.static_modf(x)
{
a: (ivy.array([0.2, 0.7, 0.9]), ivy.array([1.0, 2.0, 3.0])),
b: (ivy.array([-0.5, 0.3, -0.7]), ivy.array([-1.0, 5.0, -10.0]))
}
"""
return ContainerBase.cont_multi_map_in_function(
"modf",
x,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def modf(
self: ivy.Container,
/,
*,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container instance method variant of ivy.modf. This method simply wraps the
function, and so the docstring for ivy.modf also applies to this method with
minimal changes.
Parameters
----------
self
The container whose arrays should be split into the fractional and integral parts.
out
optional output container, for writing the result to.
Returns
-------
ret
container including the fractional and integral parts of x.
Examples
--------
With one :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1.2, 2.7, 3.9]), b=ivy.array([-1.5, 5.3, -10.7]))
>>> x.modf()
{
a: (ivy.array([0.2, 0.7, 0.9]), ivy.array([1.0, 2.0, 3.0])),
b: (ivy.array([-0.5, 0.3, -0.7]), ivy.array([-1.0, 5.0, -10.0]))
}
"""
return self.static_modf(self, out=out)
9 changes: 9 additions & 0 deletions ivy/functional/backends/jax/experimental/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,12 @@ def frexp(
x: JaxArray, /, *, out: Optional[Tuple[JaxArray, JaxArray]] = None
) -> Tuple[JaxArray, JaxArray]:
return jnp.frexp(x)


def modf(
x: JaxArray,
/,
*,
out: Optional[JaxArray] = None,
) -> JaxArray:
return jnp.modf(x)
9 changes: 6 additions & 3 deletions ivy/functional/backends/jax/linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ def inv(
adjoint: bool = False,
out: Optional[JaxArray] = None,
) -> JaxArray:
if jnp.any(jnp.linalg.det(x.astype("float64")) == 0):
return x
if adjoint:
x = jnp.transpose(x)
if x.ndim < 2:
raise ValueError("Input must be at least 2D")
permutation = list(range(x.ndim))
permutation[-2], permutation[-1] = permutation[-1], permutation[-2]
x_adj = jnp.transpose(x, permutation).conj()
return jnp.linalg.inv(x_adj)
return jnp.linalg.inv(x)


Expand Down
9 changes: 9 additions & 0 deletions ivy/functional/backends/mxnet/experimental/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,12 @@ def frexp(
] = None,
) -> Union[(Tuple[(None, None)], Tuple[(mx.ndarray.NDArray, mx.ndarray.NDArray)])]:
raise IvyNotImplementedException()


def modf(
x: Union[(None, mx.ndarray.NDArray)],
/,
*,
out: Optional[Union[(None, mx.ndarray.NDArray)]] = None,
) -> Union[(None, mx.ndarray.NDArray)]:
raise IvyNotImplementedException()
24 changes: 12 additions & 12 deletions ivy/functional/backends/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

# update these to add new dtypes
valid_dtypes = {
"1.25.0 and below": (
"1.25.1 and below": (
ivy.int8,
ivy.int16,
ivy.int32,
Expand All @@ -69,7 +69,7 @@
)
}
valid_numeric_dtypes = {
"1.25.0 and below": (
"1.25.1 and below": (
ivy.int8,
ivy.int16,
ivy.int32,
Expand All @@ -86,7 +86,7 @@
)
}
valid_int_dtypes = {
"1.25.0 and below": (
"1.25.1 and below": (
ivy.int8,
ivy.int16,
ivy.int32,
Expand All @@ -97,11 +97,11 @@
ivy.uint64,
)
}
valid_float_dtypes = {"1.25.0 and below": (ivy.float16, ivy.float32, ivy.float64)}
valid_float_dtypes = {"1.25.1 and below": (ivy.float16, ivy.float32, ivy.float64)}
valid_uint_dtypes = {
"1.25.0 and below": (ivy.uint8, ivy.uint16, ivy.uint32, ivy.uint64)
"1.25.1 and below": (ivy.uint8, ivy.uint16, ivy.uint32, ivy.uint64)
}
valid_complex_dtypes = {"1.25.0 and below": (ivy.complex64, ivy.complex128)}
valid_complex_dtypes = {"1.25.1 and below": (ivy.complex64, ivy.complex128)}

# leave these untouched
valid_dtypes = _dtype_from_version(valid_dtypes, backend_version)
Expand All @@ -113,12 +113,12 @@

# invalid data types
# update these to add new dtypes
invalid_dtypes = {"1.25.0 and below": (ivy.bfloat16,)}
invalid_numeric_dtypes = {"1.25.0 and below": (ivy.bfloat16,)}
invalid_int_dtypes = {"1.25.0 and below": ()}
invalid_float_dtypes = {"1.25.0 and below": (ivy.bfloat16,)}
invalid_uint_dtypes = {"1.25.0 and below": ()}
invalid_complex_dtypes = {"1.25.0 and below": ()}
invalid_dtypes = {"1.25.1 and below": (ivy.bfloat16,)}
invalid_numeric_dtypes = {"1.25.1 and below": (ivy.bfloat16,)}
invalid_int_dtypes = {"1.25.1 and below": ()}
invalid_float_dtypes = {"1.25.1 and below": (ivy.bfloat16,)}
invalid_uint_dtypes = {"1.25.1 and below": ()}
invalid_complex_dtypes = {"1.25.1 and below": ()}


# leave these untouched
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/backends/numpy/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def broadcast_arrays(*arrays: np.ndarray) -> List[np.ndarray]:
raise ivy.utils.exceptions.IvyBroadcastShapeError(e)


@with_unsupported_dtypes({"1.25.0 and below": ("complex",)}, backend_version)
@with_unsupported_dtypes({"1.25.1 and below": ("complex",)}, backend_version)
def broadcast_to(
x: np.ndarray,
/,
Expand Down Expand Up @@ -216,7 +216,7 @@ def as_ivy_dtype(
)


@with_unsupported_dtypes({"1.25.0 and below": ("bfloat16",)}, backend_version)
@with_unsupported_dtypes({"1.25.1 and below": ("bfloat16",)}, backend_version)
def as_native_dtype(dtype_in: Union[np.dtype, str, bool, int, float], /) -> np.dtype:
if dtype_in is int:
return ivy.default_int_dtype(as_native=True)
Expand Down
Loading

0 comments on commit 6451643

Please sign in to comment.