Skip to content

Commit 4e3670a

Browse files
authored
Merge branch 'master' into dependabot/github_actions/github/codeql-action-3.28.13
2 parents bd64f6b + 5288b62 commit 4e3670a

File tree

6 files changed

+148
-131
lines changed

6 files changed

+148
-131
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
* Allowed input array of `uint64` dtype in `dpnp.bincount` [#2361](https://github.com/IntelPython/dpnp/pull/2361)
2020
* The vector norms `ord={None, 1, 2, inf}` and the matrix norms `ord={None, 1, 2, inf, "fro", "nuc"}` now consistently return zero for empty arrays, which are arrays with at least one axis of size zero. This change affects `dpnp.linalg.norm`, `dpnp.linalg.vector_norm`, and `dpnp.linalg.matrix_norm`. Previously, dpnp would either raise errors or return zero depending on the parameters provided [#2371](https://github.com/IntelPython/dpnp/pull/2371)
2121
* Extended `dpnp.fft.fftfreq` and `dpnp.fft.rfftfreq` functions to support `dtype` keyword per Python Array API spec 2024.12 [#2384](https://github.com/IntelPython/dpnp/pull/2384)
22+
* Updated `dpnp.fix` to return output with the same data-type of input [#2392](https://github.com/IntelPython/dpnp/pull/2392)
2223

2324
### Fixed
2425

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"DPNPI0",
4242
"DPNPAngle",
4343
"DPNPBinaryFunc",
44+
"DPNPFix",
4445
"DPNPImag",
4546
"DPNPReal",
4647
"DPNPRound",
@@ -511,6 +512,48 @@ def __call__(self, x, deg=False, out=None, order="K"):
511512
return res
512513

513514

515+
class DPNPFix(DPNPUnaryFunc):
516+
"""Class that implements dpnp.fix unary element-wise functions."""
517+
518+
def __init__(
519+
self,
520+
name,
521+
result_type_resolver_fn,
522+
unary_dp_impl_fn,
523+
docs,
524+
):
525+
super().__init__(
526+
name,
527+
result_type_resolver_fn,
528+
unary_dp_impl_fn,
529+
docs,
530+
)
531+
532+
def __call__(self, x, out=None, order="K"):
533+
if not dpnp.is_supported_array_type(x):
534+
pass # pass to raise error in main implementation
535+
elif dpnp.issubdtype(x.dtype, dpnp.inexact):
536+
pass # for inexact types, pass to calculate in the backend
537+
elif out is not None and not dpnp.is_supported_array_type(out):
538+
pass # pass to raise error in main implementation
539+
elif out is not None and out.dtype != x.dtype:
540+
# passing will raise an error but with incorrect needed dtype
541+
raise ValueError(
542+
f"Output array of type {x.dtype} is needed, got {out.dtype}"
543+
)
544+
else:
545+
# for exact types, return the input
546+
if out is None:
547+
return dpnp.copy(x, order=order)
548+
549+
if isinstance(out, dpt.usm_ndarray):
550+
out = dpnp_array._create_from_usm_ndarray(out)
551+
out[...] = x
552+
return out
553+
554+
return super().__call__(x, out=out, order=order)
555+
556+
514557
class DPNPI0(DPNPUnaryFunc):
515558
"""Class that implements dpnp.i0 unary element-wise functions."""
516559

dpnp/dpnp_iface_mathematical.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
DPNPI0,
6464
DPNPAngle,
6565
DPNPBinaryFunc,
66+
DPNPFix,
6667
DPNPImag,
6768
DPNPReal,
6869
DPNPRound,
@@ -1728,14 +1729,14 @@ def ediff1d(ary, to_end=None, to_begin=None):
17281729
Round to nearest integer towards zero.
17291730
17301731
Round an array of floats element-wise to nearest integer towards zero.
1731-
The rounded values are returned as floats.
1732+
The rounded values have the same data-type as the input.
17321733
17331734
For full documentation refer to :obj:`numpy.fix`.
17341735
17351736
Parameters
17361737
----------
17371738
x : {dpnp.ndarray, usm_ndarray}
1738-
An array of floats to be rounded.
1739+
Input array, expected to have a real-valued data type.
17391740
out : {None, dpnp.ndarray, usm_ndarray}, optional
17401741
Output array to populate.
17411742
Array must have the correct shape and the expected data type.
@@ -1749,19 +1750,12 @@ def ediff1d(ary, to_end=None, to_begin=None):
17491750
Returns
17501751
-------
17511752
out : dpnp.ndarray
1752-
An array with the rounded values and with the same dimensions as the input.
1753-
The returned array will have the default floating point data type for the
1754-
device where `a` is allocated.
1755-
If `out` is ``None`` then a float array is returned with the rounded values.
1753+
An array with the same dimensions and data-type as the input.
1754+
If `out` is ``None`` then a new array is returned
1755+
with the rounded values.
17561756
Otherwise the result is stored there and the return value `out` is
17571757
a reference to that array.
17581758
1759-
Limitations
1760-
-----------
1761-
Parameters `where` and `subok` are supported with their default values.
1762-
Keyword argument `kwargs` is currently unsupported.
1763-
Otherwise ``NotImplementedError`` exception will be raised.
1764-
17651759
See Also
17661760
--------
17671761
:obj:`dpnp.round` : Round to given number of decimals.
@@ -1782,7 +1776,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
17821776
array([ 2., 2., -2., -2.])
17831777
"""
17841778

1785-
fix = DPNPUnaryFunc(
1779+
fix = DPNPFix(
17861780
"fix",
17871781
ufi._fix_result_type,
17881782
ufi._fix,
@@ -1934,8 +1928,10 @@ def ediff1d(ary, to_end=None, to_begin=None):
19341928
19351929
Notes
19361930
-----
1937-
Some spreadsheet programs calculate the "floor-towards-zero", in other words floor(-2.5) == -2.
1938-
DPNP instead uses the definition of floor where floor(-2.5) == -3.
1931+
Some spreadsheet programs calculate the "floor-towards-zero", where
1932+
``floor(-2.5) == -2``. DPNP instead uses the definition of :obj:`dpnp.floor`
1933+
where ``floor(-2.5) == -3``. The "floor-towards-zero" function is called
1934+
:obj:`dpnp.fix` in DPNP.
19391935
19401936
Examples
19411937
--------

dpnp/dpnp_iface_statistics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ def cov(
797797
Default: ``None``.
798798
dtype : {None, str, dtype object}, optional
799799
Data-type of the result. By default, the return data-type will have
800-
at least floating point type based on the capabilities of the device on
801-
which the input arrays reside.
800+
the default floating point data-type of the device on which the input
801+
arrays reside.
802802
803803
Default: ``None``.
804804

dpnp/tests/test_binary_ufuncs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ def test_float_nan(self, dt):
684684
expected = numpy.nextafter(numpy.nan, a)
685685
assert_equal(result, expected)
686686

687+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
687688
@pytest.mark.parametrize("val", [0x7C00, 0x8000], ids=["val1", "val2"])
688689
def test_f16_strides(self, val):
689690
a = numpy.arange(val, dtype=numpy.uint16).astype(numpy.float16)
@@ -702,6 +703,7 @@ def test_f16_strides(self, val):
702703
expected = numpy.nextafter(a[1:], -hinf)
703704
assert_equal(result, expected)
704705

706+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
705707
@pytest.mark.parametrize("val", [0x7C00, 0x8000], ids=["val1", "val2"])
706708
def test_f16_array_inf(self, val):
707709
a = numpy.arange(val, dtype=numpy.uint16).astype(numpy.float16)
@@ -716,6 +718,7 @@ def test_f16_array_inf(self, val):
716718
expected = numpy.nextafter(-hinf, a)
717719
assert_equal(result, expected)
718720

721+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
719722
@pytest.mark.parametrize(
720723
"sign1, sign2",
721724
[
@@ -734,6 +737,7 @@ def test_f16_inf(self, sign1, sign2):
734737
expected = numpy.nextafter(hinf1, hinf2)
735738
assert_equal(result, expected)
736739

740+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
737741
@pytest.mark.parametrize("val", [0x7C00, 0x8000], ids=["val1", "val2"])
738742
def test_f16_array_nan(self, val):
739743
a = numpy.arange(val, dtype=numpy.uint16).astype(numpy.float16)
@@ -748,6 +752,7 @@ def test_f16_array_nan(self, val):
748752
expected = numpy.nextafter(nan, a)
749753
assert_equal(result, expected)
750754

755+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
751756
@pytest.mark.parametrize(
752757
"val1, val2",
753758
[
@@ -765,6 +770,7 @@ def test_f16_inf_nan(self, val1, val2):
765770
expected = numpy.nextafter(v1, v2)
766771
assert_equal(result, expected)
767772

773+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
768774
@pytest.mark.parametrize(
769775
"val, scalar",
770776
[

0 commit comments

Comments
 (0)