human/src/util/profile.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Profiling calculations
* Debug only
2021-05-25 14:58:20 +02:00
*/
import { log } from './util';
2020-12-08 15:00:44 +01:00
2021-02-08 17:39:09 +01:00
export const data = {};
2020-11-01 19:07:53 +01:00
export function run(modelName: string, profileData: Record<string, unknown>): void { // profileData is tfjs internal type
2021-03-27 15:25:31 +01:00
if (!profileData || !profileData.kernels) return;
2021-04-25 19:16:04 +02:00
const maxDetected = 5;
// @ts-ignore profileData.kernels is tfjs internal type
2021-03-27 15:25:31 +01:00
const time = profileData.kernels
2020-11-01 19:07:53 +01:00
.filter((a) => a.kernelTimeMs > 0)
.reduce((a, b) => a += b.kernelTimeMs, 0);
// @ts-ignore profileData.kernels is tfjs internal type
2021-03-27 15:25:31 +01:00
const slowest = profileData.kernels
2020-11-01 19:07:53 +01:00
.map((a, i) => { a.id = i; return a; })
.filter((a) => a.kernelTimeMs > 0)
.sort((a, b) => b.kernelTimeMs - a.kernelTimeMs);
// @ts-ignore profileData.kernels is tfjs internal type
2021-03-27 15:25:31 +01:00
const largest = profileData.kernels
2020-11-01 19:07:53 +01:00
.map((a, i) => { a.id = i; return a; })
.filter((a) => a.totalBytesSnapshot > 0)
.sort((a, b) => b.totalBytesSnapshot - a.totalBytesSnapshot);
2021-04-25 19:16:04 +02:00
if (slowest.length > maxDetected) slowest.length = maxDetected;
if (largest.length > maxDetected) largest.length = maxDetected;
2021-03-27 15:25:31 +01:00
data[modelName] = {
model: modelName,
newBytes: profileData.newBytes,
newTensors: profileData.newTensors,
peakBytes: profileData.peakBytes,
numKernelOps: (profileData['kernels'] as Array<unknown>).length,
2021-03-27 15:25:31 +01:00
timeKernelOps: time,
slowestKernelOps: slowest,
largestKernelOps: largest,
};
log('profiler', modelName, data[modelName]);
2020-11-01 19:07:53 +01:00
}