Repository navigation
Bug: changes to suppress deprecation warning on float of 1-element array - #210
Merged
Merged
Conversation
mrava87
marked this pull request as draft
August 28, 2026 20:26
mrava87
marked this pull request as ready for review
August 29, 2026 13:18
Contributor
Author
|
@rohanbabbar04 do you mind reviewing this 😄 Note that:
|
rohanbabbar04
approved these changes
Aug 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NumPy
ndim > 0to-scalar deprecation: root cause and fixThe warning
Triggered by any caller doing
float(...)on the result ofDistributedArray.norm(), becausenorm()never actually returned a scalar.Root cause
DistributedArray.norm()documented afloatreturn but returned a shape-(1,)array:norm(axis=None)flattens the local array and calls_compute_vector_norm(..., axis=0, ord=ord)._allreduce_subcomm.mpi_allreduce(pylops_mpi/utils/_mpi.py:104) always allocatesrecv_buf = ncp.zeros(send_buf.size, dtype=...)— a 1-D buffer. For a scalarreduction
send_buf.size == 1, so the result isarray([...])withndim == 1, size == 1.float()on such an array is the deprecated implicit array-to-scalar conversion (an error in a future NumPy).DistributedArray.dot()had the identical issue: it returned the raw size-1 allreduce buffer.The fix
New module-level helper in
pylops_mpi/DistributedArray.py:.item()works for numpy and cupy arrays, and thehasattrguard covers the non-buffered-MPI paths that already return aplain scalar.
floatin most cases, but for examplea complex
vdotstayscomplex.Applied to:
DistributedArray.norm(axis=Nonebranch)float; docstring notes scalar vs array peraxisDistributedArray.dotfloat/complex; docstring return type fixedStackedDistributedArray.normfloat; return annotationbool | float->floatnorm(axis=...)is unchanged and still returns an array.StackedDistributedArray.normhad to change beyond the return value: it aggregated the per-array norms withncp.hstack(...), and since those are now host scalars,cupy.hstackon them would raise. It now aggregates with numpy (correct, because the inputs are host scalars).Call sites updated
floathas no.item(), so every.norm().item()/.dot(...).item()chain had to be simplified.pylops_mpi/optimization/cls_basic.py— 10 spots across CG/CGLS. Notea = float((self.kold / cOpc).item())->float(self.kold / cOpc)would haveraised
AttributeErrorotherwise (both operands are plain scalars now).pylops_mpi/optimization/cls_sparsity.py— 6 spots in ISTA/FISTA(
xupdate,costdata,costreg).pylops_mpi/optimization/eigs.py:86—b_k.dot(b1_k, vdot=True).item()->b_k.dot(b1_k, vdot=True).tests_nccl/test_distributedarray_nccl.py,tests_nccl/test_stackedarray_nccl.py— the GPU tests called
.get()ondot()/ axis-lessnorm()results, whichwould now be
AttributeErroron a Python scalar. Removed on the 3dotand 9normassertions; kept onnorm(axis=...), which still returns a cupy array.Behaviour change to be aware of (cupy)
norm(axis=None)anddot()now return host scalars instead of device size-1 arrays, so they force a device-to-host sync. Nearly every call site already did.item()(same sync); the rest only divide aDistributedArrayby the result, which is fine with a host scalar.