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

Support negative indices of gather op #104

Merged
merged 2 commits into from
Oct 8, 2024
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
48 changes: 11 additions & 37 deletions src/gather.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,21 @@ import {validateGatherParams} from './lib/validate-input.js';
*/
export function gather(input, indices, {axis = 0} = {}) {
validateGatherParams(...arguments);
const shapeInput = input.shape;
const inputShape = input.shape;
const outputShape = inputShape.slice(0, axis).concat(indices.shape, inputShape.slice(axis + 1));
const output = new Tensor(outputShape);

// set outputShape following Spec Algorithm
// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-gather
//
// let dimCount = 0
// let rankOutput = 0;
// let shapeOutput = [];
// for (dimCount = 0; dimCount < shapeInput.length; dimCount++) {
// if (dimCount === axis) {
// break;
// } else {
// shapeOutput[dimCount] = shapeInput[dimCount];
// }
// }
// rankOutput = dimCount;
// for (dimCount = 0; dimCount < indices.shape.length; dimCount++) {
// shapeOutput[rankOutput + dimCount] = indices.shape[dimCount];
// }
// rankOutput = rankOutput + dimCount;
// for (dimCount = 0; dimCount < shapeInput.length; dimCount++) {
// if (dimCount <= axis) {
// continue;
// } else {
// shapeOutput[rankOutput + dimCount - axis - 1] = shapeInput[dimCount];
// }
// }

// optimized set outputShape using JavaScript slice and concat
const shapeOutput = shapeInput.slice(0, axis).concat(indices.shape, shapeInput.slice(axis + 1));
const output = new Tensor(shapeOutput);

for (let outputIndex = 0; outputIndex < sizeOfShape(shapeOutput); ++outputIndex) {
for (let outputIndex = 0; outputIndex < sizeOfShape(outputShape); ++outputIndex) {
// output[i, j, k, ...] = input[indices[i, j, k, ...], j, k, ...] // if axis == 0
// output[i, j, k, ...] = input[i, indices[i, j, k, ...], k, ...] // if axis == 1
// output[i, j, k, ...] = input[i, j, indices[i, j, k, ...], ...] // if axis == 2
const outputLoc = output.locationFromIndex(outputIndex);
const indicesLoc = outputLoc.slice(axis, axis + indices.rank);
const selectedInputLoc = outputLoc.slice(0, axis)
.concat(indices.getValueByLocation(indicesLoc), outputLoc.slice(axis + indices.rank));
const inputValue = input.getValueByLocation(selectedInputLoc);
const outputLocation = output.locationFromIndex(outputIndex);
const indicesLocation = outputLocation.slice(axis, axis + indices.rank);
let indiceValue = indices.getValueByLocation(indicesLocation);
indiceValue = indiceValue < 0 ? indiceValue + input.shape[axis] : indiceValue;
const selectedInputLocation = [
...outputLocation.slice(0, axis), indiceValue, ...outputLocation.slice(axis + indices.rank)];
const inputValue = input.getValueByLocation(selectedInputLocation);
output.setValueByIndex(outputIndex, inputValue);
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/validate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@ export function validateGatherParams(input, indices, {axis = 0} = {}) {
const axisSize = input.shape[axis];
for (let i = 0; i < sizeOfShape(indices.shape); ++i) {
const index = indices.getValueByIndex(i);
if (!Number.isInteger(index) || index < 0 || index >= axisSize) {
throw new Error(
`Invalid indices value - it should be an integer in the interval [0, ${axisSize})`);
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}]`);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/gather_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ describe('test gather', function() {
testGather(input, indices, expected);
});

it('gather 1D by negative 1D indices default', function() {
const input = {
shape: [10],
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};
const indices = {
shape: [3],
data: [0, -9, -10],
};
const expected = {
shape: [3],
data: [0, 1, 0],
};
testGather(input, indices, expected);
});

it('gather 1D by 0D indices default', function() {
const input = {
shape: [4],
Expand Down
Loading