From bba221f33f7c25f44bde69b5626de9733b276136 Mon Sep 17 00:00:00 2001 From: Linchenn <40653845+Linchenn@users.noreply.github.com> Date: Mon, 3 Aug 2020 14:48:59 -0700 Subject: [PATCH 1/5] add kernelTimeMs and extraInfor for tf.profile --- tfjs-core/src/engine.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tfjs-core/src/engine.ts b/tfjs-core/src/engine.ts index b18af07bb94..0cedb0e4e3c 100644 --- a/tfjs-core/src/engine.ts +++ b/tfjs-core/src/engine.ts @@ -57,6 +57,8 @@ type KernelInfo = { totalTensorsSnapshot: number; inputShapes: number[][]; outputShapes: number[][]; + kernelTimeMs: number | {error: string} | Promise; + extraInfo: string | Promise; }; export type ProfileInfo = { @@ -613,12 +615,12 @@ export class Engine implements TensorTracker, DataMover { } // Stop recording to a tape when running a kernel. + let kernelProfile: KernelProfile; this.scopedRun( () => this.state.kernelDepth++, () => this.state.kernelDepth--, () => { if (!this.ENV.getBool('DEBUG')) { outputs = kernelFunc(); } else { - let kernelProfile: KernelProfile; kernelProfile = this.profiler.profileKernel( kernelName, inputs, () => kernelFunc()); this.profiler.logKernelProfile(kernelProfile); @@ -640,7 +642,9 @@ export class Engine implements TensorTracker, DataMover { totalTensorsSnapshot: this.state.numTensors, inputShapes: Object.keys(inputs).map( key => inputs[key] != null ? inputs[key].shape : null), - outputShapes: outputs.map(item => item.shape) + outputShapes: outputs.map(item => item.shape), + kernelTimeMs: kernelProfile.timeMs, + extraInfo: kernelProfile.extraInfo }); } return (Array.isArray(out) ? outputs : outputs[0]) as T; @@ -866,6 +870,10 @@ export class Engine implements TensorTracker, DataMover { this.state.activeProfile.newBytes = this.state.numBytes - startBytes; this.state.activeProfile.newTensors = this.state.numTensors - startNumTensors; + for (const kernel of this.state.activeProfile.kernels) { + kernel.kernelTimeMs = await kernel.kernelTimeMs; + kernel.extraInfo = await kernel.extraInfo; + } return this.state.activeProfile; } From 73ee89cda87a98f0bcc7366cfb6becab6978b8a7 Mon Sep 17 00:00:00 2001 From: Linchenn <40653845+Linchenn@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:31:53 -0700 Subject: [PATCH 2/5] update tests --- tfjs-core/src/engine.ts | 6 ++-- tfjs-core/src/engine_test.ts | 58 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/tfjs-core/src/engine.ts b/tfjs-core/src/engine.ts index 0cedb0e4e3c..7a5a86deda1 100644 --- a/tfjs-core/src/engine.ts +++ b/tfjs-core/src/engine.ts @@ -618,12 +618,14 @@ export class Engine implements TensorTracker, DataMover { let kernelProfile: KernelProfile; this.scopedRun( () => this.state.kernelDepth++, () => this.state.kernelDepth--, () => { - if (!this.ENV.getBool('DEBUG')) { + if (!this.ENV.getBool('DEBUG') && !this.state.profiling) { outputs = kernelFunc(); } else { kernelProfile = this.profiler.profileKernel( kernelName, inputs, () => kernelFunc()); - this.profiler.logKernelProfile(kernelProfile); + if (this.ENV.getBool('DEBUG')) { + this.profiler.logKernelProfile(kernelProfile); + } outputs = kernelProfile.outputs; } }); diff --git a/tfjs-core/src/engine_test.ts b/tfjs-core/src/engine_test.ts index 937c69485d6..d1486cf9207 100644 --- a/tfjs-core/src/engine_test.ts +++ b/tfjs-core/src/engine_test.ts @@ -387,26 +387,30 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.peakBytes).toBe(24); expect(profile.newTensors).toBe(1); expectArraysClose(await result.data(), [1, 2, 3]); - expect(profile.kernels).toEqual([ - { - 'name': 'Square', - 'bytesAdded': 12, - 'totalBytesSnapshot': 24, - 'tensorsAdded': 1, - 'totalTensorsSnapshot': 2, - 'inputShapes': [[3]], - 'outputShapes': [[3]] - }, - { - 'name': 'Square', - 'bytesAdded': 12, - 'totalBytesSnapshot': 24, - 'tensorsAdded': 1, - 'totalTensorsSnapshot': 2, - 'inputShapes': [[3]], - 'outputShapes': [[3]] - } - ]); + expect(profile.kernels.length).toBe(2); + expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); + expect(typeof profile.kernels[0].extraInfo).toBe('string'); + expect(typeof profile.kernels[1].kernelTimeMs).toBe('number'); + expect(typeof profile.kernels[1].extraInfo).toBe('string'); + expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ + 'name': 'Square', + 'bytesAdded': 12, + 'totalBytesSnapshot': 24, + 'tensorsAdded': 1, + 'totalTensorsSnapshot': 2, + 'inputShapes': [[3]], + 'outputShapes': [[3]] + })); + + expect(profile.kernels[1]).toEqual(jasmine.objectContaining({ + 'name': 'Square', + 'bytesAdded': 12, + 'totalBytesSnapshot': 24, + 'tensorsAdded': 1, + 'totalTensorsSnapshot': 2, + 'inputShapes': [[3]], + 'outputShapes': [[3]] + })); }); it('squaring without disposing', async () => { @@ -422,7 +426,10 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.peakBytes).toBe(24); expect(profile.newTensors).toBe(2); expectArraysClose(await result.data(), [1, 4, 9]); - expect(profile.kernels).toEqual([{ + expect(profile.kernels.length).toBe(1); + expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); + expect(typeof profile.kernels[0].extraInfo).toBe('string'); + expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, @@ -430,7 +437,7 @@ describeWithFlags('profile', ALL_ENVS, () => { 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], 'outputShapes': [[3]] - }]); + })); }); it('squaring in async query', async () => { @@ -448,7 +455,10 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.peakBytes).toBe(24); expect(profile.newTensors).toBe(1); expectArraysClose(await result.data(), [1, 2, 3]); - expect(profile.kernels).toEqual([{ + expect(profile.kernels.length).toBe(1); + expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); + expect(typeof profile.kernels[0].extraInfo).toBe('string'); + expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, @@ -456,7 +466,7 @@ describeWithFlags('profile', ALL_ENVS, () => { 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], 'outputShapes': [[3]] - }]); + })); }); }); From 0c16e4a001be75919ac161c80236db71d727bf1b Mon Sep 17 00:00:00 2001 From: Linchenn <40653845+Linchenn@users.noreply.github.com> Date: Mon, 3 Aug 2020 16:31:31 -0700 Subject: [PATCH 3/5] add annotations --- tfjs-core/src/engine_test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tfjs-core/src/engine_test.ts b/tfjs-core/src/engine_test.ts index d1486cf9207..8bc60c5cd69 100644 --- a/tfjs-core/src/engine_test.ts +++ b/tfjs-core/src/engine_test.ts @@ -388,10 +388,16 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.newTensors).toBe(1); expectArraysClose(await result.data(), [1, 2, 3]); expect(profile.kernels.length).toBe(2); + + // Test the types for `kernelTimeMs` and `extraInfo` to confirm the promises + // are resolved. expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[0].extraInfo).toBe('string'); expect(typeof profile.kernels[1].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[1].extraInfo).toBe('string'); + + // The `kernelTimeMs` and `extraInfo` are tested in Profiler.profileKernel + // and are excluded from here. expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ 'name': 'Square', 'bytesAdded': 12, From 9df313ba58105e733a001552b4de9296d8c7fa11 Mon Sep 17 00:00:00 2001 From: Linchenn <40653845+Linchenn@users.noreply.github.com> Date: Wed, 5 Aug 2020 08:47:33 -0700 Subject: [PATCH 4/5] update comments --- tfjs-core/src/engine_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tfjs-core/src/engine_test.ts b/tfjs-core/src/engine_test.ts index 8bc60c5cd69..b2dc6671ddf 100644 --- a/tfjs-core/src/engine_test.ts +++ b/tfjs-core/src/engine_test.ts @@ -396,8 +396,8 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(typeof profile.kernels[1].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[1].extraInfo).toBe('string'); - // The `kernelTimeMs` and `extraInfo` are tested in Profiler.profileKernel - // and are excluded from here. + // The values of `kernelTimeMs` and `extraInfo` are tested in + // Profiler.profileKernel, so they are excluded from here. expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ 'name': 'Square', 'bytesAdded': 12, From 172d5b40f7ac399fd709cf83aefbf2b6faaebd93 Mon Sep 17 00:00:00 2001 From: Linchenn <40653845+Linchenn@users.noreply.github.com> Date: Wed, 5 Aug 2020 09:49:18 -0700 Subject: [PATCH 5/5] refine tests --- tfjs-core/src/engine_test.ts | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/tfjs-core/src/engine_test.ts b/tfjs-core/src/engine_test.ts index b2dc6671ddf..c35369f28fa 100644 --- a/tfjs-core/src/engine_test.ts +++ b/tfjs-core/src/engine_test.ts @@ -396,27 +396,31 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(typeof profile.kernels[1].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[1].extraInfo).toBe('string'); - // The values of `kernelTimeMs` and `extraInfo` are tested in - // Profiler.profileKernel, so they are excluded from here. - expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ + // The specific values of `kernelTimeMs` and `extraInfo` are tested in the + // tests of Profiler.profileKernel, so their values are not tested here. + expect(profile.kernels[0]).toEqual({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, 'tensorsAdded': 1, 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], - 'outputShapes': [[3]] - })); + 'outputShapes': [[3]], + 'kernelTimeMs': profile.kernels[0].kernelTimeMs, + 'extraInfo': profile.kernels[0].extraInfo + }); - expect(profile.kernels[1]).toEqual(jasmine.objectContaining({ + expect(profile.kernels[1]).toEqual({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, 'tensorsAdded': 1, 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], - 'outputShapes': [[3]] - })); + 'outputShapes': [[3]], + 'kernelTimeMs': profile.kernels[1].kernelTimeMs, + 'extraInfo': profile.kernels[1].extraInfo + }); }); it('squaring without disposing', async () => { @@ -435,15 +439,17 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.kernels.length).toBe(1); expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[0].extraInfo).toBe('string'); - expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ + expect(profile.kernels[0]).toEqual({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, 'tensorsAdded': 1, 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], - 'outputShapes': [[3]] - })); + 'outputShapes': [[3]], + 'kernelTimeMs': profile.kernels[0].kernelTimeMs, + 'extraInfo': profile.kernels[0].extraInfo + }); }); it('squaring in async query', async () => { @@ -464,15 +470,17 @@ describeWithFlags('profile', ALL_ENVS, () => { expect(profile.kernels.length).toBe(1); expect(typeof profile.kernels[0].kernelTimeMs).toBe('number'); expect(typeof profile.kernels[0].extraInfo).toBe('string'); - expect(profile.kernels[0]).toEqual(jasmine.objectContaining({ + expect(profile.kernels[0]).toEqual({ 'name': 'Square', 'bytesAdded': 12, 'totalBytesSnapshot': 24, 'tensorsAdded': 1, 'totalTensorsSnapshot': 2, 'inputShapes': [[3]], - 'outputShapes': [[3]] - })); + 'outputShapes': [[3]], + 'kernelTimeMs': profile.kernels[0].kernelTimeMs, + 'extraInfo': profile.kernels[0].extraInfo + }); }); });