(args: {
inputs: UnsortedSegmentSumInputs,
backend: WebGPUBackend,
attrs: UnsortedSegmentSumAttrs
})
| 25 | import {transpose} from './Transpose'; |
| 26 | |
| 27 | export function unsortedSegmentSum(args: { |
| 28 | inputs: UnsortedSegmentSumInputs, |
| 29 | backend: WebGPUBackend, |
| 30 | attrs: UnsortedSegmentSumAttrs |
| 31 | }): TensorInfo { |
| 32 | const {inputs, backend, attrs} = args; |
| 33 | const {x, segmentIds} = inputs; |
| 34 | const {numSegments} = attrs; |
| 35 | |
| 36 | const xRank = x.shape.length; |
| 37 | |
| 38 | const toDispose = []; |
| 39 | |
| 40 | let axis = 0; |
| 41 | const permutation = backend_util.getAxesPermutation([axis], xRank); |
| 42 | let permutedX = x; |
| 43 | if (permutation != null) { |
| 44 | permutedX = transpose({inputs: {x}, backend, attrs: {perm: permutation}}); |
| 45 | toDispose.push(permutedX); |
| 46 | axis = backend_util.getInnerMostAxes(1, xRank)[0]; |
| 47 | } |
| 48 | |
| 49 | const outShape = backend_util.segment_util.computeOutShape( |
| 50 | permutedX.shape, axis, numSegments); |
| 51 | const inSize = util.sizeFromShape([permutedX.shape[axis]]); |
| 52 | const a2D = |
| 53 | reshape({inputs: {x: permutedX}, backend, attrs: {shape: [-1, inSize]}}); |
| 54 | toDispose.push(a2D); |
| 55 | |
| 56 | const dtype = x.dtype; |
| 57 | const shape = [a2D.shape[0], numSegments]; |
| 58 | const output = fill({backend, attrs: {shape, value: 0, dtype}}); |
| 59 | const program = new UnsortedSegmentSumProgram(a2D.shape, shape, dtype); |
| 60 | const uniformData = [ |
| 61 | {type: 'int32', data: [numSegments]}, |
| 62 | {type: 'int32', data: [util.sizeFromShape(a2D.shape)]} |
| 63 | ]; |
| 64 | const segResult = backend.runWebGPUProgram( |
| 65 | program, [a2D, segmentIds], dtype, uniformData, output); |
| 66 | |
| 67 | const reshaped = |
| 68 | reshape({inputs: {x: segResult}, backend, attrs: {shape: outShape}}); |
| 69 | toDispose.push(segResult); |
| 70 | let result = reshaped; |
| 71 | if (permutation != null) { |
| 72 | toDispose.push(reshaped); |
| 73 | const perm = backend_util.getUndoAxesPermutation(permutation); |
| 74 | result = transpose({inputs: {x: result}, backend, attrs: {perm}}); |
| 75 | } |
| 76 | |
| 77 | toDispose.forEach(t => backend.disposeData(t.dataId)); |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | export const unsortedSegmentSumConfig: KernelConfig = { |
| 82 | kernelName: UnsortedSegmentSum, |
nothing calls this directly
no test coverage detected
searching dependent graphs…