From 0c7c8381f41cd426dca43d027b4c475b02128cfd Mon Sep 17 00:00:00 2001 From: Yannick Assogba Date: Tue, 27 Oct 2020 16:22:07 -0400 Subject: [PATCH 1/2] add kernelNames getter to profile object --- tfjs-core/src/engine.ts | 17 +++++++++++++---- tfjs-core/src/engine_test.ts | 13 +++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/tfjs-core/src/engine.ts b/tfjs-core/src/engine.ts index afc462294b6..e1f02596a41 100644 --- a/tfjs-core/src/engine.ts +++ b/tfjs-core/src/engine.ts @@ -66,6 +66,7 @@ export type ProfileInfo = { newBytes: number; newTensors: number; peakBytes: number; kernels: KernelInfo[]; result: TensorContainer; + kernelNames: string[]; }; export interface TimingInfo extends BackendTimingInfo { @@ -119,8 +120,16 @@ class EngineState { }>(); profiling = false; - activeProfile: ProfileInfo = - {newBytes: 0, newTensors: 0, peakBytes: 0, kernels: [], result: null}; + activeProfile: ProfileInfo = { + newBytes: 0, + newTensors: 0, + peakBytes: 0, + kernels: [], + result: null, + get kernelNames() { + return Array.from(new Set(this.kernels.map(k => k.name))); + } + }; dispose() { for (const variableName in this.registeredVariables) { @@ -295,8 +304,8 @@ export class Engine implements TensorTracker, DataMover { previous 'Promise.resolve(backend)===backend' as we needed to account for custom Promise implementations (e.g. Angular) */ - if (backend && !(backend instanceof KernelBackend) - && typeof backend.then === 'function') { + if (backend && !(backend instanceof KernelBackend) && + typeof backend.then === 'function') { const promiseId = ++this.pendingBackendInitId; const success = backend diff --git a/tfjs-core/src/engine_test.ts b/tfjs-core/src/engine_test.ts index adae369bf4b..b6ea343fc46 100644 --- a/tfjs-core/src/engine_test.ts +++ b/tfjs-core/src/engine_test.ts @@ -482,6 +482,19 @@ describeWithFlags('profile', ALL_ENVS, () => { 'extraInfo': profile.kernels[0].extraInfo }); }); + + it('reports correct kernelNames', async () => { + const profile = await tf.profile(() => { + const x = tf.tensor1d([1, 2, 3]); + const x2 = x.square(); + const x3 = x2.abs(); + return x3; + }); + + expect(profile.kernelNames).toEqual(jasmine.arrayWithExactContents([ + 'Square', 'Abs' + ])); + }); }); describeWithFlags('disposeVariables', ALL_ENVS, () => { From 0e30d81556c2b841cd64ac67bbba69051e520600 Mon Sep 17 00:00:00 2001 From: Yannick Assogba Date: Tue, 27 Oct 2020 16:24:51 -0400 Subject: [PATCH 2/2] add docs --- tfjs-core/src/globals.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tfjs-core/src/globals.ts b/tfjs-core/src/globals.ts index 0a114e2ff79..d3823617fd9 100644 --- a/tfjs-core/src/globals.ts +++ b/tfjs-core/src/globals.ts @@ -120,6 +120,8 @@ export function memory(): MemoryInfo { * - `kernels`: an array of objects for each kernel involved that reports * their input and output shapes, number of bytes used, and number of new * tensors created. + * - `kernelNames`: an array of unique strings with just the names of the + * kernels in the `kernels` array. * * ```js * const profile = await tf.profile(() => {