-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyftBackend.js
54 lines (45 loc) · 1.44 KB
/
SyftBackend.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
const COMMAND_ADD = 'add'
const COMMAND_MUL = 'mul'
const COMMAND_MATMUL = 'matmul'
const COMMAND_DIV = 'div'
class SyftBackend extends EventTarget {
tensors = {}
toTensor(value) {
// no op
return value
}
toSyftTensor(tensorId, tensor) {
// no op
return tensor
}
addTensor(tensorId, tensor) {
this.tensors[tensorId] = this.toTensor(tensor)
this.dispatchEvent(new CustomEvent('tensor-added', {detail: tensorId}))
}
getTensor(tensorId) {
const tensor = this.tensors[tensorId]
return this.toSyftTensor(tensorId, tensor)
}
deleteTensor(tensorId) {
delete this.tensors[tensorId]
this.dispatchEvent(new CustomEvent('tensor-removed', {detail: tensorId}))
}
tensorExist(tensorId) {
return tensorId in this.tensors
}
compute(command, tensorId, argType, arg, resultId) {
const tensor = this.tensors[tensorId]
let operand
switch (argType) {
case 'number':
operand = arg
break
case 'tensorId':
operand = this.tensors[arg]
break
}
const result = this.executeOp(command, tensor, operand)
this.dispatchEvent(new CustomEvent('tensor-op', {detail: {op: command, tensorId: tensorId, arg: arg}}))
this.addTensor(resultId, result)
}
}