MCPcopy
hub / github.com/tensorflow/tfjs / runKernelFunc

Method runKernelFunc

tfjs-core/src/engine.ts:595–744  ·  view source on GitHub ↗

* Internal helper method to execute a kernel Func * * Use `runKernel` to execute kernels from outside of engine.

(
      kernelParams: RegisteredKernelInvocation<I>|
      CustomGradKernelInvocation<T, I>)

Source from the content-addressed store, hash-verified

593 * Use `runKernel` to execute kernels from outside of engine.
594 */
595 private runKernelFunc<T extends Tensor|Tensor[], I extends NamedTensorMap>(
596 kernelParams: RegisteredKernelInvocation<I>|
597 CustomGradKernelInvocation<T, I>): T {
598 let outputs: Tensor[];
599 let saved: Tensor[] = [];
600 const isTapeOn = this.isTapeOn();
601
602 const startingBytecount = this.state.numBytes;
603 const startingNumTensors = this.state.numTensors;
604
605 if (this.shouldCheckForMemLeaks()) {
606 this.state.numDataMovesStack.push(0);
607 }
608
609 let kernelFunc: () => Tensor[];
610 if (this.backendName == null) {
611 // backend has not been initialized yet (backend initialization is lazy
612 // can be deferred until an op/ kernel is run).
613 // The below getter has side effects that will try to initialize the
614 // backend and set properties like this.backendName
615 // tslint:disable-next-line: no-unused-expression
616 this.backend;
617 }
618
619 let out: TensorInfo|TensorInfo[];
620
621 const kernelOrScopeName = isRegisteredKernelInvocation(kernelParams) ?
622 kernelParams.kernelName :
623 this.state.activeScope != null ? this.state.activeScope.name : '';
624
625 // Create the kernelFunc from either a registered kernel OR passed in
626 // forward/backward functions (used by custom grad). In this context a
627 // kernelFunc wraps a kernel implementation with some bookkeeping.
628
629 if (isRegisteredKernelInvocation(kernelParams)) {
630 const {kernelName, inputs, attrs} = kernelParams;
631 if (this.backendName == null) {
632 // backend has not been initialized yet (backend initialization is lazy
633 // can be deferred until an op/ kernel is run).
634 // The below getter has side effects that will try to initialize the
635 // backend and set properties like this.backendName
636 // tslint:disable-next-line: no-unused-expression
637 this.backend;
638 }
639 const kernel = getKernel(kernelName, this.backendName);
640 util.assert(
641 kernel != null,
642 () => `Cannot find registered kernel '${kernelName}' for backend '${
643 this.backendName}'`);
644
645 kernelFunc = () => {
646 const numDataIdsBefore = this.backend.numDataIds();
647 out = kernel.kernelFunc({inputs, attrs, backend: this.backend});
648 const outInfos = Array.isArray(out) ? out : [out];
649 if (this.shouldCheckForMemLeaks()) {
650 this.checkKernelForMemLeak(kernelName, numDataIdsBefore, outInfos);
651 }
652

Callers 2

runKernelMethod · 0.95
customGradMethod · 0.95

Calls 15

isTapeOnMethod · 0.95
checkKernelForMemLeakMethod · 0.95
getTensorsForGradientMethod · 0.95
tidyMethod · 0.95
scopedRunMethod · 0.95
addTapeNodeMethod · 0.95
getKernelFunction · 0.90
getBoolMethod · 0.80

Tested by

no test coverage detected