-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
186 lines (162 loc) · 6.88 KB
/
models.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Copyright 2023 Universitat Politècnica de Catalunya
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import tensorflow as tf
class DelayPredictor(tf.keras.Model):
min_max_scores_fields = {
"flow_traffic",
"flow_packets",
"flow_packet_size",
"link_capacity"
}
min_max_scores = None
name = "DelayPredictor"
def __init__(self, override_min_max_scores=None, name=None):
super(DelayPredictor, self).__init__()
self.iterations = 8
self.path_state_dim = 64
self.link_state_dim = 64
if override_min_max_scores is not None:
self.set_min_max_scores(override_min_max_scores)
if name is not None:
assert type(name) == str, "name must be a string"
self.name = name
# GRU Cells used in the Message Passing step
self.path_update = tf.keras.layers.RNN(
tf.keras.layers.GRUCell(self.path_state_dim, name="PathUpdate"),
return_sequences=True,
return_state=True,
name="PathUpdateRNN",
)
self.link_update = tf.keras.layers.GRUCell(
self.link_state_dim, name="LinkUpdate"
)
self.flow_embedding = tf.keras.Sequential(
[
tf.keras.layers.Input(shape=16),
tf.keras.layers.Dense(
self.path_state_dim, activation=tf.keras.activations.relu
),
tf.keras.layers.Dense(
self.path_state_dim, activation=tf.keras.activations.relu
),
],
name="PathEmbedding",
)
self.link_embedding = tf.keras.Sequential(
[
tf.keras.layers.Input(shape=2),
tf.keras.layers.Dense(
self.link_state_dim, activation=tf.keras.activations.relu
),
tf.keras.layers.Dense(
self.link_state_dim, activation=tf.keras.activations.relu
),
],
name="LinkEmbedding",
)
self.readout_path = tf.keras.Sequential(
[
tf.keras.layers.Input(shape=(None, self.path_state_dim)),
tf.keras.layers.Dense(
self.link_state_dim // 2, activation=tf.keras.activations.relu
),
tf.keras.layers.Dense(
self.link_state_dim // 4, activation=tf.keras.activations.relu
),
tf.keras.layers.Dense(1, activation=tf.keras.activations.softplus),
],
name="PathReadout",
)
def set_min_max_scores(self, override_min_max_scores):
assert (
type(override_min_max_scores) == dict
and all(kk in override_min_max_scores for kk in self.min_max_scores_fields)
and all(len(val) == 2 for val in override_min_max_scores.values())
), "overriden min-max dict is not valid!"
self.min_max_scores = override_min_max_scores
@tf.function
def call(self, inputs):
# Ensure that the min-max scores are set
assert self.min_max_scores is not None, "the model cannot be called before setting the min-max scores!"
# Process raw inputs
flow_traffic = inputs["flow_traffic"]
flow_packets = inputs["flow_packets"]
flow_packet_size = inputs["flow_packet_size"]
flow_type = inputs["flow_type"]
flow_ipg_mean = inputs["flow_ipg_mean"] / 1e6
flow_ipg_var = inputs["flow_ipg_var"] / 1e12
flow_ipg_percentiles = inputs["flow_ipg_percentiles"] / 1e6
link_capacity = inputs["link_capacity"]
link_to_path = inputs["link_to_path"]
path_to_link = inputs["path_to_link"]
path_gather_traffic = tf.gather(flow_traffic, path_to_link[:, :, 0])
load = tf.math.reduce_sum(path_gather_traffic, axis=1) / (link_capacity * 1e9)
# Initialize the initial hidden state for paths
path_state = self.flow_embedding(
tf.concat(
[
(flow_traffic - self.min_max_scores["flow_traffic"][0])
* self.min_max_scores["flow_traffic"][1],
(flow_packets - self.min_max_scores["flow_packets"][0])
* self.min_max_scores["flow_packets"][1],
(flow_packet_size - self.min_max_scores["flow_packet_size"][0])
* self.min_max_scores["flow_packet_size"][1],
flow_ipg_mean,
flow_ipg_var,
flow_ipg_percentiles,
flow_type,
],
axis=1,
)
)
# Initialize the initial hidden state for links
link_state = self.link_embedding(
tf.concat(
[
(link_capacity - self.min_max_scores["link_capacity"][0])
* self.min_max_scores["link_capacity"][1],
load,
],
axis=1,
),
)
# Iterate t times doing the message passing
for _ in range(self.iterations):
####################
# LINKS TO PATH #
####################
link_gather = tf.gather(link_state, link_to_path, name="LinkToPath")
previous_path_state = path_state
path_state_sequence, path_state = self.path_update(
link_gather, initial_state=path_state
)
# We select the element in path_state_sequence so that it corresponds to the state before the link was considered
path_state_sequence = tf.concat(
[tf.expand_dims(previous_path_state, 1), path_state_sequence], axis=1
)
###################
# PATH TO LINK #
###################
path_gather = tf.gather_nd(
path_state_sequence, path_to_link, name="PathToRLink"
)
path_sum = tf.math.reduce_sum(path_gather, axis=1)
link_state, _ = self.link_update(path_sum, states=link_state)
################
# READOUT #
################
occupancy = self.readout_path(path_state_sequence[:, 1:])
capacity_gather = tf.gather(link_capacity, link_to_path)
delay_sequence = occupancy / capacity_gather
delay = tf.math.reduce_sum(delay_sequence, axis=1)
return delay