(
kernelName: string, supportsFullBroadcast: boolean,
dtype?: DataType)
| 22 | import {CppDType} from './types'; |
| 23 | |
| 24 | export function createBinaryKernelConfig( |
| 25 | kernelName: string, supportsFullBroadcast: boolean, |
| 26 | dtype?: DataType): KernelConfig { |
| 27 | let wasmFunc: |
| 28 | (aId: number, aShape: Uint8Array, aShapeLen: number, bId: number, |
| 29 | bShape: Uint8Array, bShapeLen: number, dtype: number, outId: number) => |
| 30 | void; |
| 31 | |
| 32 | function setupFunc(backend: BackendWasm): void { |
| 33 | wasmFunc = backend.wasm.cwrap(kernelName, null /* void */, [ |
| 34 | 'number', // a_id, |
| 35 | 'array', // a_shape |
| 36 | 'number', // a_shape.length |
| 37 | 'number', // b_id |
| 38 | 'array', // b_shape |
| 39 | 'number', // b_shape.length |
| 40 | 'number', // dtype |
| 41 | 'number' // out_id |
| 42 | ]); |
| 43 | } |
| 44 | |
| 45 | function kernelFunc(args: {backend: BackendWasm, inputs: BinaryInputs}): |
| 46 | TensorInfo { |
| 47 | const {backend, inputs} = args; |
| 48 | const {a, b} = inputs; |
| 49 | const aId = backend.dataIdMap.get(a.dataId).id; |
| 50 | const bId = backend.dataIdMap.get(b.dataId).id; |
| 51 | |
| 52 | const outputType = dtype != null ? dtype : a.dtype; |
| 53 | const newShape = backend_util.assertAndGetBroadcastShape(a.shape, b.shape); |
| 54 | const out = backend.makeOutput(newShape, outputType); |
| 55 | |
| 56 | // Short-circuit zero-sized tensors. |
| 57 | if (util.sizeFromShape(newShape) === 0) { |
| 58 | return out; |
| 59 | } |
| 60 | |
| 61 | const aShapeBytes = new Uint8Array(new Int32Array(a.shape).buffer); |
| 62 | const bShapeBytes = new Uint8Array(new Int32Array(b.shape).buffer); |
| 63 | const outId = backend.dataIdMap.get(out.dataId).id; |
| 64 | const kernelFunc = () => wasmFunc( |
| 65 | aId, aShapeBytes, a.shape.length, bId, bShapeBytes, b.shape.length, |
| 66 | CppDType[a.dtype], outId); |
| 67 | |
| 68 | kernelFunc(); |
| 69 | return out; |
| 70 | } |
| 71 | |
| 72 | return {kernelName, backendName: 'wasm', setupFunc, kernelFunc}; |
| 73 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…