ENH: allow user-DType promoters that only apply to an explicitly requested output DType

Aperta
#33,208 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
numpy, python
Ambito
backend

Direzione di ricerca

Start in numpy/_core/src/umath/dispatching.cpp at resolve_implementation_info and trace how promoter patterns match when the output DType is unspecified versus explicitly requested. The issue leaves two possible designs open and notes compatibility concerns, so first investigate existing matching behavior and registrations. Done means a chosen approach allows output-keyed user-DType promoters to work for explicit output requests without unexpectedly changing calls that omit an output DType.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

01 - Enhancement
Proposed new feature or change

A user DType cannot register a promoter for "inputs of any DType, output explicitly requested as my DType" (np.add(f64, f64, dtype=MyDType)) without also changing NumPy's own promotion.

In resolve_implementation_info (numpy/_core/src/umath/dispatching.cpp), an unspecified output matches any pattern:

if (given_dtype == NULL) {
    if (i >= nin) {
        /* Unspecified out always matches (see below for inputs) */
        continue;

So a promoter registered for (PyArrayDescr_Type, PyArrayDescr_Type, MyDType) matches every call where no output DType is given. For numpy-quaddtype, registering such a promoter for add makes

np.array([1.0, 2.0], dtype=np.longdouble) + 1.0

return a QuadPrecDType array instead of np.longdouble, as soon as the package is imported. Today the only safe rule is the one from numpy/numpy-quaddtype#76: every promoter needs a user-DType input. As a result, this fails:

np.add(np.array([1.0, 2.0]), np.array([3.0, 4.0]), dtype=QuadPrecDType)
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.Float64DType'>) -> <class 'numpy_quaddtype.QuadPrecDType'>

NumPy's own dtypes support this case, e.g. np.add(f32, f32, dtype=np.float64).

Possible directions:

  1. When the output DType is not given, it does not match a pattern that names a concrete output DType. Only abstract or None outputs match, so output-keyed promoters apply only when the user passed dtype=/signature=.
  2. Let a promoter decline (e.g. return a sentinel), so dispatch continues with the remaining candidates. This is the "trusting promoters to do nothing" idea from numpy/numpy-quaddtype#76.

Option 1 may change matching for existing registrations, so it may need care or opt-in.

Lingua principale
Python
Stelle
33.5k
Fork
13.1k
Merge medio
1g 19h
PR unite (30g)
143

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di numpy/numpy

Tutte le issue di numpy/numpy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.