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

Abierto
#970 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Refactorización
Claridad
Bastante claro
Estado de actividad
Activo
Área
api

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Bikeshed
Estrellas
566
Forks
72
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de webmachinelearning/webnn

Todos los issues de webmachinelearning/webnn

Issues similares

Más issues de Backend & API Design

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.