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

Function createSimpleBinaryKernelImpl

tfjs-backend-cpu/src/utils/binary_impl.ts:25–69  ·  view source on GitHub ↗
(op: SimpleBinaryOperation)

Source from the content-addressed store, hash-verified

23 * Template that creates implementation for binary ops. Supports broadcast.
24 */
25export function createSimpleBinaryKernelImpl(op: SimpleBinaryOperation):
26 SimpleBinaryKernelImpl {
27 return (aShape: number[], bShape: number[], aVals: DataValues,
28 bVals: DataValues, dtype: DataType): [TypedArray, number[]] => {
29 const newShape = backend_util.assertAndGetBroadcastShape(aShape, bShape);
30
31 const resultRank = newShape.length;
32 const resultStrides = util.computeStrides(newShape);
33 const resultSize = util.sizeFromShape(newShape);
34
35 const result =
36 util.getTypedArrayFromDType(dtype as NumericDataType, resultSize);
37
38 const aRank = aShape.length;
39 const bRank = bShape.length;
40
41 const aStrides = util.computeStrides(aShape);
42 const bStrides = util.computeStrides(bShape);
43
44 const aBroadcastDims = backend_util.getBroadcastDims(aShape, newShape);
45 const bBroadcastDims = backend_util.getBroadcastDims(bShape, newShape);
46
47 if (aBroadcastDims.length + bBroadcastDims.length === 0) {
48 for (let i = 0; i < result.length; ++i) {
49 result[i] = op(aVals[i % aVals.length], bVals[i % bVals.length]);
50 }
51 } else {
52 for (let i = 0; i < result.length; ++i) {
53 const loc = util.indexToLoc(i, resultRank, resultStrides);
54
55 const aLoc = loc.slice(-aRank);
56 aBroadcastDims.forEach(d => aLoc[d] = 0);
57 const aIndex = util.locToIndex(aLoc, aRank, aStrides);
58
59 const bLoc = loc.slice(-bRank);
60 bBroadcastDims.forEach(d => bLoc[d] = 0);
61 const bIndex = util.locToIndex(bLoc, bRank, bStrides);
62
63 result[i] = op(aVals[aIndex], bVals[bIndex]);
64 }
65 }
66
67 return [result, newShape];
68 };
69}

Callers 15

Atan2.tsFile · 0.90
LogicalAnd.tsFile · 0.90
Greater.tsFile · 0.90
BitwiseAnd.tsFile · 0.90
Pow.tsFile · 0.90
Less.tsFile · 0.90
Equal.tsFile · 0.90
Multiply.tsFile · 0.90
Mod.tsFile · 0.90
Maximum.tsFile · 0.90
Add.tsFile · 0.90
GreaterEqual.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…