MCPcopy Index your code
hub / github.com/tensorflow/tfjs / timeFirstInference

Function timeFirstInference

e2e/benchmarks/benchmark_util.js:310–343  ·  view source on GitHub ↗

* Time one model inference with parallel compilation feature, based on the * current backend. * * The inference time contains the time spent by both `predict()` and `data()` * called by tensors in the prediction. * * ```js * // Benchmark the first infernece time with parallel compilation. *

(predict, parallelCompile = false)

Source from the content-addressed store, hash-verified

308 * backend.
309 */
310async function timeFirstInference(predict, parallelCompile = false) {
311 const start = performance.now();
312
313 // Parallel Compile
314 if (parallelCompile && tf.getBackend() === 'webgl') {
315 tf.env().set('ENGINE_COMPILE_ONLY', true);
316 const compileRes = predict();
317 tf.env().set('ENGINE_COMPILE_ONLY', false);
318 await tf.backend().checkCompileCompletionAsync();
319 tf.backend().getUniformLocations();
320 tf.dispose(compileRes);
321 } else if (parallelCompile && tf.getBackend() === 'webgpu') {
322 tf.env().set('WEBGPU_ENGINE_COMPILE_ONLY', true);
323 const compileRes = predict();
324 tf.env().set('WEBGPU_ENGINE_COMPILE_ONLY', false);
325 await tf.backend().checkCompileCompletionAsync();
326 tf.dispose(compileRes);
327 } else if (parallelCompile && isTflite()) {
328 throw new Error('Parallel Compilation for TFlite is not supported.');
329 }
330
331 // First inference
332 let res = predict();
333 if (parallelCompile && res instanceof Promise) {
334 throw new Error(
335 'Parallel Compilation for async function is not supported.');
336 }
337 res = await res;
338 await downloadValuesFromTensorContainer(res);
339 const elapsedTime = performance.now() - start;
340
341 tf.dispose(res);
342 return elapsedTime;
343}
344
345/**
346 * Downloads the values from the `tensorContainer` from any `tf.Tensor`s found

Callers

nothing calls this directly

Calls 8

backendMethod · 0.80
getUniformLocationsMethod · 0.80
predictFunction · 0.70
nowMethod · 0.65
setMethod · 0.45
disposeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…