Skip to content

Commit e3a9b5e

Browse files
authored
Avoid negating unsigned integers in dpnp.resize (#2508)
This PR resolves an issue when calling `dpnp.resize` with unsigned integer size. The negation of an unsigned int underflows and creates a large positive repeats, which leads to a runtime error. The PR fixes the used ceil division.
1 parent 7d2380d commit e3a9b5e

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131

3232
* Updated `pre-commit` GitHub workflow to pass `no-commit-to-branch` check [#2501](https://github.com/IntelPython/dpnp/pull/2501)
3333
* Updated the math formulas in summary of `dpnp.matvec` and `dpnp.vecmat` to correct a typo [#2503](https://github.com/IntelPython/dpnp/pull/2503)
34+
* Avoided negating unsigned integers in ceil division used in `dpnp.resize` implementation [#2508](https://github.com/IntelPython/dpnp/pull/2508)
3435

3536
### Security
3637

dpnp/dpnp_iface_manipulation.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,9 +3129,9 @@ def resize(a, new_shape):
31293129
Returns
31303130
-------
31313131
out : dpnp.ndarray
3132-
The new array is formed from the data in the old array, repeated
3133-
if necessary to fill out the required number of elements. The
3134-
data are repeated iterating over the array in C-order.
3132+
The new array is formed from the data in the old array, repeated if
3133+
necessary to fill out the required number of elements. The data are
3134+
repeated iterating over the array in C-order.
31353135
31363136
See Also
31373137
--------
@@ -3146,8 +3146,10 @@ def resize(a, new_shape):
31463146
be used. In most other cases either indexing (to reduce the size) or
31473147
padding (to increase the size) may be a more appropriate solution.
31483148
3149-
Warning: This functionality does **not** consider axes separately,
3150-
i.e. it does not apply interpolation/extrapolation.
3149+
Warning
3150+
-------
3151+
This functionality does **not** consider axes separately, i.e. it does not
3152+
apply interpolation/extrapolation.
31513153
It fills the return array with the required number of elements, iterating
31523154
over `a` in C-order, disregarding axes (and cycling back from the start if
31533155
the new shape is larger). This functionality is therefore not suitable to
@@ -3187,7 +3189,8 @@ def resize(a, new_shape):
31873189
# First case must zero fill. The second would have repeats == 0.
31883190
return dpnp.zeros_like(a, shape=new_shape)
31893191

3190-
repeats = -(-new_size // a_size) # ceil division
3192+
# ceiling division without negating new_size
3193+
repeats = (new_size + a_size - 1) // a_size
31913194
a = dpnp.concatenate((dpnp.ravel(a),) * repeats)[:new_size]
31923195

31933196
return a.reshape(new_shape)

dpnp/tests/test_manipulation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,16 @@ def test_negative_resize(self, xp):
12381238
with pytest.raises(ValueError, match=r"negative"):
12391239
xp.resize(a, new_shape=new_shape)
12401240

1241+
@testing.with_requires("numpy>=2.3.1")
1242+
@pytest.mark.parametrize("dt", [dpnp.uint32, dpnp.uint64])
1243+
def test_unsigned_resize(self, dt):
1244+
a = numpy.array([[23, 95], [66, 37]])
1245+
ia = dpnp.array(a)
1246+
1247+
result = dpnp.resize(ia, dt(1))
1248+
expected = numpy.resize(a, dt(1))
1249+
assert_array_equal(result, expected)
1250+
12411251

12421252
class TestRot90:
12431253
@pytest.mark.parametrize("xp", [numpy, dpnp])

0 commit comments

Comments
 (0)