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

implement gather #61

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 60 additions & 0 deletions src/gather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

import {Tensor, sizeOfShape} from './lib/tensor.js';
import {validateGatherParams} from './lib/validate-input.js';

/**
* Gather values of the input tensor along an axis according to the indices.
* @param {Tensor} input
* @param {Tensor} indices
* @param {MLGatherOptions} [options]
* @return {Tensor}
*/
export function gather(input, indices, {axis = 0} = {}) {
validateGatherParams(...arguments);
const shapeInput = input.shape;

// set outputShape following Spec Algorithm
huningxin marked this conversation as resolved.
Show resolved Hide resolved
// 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 JavaScipt 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) {
// 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);
output.setValueByIndex(outputIndex, inputValue);
}

return output;
}
20 changes: 20 additions & 0 deletions src/lib/validate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,23 @@ export function validateNotParams(input) {
}
}
}

export function validateGatherParams(input, indices, {axis = 0} = {}) {
if (axis !== undefined) {
const rank = input.rank;
if (!Number.isInteger(axis) || axis < 0) {
throw new Error(`The axis ${axis} should be an unsigned integer.`);
}
if (axis >= rank) {
throw new Error(`The axis ${axis} should be in the interval [0, ${rank}).`);
}
}
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current WebNN DirectML backend allows negative index value. @fdwr

Copy link

@fdwr fdwr Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.w3.org/TR/webnn/#api-mlgraphbuilder-gather

Appears the spec settled on not supporting negative indices, 8. If index is greater than or equal to axisSize, then throw, and that is tracked via webmachinelearning/webnn#484.

I will look through the models I have for any negative indices to see if this will actually matter in practice...

[update] I need some better tools, as this is way too tedious... 🤔⏳

throw new Error(
`Invalid indices value - it should be an integer in the interval [0, ${axisSize})`);
}
}
}
Loading