|
| 1 | +/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +/** |
| 17 | + * @fileoverview Utility functions for the TensorFlow profile plugin. |
| 18 | + */ |
| 19 | + |
| 20 | +module tf.profile.util { |
| 21 | + /** |
| 22 | + * Tracks task progress. Each task being passed a progress tracker needs |
| 23 | + * to call the below-defined methods to notify the caller about the gradual |
| 24 | + * progress of the task. |
| 25 | + */ |
| 26 | + export interface ProgressTracker { |
| 27 | + updateProgress(incrementValue: number): void; |
| 28 | + setMessage(msg: string): void; |
| 29 | + reportError(msg: string, err: Error): void; |
| 30 | + } |
| 31 | + |
| 32 | + export function time<T>(msg: string, task: () => T) { |
| 33 | + let start = Date.now(); |
| 34 | + let result = task(); |
| 35 | + /* tslint:disable */ |
| 36 | + console.log(msg, ':', Date.now() - start, 'ms'); |
| 37 | + /* tslint:enable */ |
| 38 | + return result; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a tracker that sets the progress property of the |
| 43 | + * provided polymer component. The provided component must have |
| 44 | + * a property called 'progress' that is not read-only. The progress |
| 45 | + * property is an object with a numerical 'value' property and a |
| 46 | + * string 'msg' property. |
| 47 | + */ |
| 48 | + export function getTracker(polymerComponent: any) { |
| 49 | + return { |
| 50 | + setMessage: function(msg) { |
| 51 | + polymerComponent.set( |
| 52 | + 'progress', {value: polymerComponent.progress.value, msg: msg}); |
| 53 | + }, |
| 54 | + updateProgress: function(value) { |
| 55 | + polymerComponent.set('progress', { |
| 56 | + value: polymerComponent.progress.value + value, |
| 57 | + msg: polymerComponent.progress.msg |
| 58 | + }); |
| 59 | + }, |
| 60 | + reportError: function(msg: string, err) { |
| 61 | + // Log the stack trace in the console. |
| 62 | + console.error(err.stack); |
| 63 | + // And send a user-friendly message to the UI. |
| 64 | + polymerComponent.set( |
| 65 | + 'progress', |
| 66 | + {value: polymerComponent.progress.value, msg: msg, error: true}); |
| 67 | + }, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Runs an expensive task and return the result. |
| 73 | + */ |
| 74 | + export function runTask<T>( |
| 75 | + msg: string, incProgressValue: number, task: () => T, |
| 76 | + tracker: ProgressTracker): T { |
| 77 | + // Update the progress message to say the current running task. |
| 78 | + tracker.setMessage(msg); |
| 79 | + // Run the expensive task with a delay that gives enough time for the |
| 80 | + // UI to update. |
| 81 | + try { |
| 82 | + let result = tf.profile.util.time(msg, task); |
| 83 | + // Update the progress value. |
| 84 | + tracker.updateProgress(incProgressValue); |
| 85 | + // Return the result to be used by other tasks. |
| 86 | + return result; |
| 87 | + } catch (e) { |
| 88 | + // Errors that happen inside asynchronous tasks are |
| 89 | + // reported to the tracker using a user-friendly message. |
| 90 | + tracker.reportError('Failed ' + msg, e); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments