Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

webgl: fix 'Gather' with 0-rank indices #246

Merged
merged 1 commit into from
Dec 21, 2020
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
9 changes: 3 additions & 6 deletions lib/backends/webgl/ops/gather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export class WebGLGather extends Gather implements WebGLOperator {
const indexDataShape = inputs[1].dims.slice();
const outputShape = new Array(inputShape.length + indexDataShape.length - 1);

if (outputShape.length === 0) {
throw Error('A scalar tensor output has not been supported');
}

const axis = ShapeUtil.normalizeAxis(this.axis, inputShape.length);
const indexCopyOps: string[] = [];
for (let i = 0; i < outputShape.length; i++) {
Expand All @@ -42,13 +38,14 @@ export class WebGLGather extends Gather implements WebGLOperator {
}
}

const orank = outputShape.length;
const orank = outputShape.length || 1;
const irank = inputShape.length;
const iDrank = indexDataShape.length;
const iDrank = indexDataShape.length || 1;
const shaderSource = `
float process(int outputIdx[${orank}]) {
int inputIdx[${irank}];
int indexDataIdx[${iDrank}];
indexDataIdx[0] = 0;
${indexCopyOps.join('\n ')}
int idx = int(_B(indexDataIdx));
inputIdx[${axis}] = idx < 0 ? idx + ${inputShape[axis]} : idx;
Expand Down
97 changes: 97 additions & 0 deletions test/data/ops/gather.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
[
{
"name": "Gather",
"operator": "Gather",
"attributes": [],
"cases": [
{
"name": "data[4] indices[]",
"inputs": [
{
"data": [1, 2, 3, 4],
"dims": [4],
"type": "float32"
},
{
"data": [1],
"dims": [],
"type": "int32"
}
],
"outputs": [
{
"data": [2],
"dims": [],
"type": "float32"
}
]
},
{
"name": "data[4] indices[1]",
"inputs": [
{
"data": [1, 2, 3, 4],
"dims": [4],
"type": "float32"
},
{
"data": [1],
"dims": [1],
"type": "int32"
}
],
"outputs": [
{
"data": [2],
"dims": [1],
"type": "float32"
}
]
},
{
"name": "data[2,4] indices[]",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
"dims": [2, 4],
"type": "float32"
},
{
"data": [1],
"dims": [],
"type": "int32"
}
],
"outputs": [
{
"data": [5, 6, 7, 8],
"dims": [4],
"type": "float32"
}
]
},
{
"name": "data[2,4] indices[1]",
"inputs": [
{
"data": [1, 2, 3, 4, 5, 6, 7, 8],
"dims": [2, 4],
"type": "float32"
},
{
"data": [1],
"dims": [1],
"type": "int32"
}
],
"outputs": [
{
"data": [5, 6, 7, 8],
"dims": [1, 4],
"type": "float32"
}
]
}
]
}
]