GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: add a casting option 'same_value' and use it in np.astype by mattip · Pull Request #29129 · numpy/numpy

Conversation

@mattip

@mattip mattip commented Jun 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Users have asked for an option to raise an error when casting overflows. This is hard to do with the NEP-50 based casting, which only offers blanket rules for casting one dtype to another, without taking the actual values into account. This PR adds a new same_value argument to the casting kwarg (implemented in PyArray_CastingConverter), and extends the casting loop functions to raise a ValueError if same_value casting and the value would be changed by the cast. So far this is only implemented for ndarray.astype, i.e. np.array([1000]).astype(np.int8, casting='same_value') will now raise an error.

Performance: since the PR touches the assignment deep in the inner loop, I checked early on that it does not impact performance when not using same_value casting. The loop pseudo code now looks like this, and when compiled with -O3 (via a gcc pragma specific to the loop), it seems the compiler is smart enough to make the if condition not impact performance. seems there is a small performance impact for some casting functions

int same_value_casting = <check some condition>
type2 dst_value; type1 src_value;
while (<condition>) {
    <setup dst_value, src_value>
    dst_value = (cast)src_value;
    if (same_value_casting) {
        <do some checking>;
    }
    <iterate>;
}

Protection: This PR only changes ndarray.astype. Future PRs may implement more. I tried to guard the places that use PyArray_CastingConverter to raise an error if 'same_value' is passed in:

  • array_datetime_as_string (by disallowing 'same_value' in can_cast_datetime64_units)
  • array_copyto
  • array_concatenate (by disallowing 'same_value' in PyArray_ConcatenateInto)
  • array_einsum (by disallowing 'same_value' in PyArray_EinsteinSum)
  • ufunc_generic_fastcall

'same_value' is allowed in

  • array_astype (the whole goal of this PR)
  • array_can_cast_safely
  • npyiter_init (I am pretty sure that is OK?)
  • NpyIter_NestedIters (I am pretty sure that is OK?)

Testing: I added tests for ndarray.astype() covering all combinations of built-in types, and also some tests for properly blocking casting='same_value'.

TODO:

  • disallow astype(..., casting='same_value') with datetime64 or user-defined dtypes?
  • rerun benchmarks against main

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the 01 - Enhancement label Jun 5, 2025
seberg
seberg reviewed Jun 5, 2025
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It is pretty cool that we can do it! That said, I would prefer some more tinkering about the approach.

Basically, I think making it work everywhere is not nearly as hard as it may seem and so I think we have to at least try.

I think the only good way to do this is that the new cast level(s) are reported by the loops (via resolve_descriptors) and integrated into PyArray_MinCastSafety. By doing so, we will achieve two things:

  1. Things should just work everywhere as long as you also set the context.flag correctly everywhere (should also not be too much).
  2. I am very sure that currently things are broken for datatypes that don't implement this flag yet. And unfortunately, that is not great. If a user does casting="same_value" a "safe" cast is fine but we need to reject "unsafe" and "same_kind" casts.

So, I think we really need something for 2, and honestly think the easiest way to solve it is this same path that gives us full support everywhere.
(And yes, I don't love having two "same_value" levels, but I doubt it can be helped.)

(All other things are just nitpicks at this time.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/datetime.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/multiarray/datetime_strings.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/include/numpy/dtype_api.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/include/numpy/dtype_api.h Outdated
#define _NPY_METH_static_data 10

/*
* Constants for same_value casting

@seberg seberg Jun 5, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This would be constant for special array method return values, I think (i.e. not just applicable to casts).

Is it really worth to just keep this inside the inner-loop? Yes, you need to grab the GIL there, but on the plus side, you could actually consider reporting the error.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mattip mattip Jun 6, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I thought the point of having a return value from the inner loop function is so we can use it to report errors. In general I prefer a code style that tries as much as possible to separate programming metaphors: keep the python c-api calls separate from the "pure" C functions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Jun 6, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I can be convinced to add this. But it is public API and we call these loops from a lot of places so we need machinery to make sure that all of these places give the same errors.

That may be a great refactor! E.g. we could have a single function that does HandleArrayMethodError("name", method_result, method_flags).
But I guess without such a refactor it feels very bolted on to me, because whether or not we check for this return depends on where we call/expect it.

EDIT: I.e. basically, wherever we have PyUFunc_GiveFloatinpointErrors() we would also pass the actual return value and do the needed logic there.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mattip mattip Jul 27, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I will try to do this as a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/array_assign_array.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/umath/ufunc_object.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/multiarray/array_assign_scalar.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/multiarray/array_assign_array.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@seberg seberg added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jun 5, 2025
jorenham
jorenham reviewed Jun 5, 2025
View reviewed changes

@jorenham jorenham left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

could you add this option to

numpy/numpy/__init__.pyi

Line 969 in c6eed9a

_CastingKind: TypeAlias = L["no", "equiv", "safe", "same_kind", "unsafe"]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mattip reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@mattip

mattip commented Jun 5, 2025

Copy link
Copy Markdown
Member Author

It seems I touched something in the float -> complex astype. Running

$ spin bench --compare -t bench_ufunc.NDArrayAsType.time_astype

gives me

Change Before [d52bccb] After [23dda18] Ratio Benchmark (Parameter)
+ 2.60±0.04μs 6.45±0.05μs 2.48 bench_ufunc.NDArrayAsType.time_astype(('float32', 'complex64'))
+ 4.08±0.01μs 9.21±0.3μs 2.26 bench_ufunc.NDArrayAsType.time_astype(('float64', 'complex128'))
+ 3.99±0.04μs 8.62±0.04μs 2.16 bench_ufunc.NDArrayAsType.time_astype(('float32', 'complex128'))
+ 15.8±0.4μs 17.8±0.2μs 1.13 bench_ufunc.NDArrayAsType.time_astype(('float16', 'float32'))
+ 16.0±0.1μs 18.0±0.3μs 1.13 bench_ufunc.NDArrayAsType.time_astype(('float16', 'int64'))
+ 2.14±0μs 2.39±0.03μs 1.11 bench_ufunc.NDArrayAsType.time_astype(('int32', 'float32'))
+ 18.0±0.2μs 19.9±0.2μs 1.1 bench_ufunc.NDArrayAsType.time_astype(('float16', 'complex128'))
- 2.20±0.03μs 1.98±0.04μs 0.9 bench_ufunc.NDArrayAsType.time_astype(('int16', 'int32'))
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Jun 6, 2025

Copy link
Copy Markdown
Member

It seems I touched something in the float -> complex astype.

Maybe the compiler just didn't decide to lift the branch for some reason? I have to say that complex -> real casts, wouldn't be my biggest concern, there is this weird ComplexWarning on it (in some branches at least), for a reason...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip mattip mentioned this pull request Jun 6, 2025
BUG: Integer overflow warning applies to scalars but not arrays #8987
Open
@mattip

mattip commented Jun 15, 2025

Copy link
Copy Markdown
Member Author

The linting failures are from #29197 and PR to fix them in #29210.

The OpenSUSE failure seems to be due to network failures downloading a package.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mattip and others added 10 commits July 21, 2025 11:55
@mattip
start implementing same_value casting
cb8ea97
Matti Picus @mattip
work through more places that check 'cast', add a TODO
3859a73
@mattip
add a test, percolate casting closer to inner loops
9dc63a5
@mattip
use SAME_VALUE_CAST flag for one inner loop variant
7cad6af
@mattip
aligned test of same_value passes. Need more tests
03e9ceb
@mattip
handle unaligned casting with 'same_value'
93b8bce
@mattip
extend tests to use source-is-complex
87e2487
@mattip
fix more interfaces to pass casting around, disallow using 'same_valu…
9aa1e5a
…e' in raw_array_assign_scalar and raw_array_wheremasked_assign_scalar
@mattip
raise in places that have a kwarg casting, besides np.astype
953714e
@mattip
refactor based on review comments
cd3e144
@mattip
mattip force-pushed the value-based branch from 58b66f1 to cd3e144 Compare July 21, 2025 01:59
@mattip
CHAR_MAX,MIN -> SCHAR_MAX,MIN
6d6b045
@mattip

mattip commented Jul 21, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Somehow this caused test_nditer.py::test_iter_copy_casts to fail for [e-f], [e-d], [e-F], [e-D], [e-g], [F-e], [D-e], [G-e], but only on the debug build, which would suggest I touched something in the half casting.

Edit: it was due to the debug build exposing a missing initialization of context.flags

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip
copy context flags
1293657
@mattip

mattip commented Jul 22, 2025

Copy link
Copy Markdown
Member Author

CI is passing

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Jul 22, 2025

Copy link
Copy Markdown
Member Author

Hmm. I wonder what we should do with casting float-to-int or float-to-smaller-float? I think the second example should raise, but what about the third?

>>> np.array([1.0, 2.0, 100.0]).astype('int64', casting='same_value')
array([  1,   2, 100])
>>> np.array([1.2, 2.45, 100.0]).astype('int64', casting='same_value')
array([  1,   2, 100])

>>> a = np.array([1.2, 2.45, 3.14156])
>>> a == a.astype(np.half, casting='same_value')
[False, False, False]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Jul 23, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I had a list of 5-6 different possible definitions, but for now skipping it. I am tempted to go with your a == a.astype(new_dtype, casting='same_value') must be true definition (one caveat below).
(This implies that a.astype(new_dtype, casting='same_value').astype(a.dtype) round-trips, but is stronger due to == indicating that the two dtype must be promotable int he context of comparing.)

Unless we go all out and say that a same_value cast must also be a same_kind one and disallow float to integer casts entirely here, the float must clearly be an integer.
That allows the use-case of wanting to use a float array as an integer one safely. If this is not desired, the best solution may be an "is_integral" check for the dtype.

  • One additional subtlety: The float value 2e60 lacks the mantissa to exactly represent an integral value. Even though a == a.astype(new_dtype, casting='same_value') is true one could consider it unsafe from this perspective.

It may actually be worth bringing it up on the mailing list and meeting and sketching at least the main alternatives. (I don't usually expect much input, but sketching the alternatives tends to clarify things a bit either way.)

I'll try to look over the recent changes here soon, but ping me here or on slack if I forget!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Jul 23, 2025

Copy link
Copy Markdown
Member Author

I think it makes sense to do the most conservative thing and require "ability to accurately round-trip" for 'same_value' in this first implementation.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip
add 'same_value' to typing stubs
d151c91
@seberg

seberg commented Jul 24, 2025

Copy link
Copy Markdown
Member

I think it makes sense to do the most conservative thing and require "ability to accurately round-trip" for 'same_value' in this first implementation.

I suppose we have at least a brief window where we could still broaden the definition slightly.
There is one use-case where a different definition might be nice, and that is if we were to consider moving towards this cast mode as a default one output parameters/assignments:

arr += something
arr[...] = other_array

That is currently same-kind I think. In that context I think it is best to forgive all float inaccuracies (including over and underflows).

But, I don't want to get lost too much in this. Unless we are sure that we are only interested in one of these two use-cases for floats (or float to int), it seems fine to just focus on one now and add the other if we want to later.

(I suppose I wouldn't mind asking around if there is a preference about the insufficient mantissa for float -> int casts, but it is a detail. -- but more of a sanity check because I don't have a strong gut feeling.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mattip added 2 commits July 25, 2025 05:39
@mattip
document new feature
6254ae5
@mattip
test, check exact float->int casting: refactor same_value check into …
6922b35
…a function
@mattip

mattip commented Sep 8, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There is still a slowdown, even when disabling the same_value check and dispatching. Playing with a version of the code in the compiler explorer, I think the NPY_GCC_OPT_3 attribute is insufficient to efficiently inline things to get rid of function calls. I need to decorate the half conversion utilities as well, and also inline the To BitCast(const From &from)

I also noticed the benchmarks are using itertools.combinations() and not itertools.product, so we are only benchmarking half the casting type combinations.

Edit: we cannot inline things like the float16 conversion routines, since they come from libnpymath.a which we link to.

mhvk reacted with confused emoji
All reactions
  • 😕 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Sep 9, 2025

Copy link
Copy Markdown
Member Author

Even if I replace lowlevel_strided_loops.c.src with the one from main, I still see a slowdown. But if I also remove the flags field from PyArrayMethod_Context, I get no slowdown. Could the size of that struct be influencing runtime?

mhvk reacted with confused emoji
All reactions
  • 😕 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mattip added 2 commits September 14, 2025 11:39
@mattip
inline inner casting loop and float16 -> float64 calls
3b5d330
@mattip
typo
659ae4f
@mattip
mattip force-pushed the value-based branch from 57f2b2c to 659ae4f Compare September 14, 2025 09:21
@github-actions

This comment has been minimized.

Sign in to view
2 similar comments
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@mattip
fixes for AVX512 and debug builds, inline npy_halfbits_to_doublebits …
7d72ca1
…only on specific platforms
@mattip
mattip force-pushed the value-based branch from 3c4f12e to 7d72ca1 Compare September 14, 2025 15:58
@github-actions

This comment has been minimized.

Sign in to view
@mattip
use optimize instead of inline
31ee1aa
@mattip

mattip commented Sep 14, 2025

Copy link
Copy Markdown
Member Author

Running benchmarks locally on a Ryzen AMD machine (the AsType benchmarks now run all possible pairings of int/float/complex dtypes) there is still some slowdowns and some speedups. I think this is acceptable?

| Change   | Before [73f8bd0f] <test_casting~1>   | After [31ee1aa4] <value-based>   |   Ratio | Benchmark (Parameter)                                            |
|----------|--------------------------------------|----------------------------------|---------|------------------------------------------------------------------|
| +        | 18.6±0.2μs                           | 21.0±0.2μs                       |    1.12 | bench_ufunc.NDArrayAsType.time_astype(('float16', 'complex64'))  |
| +        | 2.02±0.01μs                          | 2.24±0.01μs                      |    1.11 | bench_ufunc.NDArrayAsType.time_astype(('int16', 'int32'))        |
| +        | 4.47±0.04μs                          | 4.76±0.05μs                      |    1.07 | bench_ufunc.NDArrayAsType.time_astype(('complex128', 'float64')) |
| +        | 3.05±0.05μs                          | 3.24±0.05μs                      |    1.06 | bench_ufunc.NDArrayAsType.time_astype(('complex64', 'float32'))  |
| -        | 2.64±0.05μs                          | 2.51±0.05μs                      |    0.95 | bench_ufunc.NDArrayAsType.time_astype(('int64', 'int16'))        |
| -        | 24.5±0.5μs                           | 22.7±0.3μs                       |    0.93 | bench_ufunc.NDArrayAsType.time_astype(('complex128', 'float16')) |
| -        | 16.2±0.2μs                           | 15.0±0.2μs                       |    0.93 | bench_ufunc.NDArrayAsType.time_astype(('float16', 'float64'))    |
| -        | 8.06±0.07μs                          | 7.19±0.04μs                      |    0.89 | bench_ufunc.NDArrayAsType.time_astype(('complex128', 'int64'))   |
| -        | 18.7±0.1μs                           | 16.7±0.1μs                       |    0.89 | bench_ufunc.NDArrayAsType.time_astype(('float16', 'int64'))      |
| -        | 18.7±0.2μs                           | 16.3±0.3μs                       |    0.88 | bench_ufunc.NDArrayAsType.time_astype(('float16', 'int32'))      |
| -        | 18.9±0.2μs                           | 16.2±0.2μs                       |    0.86 | bench_ufunc.NDArrayAsType.time_astype(('float16', 'int16'))      |
| -        | 2.54±0.01μs                          | 2.13±0.02μs                      |    0.84 | bench_ufunc.NDArrayAsType.time_astype(('int32', 'float32'))      |

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Sep 14, 2025

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

spark (https://github.com/apache/spark)
- python/pyspark/ml/functions.py:244: note:     def [_ScalarT: generic[Any]] vstack(tup: Sequence[_SupportsArray[dtype[_ScalarT]] | _NestedSequence[_SupportsArray[dtype[_ScalarT]]]], *, dtype: None = ..., casting: Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
+ python/pyspark/ml/functions.py:244: note:     def [_ScalarT: generic[Any]] vstack(tup: Sequence[_SupportsArray[dtype[_ScalarT]] | _NestedSequence[_SupportsArray[dtype[_ScalarT]]]], *, dtype: None = ..., casting: Literal['no', 'equiv', 'safe', 'same_kind', 'same_value', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
- python/pyspark/ml/functions.py:244: note:     def [_ScalarT: generic[Any]] vstack(tup: Sequence[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]], *, dtype: type[_ScalarT] | dtype[_ScalarT] | _SupportsDType[dtype[_ScalarT]], casting: Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
+ python/pyspark/ml/functions.py:244: note:     def [_ScalarT: generic[Any]] vstack(tup: Sequence[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]], *, dtype: type[_ScalarT] | dtype[_ScalarT] | _SupportsDType[dtype[_ScalarT]], casting: Literal['no', 'equiv', 'safe', 'same_kind', 'same_value', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
- python/pyspark/ml/functions.py:244: note:     def vstack(tup: Sequence[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]], *, dtype: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None = ..., casting: Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[Any]]
+ python/pyspark/ml/functions.py:244: note:     def vstack(tup: Sequence[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]], *, dtype: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None = ..., casting: Literal['no', 'equiv', 'safe', 'same_kind', 'same_value', 'unsafe'] = ...) -> ndarray[tuple[Any, ...], dtype[Any]]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Sep 15, 2025

Copy link
Copy Markdown
Member Author

Extending the benchmarks for all combinations of dtypes in astype: the benchmarks ran in 3m41sec instead of approximately 3m30secs. I think the extra 10 secs is worth the more extensive benchmarking.

mhvk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Sep 17, 2025

Copy link
Copy Markdown
Member Author

Any more thoughts here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg
seberg approved these changes Sep 17, 2025
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think I am happy with it, and don't think there were many new things. I just scrolled through, but may not have looked at the most recent changes very closely, but I think they were all re-organizations.

Unless @mhvk/someone speaks up very soon about concerns, please go ahead and merge when you are happy @mattip (and then open a follow up issue).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/lowlevel_strided_loops.c.src
#else
return (double)(ToDoubleBits(h));
#endif
}

@seberg seberg Sep 17, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I wonder if that can't be in the header then, but maybe it doens't matter (and I guess that header may be public), but not sure.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhvk

mhvk commented Sep 17, 2025

Copy link
Copy Markdown
Contributor

Don't really have the time for another review, but I was already happy, and you addressed all my more immediate comments. Please do raise follow-up issues about things not done, such as the various casting flags and how they are used and passed on. (@seberg might this also be relevant for how we are trying to implement sorting flags -- a general mechanism to pass on flags may be useful... but maybe I'm missing the boat completely here, don't really have the time to look properly now...)

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Sep 17, 2025

Copy link
Copy Markdown
Member

a general mechanism to pass on flags may be useful... but maybe I'm missing the boat completely here, don't really have the time to look properly now

I think it is almost the same! But here, it seems right -- or at least OK -- to apply this flag to (almost) all ArrayMethods in the future (even ufuncs in principle, that right now are allowed to absorb casts even if there is no such ufunc inside or outside of NumPy, I am sure).

While for sorts it could look basically the same, but the flag and flag space must be specific to the sort gufunc, I think.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg
seberg merged commit abb8dac into numpy:main Sep 17, 2025
@seberg seberg mentioned this pull request Sep 17, 2025
Followups for same-value casting #29765
Open
@mattip mattip mentioned this pull request Sep 18, 2025
DOC: add another mention of 'same_value' #29767
Merged
bwhitt7 pushed a commit to bwhitt7/numpy that referenced this pull request Sep 23, 2025
@mattip Matti Picus
@seberg @bwhitt7
ENH: add a casting option 'same_value' and use it in np.astype (numpy…
35564bf
…#29129)

* start implementing same_value casting

* work through more places that check 'cast', add a TODO

* add a test, percolate casting closer to inner loops

* use SAME_VALUE_CAST flag for one inner loop variant

* aligned test of same_value passes. Need more tests

* handle unaligned casting with 'same_value'

* extend tests to use source-is-complex

* fix more interfaces to pass casting around, disallow using 'same_value' in raw_array_assign_scalar and raw_array_wheremasked_assign_scalar

* raise in places that have a kwarg casting, besides np.astype

* refactor based on review comments

* CHAR_MAX,MIN -> SCHAR_MAX,MIN

* copy context flags

* add 'same_value' to typing stubs

* document new feature

* test, check exact float->int casting: refactor same_value check into a function

* enable astype same_value casting for scalars

* typo

* fix ptr-to-src_value -> value casting errors

* fix linting and docs, ignore warning better

* gcc warning is different

* fixes from review, typos

* fix compile warning ignore and make filter in tests more specific, disallow non-numeric 'same_value'

* fix warning filters

* emit PyErr inside the loop

* macOS can emit FPEs when touching NAN

* Fix can-cast logic everywhere for same-value casts (only allow numeric)

* reorder and simplify, from review

* revert last commit and remove redundant checks

* gate and document SAME_VALUE_CASTING for v2.4

* make SAME_VALUE a flag, not an enum

* fixes from review

* fixes from review

* inline inner casting loop and float16 -> float64 calls

* typo

* fixes for AVX512 and debug builds, inline npy_halfbits_to_doublebits only on specific platforms

* use optimize instead of inline

---------

Co-authored-by: Matti Picus <matti.picus#@gmail.com>
Co-authored-by: Sebastian Berg <[email protected]>
@ngoldbaum ngoldbaum mentioned this pull request Sep 24, 2025
BUG: unused function warnings building NumPy #29805
Closed
@mattip mattip mentioned this pull request Sep 25, 2025
BLD: refactor to avoid 'unused function' warnings' #29812
Merged
@seiko2plus seiko2plus mentioned this pull request Oct 1, 2025
New float16 failures in 32-bit Windows wheel build jobs #29808
Closed
@mattip mattip mentioned this pull request Oct 15, 2025
ENH: Add casting option to astype that would raise in case of overflow #25396
Closed
ssam18 added a commit to ssam18/numpy that referenced this pull request Nov 12, 2025
@ssam18
ENH: Extend same_value casting to np.can_cast and improve documentation
d6e1520
This commit implements follow-up work for same-value casting (PR numpy#29129):

1. **Enhanced Documentation**:
   - Added comprehensive documentation for NPY_SAME_VALUE_CONTEXT_FLAG
   - Documented the flag's purpose, usage, and relationship to casting system
   - Added examples and cross-references to related functionality

2. **Extended np.can_cast Support**:
   - Modified can_cast to use PyArray_CastingConverterSameValue instead of PyArray_CastingConverter
   - Updated can_cast docstring to include 'same_value' as valid casting option
   - Added examples demonstrating same_value casting usage
   - Added version note for same_value support

3. **Updated Tests**:
   - Modified test_conversion_utils.py to expect same_value support
   - Added comprehensive test suite for same_value casting in can_cast
   - Created test cases for identity casts, widening casts, and edge cases
   - Added parameter validation tests

4. **Implementation Details**:
   - Changed multiarraymodule.c to use PyArray_CastingConverterSameValue for can_cast
   - This enables same_value casting support without breaking existing functionality
   - Type hints already supported same_value through _CastingKind

This addresses the main items from issue numpy#29765 regarding extending
same_value casting beyond ndarray.astype() to other NumPy operations.

Fixes: numpy#29765
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
@mattip Matti Picus
@seberg @IndifferentArea
ENH: add a casting option 'same_value' and use it in np.astype (numpy…
ffbeb27
…#29129)

* start implementing same_value casting

* work through more places that check 'cast', add a TODO

* add a test, percolate casting closer to inner loops

* use SAME_VALUE_CAST flag for one inner loop variant

* aligned test of same_value passes. Need more tests

* handle unaligned casting with 'same_value'

* extend tests to use source-is-complex

* fix more interfaces to pass casting around, disallow using 'same_value' in raw_array_assign_scalar and raw_array_wheremasked_assign_scalar

* raise in places that have a kwarg casting, besides np.astype

* refactor based on review comments

* CHAR_MAX,MIN -> SCHAR_MAX,MIN

* copy context flags

* add 'same_value' to typing stubs

* document new feature

* test, check exact float->int casting: refactor same_value check into a function

* enable astype same_value casting for scalars

* typo

* fix ptr-to-src_value -> value casting errors

* fix linting and docs, ignore warning better

* gcc warning is different

* fixes from review, typos

* fix compile warning ignore and make filter in tests more specific, disallow non-numeric 'same_value'

* fix warning filters

* emit PyErr inside the loop

* macOS can emit FPEs when touching NAN

* Fix can-cast logic everywhere for same-value casts (only allow numeric)

* reorder and simplify, from review

* revert last commit and remove redundant checks

* gate and document SAME_VALUE_CASTING for v2.4

* make SAME_VALUE a flag, not an enum

* fixes from review

* fixes from review

* inline inner casting loop and float16 -> float64 calls

* typo

* fixes for AVX512 and debug builds, inline npy_halfbits_to_doublebits only on specific platforms

* use optimize instead of inline

---------

Co-authored-by: Matti Picus <matti.picus#@gmail.com>
Co-authored-by: Sebastian Berg <[email protected]>
@claude claude Bot mentioned this pull request Aug 11, 2026
fix(comfyui): 動画生成でホストが応答しなくなるのを直します ncaq/dotfiles#1483
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@mhvk mhvk mhvk left review comments
@jorenham jorenham jorenham left review comments
@seberg seberg seberg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

01 - Enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@mattip @seberg @jorenham @mhvk
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.