Skip to content

Commit 6eb8c2b

Browse files
committed
scalars: don’t multiplex fetches in Colab
Summary: In #4050, we added multiplexing to scalar chart fetches for performance. This works nicely in local TensorBoard and public Colab, but the POST request machinery isn’t yet supported in the Google-internal version of Colab, so #4050 caused a regression there. This patch conditionally rolls back the change for Colab only. This includes rolling it back for public Colab, where it worked fine. We hope that this isn’t too big of a problem, since we expect that most Colab users are inspecting datasets with few runs (generated from within Colab) rather than massive hyperparameter sweeps. We also hope that we can simply revert this patch once the Google-internal behavior is fixed. Jupyter environments are unaffected (and still work). We used to have a global `window.TENSORBOARD_ENV` that listed whether we were in Colab, but we removed that in #2798 because we thought that it was no longer necessary. Since in that PR we switched to create an iframe rather than manually linking and loading the HTML (much nicer, to be sure), we now plumb this information via a query parameter. This also has the advantage that it’s easy to test the Colab codepaths by simply adding that query parameter to a normal TensorBoard instance. Test Plan: First, test that normal TensorBoard (`bazel run //tensorboard`) still works with multiplexing, and that adding `?tensorboardColab=true` causes it to send multiple GET requests instead. Then, build the Pip package (`bazel run //tensorboard/pip_package:extract_pip_packages`), upload it to public Colab, and install it into the runtime. Verify that the scalar charts still work there, albeit without multiplexing. Finally, cherry-pick these changes into google3 via a test sync and follow the test plan at <http://cl/333398676> to verify the fix. wchargin-branch: scalars-nomux-colab wchargin-source: 453503f1dea196985e889be483c2a7675cc87aa1
1 parent 2b699bd commit 6eb8c2b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

tensorboard/notebook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def _display_colab(port, height, display_handle):
345345

346346
shell = """
347347
(async () => {
348-
const url = await google.colab.kernel.proxyPort(%PORT%, {"cache": true});
348+
const url = new URL(await google.colab.kernel.proxyPort(%PORT%, {'cache': true}));
349+
url.searchParams.set('tensorboardColab', 'true');
349350
const iframe = document.createElement('iframe');
350351
iframe.src = url;
351352
iframe.setAttribute('width', '100%');

tensorboard/plugins/scalar/tf_scalar_dashboard/tf-scalar-card.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ export class TfScalarCard extends PolymerElement {
264264
items,
265265
onLoad,
266266
onFinish
267+
) => {
268+
const inColab =
269+
new URLSearchParams(window.location.search).get('tensorboardColab') ===
270+
'true';
271+
if (inColab) {
272+
// Google-internal Colab doesn't support HTTP POST requests, so we fall
273+
// back to HTTP GET (even though public Colab supports POST).
274+
return this._requestDataGet(items, onLoad, onFinish);
275+
} else {
276+
return this._requestDataPost(items, onLoad, onFinish);
277+
}
278+
};
279+
280+
_requestDataGet: RequestDataCallback<RunTagItem, ScalarDatum[] | null> = (
281+
items,
282+
onLoad,
283+
onFinish
284+
) => {
285+
const router = getRouter();
286+
const baseUrl = router.pluginRoute('scalars', '/scalars');
287+
Promise.all(
288+
items.map((item) => {
289+
const url = addParams(baseUrl, {tag: item.tag, run: item.run});
290+
return this.requestManager
291+
.request(url)
292+
.then((data) => void onLoad({item, data}));
293+
})
294+
).finally(() => void onFinish());
295+
};
296+
297+
_requestDataPost: RequestDataCallback<RunTagItem, ScalarDatum[] | null> = (
298+
items,
299+
onLoad,
300+
onFinish
267301
) => {
268302
const router = getRouter();
269303
const url = router.pluginRoute('scalars', '/scalars_multirun');

0 commit comments

Comments
 (0)