5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
110 lines
3.3 KiB
Python
110 lines
3.3 KiB
Python
# Copyright (c) ONNX Project Contributors
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import numpy as np
|
|
|
|
import onnx
|
|
from onnx import helper
|
|
from onnx.backend.test.case.base import Base
|
|
from onnx.backend.test.case.node import expect
|
|
|
|
|
|
# The below Scatter's numpy implementation is from https://stackoverflow.com/a/46204790/11767360
|
|
def scatter(
|
|
data: np.ndarray, indices: np.ndarray, updates: np.ndarray, axis: int = 0
|
|
) -> np.ndarray:
|
|
if axis < 0:
|
|
axis = data.ndim + axis
|
|
|
|
idx_xsection_shape = indices.shape[:axis] + indices.shape[axis + 1 :]
|
|
|
|
def make_slice(arr: np.ndarray, axis: int, i: int) -> list[slice | int]:
|
|
slc: list[slice | int] = [slice(None)] * arr.ndim
|
|
slc[axis] = i
|
|
return slc
|
|
|
|
def unpack(packed: Any) -> Any:
|
|
unpacked = packed[0]
|
|
for i in range(1, len(packed)):
|
|
unpacked = unpacked, packed[i]
|
|
return unpacked
|
|
|
|
# We use indices and axis parameters to create idx
|
|
# idx is in a form that can be used as a NumPy advanced indices for scattering of updates param. in data
|
|
idx = [
|
|
[
|
|
unpack(np.indices(idx_xsection_shape).reshape(indices.ndim - 1, -1)),
|
|
indices[tuple(make_slice(indices, axis, i))].reshape(1, -1)[0],
|
|
]
|
|
for i in range(indices.shape[axis])
|
|
]
|
|
idx = list(np.concatenate(idx, axis=1))
|
|
idx.insert(axis, idx.pop())
|
|
|
|
# updates_idx is a NumPy advanced indices for indexing of elements in the updates
|
|
updates_idx = list(idx)
|
|
updates_idx.pop(axis)
|
|
updates_idx.insert(
|
|
axis, np.repeat(np.arange(indices.shape[axis]), np.prod(idx_xsection_shape))
|
|
)
|
|
|
|
scattered = np.copy(data)
|
|
scattered[tuple(idx)] = updates[tuple(updates_idx)]
|
|
return scattered
|
|
|
|
|
|
class Scatter(Base):
|
|
@staticmethod
|
|
def export_scatter_without_axis() -> None:
|
|
node = onnx.helper.make_node(
|
|
"Scatter",
|
|
inputs=["data", "indices", "updates"],
|
|
outputs=["y"],
|
|
)
|
|
data = np.zeros((3, 3), dtype=np.float32)
|
|
indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)
|
|
updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
|
|
|
|
y = scatter(data, indices, updates)
|
|
# print(y) produces
|
|
# [[2.0, 1.1, 0.0],
|
|
# [1.0, 0.0, 2.2],
|
|
# [0.0, 2.1, 1.2]]
|
|
|
|
expect(
|
|
node,
|
|
inputs=[data, indices, updates],
|
|
outputs=[y],
|
|
name="test_scatter_without_axis",
|
|
opset_imports=[helper.make_opsetid("", 10)],
|
|
)
|
|
|
|
@staticmethod
|
|
def export_scatter_with_axis() -> None:
|
|
axis = 1
|
|
node = onnx.helper.make_node(
|
|
"Scatter",
|
|
inputs=["data", "indices", "updates"],
|
|
outputs=["y"],
|
|
axis=axis,
|
|
)
|
|
data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
|
|
indices = np.array([[1, 3]], dtype=np.int64)
|
|
updates = np.array([[1.1, 2.1]], dtype=np.float32)
|
|
|
|
y = scatter(data, indices, updates, axis=axis)
|
|
# print(y) produces
|
|
# [[1.0, 1.1, 3.0, 2.1, 5.0]]
|
|
|
|
expect(
|
|
node,
|
|
inputs=[data, indices, updates],
|
|
outputs=[y],
|
|
name="test_scatter_with_axis",
|
|
opset_imports=[helper.make_opsetid("", 10)],
|
|
)
|