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

Scatter ops for webworker #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/backends/cpu/op-resolve-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {CpuPad} from './ops/pad';
import {CpuAveragePool, CpuGlobalAveragePool, CpuGlobalMaxPool, CpuMaxPool} from './ops/pool';
import * as cpuReduce from './ops/reduce';
import {CpuReshape} from './ops/reshape';
import {CpuScatter} from './ops/scatter';
import {CpuSlice, CpuSliceV10} from './ops/slice';
import {CpuSoftmax} from './ops/softmax';
import {CpuSqueeze} from './ops/squeeze';
Expand Down Expand Up @@ -104,4 +105,5 @@ export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['Unsqueeze', '', '1+', () => new CpuUnsqueeze()],
['Upsample', '', '7-8', () => new CpuUpsample()],
['Xor', '', '7+', () => new CpuBinaryOp(['bool'], (e1, e2) => (e1 ^ e2))],
['Scatter', '', '7+', () => new CpuScatter()],
];
29 changes: 29 additions & 0 deletions lib/backends/cpu/ops/scatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Scatter} from '../../../ops/scatter';
import {Tensor} from '../../../tensor';
import {CpuInferenceHandler} from '../inference-handler';

export class CpuScatter extends Scatter {
run(inferenceHandler: CpuInferenceHandler, inputs: Tensor[]): Tensor[]|Promise<Tensor[]> {
const output = scatter(inputs[0], inputs[1], inputs[2]);
return [output];
}
}

export function scatter(data: Tensor, indices: Tensor, updates: Tensor): Tensor {
const datadims = data.dims;
const indicedims = indices.dims;
const updatedims = updates.dims;
const datanew = new Tensor(datadims, data.type);
const Y = datanew.data;
const X = updates.data;
let flatIndex = 0;
let updateFlatIndex = 0;
for (let i = 0; i < datadims[0]; ++i) {
for (let j = 0; j < indicedims[1]; ++j) {
flatIndex = i * datadims[1] + (indices.data[j] as number);
updateFlatIndex = i * updatedims[1] + j;
Y[flatIndex] = X[updateFlatIndex];
}
}
return new Tensor(datadims, data.type, undefined, undefined, Y);
}
39 changes: 39 additions & 0 deletions lib/ops/scatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import {Attribute} from '../attribute';
import {InferenceHandler} from '../backend';
import {Operator} from '../operators';
import {Tensor} from '../tensor';

export abstract class Scatter implements Operator {
// Inputs are {data_tensor->float32/float64, indices_tensor-> int32, update_data_tensor->float32/float64}
abstract run(inferenceHandler: InferenceHandler, inputs: Tensor[]): Tensor[]|Promise<Tensor[]>;

initialize(attributes: Attribute): void {}

checkInputs(inputs: Tensor[]): boolean {
if (!inputs || inputs.length !== 3) {
return false;
}
const tensorRank = inputs[0].dims.length;
if (tensorRank < 1) {
return false;
}

return this.checkInputTypes(inputs);
}

protected checkInputTypes(inputs: Tensor[]): boolean {
if (inputs[0].type !== 'float32' && inputs[0].type !== 'float64') {
return false;
}
if (inputs[1].type !== 'int32' && inputs[1].type !== 'int16') {
return false;
}
if (inputs[2].type !== 'float32' && inputs[2].type !== 'float64') {
return false;
}
return true;
}
}
1 change: 1 addition & 0 deletions test/test-suite-whitelist.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"test_flatten_default_axis",
"test_gather_0",
"test_gather_1",
"test_scatter_0",
Copy link
Contributor

Choose a reason for hiding this comment

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

"test_gemm_broadcast",
"test_gemm_nobroadcast",
"test_globalaveragepool_precomputed",
Expand Down