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

Method constructor

tfjs-backend-webgl/src/scatter_gpu.ts:26–77  ·  view source on GitHub ↗
(
      updateSize: number, sliceDim: number, indicesRank: number,
      updatesRank: number, strides: number[], shape: number[],
      summingDupeIndex = true, defaultIsTensor = false)

Source from the content-addressed store, hash-verified

24 userCode: string;
25
26 constructor(
27 updateSize: number, sliceDim: number, indicesRank: number,
28 updatesRank: number, strides: number[], shape: number[],
29 summingDupeIndex = true, defaultIsTensor = false) {
30 this.outputShape = shape;
31 const stridesType = getCoordsDataType(strides.length);
32 const dtype = getCoordsDataType(shape.length);
33 let indicesString = '';
34 if (indicesRank === 1) {
35 indicesString = 'i';
36 } else if (indicesRank === 2) {
37 indicesString = 'i, j';
38 }
39 const indicesSnippet = `getIndices(${indicesString})`;
40
41 let updatesString = '';
42 if (updatesRank === 1) {
43 updatesString = 'i';
44 } else if (updatesRank === 2) {
45 updatesString = 'i, coords[1]';
46 }
47 const updatesSnippet = `getUpdates(${updatesString})`;
48
49 let defaultValuesString = '';
50 if (defaultIsTensor) {
51 defaultValuesString = 'coords[0], coords[1]';
52 }
53 const defaultValueSnippet = `getDefaultValue(${defaultValuesString})`;
54
55 const strideString = sliceDim > 1 ? 'strides[j]' : 'strides';
56 this.userCode = `
57 ${stridesType} strides = ${stridesType}(${strides});
58
59 void main() {
60 ${dtype} coords = getOutputCoords();
61 float sum = 0.0;
62 bool found = false;
63 for (int i = 0; i < ${updateSize}; i++) {
64 int flattenedIndex = 0;
65 for (int j = 0; j < ${sliceDim}; j++) {
66 int index = round(${indicesSnippet});
67 flattenedIndex += index * ${strideString};
68 }
69 if (flattenedIndex == coords[0]) {
70 sum += ${updatesSnippet};
71 found = true;
72 }
73 }
74 setOutput(mix(${defaultValueSnippet}, sum, float(found)));
75 }
76 `;
77 }
78}

Callers

nothing calls this directly

Calls 1

getCoordsDataTypeFunction · 0.90

Tested by

no test coverage detected