federer
Version:
Experiments in asynchronous federated learning and decentralized learning
52 lines • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Instrumentation = void 0;
const tslib_1 = require("tslib");
const assert = require("assert");
const tf = tslib_1.__importStar(require("@tensorflow/tfjs-node"));
/**
* Helper class which can be notified of events happening on the server, and
* keep track of the metrics that is configured to track.
*/
class Instrumentation {
constructor(shouldInstrument) {
this.shouldInstrument = shouldInstrument;
if (this.shouldInstrument.uploadStaleness) {
this.stalenessCounts = new Map();
}
}
/** Register that a client has uploaded a message. */
registerUpload(currentRound, uploadRound) {
if (this.shouldInstrument.uploadStaleness) {
assert(this.stalenessCounts !== undefined);
const age = currentRound - uploadRound;
const count = this.stalenessCounts.get(age) ?? 0;
this.stalenessCounts.set(age, count + 1);
}
}
/** Register that the round has ended. */
registerRoundEnd() {
if (this.shouldInstrument.memoryUsage) {
this.memory = {
tf: tf.memory(),
node: process.memoryUsage(),
};
}
}
/** Get metrics that have been accumulated. */
metrics() {
let stalenessCounts = undefined;
if (this.shouldInstrument.uploadStaleness) {
assert(this.stalenessCounts !== undefined);
// We must transmit staleness counts as a JSON object, as Maps don't
// serialize to JSON automatically.
stalenessCounts = Object.fromEntries(this.stalenessCounts.entries());
}
return {
stalenessCounts,
memory: this.memory,
};
}
}
exports.Instrumentation = Instrumentation;
//# sourceMappingURL=Instrumentation.js.map