@epfml/discojs-node 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/dist/core/async_buffer.d.ts +41 -0
- package/dist/core/async_buffer.js +97 -0
- package/dist/core/async_informant.d.ts +20 -0
- package/dist/core/async_informant.js +69 -0
- package/dist/core/client/base.d.ts +33 -0
- package/dist/core/client/base.js +35 -0
- package/dist/core/client/decentralized/base.d.ts +32 -0
- package/dist/core/client/decentralized/base.js +212 -0
- package/dist/core/client/decentralized/clear_text.d.ts +14 -0
- package/dist/core/client/decentralized/clear_text.js +96 -0
- package/dist/core/client/decentralized/index.d.ts +4 -0
- package/dist/core/client/decentralized/index.js +9 -0
- package/dist/core/client/decentralized/messages.d.ts +41 -0
- package/dist/core/client/decentralized/messages.js +54 -0
- package/dist/core/client/decentralized/peer.d.ts +26 -0
- package/dist/core/client/decentralized/peer.js +210 -0
- package/dist/core/client/decentralized/peer_pool.d.ts +14 -0
- package/dist/core/client/decentralized/peer_pool.js +92 -0
- package/dist/core/client/decentralized/sec_agg.d.ts +22 -0
- package/dist/core/client/decentralized/sec_agg.js +190 -0
- package/dist/core/client/decentralized/secret_shares.d.ts +3 -0
- package/dist/core/client/decentralized/secret_shares.js +39 -0
- package/dist/core/client/decentralized/types.d.ts +2 -0
- package/dist/core/client/decentralized/types.js +7 -0
- package/dist/core/client/event_connection.d.ts +37 -0
- package/dist/core/client/event_connection.js +158 -0
- package/dist/core/client/federated/client.d.ts +37 -0
- package/dist/core/client/federated/client.js +273 -0
- package/dist/core/client/federated/index.d.ts +2 -0
- package/dist/core/client/federated/index.js +7 -0
- package/dist/core/client/federated/messages.d.ts +38 -0
- package/dist/core/client/federated/messages.js +25 -0
- package/dist/core/client/index.d.ts +5 -0
- package/dist/core/client/index.js +11 -0
- package/dist/core/client/local.d.ts +8 -0
- package/dist/core/client/local.js +36 -0
- package/dist/core/client/messages.d.ts +28 -0
- package/dist/core/client/messages.js +33 -0
- package/dist/core/client/utils.d.ts +2 -0
- package/dist/core/client/utils.js +19 -0
- package/dist/core/dataset/data/data.d.ts +11 -0
- package/dist/core/dataset/data/data.js +20 -0
- package/dist/core/dataset/data/data_split.d.ts +5 -0
- package/dist/core/dataset/data/data_split.js +2 -0
- package/dist/core/dataset/data/image_data.d.ts +8 -0
- package/dist/core/dataset/data/image_data.js +64 -0
- package/dist/core/dataset/data/index.d.ts +5 -0
- package/dist/core/dataset/data/index.js +11 -0
- package/dist/core/dataset/data/preprocessing.d.ts +13 -0
- package/dist/core/dataset/data/preprocessing.js +33 -0
- package/dist/core/dataset/data/tabular_data.d.ts +8 -0
- package/dist/core/dataset/data/tabular_data.js +40 -0
- package/dist/core/dataset/data_loader/data_loader.d.ts +15 -0
- package/dist/core/dataset/data_loader/data_loader.js +10 -0
- package/dist/core/dataset/data_loader/image_loader.d.ts +17 -0
- package/dist/core/dataset/data_loader/image_loader.js +141 -0
- package/dist/core/dataset/data_loader/index.d.ts +3 -0
- package/dist/core/dataset/data_loader/index.js +9 -0
- package/dist/core/dataset/data_loader/tabular_loader.d.ts +29 -0
- package/dist/core/dataset/data_loader/tabular_loader.js +101 -0
- package/dist/core/dataset/dataset.d.ts +2 -0
- package/dist/core/dataset/dataset.js +2 -0
- package/dist/core/dataset/dataset_builder.d.ts +18 -0
- package/dist/core/dataset/dataset_builder.js +96 -0
- package/dist/core/dataset/index.d.ts +4 -0
- package/dist/core/dataset/index.js +14 -0
- package/dist/core/index.d.ts +18 -0
- package/dist/core/index.js +41 -0
- package/dist/core/informant/graph_informant.d.ts +10 -0
- package/dist/core/informant/graph_informant.js +23 -0
- package/dist/core/informant/index.d.ts +3 -0
- package/dist/core/informant/index.js +9 -0
- package/dist/core/informant/training_informant/base.d.ts +31 -0
- package/dist/core/informant/training_informant/base.js +83 -0
- package/dist/core/informant/training_informant/decentralized.d.ts +5 -0
- package/dist/core/informant/training_informant/decentralized.js +22 -0
- package/dist/core/informant/training_informant/federated.d.ts +14 -0
- package/dist/core/informant/training_informant/federated.js +32 -0
- package/dist/core/informant/training_informant/index.d.ts +4 -0
- package/dist/core/informant/training_informant/index.js +11 -0
- package/dist/core/informant/training_informant/local.d.ts +6 -0
- package/dist/core/informant/training_informant/local.js +20 -0
- package/dist/core/logging/console_logger.d.ts +18 -0
- package/dist/core/logging/console_logger.js +33 -0
- package/dist/core/logging/index.d.ts +3 -0
- package/dist/core/logging/index.js +9 -0
- package/dist/core/logging/logger.d.ts +12 -0
- package/dist/core/logging/logger.js +9 -0
- package/dist/core/logging/trainer_logger.d.ts +24 -0
- package/dist/core/logging/trainer_logger.js +59 -0
- package/dist/core/memory/base.d.ts +22 -0
- package/dist/core/memory/base.js +9 -0
- package/dist/core/memory/empty.d.ts +14 -0
- package/dist/core/memory/empty.js +75 -0
- package/dist/core/memory/index.d.ts +3 -0
- package/dist/core/memory/index.js +9 -0
- package/dist/core/memory/model_type.d.ts +4 -0
- package/dist/core/memory/model_type.js +9 -0
- package/dist/core/privacy.d.ts +11 -0
- package/dist/core/privacy.js +47 -0
- package/dist/core/serialization/index.d.ts +2 -0
- package/dist/core/serialization/index.js +6 -0
- package/dist/core/serialization/model.d.ts +5 -0
- package/dist/core/serialization/model.js +55 -0
- package/dist/core/serialization/weights.d.ts +5 -0
- package/dist/core/serialization/weights.js +64 -0
- package/dist/core/task/data_example.d.ts +5 -0
- package/dist/core/task/data_example.js +24 -0
- package/dist/core/task/display_information.d.ts +15 -0
- package/dist/core/task/display_information.js +49 -0
- package/dist/core/task/index.d.ts +3 -0
- package/dist/core/task/index.js +8 -0
- package/dist/core/task/model_compile_data.d.ts +6 -0
- package/dist/core/task/model_compile_data.js +22 -0
- package/dist/core/task/summary.d.ts +5 -0
- package/dist/core/task/summary.js +19 -0
- package/dist/core/task/task.d.ts +10 -0
- package/dist/core/task/task.js +31 -0
- package/dist/core/task/training_information.d.ts +28 -0
- package/dist/core/task/training_information.js +66 -0
- package/dist/core/tasks/cifar10.d.ts +3 -0
- package/dist/core/tasks/cifar10.js +65 -0
- package/dist/core/tasks/geotags.d.ts +3 -0
- package/dist/core/tasks/geotags.js +67 -0
- package/dist/core/tasks/index.d.ts +6 -0
- package/dist/core/tasks/index.js +10 -0
- package/dist/core/tasks/lus_covid.d.ts +3 -0
- package/dist/core/tasks/lus_covid.js +87 -0
- package/dist/core/tasks/mnist.d.ts +3 -0
- package/dist/core/tasks/mnist.js +60 -0
- package/dist/core/tasks/simple_face.d.ts +2 -0
- package/dist/core/tasks/simple_face.js +41 -0
- package/dist/core/tasks/titanic.d.ts +3 -0
- package/dist/core/tasks/titanic.js +88 -0
- package/dist/core/training/disco.d.ts +23 -0
- package/dist/core/training/disco.js +130 -0
- package/dist/core/training/index.d.ts +2 -0
- package/dist/core/training/index.js +7 -0
- package/dist/core/training/trainer/distributed_trainer.d.ts +20 -0
- package/dist/core/training/trainer/distributed_trainer.js +65 -0
- package/dist/core/training/trainer/local_trainer.d.ts +11 -0
- package/dist/core/training/trainer/local_trainer.js +34 -0
- package/dist/core/training/trainer/round_tracker.d.ts +30 -0
- package/dist/core/training/trainer/round_tracker.js +47 -0
- package/dist/core/training/trainer/trainer.d.ts +65 -0
- package/dist/core/training/trainer/trainer.js +160 -0
- package/dist/core/training/trainer/trainer_builder.d.ts +25 -0
- package/dist/core/training/trainer/trainer_builder.js +95 -0
- package/dist/core/training/training_schemes.d.ts +5 -0
- package/dist/core/training/training_schemes.js +10 -0
- package/dist/core/types.d.ts +4 -0
- package/dist/core/types.js +2 -0
- package/dist/core/validation/index.d.ts +1 -0
- package/dist/core/validation/index.js +5 -0
- package/dist/core/validation/validator.d.ts +17 -0
- package/dist/core/validation/validator.js +104 -0
- package/dist/core/weights/aggregation.d.ts +8 -0
- package/dist/core/weights/aggregation.js +96 -0
- package/dist/core/weights/index.d.ts +2 -0
- package/dist/core/weights/index.js +7 -0
- package/dist/core/weights/weights_container.d.ts +19 -0
- package/dist/core/weights/weights_container.js +64 -0
- package/dist/dataset/data_loader/image_loader.d.ts +4 -0
- package/dist/dataset/data_loader/image_loader.js +21 -0
- package/dist/dataset/data_loader/index.d.ts +2 -0
- package/dist/dataset/data_loader/index.js +7 -0
- package/dist/dataset/data_loader/tabular_loader.d.ts +4 -0
- package/dist/dataset/data_loader/tabular_loader.js +20 -0
- package/dist/imports.d.ts +1 -0
- package/dist/imports.js +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Disco.js Node Module
|
|
2
|
+
|
|
3
|
+
`discojs-node` contains the Node.js code of Disco.js, based off and extending `discojs-core`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
The `discojs-node` project is available as the `@epfml/discojs-node` NPM package, which can be installed with
|
|
8
|
+
`npm i @epfml/discojs-node`.
|
|
9
|
+
|
|
10
|
+
### Development Environment
|
|
11
|
+
|
|
12
|
+
The dev tools run on Node.js and require `npm`, a package manager for the Node.js runtime environment.
|
|
13
|
+
We recommend using [nvm](https://github.com/nvm-sh/nvm) for installing both Node.js and NPM.
|
|
14
|
+
|
|
15
|
+
To install the project's dependencies, run:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
cd ..
|
|
19
|
+
npm ci
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Since the dependencies of `discojs-core`, `discojs-web` and `discojs-node` are the same, they are specified in a top-level `package.json` file, to ease installation and building.
|
|
23
|
+
|
|
24
|
+
> **⚠ WARNING: Apple Silicon.**
|
|
25
|
+
> `TensorFlow.js` version `3` do support M1 processors for macs. To do so, make sure you have an `arm` Node.js executable installed (not `x86_64`). It can be checked using:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
node -p "process.arch"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
which should return something similar to `arm64`.
|
|
32
|
+
|
|
33
|
+
## Build
|
|
34
|
+
|
|
35
|
+
The server and CLI modules, as well as all unit tests (except Cypress) use the `discojs-node` interface, i.e. they all run on Node.js. This Disco.js Node module is build on top of and extends `discojs-core`, whose code is [symlinked](https://en.wikipedia.org/wiki/Symbolic_link) into `discojs-node/src/core`. To build this project:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
npm run build
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This invokes the TypeScript compiler (`tsc`). It will output the compilation files of `discojs-node` in a `dist/` directory. To recompile from stratch, simply `rm -rf dist/` before running `npm run build` again.
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
### Contributing
|
|
46
|
+
|
|
47
|
+
Contributions to `discojs-node` must only include Node-specific code. Code common to both Node and the browser must be added to `discojs-core` instead.
|
|
48
|
+
|
|
49
|
+
As a rule of thumb, the `src/core/` directory must never be modified when modifying `discojs-node`, since it is [symlinked](https://en.wikipedia.org/wiki/Symbolic_link) to `discojs-core`.
|
|
50
|
+
|
|
51
|
+
If you wish to add a new file or submodule to the project, please do so in a similar way as `src/core/` is structured. That is, [adding a new task](../../docs/TASK.md) to `discojs-node` would mean adding a new file to `src/tasks/` and modifying `src/tasks/index.ts` (NOT `src/core/tasks/...`).
|
|
52
|
+
|
|
53
|
+
Note that, if you end up making calls to the Tensorflow.js API, you must import it from the root index. This is to ensure the Node version of TF.js is loaded, and only once.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Map } from 'immutable';
|
|
2
|
+
import { TaskID, AsyncInformant } from '.';
|
|
3
|
+
/**
|
|
4
|
+
* The AsyncWeightsBuffer class holds and manipulates information about the
|
|
5
|
+
* async weights buffer. It works as follows:
|
|
6
|
+
*
|
|
7
|
+
* Setup: Init round to zero and create empty buffer (a map from user id to weights)
|
|
8
|
+
*
|
|
9
|
+
* - When a user adds weights only do so when they are recent weights: i.e. this.round - round <= roundCutoff.
|
|
10
|
+
* - If a user already added weights, update them. (-> there can be at most one entry of weights per id in a buffer).
|
|
11
|
+
* - When the buffer is full, call aggregateAndStoreWeights with the weights in the buffer and then increment round by one and reset the buffer.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* taskID: corresponds to the task that weights correspond to.
|
|
15
|
+
* bufferCapacity: size of the buffer.
|
|
16
|
+
* buffer: holds a map of users to their added weights.
|
|
17
|
+
* round: the latest round of the weight buffer.
|
|
18
|
+
* roundCutoff: cutoff for accepted rounds.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AsyncBuffer<T> {
|
|
21
|
+
readonly taskID: TaskID;
|
|
22
|
+
private readonly bufferCapacity;
|
|
23
|
+
private readonly aggregateAndStoreWeights;
|
|
24
|
+
private readonly roundCutoff;
|
|
25
|
+
buffer: Map<string, T>;
|
|
26
|
+
round: number;
|
|
27
|
+
private observer;
|
|
28
|
+
constructor(taskID: TaskID, bufferCapacity: number, aggregateAndStoreWeights: (weights: Iterable<T>) => Promise<void>, roundCutoff?: number);
|
|
29
|
+
registerObserver(observer: AsyncInformant<T>): void;
|
|
30
|
+
bufferIsFull(): boolean;
|
|
31
|
+
private updateWeightsIfBufferIsFull;
|
|
32
|
+
isNotWithinRoundCutoff(round: number): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Add weights originating from weights of a given round.
|
|
35
|
+
* Only add to buffer if the given round is not old.
|
|
36
|
+
* @param weights
|
|
37
|
+
* @param round
|
|
38
|
+
* @returns true if weights were added, and false otherwise
|
|
39
|
+
*/
|
|
40
|
+
add(id: string, weights: T, round: number): Promise<boolean>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncBuffer = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var immutable_1 = require("immutable");
|
|
6
|
+
/**
|
|
7
|
+
* The AsyncWeightsBuffer class holds and manipulates information about the
|
|
8
|
+
* async weights buffer. It works as follows:
|
|
9
|
+
*
|
|
10
|
+
* Setup: Init round to zero and create empty buffer (a map from user id to weights)
|
|
11
|
+
*
|
|
12
|
+
* - When a user adds weights only do so when they are recent weights: i.e. this.round - round <= roundCutoff.
|
|
13
|
+
* - If a user already added weights, update them. (-> there can be at most one entry of weights per id in a buffer).
|
|
14
|
+
* - When the buffer is full, call aggregateAndStoreWeights with the weights in the buffer and then increment round by one and reset the buffer.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* taskID: corresponds to the task that weights correspond to.
|
|
18
|
+
* bufferCapacity: size of the buffer.
|
|
19
|
+
* buffer: holds a map of users to their added weights.
|
|
20
|
+
* round: the latest round of the weight buffer.
|
|
21
|
+
* roundCutoff: cutoff for accepted rounds.
|
|
22
|
+
*/
|
|
23
|
+
var AsyncBuffer = /** @class */ (function () {
|
|
24
|
+
function AsyncBuffer(taskID, bufferCapacity, aggregateAndStoreWeights, roundCutoff) {
|
|
25
|
+
if (roundCutoff === void 0) { roundCutoff = 0; }
|
|
26
|
+
this.taskID = taskID;
|
|
27
|
+
this.bufferCapacity = bufferCapacity;
|
|
28
|
+
this.aggregateAndStoreWeights = aggregateAndStoreWeights;
|
|
29
|
+
this.roundCutoff = roundCutoff;
|
|
30
|
+
this.buffer = (0, immutable_1.Map)();
|
|
31
|
+
this.round = 0;
|
|
32
|
+
}
|
|
33
|
+
AsyncBuffer.prototype.registerObserver = function (observer) {
|
|
34
|
+
this.observer = observer;
|
|
35
|
+
};
|
|
36
|
+
// TODO do not test private
|
|
37
|
+
AsyncBuffer.prototype.bufferIsFull = function () {
|
|
38
|
+
return this.buffer.size >= this.bufferCapacity;
|
|
39
|
+
};
|
|
40
|
+
AsyncBuffer.prototype.updateWeightsIfBufferIsFull = function () {
|
|
41
|
+
var _a;
|
|
42
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
43
|
+
return (0, tslib_1.__generator)(this, function (_b) {
|
|
44
|
+
switch (_b.label) {
|
|
45
|
+
case 0:
|
|
46
|
+
if (!this.bufferIsFull()) return [3 /*break*/, 2];
|
|
47
|
+
return [4 /*yield*/, this.aggregateAndStoreWeights(this.buffer.values())];
|
|
48
|
+
case 1:
|
|
49
|
+
_b.sent();
|
|
50
|
+
this.round += 1;
|
|
51
|
+
(_a = this.observer) === null || _a === void 0 ? void 0 : _a.update();
|
|
52
|
+
this.buffer = (0, immutable_1.Map)();
|
|
53
|
+
console.log('\n************************************************************');
|
|
54
|
+
console.log("Buffer is full; Aggregating weights and starting round: " + this.round + "\n");
|
|
55
|
+
_b.label = 2;
|
|
56
|
+
case 2: return [2 /*return*/];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
// TODO do not test private
|
|
62
|
+
AsyncBuffer.prototype.isNotWithinRoundCutoff = function (round) {
|
|
63
|
+
// Note that always this.round >= round
|
|
64
|
+
return this.round - round > this.roundCutoff;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Add weights originating from weights of a given round.
|
|
68
|
+
* Only add to buffer if the given round is not old.
|
|
69
|
+
* @param weights
|
|
70
|
+
* @param round
|
|
71
|
+
* @returns true if weights were added, and false otherwise
|
|
72
|
+
*/
|
|
73
|
+
AsyncBuffer.prototype.add = function (id, weights, round) {
|
|
74
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
75
|
+
var weightsUpdatedByUser, msg;
|
|
76
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
77
|
+
switch (_a.label) {
|
|
78
|
+
case 0:
|
|
79
|
+
if (this.isNotWithinRoundCutoff(round)) {
|
|
80
|
+
console.log("Did not add weights of " + id + " to buffer. Due to old round update: " + round + ", current round is " + this.round);
|
|
81
|
+
return [2 /*return*/, false];
|
|
82
|
+
}
|
|
83
|
+
weightsUpdatedByUser = this.buffer.has(id);
|
|
84
|
+
msg = weightsUpdatedByUser ? '\tUpdating' : '-> Adding new';
|
|
85
|
+
console.log(msg + " weights of " + id + " to buffer.");
|
|
86
|
+
this.buffer = this.buffer.set(id, weights);
|
|
87
|
+
return [4 /*yield*/, this.updateWeightsIfBufferIsFull()];
|
|
88
|
+
case 1:
|
|
89
|
+
_a.sent();
|
|
90
|
+
return [2 /*return*/, true];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
return AsyncBuffer;
|
|
96
|
+
}());
|
|
97
|
+
exports.AsyncBuffer = AsyncBuffer;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AsyncBuffer } from './async_buffer';
|
|
2
|
+
export declare class AsyncInformant<T> {
|
|
3
|
+
private readonly asyncBuffer;
|
|
4
|
+
private round;
|
|
5
|
+
private currentNumberOfParticipants;
|
|
6
|
+
private totalNumberOfParticipants;
|
|
7
|
+
private averageNumberOfParticipants;
|
|
8
|
+
constructor(asyncBuffer: AsyncBuffer<T>);
|
|
9
|
+
update(): void;
|
|
10
|
+
private updateRound;
|
|
11
|
+
private updateNumberOfParticipants;
|
|
12
|
+
private updateAverageNumberOfParticipants;
|
|
13
|
+
private updateTotalNumberOfParticipants;
|
|
14
|
+
getCurrentRound(): number;
|
|
15
|
+
getNumberOfParticipants(): number;
|
|
16
|
+
getTotalNumberOfParticipants(): number;
|
|
17
|
+
getAverageNumberOfParticipants(): number;
|
|
18
|
+
getAllStatistics(): Record<'round' | 'currentNumberOfParticipants' | 'totalNumberOfParticipants' | 'averageNumberOfParticipants', number>;
|
|
19
|
+
printAllInfos(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncInformant = void 0;
|
|
4
|
+
var AsyncInformant = /** @class */ (function () {
|
|
5
|
+
function AsyncInformant(asyncBuffer) {
|
|
6
|
+
this.asyncBuffer = asyncBuffer;
|
|
7
|
+
this.round = 0;
|
|
8
|
+
this.currentNumberOfParticipants = 0;
|
|
9
|
+
this.totalNumberOfParticipants = 0;
|
|
10
|
+
this.averageNumberOfParticipants = 0;
|
|
11
|
+
this.asyncBuffer.registerObserver(this);
|
|
12
|
+
}
|
|
13
|
+
// Update functions
|
|
14
|
+
AsyncInformant.prototype.update = function () {
|
|
15
|
+
// DEBUG
|
|
16
|
+
console.log('Before update');
|
|
17
|
+
this.printAllInfos();
|
|
18
|
+
this.updateRound();
|
|
19
|
+
this.updateNumberOfParticipants();
|
|
20
|
+
// DEBUG
|
|
21
|
+
console.log('After update');
|
|
22
|
+
this.printAllInfos();
|
|
23
|
+
};
|
|
24
|
+
AsyncInformant.prototype.updateRound = function () {
|
|
25
|
+
this.round = this.asyncBuffer.round;
|
|
26
|
+
};
|
|
27
|
+
AsyncInformant.prototype.updateNumberOfParticipants = function () {
|
|
28
|
+
this.currentNumberOfParticipants = this.asyncBuffer.buffer.size;
|
|
29
|
+
this.updateTotalNumberOfParticipants(this.currentNumberOfParticipants);
|
|
30
|
+
this.updateAverageNumberOfParticipants();
|
|
31
|
+
};
|
|
32
|
+
AsyncInformant.prototype.updateAverageNumberOfParticipants = function () {
|
|
33
|
+
this.averageNumberOfParticipants = this.totalNumberOfParticipants / this.round;
|
|
34
|
+
};
|
|
35
|
+
AsyncInformant.prototype.updateTotalNumberOfParticipants = function (currentNumberOfParticipants) {
|
|
36
|
+
this.totalNumberOfParticipants += currentNumberOfParticipants;
|
|
37
|
+
};
|
|
38
|
+
// Getter functions
|
|
39
|
+
AsyncInformant.prototype.getCurrentRound = function () {
|
|
40
|
+
return this.round;
|
|
41
|
+
};
|
|
42
|
+
AsyncInformant.prototype.getNumberOfParticipants = function () {
|
|
43
|
+
return this.currentNumberOfParticipants;
|
|
44
|
+
};
|
|
45
|
+
AsyncInformant.prototype.getTotalNumberOfParticipants = function () {
|
|
46
|
+
return this.totalNumberOfParticipants;
|
|
47
|
+
};
|
|
48
|
+
AsyncInformant.prototype.getAverageNumberOfParticipants = function () {
|
|
49
|
+
return this.averageNumberOfParticipants;
|
|
50
|
+
};
|
|
51
|
+
AsyncInformant.prototype.getAllStatistics = function () {
|
|
52
|
+
return {
|
|
53
|
+
round: this.getCurrentRound(),
|
|
54
|
+
currentNumberOfParticipants: this.getNumberOfParticipants(),
|
|
55
|
+
totalNumberOfParticipants: this.getTotalNumberOfParticipants(),
|
|
56
|
+
averageNumberOfParticipants: this.getAverageNumberOfParticipants()
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
// Debug
|
|
60
|
+
AsyncInformant.prototype.printAllInfos = function () {
|
|
61
|
+
console.log('task : ', this.asyncBuffer.taskID);
|
|
62
|
+
console.log('round : ', this.getCurrentRound());
|
|
63
|
+
console.log('participants : ', this.getNumberOfParticipants());
|
|
64
|
+
console.log('total : ', this.getTotalNumberOfParticipants());
|
|
65
|
+
console.log('average : ', this.getAverageNumberOfParticipants());
|
|
66
|
+
};
|
|
67
|
+
return AsyncInformant;
|
|
68
|
+
}());
|
|
69
|
+
exports.AsyncInformant = AsyncInformant;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { tf, WeightsContainer, Task, TrainingInformant } from '..';
|
|
2
|
+
export declare abstract class Base {
|
|
3
|
+
readonly url: URL;
|
|
4
|
+
readonly task: Task;
|
|
5
|
+
protected connected: boolean;
|
|
6
|
+
constructor(url: URL, task: Task);
|
|
7
|
+
/**
|
|
8
|
+
* Handles the connection process from the client to any sort of
|
|
9
|
+
* centralized server.
|
|
10
|
+
*/
|
|
11
|
+
abstract connect(): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Handles the disconnection process of the client from any sort
|
|
14
|
+
* of centralized server.
|
|
15
|
+
*/
|
|
16
|
+
abstract disconnect(): Promise<void>;
|
|
17
|
+
getLatestModel(): Promise<tf.LayersModel>;
|
|
18
|
+
/**
|
|
19
|
+
* The training manager matches this function with the training loop's
|
|
20
|
+
* onTrainEnd callback when training a TFJS model object. See the
|
|
21
|
+
* training manager for more details.
|
|
22
|
+
*/
|
|
23
|
+
abstract onTrainEndCommunication(weights: WeightsContainer, trainingInformant: TrainingInformant): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* This function will be called whenever a local round has ended.
|
|
26
|
+
*
|
|
27
|
+
* @param updatedWeights
|
|
28
|
+
* @param staleWeights
|
|
29
|
+
* @param round
|
|
30
|
+
* @param trainingInformant
|
|
31
|
+
*/
|
|
32
|
+
abstract onRoundEndCommunication(updatedWeights: WeightsContainer, staleWeights: WeightsContainer, round: number, trainingInformant: TrainingInformant): Promise<WeightsContainer>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Base = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var axios_1 = (0, tslib_1.__importDefault)(require("axios"));
|
|
6
|
+
var __1 = require("..");
|
|
7
|
+
var Base = /** @class */ (function () {
|
|
8
|
+
function Base(url, task) {
|
|
9
|
+
this.url = url;
|
|
10
|
+
this.task = task;
|
|
11
|
+
this.connected = false;
|
|
12
|
+
}
|
|
13
|
+
Base.prototype.getLatestModel = function () {
|
|
14
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
15
|
+
var url, response;
|
|
16
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
17
|
+
switch (_a.label) {
|
|
18
|
+
case 0:
|
|
19
|
+
url = new URL('', this.url.href);
|
|
20
|
+
if (!url.pathname.endsWith('/')) {
|
|
21
|
+
url.pathname += '/';
|
|
22
|
+
}
|
|
23
|
+
url.pathname += "tasks/" + this.task.taskID + "/model.json";
|
|
24
|
+
return [4 /*yield*/, axios_1.default.get(url.href)];
|
|
25
|
+
case 1:
|
|
26
|
+
response = _a.sent();
|
|
27
|
+
return [4 /*yield*/, __1.serialization.model.decode(response.data)];
|
|
28
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
return Base;
|
|
34
|
+
}());
|
|
35
|
+
exports.Base = Base;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { List, Map } from 'immutable';
|
|
2
|
+
import { TrainingInformant, WeightsContainer, Task } from '../..';
|
|
3
|
+
import { Base as ClientBase } from '../base';
|
|
4
|
+
import { PeerID } from './types';
|
|
5
|
+
import * as messages from './messages';
|
|
6
|
+
import { PeerConnection } from '../event_connection';
|
|
7
|
+
/**
|
|
8
|
+
* Abstract class for decentralized clients, executes onRoundEndCommunication as well as connecting
|
|
9
|
+
* to the signaling server
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class Base extends ClientBase {
|
|
12
|
+
readonly url: URL;
|
|
13
|
+
readonly task: Task;
|
|
14
|
+
protected readonly minimumReadyPeers: number;
|
|
15
|
+
private server?;
|
|
16
|
+
private peers?;
|
|
17
|
+
private ID?;
|
|
18
|
+
private pool?;
|
|
19
|
+
constructor(url: URL, task: Task);
|
|
20
|
+
private waitForPeers;
|
|
21
|
+
protected sendMessagetoPeer(peer: PeerConnection, msg: messages.PeerMessage): void;
|
|
22
|
+
private connectServer;
|
|
23
|
+
/**
|
|
24
|
+
* Initialize the connection to the peers and to the other nodes.
|
|
25
|
+
*/
|
|
26
|
+
connect(): Promise<void>;
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
onTrainEndCommunication(_: WeightsContainer, trainingInformant: TrainingInformant): Promise<void>;
|
|
29
|
+
onRoundEndCommunication(updatedWeights: WeightsContainer, staleWeights: WeightsContainer, round: number, trainingInformant: TrainingInformant): Promise<WeightsContainer>;
|
|
30
|
+
abstract sendAndReceiveWeights(peers: Map<PeerID, PeerConnection>, noisyWeights: WeightsContainer, round: number, trainingInformant: TrainingInformant): Promise<List<WeightsContainer>>;
|
|
31
|
+
abstract clientHandle(peers: Map<PeerID, PeerConnection>): void;
|
|
32
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Base = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var immutable_1 = require("immutable");
|
|
6
|
+
var nodeUrl = (0, tslib_1.__importStar)(require("url"));
|
|
7
|
+
var __1 = require("../..");
|
|
8
|
+
var base_1 = require("../base");
|
|
9
|
+
var peer_pool_1 = require("./peer_pool");
|
|
10
|
+
var messages = (0, tslib_1.__importStar)(require("./messages"));
|
|
11
|
+
var messages_1 = require("../messages");
|
|
12
|
+
var event_connection_1 = require("../event_connection");
|
|
13
|
+
var utils_1 = require("../utils");
|
|
14
|
+
/**
|
|
15
|
+
* Abstract class for decentralized clients, executes onRoundEndCommunication as well as connecting
|
|
16
|
+
* to the signaling server
|
|
17
|
+
*/
|
|
18
|
+
var Base = /** @class */ (function (_super) {
|
|
19
|
+
(0, tslib_1.__extends)(Base, _super);
|
|
20
|
+
function Base(url, task) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
var _this = _super.call(this, url, task) || this;
|
|
23
|
+
_this.url = url;
|
|
24
|
+
_this.task = task;
|
|
25
|
+
_this.minimumReadyPeers = (_b = (_a = _this.task.trainingInformation) === null || _a === void 0 ? void 0 : _a.minimumReadyPeers) !== null && _b !== void 0 ? _b : 3;
|
|
26
|
+
return _this;
|
|
27
|
+
}
|
|
28
|
+
// send message to server that client is ready
|
|
29
|
+
Base.prototype.waitForPeers = function (round) {
|
|
30
|
+
var _a;
|
|
31
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
32
|
+
var msg, receivedMessage, peers, ret;
|
|
33
|
+
var _this = this;
|
|
34
|
+
return (0, tslib_1.__generator)(this, function (_b) {
|
|
35
|
+
switch (_b.label) {
|
|
36
|
+
case 0:
|
|
37
|
+
console.debug(this.ID, 'is ready for round', round);
|
|
38
|
+
// clean old round
|
|
39
|
+
this.peers = undefined;
|
|
40
|
+
msg = { type: messages_1.type.PeerIsReady };
|
|
41
|
+
if (this.server === undefined) {
|
|
42
|
+
throw new Error('server undefined, could not connect peers');
|
|
43
|
+
}
|
|
44
|
+
this.server.send(msg);
|
|
45
|
+
return [4 /*yield*/, (0, event_connection_1.waitMessageWithTimeout)(this.server, messages_1.type.PeersForRound, utils_1.MAX_WAIT_PER_ROUND)];
|
|
46
|
+
case 1:
|
|
47
|
+
receivedMessage = _b.sent();
|
|
48
|
+
peers = (0, immutable_1.Set)(receivedMessage.peers);
|
|
49
|
+
if (this.ID !== undefined && peers.has(this.ID)) {
|
|
50
|
+
throw new Error('received peer list contains our own id');
|
|
51
|
+
}
|
|
52
|
+
if (this.peers !== undefined) {
|
|
53
|
+
throw new Error('got new peer list from server but was already received for this round');
|
|
54
|
+
}
|
|
55
|
+
if (peers.size + 1 < this.minimumReadyPeers) {
|
|
56
|
+
throw new Error('new peer list do not contain enough ready peers');
|
|
57
|
+
}
|
|
58
|
+
this.peers = peers;
|
|
59
|
+
console.debug(this.ID, 'got peers for round:', peers.toJS());
|
|
60
|
+
if (this.pool === undefined) {
|
|
61
|
+
throw new Error('waiting for peers but peer pool is undefined');
|
|
62
|
+
}
|
|
63
|
+
return [4 /*yield*/, this.pool];
|
|
64
|
+
case 2: return [4 /*yield*/, (_b.sent()).getPeers((0, immutable_1.Set)((_a = this.peers) !== null && _a !== void 0 ? _a : []), this.server, function (p) { return _this.clientHandle(p); })];
|
|
65
|
+
case 3:
|
|
66
|
+
ret = _b.sent();
|
|
67
|
+
console.debug(this.ID, "got peers for round " + round + ":", ret.keySeq().toJS());
|
|
68
|
+
return [2 /*return*/, ret];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
// TODO inline? have a serialization mod
|
|
74
|
+
Base.prototype.sendMessagetoPeer = function (peer, msg) {
|
|
75
|
+
console.debug(this.ID, 'sends message to peer', msg.peer, msg);
|
|
76
|
+
peer.send(msg);
|
|
77
|
+
};
|
|
78
|
+
/*
|
|
79
|
+
creation of the websocket for the server, connection of client to that webSocket,
|
|
80
|
+
deals with message reception from decentralized client perspective (messages received by client)
|
|
81
|
+
*/
|
|
82
|
+
Base.prototype.connectServer = function (url) {
|
|
83
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
84
|
+
var server;
|
|
85
|
+
var _this = this;
|
|
86
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0: return [4 /*yield*/, event_connection_1.WebSocketServer.connect(url, messages.isMessageFromServer, messages.isMessageToServer)];
|
|
89
|
+
case 1:
|
|
90
|
+
server = _a.sent();
|
|
91
|
+
server.on(messages_1.type.SignalForPeer, function (event) {
|
|
92
|
+
console.debug(_this.ID, 'got signal from', event.peer);
|
|
93
|
+
if (_this.pool === undefined) {
|
|
94
|
+
throw new Error('got signal but peer pool is undefined');
|
|
95
|
+
}
|
|
96
|
+
void _this.pool.then(function (pool) { return pool.signal(event.peer, event.signal); });
|
|
97
|
+
});
|
|
98
|
+
return [2 /*return*/, server];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Initialize the connection to the peers and to the other nodes.
|
|
105
|
+
*/
|
|
106
|
+
Base.prototype.connect = function () {
|
|
107
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
108
|
+
var URL, serverURL, _a, msg, peerIdMsg;
|
|
109
|
+
return (0, tslib_1.__generator)(this, function (_b) {
|
|
110
|
+
switch (_b.label) {
|
|
111
|
+
case 0:
|
|
112
|
+
URL = typeof window !== 'undefined' ? window.URL : nodeUrl.URL;
|
|
113
|
+
serverURL = new URL('', this.url.href);
|
|
114
|
+
switch (this.url.protocol) {
|
|
115
|
+
case 'http:':
|
|
116
|
+
serverURL.protocol = 'ws:';
|
|
117
|
+
break;
|
|
118
|
+
case 'https:':
|
|
119
|
+
serverURL.protocol = 'wss:';
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
throw new Error("unknown protocol: " + this.url.protocol);
|
|
123
|
+
}
|
|
124
|
+
serverURL.pathname += "deai/" + this.task.taskID;
|
|
125
|
+
_a = this;
|
|
126
|
+
return [4 /*yield*/, this.connectServer(serverURL)];
|
|
127
|
+
case 1:
|
|
128
|
+
_a.server = _b.sent();
|
|
129
|
+
msg = {
|
|
130
|
+
type: messages_1.type.clientConnected
|
|
131
|
+
};
|
|
132
|
+
this.server.send(msg);
|
|
133
|
+
return [4 /*yield*/, (0, event_connection_1.waitMessage)(this.server, messages_1.type.PeerID)];
|
|
134
|
+
case 2:
|
|
135
|
+
peerIdMsg = _b.sent();
|
|
136
|
+
console.debug(peerIdMsg.id, 'got own id from server');
|
|
137
|
+
if (this.ID !== undefined) {
|
|
138
|
+
throw new Error('got ID from server but was already received');
|
|
139
|
+
}
|
|
140
|
+
this.ID = peerIdMsg.id;
|
|
141
|
+
this.pool = peer_pool_1.PeerPool.init(peerIdMsg.id);
|
|
142
|
+
this.connected = true; // Is this still needed?
|
|
143
|
+
console.debug(this.ID, 'client connected to server');
|
|
144
|
+
return [2 /*return*/];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
// disconnect from server & peers
|
|
150
|
+
Base.prototype.disconnect = function () {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
153
|
+
return (0, tslib_1.__generator)(this, function (_c) {
|
|
154
|
+
switch (_c.label) {
|
|
155
|
+
case 0:
|
|
156
|
+
console.debug(this.ID, 'disconnect');
|
|
157
|
+
return [4 /*yield*/, this.pool];
|
|
158
|
+
case 1:
|
|
159
|
+
(_a = (_c.sent())) === null || _a === void 0 ? void 0 : _a.shutdown();
|
|
160
|
+
this.pool = undefined;
|
|
161
|
+
(_b = this.server) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
162
|
+
this.server = undefined;
|
|
163
|
+
this.connected = false;
|
|
164
|
+
return [2 /*return*/];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
Base.prototype.onTrainEndCommunication = function (_, trainingInformant) {
|
|
170
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
171
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
172
|
+
// TODO: enter seeding mode?
|
|
173
|
+
trainingInformant.addMessage('Training finished.');
|
|
174
|
+
return [2 /*return*/];
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
Base.prototype.onRoundEndCommunication = function (updatedWeights, staleWeights, round, trainingInformant) {
|
|
179
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
180
|
+
var peers, noisyWeights, finalWeights, e_1, msg;
|
|
181
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
182
|
+
switch (_a.label) {
|
|
183
|
+
case 0:
|
|
184
|
+
_a.trys.push([0, 3, , 4]);
|
|
185
|
+
return [4 /*yield*/, this.waitForPeers(round)
|
|
186
|
+
// centralized phase of communication --> client tells server that they have finished a local round and are ready to aggregate
|
|
187
|
+
// Apply clipping and DP to updates that will be sent
|
|
188
|
+
];
|
|
189
|
+
case 1:
|
|
190
|
+
peers = _a.sent();
|
|
191
|
+
noisyWeights = __1.privacy.addDifferentialPrivacy(updatedWeights, staleWeights, this.task);
|
|
192
|
+
return [4 /*yield*/, this.sendAndReceiveWeights(peers, noisyWeights, round, trainingInformant)];
|
|
193
|
+
case 2:
|
|
194
|
+
finalWeights = _a.sent();
|
|
195
|
+
console.debug(this.ID, 'sent and received', finalWeights.size, 'weights at round', round);
|
|
196
|
+
return [2 /*return*/, __1.aggregation.avg(finalWeights)];
|
|
197
|
+
case 3:
|
|
198
|
+
e_1 = _a.sent();
|
|
199
|
+
msg = "errored on round " + round;
|
|
200
|
+
if (e_1 instanceof Error) {
|
|
201
|
+
msg += ": " + e_1.message;
|
|
202
|
+
}
|
|
203
|
+
console.warn(this.ID, msg);
|
|
204
|
+
return [2 /*return*/, updatedWeights];
|
|
205
|
+
case 4: return [2 /*return*/];
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
return Base;
|
|
211
|
+
}(base_1.Base));
|
|
212
|
+
exports.Base = Base;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { List, Map } from 'immutable';
|
|
2
|
+
import { TrainingInformant, WeightsContainer } from '../..';
|
|
3
|
+
import { Base } from './base';
|
|
4
|
+
import { PeerID } from './types';
|
|
5
|
+
import { PeerConnection } from '../event_connection';
|
|
6
|
+
/**
|
|
7
|
+
* Decentralized client that does not utilize secure aggregation, but sends model updates in clear text
|
|
8
|
+
*/
|
|
9
|
+
export declare class ClearText extends Base {
|
|
10
|
+
private receivedWeights?;
|
|
11
|
+
sendAndReceiveWeights(peers: Map<PeerID, PeerConnection>, noisyWeights: WeightsContainer, round: number, trainingInformant: TrainingInformant): Promise<List<WeightsContainer>>;
|
|
12
|
+
private receiveWeights;
|
|
13
|
+
clientHandle(peers: Map<PeerID, PeerConnection>): void;
|
|
14
|
+
}
|