/
protobuf.test.js
164 lines (151 loc) · 4.85 KB
/
protobuf.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { protobuf, unserialize, getPbId, serialize } from '../src/protobuf';
import { ObjectMessage } from '../src/types/message';
import Protocol from '../src/types/protocol';
import { Plan } from '../src/types/plan';
import { State } from '../src/types/state';
import { MNIST_PLAN, MNIST_MODEL_PARAMS, PROTOCOL } from './data/dummy';
import { TorchTensor } from '../src/types/torch';
import * as tf from '@tensorflow/tfjs-core';
import { Placeholder } from '../src/types/placeholder';
describe('Protobuf', () => {
test('can unserialize an ObjectMessage', () => {
const obj = unserialize(
null,
'CjcKBwi91JDfnQISKgoECgICAxIHZmxvYXQzMrIBGAAAgD8AAABAAABAQAAAgEAAAKBAMzPDQEAE',
protobuf.syft_proto.messaging.v1.ObjectMessage
);
expect(obj).toBeInstanceOf(ObjectMessage);
});
test('can unserialize a Protocol', () => {
const protocol = unserialize(
null,
PROTOCOL,
protobuf.syft_proto.execution.v1.Protocol
);
expect(protocol).toBeInstanceOf(Protocol);
});
test('can unserialize a Plan', () => {
const plan = unserialize(
null,
MNIST_PLAN,
protobuf.syft_proto.execution.v1.Plan
);
expect(plan).toBeInstanceOf(Plan);
});
test('can unserialize a State', () => {
const state = unserialize(
null,
MNIST_MODEL_PARAMS,
protobuf.syft_proto.execution.v1.State
);
expect(state).toBeInstanceOf(State);
});
test('can serialize a State', async () => {
const placeholders = [
new Placeholder('123', ['tag1', 'tag2'], 'placeholder')
];
const tensors = [
await TorchTensor.fromTfTensor(
tf.tensor([
[1.1, 2.2],
[3.3, 4.4]
])
)
];
const state = new State(placeholders, tensors);
const serialized = serialize(null, state);
// unserialize back to check
const unserState = unserialize(
null,
serialized,
protobuf.syft_proto.execution.v1.State
);
expect(unserState).toBeInstanceOf(State);
expect(unserState.id).toStrictEqual(state.id);
expect(unserState.placeholders).toStrictEqual(placeholders);
expect(
tf
.equal(unserState.tensors[0].toTfTensor(), tensors[0].toTfTensor())
.all()
.dataSync()[0]
).toBe(1);
});
test('can serialize TorchTensor', async () => {
const tensor = tf.tensor([
[1.1, 2.2],
[3.3, 4.4]
]);
const torchTensor = await TorchTensor.fromTfTensor(tensor);
torchTensor.tags = ['tag1', 'tag2'];
torchTensor.description = 'description of tensor';
// serialize
const bin = serialize(null, torchTensor);
expect(bin).toBeInstanceOf(ArrayBuffer);
// check unserialized matches to original
const unserTorchTensor = unserialize(
null,
bin,
protobuf.syft_proto.types.torch.v1.TorchTensor
);
expect(unserTorchTensor.shape).toStrictEqual(torchTensor.shape);
expect(unserTorchTensor.dtype).toStrictEqual(torchTensor.dtype);
expect(unserTorchTensor.contents).toStrictEqual(torchTensor.contents);
// resulting TF tensors are equal
expect(
tf
.equal(unserTorchTensor.toTfTensor(), tensor)
.all()
.dataSync()[0]
).toBe(1);
expect(unserTorchTensor.tags).toStrictEqual(torchTensor.tags);
expect(unserTorchTensor.description).toStrictEqual(torchTensor.description);
});
test('can serialize TorchTensor', async () => {
const tensor = tf.tensor([
[1.1, 2.2],
[3.3, 4.4]
]);
const torchTensor = await TorchTensor.fromTfTensor(tensor);
torchTensor.tags = ['tag1', 'tag2'];
torchTensor.description = 'description of tensor';
// serialize
const bin = serialize(null, torchTensor);
expect(bin).toBeInstanceOf(ArrayBuffer);
// check unserialized matches to original
const unserTorchTensor = unserialize(
null,
bin,
protobuf.syft_proto.types.torch.v1.TorchTensor
);
expect(unserTorchTensor.shape).toStrictEqual(torchTensor.shape);
expect(unserTorchTensor.dtype).toStrictEqual(torchTensor.dtype);
expect(unserTorchTensor.contents).toStrictEqual(torchTensor.contents);
// resulting TF tensors are equal
expect(
tf
.equal(unserTorchTensor.toTfTensor(), tensor)
.all()
.dataSync()[0]
).toBe(1);
expect(unserTorchTensor.tags).toStrictEqual(torchTensor.tags);
expect(unserTorchTensor.description).toStrictEqual(torchTensor.description);
});
test('gets id from types.syft.Id', () => {
const protocolWithIntId = protobuf.syft_proto.execution.v1.Protocol.fromObject(
{
id: {
id_int: 123
}
}
);
const protocolWithStrId = protobuf.syft_proto.execution.v1.Protocol.fromObject(
{
id: {
id_str: '321'
}
}
);
expect(getPbId(protocolWithIntId.id)).toBe('123');
expect(getPbId(protocolWithStrId.id)).toBe('321');
});
});