Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check overlapping indices of scatterElements #122

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/lib/validate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,26 @@ export function validateScatterElementsParams(input, indices, updates, {axis = 0
`The axis ${axis} should be an unsigned integer in the interval [0, ${inputRank}).`);
}
const axisSize = input.shape[axis];
const updatedLocationDict = {};
for (let i = 0; i < sizeOfShape(indices.shape); ++i) {
const index = indices.getValueByIndex(i);
let index = indices.getValueByIndex(i);
if (!Number.isInteger(index) || index < -axisSize || index >= axisSize) {
throw new Error(`Invalid indices value - it should be an integer in the interval ` +
`[${-axisSize}, ${axisSize - 1}]`);
}
const indicesLocation = indices.locationFromIndex(i);
const originOutputLocation =
[...indicesLocation.slice(0, axis), index, ...indicesLocation.slice(axis + 1)];
index = index < 0 ? index + input.shape[axis] : index;
const outputLocation =
[...indicesLocation.slice(0, axis), index, ...indicesLocation.slice(axis + 1)];
const locationString = outputLocation.toString();
if (Object.hasOwn(updatedLocationDict, locationString)) {
BruceDai marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Invalid indices, [${originOutputLocation}] and ` +
`[${updatedLocationDict[locationString]}] point to the same output location.`);
} else {
updatedLocationDict[locationString] = originOutputLocation;
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/scatter_elements_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,26 @@ describe('test scatterElements', function() {
};
testScatterElements(input, indices, updates, expected, {axis: 1});
});

it('scatterElements throws error with overlapping indices', function() {
const input = {
shape: [1, 5],
data: [1, 2, 3, 4, 5],
};
const indices = {
shape: [1, 2],
data: [3, -2],
};
const updates = {
shape: [1, 2],
data: [1.1, 2.1],
};
const expected = {
shape: [1, 5],
data: [1, 2, 3, 2.1, 5],
};
utils.expectThrowError(() => {
testScatterElements(input, indices, updates, expected, {axis: 1});
}, 'Invalid indices, [0,-2] and [0,3] point to the same output location.');
});
});
5 changes: 5 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = chai.assert;
const expect = chai.expect;

/**
* Get bitwise of the given value.
Expand Down Expand Up @@ -40,6 +41,10 @@ assert.isAlmostEqualUlp = function(a, b, nulp, message) {
}
};

export function expectThrowError(errorFunc, message) {
expect(errorFunc).to.throw(message);
}

export function checkValue(tensor, expected, nulp = 0) {
assert.isTrue(tensor.size === expected.length);
for (let i = 0; i < expected.length; ++i) {
Expand Down
Loading