|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {backend_util, KernelConfig, KernelFunc, ScatterNd, ScatterNdAttrs, ScatterNdInputs, TensorInfo} from '@tensorflow/tfjs-core'; |
| 19 | + |
| 20 | +import {WebGPUBackend} from '../backend_webgpu'; |
| 21 | + |
| 22 | +import {reshape} from './Reshape'; |
| 23 | +import {ScatterProgram} from './scatter_webgpu'; |
| 24 | + |
| 25 | +export function scatterNd(args: { |
| 26 | + inputs: ScatterNdInputs, |
| 27 | + backend: WebGPUBackend, |
| 28 | + attrs: ScatterNdAttrs |
| 29 | +}): TensorInfo { |
| 30 | + const {inputs, backend, attrs} = args; |
| 31 | + const {indices, updates} = inputs; |
| 32 | + const {shape} = attrs; |
| 33 | + |
| 34 | + const {sliceRank, numUpdates, sliceSize, strides, outputSize} = |
| 35 | + backend_util.calculateShapes(updates, indices, shape); |
| 36 | + |
| 37 | + const flattenShape = [outputSize / sliceSize, sliceSize]; |
| 38 | + |
| 39 | + if (outputSize === 0) { |
| 40 | + return backend.makeTensorInfo(shape, indices.dtype); |
| 41 | + } |
| 42 | + |
| 43 | + const flattenIndices = reshape( |
| 44 | + {inputs: {x: indices}, backend, attrs: {shape: [numUpdates, sliceRank]}}); |
| 45 | + const flattenX = reshape( |
| 46 | + {inputs: {x: updates}, backend, attrs: {shape: [numUpdates, sliceSize]}}); |
| 47 | + |
| 48 | + const defaultValue = backend.makeTensorInfo( |
| 49 | + [], 'float32', new Float32Array([0])); // scalar(0) |
| 50 | + const uniformData = [ |
| 51 | + {type: 'int32', data: [numUpdates]}, |
| 52 | + {type: 'int32', data: [sliceRank]}, |
| 53 | + {type: 'int32', data: strides}, |
| 54 | + ]; |
| 55 | + const program = new ScatterProgram( |
| 56 | + numUpdates, sliceRank, flattenIndices.shape.length, flattenX.shape.length, |
| 57 | + strides, flattenShape); |
| 58 | + const res = backend.runWebGPUProgram( |
| 59 | + program, [flattenX, flattenIndices, defaultValue], flattenX.dtype, |
| 60 | + uniformData); |
| 61 | + |
| 62 | + const reshaped = reshape({inputs: {x: res}, backend, attrs: {shape}}); |
| 63 | + |
| 64 | + backend.disposeData(flattenIndices.dataId); |
| 65 | + backend.disposeData(flattenX.dataId); |
| 66 | + backend.disposeData(res.dataId); |
| 67 | + backend.disposeData(defaultValue.dataId); |
| 68 | + |
| 69 | + return reshaped; |
| 70 | +} |
| 71 | + |
| 72 | +export const scatterNdConfig: KernelConfig = { |
| 73 | + kernelName: ScatterNd, |
| 74 | + backendName: 'webgpu', |
| 75 | + kernelFunc: scatterNd as {} as KernelFunc |
| 76 | +}; |
0 commit comments