MCPcopy Index your code
hub / github.com/tensorflow/tfjs / createComplexBinaryKernelImpl

Function createComplexBinaryKernelImpl

tfjs-backend-cpu/src/utils/binary_utils.ts:135–194  ·  view source on GitHub ↗
(op: ComplexBinaryOperation)

Source from the content-addressed store, hash-verified

133 * Supports broadcast.
134 */
135export function createComplexBinaryKernelImpl(op: ComplexBinaryOperation):
136 ComplexBinaryKernelImpl {
137 return (aShape: number[], bShape: number[], aRealVals: Float32Array,
138 aImagVals: Float32Array, bRealVals: Float32Array,
139 bImagVals: Float32Array): [TypedArray, TypedArray, number[]] => {
140 const resultShape = backend_util.assertAndGetBroadcastShape(aShape, bShape);
141 const resultSize = util.sizeFromShape(resultShape);
142 const resultRank = resultShape.length;
143 const resultStrides = util.computeStrides(resultShape);
144
145 const resultRealVals = util.getTypedArrayFromDType('float32', resultSize);
146 const resultImagVals = util.getTypedArrayFromDType('float32', resultSize);
147
148 const aBroadcastDims = backend_util.getBroadcastDims(aShape, resultShape);
149 const bBroadcastDims = backend_util.getBroadcastDims(bShape, resultShape);
150
151 const aVals = backend_util.mergeRealAndImagArrays(aRealVals, aImagVals);
152 const bVals = backend_util.mergeRealAndImagArrays(bRealVals, bImagVals);
153
154 const aRank = aShape.length;
155 const aStrides = util.computeStrides(aShape);
156
157 const bRank = bShape.length;
158 const bStrides = util.computeStrides(bShape);
159
160 if (aBroadcastDims.length + bBroadcastDims.length === 0) {
161 for (let i = 0; i < resultRealVals.length; i++) {
162 const aIdx = i % aVals.length;
163 const bIdx = i % bVals.length;
164
165 const result =
166 op(aVals[aIdx * 2], aVals[aIdx * 2 + 1], bVals[bIdx * 2],
167 bVals[bIdx * 2 + 1]);
168
169 resultRealVals[i] = result.real;
170 resultImagVals[i] = result.imag;
171 }
172 } else {
173 for (let i = 0; i < resultRealVals.length; i++) {
174 const loc = util.indexToLoc(i, resultRank, resultStrides);
175
176 const aLoc = loc.slice(-aRank);
177 aBroadcastDims.forEach(d => aLoc[d] = 0);
178 const aIndex = util.locToIndex(aLoc, aRank, aStrides);
179
180 const bLoc = loc.slice(-bRank);
181 bBroadcastDims.forEach(d => bLoc[d] = 0);
182 const bIndex = util.locToIndex(bLoc, bRank, bStrides);
183
184 const opResult =
185 op(aVals[aIndex * 2], aVals[aIndex * 2 + 1], bVals[bIndex * 2],
186 bVals[bIndex * 2 + 1]);
187
188 resultRealVals[i] = opResult.real;
189 resultImagVals[i] = opResult.imag;
190 }
191 }
192 return [resultRealVals, resultImagVals, resultShape];

Callers 3

Multiply.tsFile · 0.90
Add.tsFile · 0.90
Sub.tsFile · 0.90

Calls 4

opFunction · 0.85
indexToLocMethod · 0.80
locToIndexMethod · 0.80
sliceMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…