Fix np.mean with list/tuple containing ArrayBox objects#757
Open
tylerflex wants to merge 1 commit intoHIPS:masterfrom
Open
Fix np.mean with list/tuple containing ArrayBox objects#757tylerflex wants to merge 1 commit intoHIPS:masterfrom
np.mean with list/tuple containing ArrayBox objects#757tylerflex wants to merge 1 commit intoHIPS:masterfrom
Conversation
When np.mean() is called with a list containing ArrayBox objects (e.g., np.mean([x, x+2])), numpy's internal implementation calls float() on the result, which fails for ArrayBox. This fix adds a wrapper for mean() that converts list/tuple inputs to arrays first, avoiding the problematic code path in numpy's _mean. Fixes the error: TypeError: float() argument must be a string or a real number, not 'ArrayBox' Co-Authored-By: Claude Opus 4.5 <[email protected]>
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.
Summary
Fix
autograd.numpy.mean()failing when passed a list/tuple containingArrayBoxobjects.Previously raised:
TypeError: float() argument must be a string or a real number, not 'ArrayBox'Root cause
When
np.mean()receives a list containingArrayBoxobjects,numpy's internal_meanimplementation callsret.dtype.type(result)to cast the output.Since
ArrayBox.dtypereturnsfloat64, this becomesnp.float64(ArrayBox), which callsfloat()on theArrayBoxand fails.Fix
meanwrapper that convertslist/tupleinputs to arrays using autograd'sarray()function before calling the primitive_primitive_mean(the underlying primitive)**kwargstograd_np_meanto accept optional arguments likedtyperan tests and linting with
nox