Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion src/lib/validate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,25 @@ 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 updatedLocationSet = new Set();
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 (updatedLocationSet.has(locationString)) {
throw new Error(`Invalid indices, [${originOutputLocation}] is not unique.`);
} else {
updatedLocationSet.add(locationString);
}
}
}

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] is not unique.');
});
});
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