Skip to content

Commit

Permalink
support negative indices
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceDai committed Dec 18, 2024
1 parent 22aa524 commit fe29596
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/gather_nd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function gatherND(input, indices) {
const indicesLocation = indices.locationFromIndex(indicesIndex);
const indicesArray = [];
for (let i = 0; i < lastIndicesSize; i++) {
indicesArray.push(indices.getValueByIndex(indicesIndex + i));
const indicesValue = indices.getValueByIndex(indicesIndex + i);
indicesArray.push(indicesValue >= 0 ? indicesValue : inputShape[i] + indicesValue);
}
for (let tmpIndex = 0; tmpIndex < tmpTotal; ++tmpIndex) {
const tmpLocation = tmp.locationFromIndex(tmpIndex);
Expand Down
23 changes: 23 additions & 0 deletions test/gather_nd_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ describe('test gatherND', function() {
testGatherND(input, indices, expected);
});

it('gatherND 2D inputs when 3D negative indices has leading dimensions', function() {
const input = {
shape: [2, 2],
data: [
1, 2,
3, 4,
],
};
const indices = {
shape: [2, 1, 1],
data: [-1, -2],
};
const expected = {
shape: [2, 1, 2],
data: [
3, 4,
1, 2,
],
};
testGatherND(input, indices, expected);
});

it('gatherND 3D inputs by 2D indices', function() {
// Refer to Example 3 on https://onnx.ai/onnx/operators/onnx__GatherND.html
const input = {
Expand Down Expand Up @@ -113,6 +135,7 @@ describe('test gatherND', function() {
testGatherND(input, indices, expected);
});


it('gatherND 3D inputs by 3D indices', function() {
// Refer to Example 4 on https://onnx.ai/onnx/operators/onnx__GatherND.html
const input = {
Expand Down

0 comments on commit fe29596

Please sign in to comment.