Skip to content

Conversation

Saransh-cpp
Copy link
Member

@Saransh-cpp Saransh-cpp commented Nov 14, 2024

WIP (not draft to test array API in the CI)

Has some caveats that I am working on, but opening this PR for visibility.

Closes: #405

@Saransh-cpp Saransh-cpp self-assigned this Nov 14, 2024
@paddyroddy paddyroddy added the array-api Work is related to the Array API label Nov 14, 2024
@ntessore
Copy link
Collaborator

Perhaps, before merging any changes, we should figure out a way to

  1. get the typing done in a backend-agnostic way (see Array API: Typing #415),
  2. figure out a way to test different backends, depending on what's available on the machine running the tests (maybe scipy has cracked this particular nut).

msg = "input arrays should belong to the same array library"
raise ValueError(msg)

xp = a.__array_namespace__()
Copy link
Member

Choose a reason for hiding this comment

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

Was talking to @matt-graham and he wondered if we should be using array-api-compat here. That should solve the cupy issue. I believe only NumPy and JAX are stable at this point.

Copy link
Member Author

Choose a reason for hiding this comment

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

The CuPy support will become official in their next major release (v14) where they will start supporting NumPy v2. I hope they push the release in the next 3 months, so that we don't have to experiment with array-api-compat.

@Saransh-cpp Saransh-cpp changed the base branch from main to saransh/array-api-test November 26, 2024 15:21
@Saransh-cpp Saransh-cpp force-pushed the saransh/array-api-core branch 2 times, most recently from 6e8ef45 to 761795c Compare November 26, 2024 15:22
@Saransh-cpp Saransh-cpp marked this pull request as ready for review November 26, 2024 15:23
@Saransh-cpp Saransh-cpp force-pushed the saransh/array-api-test branch from fa75b7a to 1b0d92b Compare November 28, 2024 15:48
Base automatically changed from saransh/array-api-test to main January 23, 2025 10:14
@paddyroddy paddyroddy marked this pull request as draft January 23, 2025 10:15
@paddyroddy paddyroddy added the blocked The issue or pull request is blocked by something label Jan 23, 2025
@paddyroddy paddyroddy marked this pull request as ready for review February 19, 2025 16:15
@paddyroddy paddyroddy marked this pull request as draft February 19, 2025 16:15
@Saransh-cpp Saransh-cpp force-pushed the saransh/array-api-core branch from ee54338 to 536f1ab Compare April 2, 2025 10:36
@Saransh-cpp Saransh-cpp force-pushed the saransh/array-api-core branch from 536f1ab to 038bbbf Compare April 2, 2025 10:38
Copy link
Member Author

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Took some time to revive this PR 😅

def nnls(
a: NDArray[np.float64],
b: NDArray[np.float64],
a: NDArray[np.float64] | ArrayLike,
Copy link
Member Author

Choose a reason for hiding this comment

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

See #415

Comment on lines 20 to +21
a = rng.uniform(low=-10, high=10, size=[50, 10])
b = np.abs(rng.uniform(low=-2, high=2, size=[10]))
b = xp.abs(rng.uniform(low=-2, high=2, size=[10]))
Copy link
Member Author

Choose a reason for hiding this comment

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

The functions might not use RNGs but their tests might, so we do need to come up with a consensus on RNGs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess for tests we can always do -

xp.asarray(rng(...))  # numpy rng output casted to xp.array

Copy link
Member Author

Choose a reason for hiding this comment

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

Use the workaround (NumPy-like - rng-jax) for now, and see if we need a more detailed (JAX-like) solution in the future.

@Saransh-cpp Saransh-cpp removed the blocked The issue or pull request is blocked by something label Apr 2, 2025
Comment on lines 82 to 92
p[m] = True
while True:
ap = a[:, p]
xp = x[p]
sp = np.linalg.solve(ap.T @ ap, b @ ap)
x_new = x[p]
sp = xp.linalg.solve(ap.T @ ap, b @ ap)
t = sp <= 0
if not np.any(t):
if not xp.any(t):
break
alpha = -np.min(xp[t] / (xp[t] - sp[t]))
alpha = -xp.min(x_new[t] / (x_new[t] - sp[t]))
x[p] += alpha * (sp - xp)
p[x <= 0] = False
Copy link
Member Author

Choose a reason for hiding this comment

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

@Saransh-cpp Saransh-cpp added the blocked The issue or pull request is blocked by something label Apr 4, 2025
Comment on lines +51 to +55
if a.__array_namespace__() != b.__array_namespace__():
msg = "input arrays should belong to the same array library"
raise ValueError(msg)

xp = a.__array_namespace__()
Copy link
Member

Choose a reason for hiding this comment

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

Should we make this into a generic helper? Seeing as all functions will need it

Copy link
Member Author

Choose a reason for hiding this comment

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

That sounds good. I'll open a new issue and take this up.

@connoraird
Copy link
Contributor

@paddyroddy I think I've now done this in #643

@paddyroddy
Copy link
Member

@paddyroddy I think I've now done this in #643

Agreed, sorry @Saransh-cpp

@paddyroddy paddyroddy closed this Oct 15, 2025
@paddyroddy paddyroddy deleted the saransh/array-api-core branch October 15, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array-api Work is related to the Array API blocked The issue or pull request is blocked by something

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array API: port straightforward functions in core.algorithm

4 participants