Skip to content

Commit eea9ee8

Browse files
qiuminxunfelt
authored andcommitted
Add a progress bar to TPU profile plugin. (#1286)
1 parent 3f9c511 commit eea9ee8

File tree

5 files changed

+265
-30
lines changed

5 files changed

+265
-30
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package(default_visibility = ["//tensorboard:internal"])
2+
3+
load("//tensorboard/defs:defs.bzl", "tensorboard_webcomponent_library")
4+
load("//tensorboard/defs:web.bzl", "tf_web_library")
5+
6+
licenses(["notice"]) # Apache 2.0
7+
8+
tf_web_library(
9+
name = "tf_profile_common",
10+
srcs = [
11+
"tf-profile-common.html",
12+
"util.ts",
13+
],
14+
path = "/tf-profile-common",
15+
deps = [
16+
"//tensorboard/components/tf_imports:polymer",
17+
],
18+
)
19+
20+
tensorboard_webcomponent_library(
21+
name = "legacy",
22+
srcs = [":tf_profile_common"],
23+
destdir = "tf-profile-common",
24+
deps = [
25+
"//tensorboard/components/tf_imports_google:lib",
26+
"//third_party/javascript/polymer/v1/polymer:lib",
27+
],
28+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
@license
3+
Copyright 2016 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<script src="util.js"></script>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

tensorboard/plugins/profile/tf_profile_dashboard/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ tf_web_library(
2020
"//tensorboard/plugins/profile/memory_viewer/memory_viewer_dashboard",
2121
"//tensorboard/plugins/profile/overview_page",
2222
"//tensorboard/plugins/profile/tf_op_profile",
23+
"//tensorboard/plugins/profile/tf_profile_common",
2324
"@org_polymer",
2425
"@org_polymer_paper_dropdown_menu",
2526
"@org_polymer_paper_item",
2627
"@org_polymer_paper_menu",
28+
"@org_polymer_paper_progress",
2729
],
2830
)

0 commit comments

Comments
 (0)