diff --git a/tensorboard/plugins/profile/tf_profile_common/BUILD b/tensorboard/plugins/profile/tf_profile_common/BUILD new file mode 100644 index 0000000000..dece6c6cd6 --- /dev/null +++ b/tensorboard/plugins/profile/tf_profile_common/BUILD @@ -0,0 +1,28 @@ +package(default_visibility = ["//tensorboard:internal"]) + +load("//tensorboard/defs:defs.bzl", "tensorboard_webcomponent_library") +load("//tensorboard/defs:web.bzl", "tf_web_library") + +licenses(["notice"]) # Apache 2.0 + +tf_web_library( + name = "tf_profile_common", + srcs = [ + "tf-profile-common.html", + "util.ts", + ], + path = "/tf-profile-common", + deps = [ + "//tensorboard/components/tf_imports:polymer", + ], +) + +tensorboard_webcomponent_library( + name = "legacy", + srcs = [":tf_profile_common"], + destdir = "tf-profile-common", + deps = [ + "//tensorboard/components/tf_imports_google:lib", + "//third_party/javascript/polymer/v1/polymer:lib", + ], +) diff --git a/tensorboard/plugins/profile/tf_profile_common/tf-profile-common.html b/tensorboard/plugins/profile/tf_profile_common/tf-profile-common.html new file mode 100644 index 0000000000..30514bbfa6 --- /dev/null +++ b/tensorboard/plugins/profile/tf_profile_common/tf-profile-common.html @@ -0,0 +1,18 @@ + + + diff --git a/tensorboard/plugins/profile/tf_profile_common/util.ts b/tensorboard/plugins/profile/tf_profile_common/util.ts new file mode 100644 index 0000000000..2c53b9a358 --- /dev/null +++ b/tensorboard/plugins/profile/tf_profile_common/util.ts @@ -0,0 +1,93 @@ +/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the 'License'); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an 'AS IS' BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +/** + * @fileoverview Utility functions for the TensorFlow profile plugin. + */ + +module tf.profile.util { + /** + * Tracks task progress. Each task being passed a progress tracker needs + * to call the below-defined methods to notify the caller about the gradual + * progress of the task. + */ + export interface ProgressTracker { + updateProgress(incrementValue: number): void; + setMessage(msg: string): void; + reportError(msg: string, err: Error): void; + } + + export function time(msg: string, task: () => T) { + let start = Date.now(); + let result = task(); + /* tslint:disable */ + console.log(msg, ':', Date.now() - start, 'ms'); + /* tslint:enable */ + return result; + } + + /** + * Creates a tracker that sets the progress property of the + * provided polymer component. The provided component must have + * a property called 'progress' that is not read-only. The progress + * property is an object with a numerical 'value' property and a + * string 'msg' property. + */ + export function getTracker(polymerComponent: any) { + return { + setMessage: function(msg) { + polymerComponent.set( + 'progress', {value: polymerComponent.progress.value, msg: msg}); + }, + updateProgress: function(value) { + polymerComponent.set('progress', { + value: polymerComponent.progress.value + value, + msg: polymerComponent.progress.msg + }); + }, + reportError: function(msg: string, err) { + // Log the stack trace in the console. + console.error(err.stack); + // And send a user-friendly message to the UI. + polymerComponent.set( + 'progress', + {value: polymerComponent.progress.value, msg: msg, error: true}); + }, + }; + } + + /** + * Runs an expensive task and return the result. + */ + export function runTask( + msg: string, incProgressValue: number, task: () => T, + tracker: ProgressTracker): T { + // Update the progress message to say the current running task. + tracker.setMessage(msg); + // Run the expensive task with a delay that gives enough time for the + // UI to update. + try { + let result = tf.profile.util.time(msg, task); + // Update the progress value. + tracker.updateProgress(incProgressValue); + // Return the result to be used by other tasks. + return result; + } catch (e) { + // Errors that happen inside asynchronous tasks are + // reported to the tracker using a user-friendly message. + tracker.reportError('Failed ' + msg, e); + } + } +} diff --git a/tensorboard/plugins/profile/tf_profile_dashboard/BUILD b/tensorboard/plugins/profile/tf_profile_dashboard/BUILD index 62d322d5dc..ae8d2829fa 100644 --- a/tensorboard/plugins/profile/tf_profile_dashboard/BUILD +++ b/tensorboard/plugins/profile/tf_profile_dashboard/BUILD @@ -20,9 +20,11 @@ tf_web_library( "//tensorboard/plugins/profile/memory_viewer/memory_viewer_dashboard", "//tensorboard/plugins/profile/overview_page", "//tensorboard/plugins/profile/tf_op_profile", + "//tensorboard/plugins/profile/tf_profile_common", "@org_polymer", "@org_polymer_paper_dropdown_menu", "@org_polymer_paper_item", "@org_polymer_paper_menu", + "@org_polymer_paper_progress", ], ) diff --git a/tensorboard/plugins/profile/tf_profile_dashboard/tf-profile-dashboard.html b/tensorboard/plugins/profile/tf_profile_dashboard/tf-profile-dashboard.html index d453b17e00..3921083396 100644 --- a/tensorboard/plugins/profile/tf_profile_dashboard/tf-profile-dashboard.html +++ b/tensorboard/plugins/profile/tf_profile_dashboard/tf-profile-dashboard.html @@ -19,6 +19,7 @@ + @@ -26,6 +27,7 @@ + @@ -41,6 +43,12 @@