Consider dropping negative indices support in gather*/scatter* operators

Aperta
#970 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Ambito
api

Direzione di ricerca

This issue is a proposal to change spec semantics, not a coded task. Locate the gather/scatter operator definitions in the WebNN spec source (e.g. index.bs) and review indexing rules. Align text to clamp out-of-range indices. The proposal is unresolved, so confirm with maintainers before starting.

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

Descrizione

Summary

The spec currently allows negative indices (index < 0 means index + size) for gather, gatherElements, gatherND, scatterElements, and scatterND. Implementing this turns out to require a workaround in most backends, so I propose relaxing this to "out-of-range indices (including negative ones) are clamped into [0, size)" instead.

Backend workarounds for negative indices support
  • TFLite: indices are clamped to [-N, N-1] and negative values are shifted to their non-negative equivalent, either computed in C++ at graph-build time for constant indices, or via a runtime subgraph (Less + Add + Select) for non-constant indices (gather, gatherND, scatterND). gatherElements/scatterElements currently only support constant indices.
  • CoreML: gather/gatherElements/gatherND are implemented with clamp and where(index < 0, index + dim, index), mirroring the TFLite approach. scatterElements and scatterND currently pass indices straight through with TODOs to handle negative/out-of-bounds indices.
  • ONNX Runtime: According to the ONNX spec ([1],[2],[3],[4],[5]) ORT should natively support negative indices, so we only insert a clamp before the indices. However, from the test results, we've observed that not all execution providers actually support this. For example, OV EP produces wrong results for scatterND/gatherND with negative indices, there is a CL under review to work around this.
Proposal

For gather, gatherElements, gatherND, scatterElements, and scatterND, an index outside [0, size), whether negative or too large, is simply clamped into range, rather than negative indices being interpreted as "offset from the end." This would let every backend only need to insert a clamp for out-of-range handling across implementations, removing both implementation complexity and some per-inference overhead.

@huningxin @reillyeon @RafaelCintron @fdwr @Honry

Lingua principale
Bikeshed
Stelle
566
Fork
72
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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 webmachinelearning/webnn

Tutte le issue di webmachinelearning/webnn

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.