(args: {
inputs: SparseToDenseInputs,
backend: MathBackendWebGL,
attrs: SparseToDenseAttrs
})
| 24 | import {reshape} from './Reshape'; |
| 25 | |
| 26 | export function sparseToDense(args: { |
| 27 | inputs: SparseToDenseInputs, |
| 28 | backend: MathBackendWebGL, |
| 29 | attrs: SparseToDenseAttrs |
| 30 | }): TensorInfo { |
| 31 | const {inputs, backend, attrs} = args; |
| 32 | const {sparseIndices, sparseValues, defaultValue} = inputs; |
| 33 | const {outputShape} = attrs; |
| 34 | |
| 35 | const {sliceRank, numUpdates, sliceSize, strides, outputSize} = |
| 36 | backend_util.calculateShapes(sparseValues, sparseIndices, outputShape); |
| 37 | const sumDupeIndices = false; |
| 38 | |
| 39 | if (sparseValues.dtype === 'string') { |
| 40 | const indicesBuf = backend.bufferSync<Rank, 'int32'>(sparseIndices); |
| 41 | const updatesBuf = backend.bufferSync<Rank, 'string'>(sparseValues); |
| 42 | const $defaultValue = util.decodeString( |
| 43 | backend.readSync(defaultValue.dataId)[0] as Uint8Array); |
| 44 | const outBuf = scatterImplCPU( |
| 45 | indicesBuf, updatesBuf, outputShape, outputSize, sliceSize, numUpdates, |
| 46 | sliceRank, strides, $defaultValue, sumDupeIndices); |
| 47 | return backend.makeTensorInfo(outputShape, outBuf.dtype, outBuf.values); |
| 48 | } |
| 49 | const program = new ScatterProgram( |
| 50 | numUpdates, sliceRank, sparseIndices.shape.length, |
| 51 | sparseValues.shape.length, strides, [outputSize, 1], sumDupeIndices); |
| 52 | |
| 53 | const res = backend.runWebGLProgram( |
| 54 | program, [sparseValues, sparseIndices, defaultValue], sparseValues.dtype); |
| 55 | |
| 56 | const reshaped = |
| 57 | reshape({inputs: {x: res}, backend, attrs: {shape: outputShape}}); |
| 58 | |
| 59 | backend.disposeIntermediateTensorInfo(res); |
| 60 | return reshaped; |
| 61 | } |
| 62 | |
| 63 | export const sparseToDenseConfig: KernelConfig = { |
| 64 | kernelName: SparseToDense, |
nothing calls this directly
no test coverage detected
searching dependent graphs…