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

Method getUserCode

tfjs-backend-webgpu/src/binary_op_webgpu.ts:93–145  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

91 }
92
93 getUserCode(): string {
94 let userCode;
95 const dType = this.outputComponent === 4 ? 'vec4<f32>' : 'f32';
96 const opFnStr = `
97 fn binaryOperation(a : ${dType}, b : ${dType}) -> ${dType} {
98 ${getBinaryOpString(this.op, this.outputComponent === 4)}
99 };
100 `;
101
102 if (this.type === 'shared') {
103 const sharedIndexSnippet = this.lastDimensionSize > 1 ?
104 `coords[${this.outputShape.length - 1}]` :
105 '0';
106 const accessDataSnippet = this.useSharedMemoryWithB ?
107 `let a = getAByOutputIndex(index);
108 let b = sharedBuf[${sharedIndexSnippet}];` :
109 `let a = sharedBuf[${sharedIndexSnippet}];
110 let b = getBByOutputIndex(index);`;
111 userCode = `
112 ${opFnStr}
113 var<workgroup> sharedBuf : array<f32, ${this.lastDimensionSize}>;
114 ${main('index')} {
115 // Fill in the shared memory buffer.
116 let localIndex = i32(localId.x);
117 if(localIndex < ${this.lastDimensionSize}) {
118 sharedBuf[localIndex] = f32(${
119 this.useSharedMemoryWithB ? 'B' : 'A'}[localIndex]);
120 }
121 workgroupBarrier();
122
123 if(index < uniforms.size) {
124 let coords = getCoordsFromIndex(index);
125 ${accessDataSnippet}
126 setOutputAtIndex(index, binaryOperation(a, b));
127 }
128 }
129 `;
130 } else {
131 userCode = `
132 ${opFnStr}
133 ${main('index')} {
134 if (index < uniforms.size) {
135 let coords = getCoordsFromIndex(index * ${this.outputComponent});
136 let a = ${dType}(getAByOutputCoords(coords));
137 let b = ${dType}(getBByOutputCoords(coords));
138 setOutputAtIndex(index, binaryOperation(a, b));
139 }
140 }
141 `;
142 }
143
144 return userCode;
145 }
146}

Callers

nothing calls this directly

Calls 2

getBinaryOpStringFunction · 0.90
mainFunction · 0.50

Tested by

no test coverage detected