Skip to content

Conversation

vtavana
Copy link
Collaborator

@vtavana vtavana commented Aug 3, 2025

In this PR, dpnp.piecewise is implemented.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@vtavana vtavana self-assigned this Aug 3, 2025
Copy link
Contributor

github-actions bot commented Aug 3, 2025

View rendered docs @ https://intelpython.github.io/dpnp/pull/2550/index.html

Copy link
Contributor

github-actions bot commented Aug 3, 2025

Array API standard conformance tests for dpnp=0.19.0dev3=py313h509198e_25 ran successfully.
Passed: 1227
Failed: 0
Skipped: 9

@vtavana vtavana added this to the 0.19.0 release milestone Aug 4, 2025
@vtavana vtavana marked this pull request as ready for review August 4, 2025 18:47
@coveralls
Copy link
Collaborator

coveralls commented Aug 5, 2025

Coverage Status

coverage: 72.251% (+0.06%) from 72.19%
when pulling 642f9f8 on impl-piecewise
into d975818 on master.

if isinstance(condlist, dpnp.ndarray) and condlist.ndim in [0, 1]:
condlist = [condlist]
elif dpnp.isscalar(condlist) or (
dpnp.isscalar(condlist[0]) and x.ndim != 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no check that condlist is exactly a list. Do we need that or it is assuming to accept any sequence?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, condlist can be a list, tuple or ndarray in both NumPy and CuPy. And dpnp follows the same practice.

import numpy
a = numpy.linspace(-2.5, 2.5, 6)
numpy.piecewise(a, [a < 0, a >= 0], [-1, 1])  # condlist is list
# array([-1., -1., -1.,  1.,  1.,  1.])

numpy.piecewise(a, (a < 0, a >= 0), [-1, 1])  # condlist is tuple
# array([-1., -1., -1.,  1.,  1.,  1.])

numpy.piecewise(a, numpy.array([a < 0, a >= 0]), [-1, 1])  # condlist is array
# array([-1., -1., -1.,  1.,  1.,  1.])

import cupy
x = cupy.array(a)

cupy.piecewise(x, [x < 0, x >= 0], [-1, 1])  # condlist is list
# array([-1., -1., -1.,  1.,  1.,  1.])

cupy.piecewise(x, (x < 0, x >= 0), [-1, 1])  # condlist is tuple
# array([-1., -1., -1.,  1.,  1.,  1.])

cupy.piecewise(x, cupy.array([x < 0, x >= 0]), [-1, 1])  # condlist is array
# array([-1., -1., -1.,  1.,  1.,  1.])

import dpnp
ia = dpnp.array(a)
dpnp.piecewise(ia, [ia < 0, ia >= 0], [-1, 1])  # condlist is list
# array([-1., -1., -1.,  1.,  1.,  1.])

dpnp.piecewise(ia, (ia < 0, ia >= 0), [-1, 1])  # condlist is tuple
# array([-1., -1., -1.,  1.,  1.,  1.])

dpnp.piecewise(ia, dpnp.array([ia < 0, ia >= 0]), [-1, 1])  # condlist is array
# array([-1., -1., -1.,  1.,  1.,  1.])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean condlist might not be a subscriptable object and so condlist[0] would raise TypeError

@vlad-perevezentsev vlad-perevezentsev self-requested a review August 13, 2025 18:06
Copy link
Collaborator

@vlad-perevezentsev vlad-perevezentsev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more comments from my side
LGTM
Thank you @vtavana

condlen = len(condlist)
try:
if isinstance(funclist, str):
raise TypeError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add exception text

x.shape, condlist, usm_type=x.usm_type, sycl_queue=x.sycl_queue
)
]
elif not isinstance(condlist[0], (dpnp.ndarray)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for usm_ndarray here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants