-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.py
72 lines (60 loc) · 1.86 KB
/
components.py
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
import torch.nn as nn
from approximations import maxpool2d, relu, leaky_relu
from profiling import Profiling
class FHEIncompatible(nn.Module):
def __init__(
self,
n=-1,
block_type="naive",
clip_before=False,
attach_profiling_layer=True,
use_profiling_layer=False,
profiling_layer_division=None,
):
super().__init__()
self.n = n
self.block_type = block_type
self.clip_before = clip_before
if attach_profiling_layer:
self.use_profiling_layer = use_profiling_layer
self.profiling_layer = Profiling(profiling_layer_division)
else:
self.use_profiling_layer = False
self.activation = 0
def operator(self, _):
raise NotImplementedError
def forward(self, x):
self.activation = x
if self.use_profiling_layer:
self.profiling_layer.update(x)
return self.operator(x)
class MaxPool2d2x2(FHEIncompatible):
def operator(self, x):
return maxpool2d(x, self.n, self.block_type, self.clip_before)
class ReLU(FHEIncompatible):
def operator(self, x):
return relu(x, self.n, self.block_type, self.clip_before)
class LeakyReLU(FHEIncompatible):
def __init__(
self,
negative_slope=0.1,
n=-1,
block_type="naive",
clip_before=False,
attach_profiling_layer=True,
use_profiling_layer=False,
profiling_layer_division=None,
):
super().__init__(
n,
block_type,
clip_before,
attach_profiling_layer,
use_profiling_layer,
profiling_layer_division,
)
self.negative_slope = negative_slope
def operator(self, x):
return leaky_relu(
x, self.n, self.negative_slope, self.block_type, self.clip_before
)