* Executes `predict()` and times the inference process for `numRuns` rounds. * Then returns a promise that resolves with information about the inference * time: * - `times`: an array of inference time for each inference * - `averageTime`: the average time of all inferences * - `averageTimeExclF
(predict, numRuns = 1)
| 244 | * @param numRuns The number of rounds for `predict` to execute and time. |
| 245 | */ |
| 246 | async function timeInference(predict, numRuns = 1) { |
| 247 | if (typeof predict !== 'function') { |
| 248 | throw new Error( |
| 249 | 'The first parameter should be a function, while ' + |
| 250 | `a(n) ${typeof predict} is found.`); |
| 251 | } |
| 252 | |
| 253 | const times = []; |
| 254 | for (let i = 0; i < numRuns; i++) { |
| 255 | const start = performance.now(); |
| 256 | const res = await predict(); |
| 257 | // Prediction from tflite backend generates in the worker thread, |
| 258 | // we don't post the result back to main thread to avoid unnecessary |
| 259 | // overhead in transferring between worker and main thread. |
| 260 | if (!isTflite()) { |
| 261 | // The prediction can be tf.Tensor|tf.Tensor[]|{[name: string]: |
| 262 | // tf.Tensor}. |
| 263 | const value = await downloadValuesFromTensorContainer(res); |
| 264 | } |
| 265 | const elapsedTime = performance.now() - start; |
| 266 | |
| 267 | tf.dispose(res); |
| 268 | times.push(elapsedTime); |
| 269 | } |
| 270 | |
| 271 | const averageTime = times.reduce((acc, curr) => acc + curr, 0) / times.length; |
| 272 | const averageTimeExclFirst = times.length > 1 ? |
| 273 | times.slice(1).reduce((acc, curr) => acc + curr, 0) / (times.length - 1) : |
| 274 | 'NA'; |
| 275 | const minTime = Math.min(...times); |
| 276 | const maxTime = Math.max(...times); |
| 277 | const timeInfo = { |
| 278 | times, |
| 279 | averageTime, |
| 280 | averageTimeExclFirst, |
| 281 | minTime, |
| 282 | maxTime |
| 283 | |
| 284 | }; |
| 285 | return timeInfo; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Time one model inference with parallel compilation feature, based on the |
no test coverage detected
searching dependent graphs…