* 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)
| 308 | * backend. |
| 309 | */ |
| 310 | async 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…