aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/tf_graph_dashboard/tf-graph-dashboard.html
blob: 0ce1e33867525457d6a27f57a563db7957d1f409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!--
@license
Copyright 2016 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.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../tf-graph-loader/tf-graph-loader.html">
<link rel="import" href="../tf-graph-board/tf-graph-board.html">
<link rel="import" href="../tf-graph-controls/tf-graph-controls.html">
<link rel="import" href="../tf-dashboard-common/tf-dashboard.html">
<link rel="import" href="../tf-backend/tf-backend.html">
<link rel="import" href="../vz-sorting/vz-sorting.html">

<!--
tf-graph-dashboard displays a graph from a TensorFlow run.

It has simple behavior: Creates a url-generator and run-generator
to talk to the backend, and then passes the runsWithGraph (list of runs with
associated graphs) along with the url generator into tf-graph-board for display.

If there are multiple runs with graphs, the first run's graph is shown
by default. The user can select a different run from a dropdown menu.
-->
<dom-module id="tf-graph-dashboard">
<template>
<tf-no-data-warning
  data-type="graph"
  show-warning="[[_datasetsEmpty(_datasets)]]"
></tf-no-data-warning>
<template is="dom-if" if="[[!_datasetsEmpty(_datasets)]]">
<tf-dashboard-layout>
<div class="sidebar">
  <tf-graph-controls id="controls"
        devices-for-stats="{{_devicesForStats}}"
        color-by-params="[[_colorByParams]]"
        stats="[[_stats]]"
        color-by="{{_colorBy}}"
        datasets="[[_datasets]]"
        render-hierarchy="[[_renderHierarchy]]"
        selected-dataset="{{_selectedDataset}}"
        selected-file="{{_selectedFile}}"
        selected-metadata-tag="{{_selectedMetadataTag}}"
        health-pills-feature-enabled="[[debuggerDataEnabled]]"
        health-pills-toggled-on="{{healthPillsToggledOn}}"
  ></tf-graph-controls>
  <tf-graph-loader id="loader"
        datasets="[[_datasets]]"
        selected-dataset="[[_selectedDataset]]"
        selected-metadata-tag="[[_selectedMetadataTag]]"
        selected-file="[[_selectedFile]]"
        out-graph-hierarchy="{{_graphHierarchy}}"
        out-graph="{{_graph}}"
        out-stats="{{_stats}}"
        progress="{{_progress}}"
out-hierarchy-params="{{_hierarchyParams}}"
  ></tf-graph-loader>
</div>
<div class="center">
    <tf-graph-board id="graphboard"
        devices-for-stats="[[_devicesForStats]]"
        color-by="[[_colorBy]]"
        color-by-params="{{_colorByParams}}"
        graph-hierarchy="[[_graphHierarchy]]"
        graph="[[_graph]]"
        hierarchy-params="[[_hierarchyParams]]"
        progress="[[_progress]]"
        debugger-data-enabled="[[debuggerDataEnabled]]"
        are-health-pills-loading="[[_areHealthPillsLoading]]"
        node-names-to-health-pills="[[_nodeNamesToHealthPills]]"
        all-steps-mode-enabled="{{allStepsModeEnabled}}"
        specific-health-pill-step="{{specificHealthPillStep}}"
        health-pill-step-index="[[_healthPillStepIndex]]"
        render-hierarchy="{{_renderHierarchy}}"
        stats="[[_stats]]"
    ></tf-graph-board>
</div>
</tf-dashboard-layout>
</template>
<style>

:host /deep/ {
  font-family: 'Roboto', sans-serif;
}

.center {
  position: relative;
  height: 100%;
}

</style>
</template>
</dom-module>

<script>
import {DashboardBehavior} from "../tf-dashboard-common/dashboard-behavior";
import {ReloadBehavior} from "../tf-dashboard-common/reload-behavior";
import {BackendBehavior} from "../tf-backend/behavior";
import {compareTagNames} from "../vz-sorting/sorting";

Polymer({
  is: 'tf-graph-dashboard',
  factoryImpl: function(backend, debuggerDataEnabled) {
    this.backend = backend;
    this.debuggerDataEnabled = debuggerDataEnabled;
  },
  behaviors: [
    DashboardBehavior("graphs"),
    ReloadBehavior("tf-graph-dashboard"),
    BackendBehavior,
  ],
  properties: {
    _datasets: Object,
    _renderHierarchy: {type: Object, observer: '_renderHierarchyChanged'},
    backend: Object,
    debuggerDataEnabled: Boolean,
    allStepsModeEnabled: Boolean,
    specificHealthPillStep: {type: Number, value: 0},
    healthPillsToggledOn: {type: Boolean, value: true, observer: '_healthPillsToggledOnChanged'},
    _isAttached: Boolean,
    // Whether this dashboard is initialized. This dashboard should only be initialized once.
    _initialized: Boolean,
    // Whether health pills are currently being loaded, in which case we may want to say show a
    // spinner.
    _areHealthPillsLoading: Boolean,
    // Maps the names of nodes to an array of health pills (HealthPillDatums).
    _nodeNamesToHealthPills: {
      type: Object,
      value: {},
    },
    _healthPillStepIndex: Number,
    // A strictly increasing ID. Each request for health pills has a unique ID. This helps us
    // identify stale requests.
    _healthPillRequestId: {type: Number, value: 1},
    // The setTimeout ID for the pending request for health pills at a specific step.
    _healthPillStepRequestTimerId: Number,
    // The request for health pills at a specific step (as opposed to all sampled health pills) may
    // involve slow disk reads. Hence, we throttle to 1 of those requests every this many ms.
    _healthPillStepRequestTimerDelay: {
      type: Number,
      value: 500,
      readOnly: true,
    },
    runs: Array,
  },
  listeners: {
    'node-toggle-expand': '_handleNodeToggleExpand',
  },
  observers: [
    '_maybeFetchHealthPills(allStepsModeEnabled, specificHealthPillStep)',
    '_maybeInitializeDashboard(backend, _isAttached)',
  ],
  attached: function() {
    this.set('_isAttached', true);
  },
  detached: function() {
    this.set('_isAttached', false);
  },
  reload: function() {
    this._maybeFetchHealthPills();
  },
  _shouldRequestHealthPills: function() {
    // Do not load debugger data if the feature is disabled, if the user toggled off the feature,
    // or if the graph itself has not loaded yet. We need the graph to load so that we know which
    // nodes to request health pills for.
    return this.debuggerDataEnabled &&
        this.healthPillsToggledOn &&
        this._renderHierarchy &&
        !this._datasetsEmpty(this._datasets);
  },
  _maybeInitializeDashboard: function(backend, isAttached) {
    if (this._initialized || !backend || !isAttached) {
      // Either this dashboard is already initialized ... or we are not yet ready to initialize.
      return;
    }
    if (typeof ga !== 'undefined' && ga != null) {
      ga('send', {hitType: 'pageview', page: '/v/graph'});
    }
    // Set this to true so we only initialize once.
    this._initialized = true;
    Promise.all([backend.graphRuns(), backend.runMetadataRuns()])
      .then(function(result) {
        var runsWithGraph = result[0].sort(compareTagNames);
        var runToMetadata = result[1];
        var datasets = _.map(runsWithGraph, function(runName) {
          return {
            name: runName,
            path: backend.router.graph(
                runName, tf.graph.LIMIT_ATTR_SIZE, tf.graph.LARGE_ATTRS_KEY),
            runMetadata: runToMetadata[runName] ? _.map(
              runToMetadata[runName].sort(compareTagNames), function(tag) {
                return {
                  tag: tag,
                  path: backend.router.runMetadata(tag, runName)
                };
              }, this) : []
          };
        }, this);
        this.set('_datasets', datasets);
      }.bind(this));
  },
  _requestHealthPills: function() {
    this.set('_areHealthPillsLoading', true);
    const requestId = ++this._healthPillRequestId;

    if (this._healthPillStepRequestTimerId !== null) {
      // A request for health pills is already scheduled to be initiated. Clear it, and schedule a
      // new request.
      window.clearTimeout(this._healthPillStepRequestTimerId);
      this._healthPillStepRequestTimerId = null;
    }

    if (this.allStepsModeEnabled) {
      // This path may be slow. Schedule network requests to start some time later. If another
      // request is scheduled in the mean time, drop this current request.
      this._healthPillStepRequestTimerId = setTimeout(function() {
        this._healthPillStepRequestTimerId = null;
        this._initiateNetworkRequestForHealthPills(requestId);
      }.bind(this), this._healthPillStepRequestTimerDelay);
    } else {
      // The user is fetching sampled steps. This path is fast, so no need to throttle. Directly
      // fetch the health pills across the network.
      this._initiateNetworkRequestForHealthPills(requestId);
    }
  },
  // Initiates the network request for health pills. Do not directly call this method - network
  // requests may be throttled. Instead, call _requestHealthPills, which uses this method.
  _initiateNetworkRequestForHealthPills: function(requestId) {
    if (this._healthPillRequestId !== requestId) {
      // This possibly scheduled request was outdated before it was even sent across the network. Do
      // not bother initiating it.
      return;
    }

    const specificStep = this.allStepsModeEnabled ? this.specificHealthPillStep : undefined;
    this.backend.healthPills(this._renderHierarchy.getNamesOfRenderedOps(), specificStep).then(
        function(result) {
      if (!this.healthPillsToggledOn) {
        // The user has opted to hide health pills via the toggle button.
        return;
      }

      if (requestId !== this._healthPillRequestId) {
        // This response is no longer relevant.
        return;
      }

      // Set the index for which step to show for the health pills. By default, show the last step.
      // A precondition we assume (that Tensorboard's reservoir sampling guarantees) is that all
      // node names should be mapped to the same number of steps.
      for (let nodeName in result) {
        this.set('_healthPillStepIndex', result[nodeName].length - 1);
        break;
      }

      this.set('_nodeNamesToHealthPills', result);
      this.set('_areHealthPillsLoading', false);
      this.set('_healthPillStepRequestTimerId', null);
    }.bind(this));
  },
  _datasetsEmpty: function(datasets) {
    return !datasets || !datasets.length;
  },
  _renderHierarchyChanged: function(renderHierarchy) {
    // Reload any data on the graph when the render hierarchy (which determines which nodes are
    // rendered) changes.
    this.reload();
  },
  _handleNodeToggleExpand: function() {
    // Nodes were toggled. We may need to request health pills for more nodes.
    this._maybeFetchHealthPills();
  },
  _healthPillsToggledOnChanged: function(healthPillsToggledOn) {
    if (healthPillsToggledOn) {
      // Load health pills.
      this.reload();
    } else {
      // Remove all health pills by setting an empty mapping.
      this.set('_nodeNamesToHealthPills', {});
    }
  },
  // Fetch health pills for a specific step if applicable.
  _maybeFetchHealthPills: function() {
    if (!this._shouldRequestHealthPills()) {
      return;
    }

    this._requestHealthPills();
  },
});
</script>