MCPcopy Create free account
hub / github.com/tensorflow/tfjs / sparseToDense

Function sparseToDense

tfjs-backend-webgl/src/kernels/SparseToDense.ts:26–61  ·  view source on GitHub ↗
(args: {
  inputs: SparseToDenseInputs,
  backend: MathBackendWebGL,
  attrs: SparseToDenseAttrs
})

Source from the content-addressed store, hash-verified

24import {reshape} from './Reshape';
25
26export 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
63export const sparseToDenseConfig: KernelConfig = {
64 kernelName: SparseToDense,

Callers

nothing calls this directly

Calls 6

reshapeFunction · 0.90
runWebGLProgramMethod · 0.80
readSyncMethod · 0.65
bufferSyncMethod · 0.45
makeTensorInfoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…