forked from Thinklab-SJTU/EDA-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonLinearPlace.py
456 lines (404 loc) · 24.5 KB
/
NonLinearPlace.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
##
# @file NonLinearPlace.py
# @author Yibo Lin
# @date Jul 2018
# @brief Nonlinear placement engine to be called with parameters and placement database
#
import os
import sys
import time
import pickle
import numpy as np
import logging
import torch
import gzip
import copy
import matplotlib.pyplot as plt
if sys.version_info[0] < 3:
import cPickle as pickle
else:
import _pickle as pickle
import BasicPlace
import PlaceObj
import NesterovAcceleratedGradientOptimizer
import EvalMetrics
import pdb
class NonLinearPlace(BasicPlace.BasicPlace):
"""
@brief Nonlinear placement engine.
It takes parameters and placement database and runs placement flow.
"""
def __init__(self, params, placedb):
"""
@brief initialization.
@param params parameters
@param placedb placement database
"""
super(NonLinearPlace, self).__init__(params, placedb)
def __call__(self, params, placedb):
"""
@brief Top API to solve placement.
@param params parameters
@param placedb placement database
"""
iteration = 0
all_metrics = []
# global placement
if params.global_place_flag:
assert len(placedb.regions) == 0, "FENCE REGIONS are not supported in global placement yet"
# global placement may run in multiple stages according to user specification
for global_place_params in params.global_place_stages:
# we formulate each stage as a 3-nested optimization problem
# f_gamma(g_density(h(x) ; density weight) ; gamma)
# Lgamma Llambda Lsub
# When optimizing an inner problem, the outer parameters are fixed.
# This is a generalization to the eplace/RePlAce approach
# As global placement may easily diverge, we record the position of best overflow
best_metric = [None]
best_pos = [None]
if params.gpu:
torch.cuda.synchronize()
tt = time.time()
# construct model and optimizer
density_weight = 0.0
# construct placement model
model = PlaceObj.PlaceObj(density_weight, params, placedb, self.data_collections, self.op_collections, global_place_params).to(self.data_collections.pos[0].device)
optimizer_name = global_place_params["optimizer"]
# determine optimizer
if optimizer_name.lower() == "adam":
optimizer = torch.optim.Adam(self.parameters(), lr=0)
elif optimizer_name.lower() == "sgd":
optimizer = torch.optim.SGD(self.parameters(), lr=0)
elif optimizer_name.lower() == "sgd_momentum":
optimizer = torch.optim.SGD(self.parameters(), lr=0, momentum=0.9, nesterov=False)
elif optimizer_name.lower() == "sgd_nesterov":
optimizer = torch.optim.SGD(self.parameters(), lr=0, momentum=0.9, nesterov=True)
elif optimizer_name.lower() == "nesterov":
optimizer = NesterovAcceleratedGradientOptimizer.NesterovAcceleratedGradientOptimizer(
self.parameters(),
lr=0,
obj_and_grad_fn=model.obj_and_grad_fn,
constraint_fn=self.op_collections.move_boundary_op,
)
else:
assert 0, "unknown optimizer %s" % (optimizer_name)
logging.info("use %s optimizer" % (optimizer_name))
model.train()
# defining evaluation ops
eval_ops = {
#"wirelength" : self.op_collections.wirelength_op,
#"density" : self.op_collections.density_op,
#"objective" : model.obj_fn,
"hpwl": self.op_collections.hpwl_op,
"overflow": self.op_collections.density_overflow_op
}
if params.routability_opt_flag:
eval_ops.update({
'route_utilization': self.op_collections.route_utilization_map_op,
'pin_utilization': self.op_collections.pin_utilization_map_op
})
# a function to initialize learning rate
def initialize_learning_rate(pos):
learning_rate = model.estimate_initial_learning_rate(pos, global_place_params["learning_rate"])
# update learning rate
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate.data
if iteration == 0:
if params.gp_noise_ratio > 0.0:
logging.info("add %g%% noise" % (params.gp_noise_ratio * 100))
model.op_collections.noise_op(model.data_collections.pos[0], params.gp_noise_ratio)
initialize_learning_rate(model.data_collections.pos[0])
# the state must be saved after setting learning rate
initial_state = copy.deepcopy(optimizer.state_dict())
if params.gpu:
torch.cuda.synchronize()
logging.info("%s initialization takes %g seconds" % (optimizer_name, (time.time() - tt)))
# as nesterov requires line search, we cannot follow the convention of other solvers
if optimizer_name.lower() in {"sgd", "adam", "sgd_momentum", "sgd_nesterov"}:
model.obj_and_grad_fn(model.data_collections.pos[0])
elif optimizer_name.lower() != "nesterov":
assert 0, "unsupported optimizer %s" % (optimizer_name)
# stopping criteria
def Lgamma_stop_criterion(Lgamma_step, metrics):
with torch.no_grad():
if len(metrics) > 1:
cur_metric = metrics[-1][-1][-1]
prev_metric = metrics[-2][-1][-1]
if Lgamma_step > 100 and ((cur_metric.overflow < params.stop_overflow and cur_metric.hpwl > prev_metric.hpwl) or cur_metric.max_density < params.target_density):
logging.debug(
"Lgamma stopping criteria: %d > 100 and (( %g < 0.1 and %g > %g ) or %g < 1.0)"
% (Lgamma_step, cur_metric.overflow,
cur_metric.hpwl, prev_metric.hpwl,
cur_metric.max_density))
return True
# a heuristic to detect divergence and stop early
if len(metrics) > 50:
cur_metric = metrics[-1][-1][-1]
prev_metric = metrics[-50][-1][-1]
# record HPWL and overflow increase, and check divergence
if cur_metric.overflow > prev_metric.overflow and cur_metric.hpwl > best_metric[0].hpwl * 2:
return True
return False
def Llambda_stop_criterion(Lgamma_step, Llambda_density_weight_step, metrics):
with torch.no_grad():
if len(metrics) > 1:
cur_metric = metrics[-1][-1]
prev_metric = metrics[-2][-1]
if (cur_metric.overflow < params.stop_overflow and cur_metric.hpwl > prev_metric.hpwl) or cur_metric.max_density < 1.0:
logging.debug(
"Llambda stopping criteria: %d and (( %g < 0.1 and %g > %g ) or %g < 1.0)"
% (Llambda_density_weight_step,
cur_metric.overflow, cur_metric.hpwl,
prev_metric.hpwl, cur_metric.max_density))
return True
return False
# use a moving average window for stopping criteria, for an example window of 3
# 0, 1, 2, 3, 4, 5, 6
# window2
# window1
moving_avg_window = max(min(model.Lsub_iteration // 2, 3), 1)
def Lsub_stop_criterion(Lgamma_step, Llambda_density_weight_step, Lsub_step, metrics):
with torch.no_grad():
if len(metrics) >= moving_avg_window * 2:
cur_avg_obj = 0
prev_avg_obj = 0
for i in range(moving_avg_window):
cur_avg_obj += metrics[-1 - i].objective
prev_avg_obj += metrics[-1 - moving_avg_window - i].objective
cur_avg_obj /= moving_avg_window
prev_avg_obj /= moving_avg_window
threshold = 0.999
if cur_avg_obj >= prev_avg_obj * threshold:
logging.debug(
"Lsub stopping criteria: %d and %g > %g * %g"
% (Lsub_step, cur_avg_obj, prev_avg_obj,
threshold))
return True
return False
def one_descent_step(Lgamma_step, Llambda_density_weight_step, Lsub_step, iteration, metrics):
t0 = time.time()
# metric for this iteration
cur_metric = EvalMetrics.EvalMetrics(iteration, (Lgamma_step, Llambda_density_weight_step, Lsub_step))
cur_metric.gamma = model.gamma.data
cur_metric.density_weight = model.density_weight.data
metrics.append(cur_metric)
pos = model.data_collections.pos[0]
# move any out-of-bound cell back to placement region
self.op_collections.move_boundary_op(pos)
if torch.eq(model.density_weight, 0.0):
model.initialize_density_weight(params, placedb)
logging.info("density_weight = %.6E" % (model.density_weight.data))
optimizer.zero_grad()
# t1 = time.time()
cur_metric.evaluate(placedb, eval_ops, pos)
model.overflow = cur_metric.overflow.data.clone()
#logging.debug("evaluation %.3f ms" % ((time.time()-t1)*1000))
#t2 = time.time()
# as nesterov requires line search, we cannot follow the convention of other solvers
if optimizer_name.lower() in ["sgd", "adam", "sgd_momentum", "sgd_nesterov"]:
obj, grad = model.obj_and_grad_fn(pos)
cur_metric.objective = obj.data.clone()
elif optimizer_name.lower() != "nesterov":
assert 0, "unsupported optimizer %s" % (optimizer_name)
# plot placement
if params.plot_flag and iteration % 100 == 0:
cur_pos = self.pos[0].data.clone().cpu().numpy()
self.plot(params, placedb, iteration, cur_pos)
t3 = time.time()
optimizer.step()
logging.info("optimizer step %.3f ms" % ((time.time() - t3) * 1000))
# nesterov has already computed the objective of the next step
if optimizer_name.lower() == "nesterov":
cur_metric.objective = optimizer.param_groups[0]['obj_k_1'][0].data.clone()
# actually reports the metric before step
logging.info(cur_metric)
# record the best overflow
if best_metric[0] is None or best_metric[0].overflow > cur_metric.overflow:
best_metric[0] = cur_metric
if best_pos[0] is None:
best_pos[0] = self.pos[0].data.clone()
else:
best_pos[0].data.copy_(self.pos[0].data)
logging.info("full step %.3f ms" % ((time.time() - t0) * 1000))
Lgamma_metrics = all_metrics
if params.routability_opt_flag:
adjust_area_flag = True
adjust_route_area_flag = params.adjust_nctugr_area_flag or params.adjust_rudy_area_flag
adjust_pin_area_flag = params.adjust_pin_area_flag
num_area_adjust = 0
Llambda_flat_iteration = 0
for Lgamma_step in range(model.Lgamma_iteration):
Lgamma_metrics.append([])
Llambda_metrics = Lgamma_metrics[-1]
for Llambda_density_weight_step in range(model.Llambda_density_weight_iteration):
Llambda_metrics.append([])
Lsub_metrics = Llambda_metrics[-1]
for Lsub_step in range(model.Lsub_iteration):
one_descent_step(Lgamma_step, Llambda_density_weight_step, Lsub_step, iteration, Lsub_metrics)
iteration += 1
# stopping criteria
if Lsub_stop_criterion(Lgamma_step, Llambda_density_weight_step, Lsub_step, Lsub_metrics):
break
Llambda_flat_iteration += 1
# update density weight
if Llambda_flat_iteration > 1:
model.op_collections.update_density_weight_op(
Llambda_metrics[-1][-1],
Llambda_metrics[-2][-1] if len(Llambda_metrics) > 1 else Lgamma_metrics[-2][-1][-1],
Llambda_flat_iteration)
#logging.debug("update density weight %.3f ms" % ((time.time()-t2)*1000))
if Llambda_stop_criterion(Lgamma_step, Llambda_density_weight_step, Llambda_metrics):
break
# for routability optimization
if params.routability_opt_flag and num_area_adjust < params.max_num_area_adjust and Llambda_metrics[-1][-1].overflow < params.node_area_adjust_overflow:
content = "routability optimization round %d: adjust area flags = (%d, %d, %d)" % (
num_area_adjust, adjust_area_flag,
adjust_route_area_flag, adjust_pin_area_flag)
pos = model.data_collections.pos[0]
#cur_metric = EvalMetrics.EvalMetrics(iteration)
#cur_metric.evaluate(placedb, {
# "hpwl" : self.op_collections.hpwl_op,
# "overflow" : self.op_collections.density_overflow_op,
# "route_utilization" : self.op_collections.route_utilization_map_op,
# "pin_utilization" : self.op_collections.pin_utilization_map_op,
# },
# pos)
#logging.info(cur_metric)
route_utilization_map = None
pin_utilization_map = None
if adjust_route_area_flag:
if params.adjust_nctugr_area_flag:
route_utilization_map = model.op_collections.nctugr_congestion_map_op(pos)
else:
route_utilization_map = model.op_collections.route_utilization_map_op(pos)
if params.plot_flag:
path = "%s/%s" % (params.result_dir, params.design_name())
figname = "%s/plot/route%d.png" % (path, num_area_adjust)
os.system("mkdir -p %s" % (os.path.dirname(figname)))
plt.imsave(figname, route_utilization_map.data.cpu().numpy().T, origin='lower')
if adjust_pin_area_flag:
pin_utilization_map = model.op_collections.pin_utilization_map_op(pos)
if params.plot_flag:
path = "%s/%s" % (params.result_dir, params.design_name())
figname = "%s/plot/pin%d.png" % (path, num_area_adjust)
os.system("mkdir -p %s" % (os.path.dirname(figname)))
plt.imsave(figname, pin_utilization_map.data.cpu().numpy().T, origin='lower')
adjust_area_flag, adjust_route_area_flag, adjust_pin_area_flag = model.op_collections.adjust_node_area_op(
pos,
route_utilization_map,
pin_utilization_map
)
content += " -> (%d, %d, %d)" % (adjust_area_flag, adjust_route_area_flag, adjust_pin_area_flag)
logging.info(content)
if adjust_area_flag:
num_area_adjust += 1
# restart Llambda
model.op_collections.density_op.reset()
model.op_collections.density_overflow_op.reset()
model.op_collections.pin_utilization_map_op.reset()
model.initialize_density_weight(params, placedb)
model.density_weight.mul_(0.1 / params.density_weight)
logging.info("density_weight = %.6E" % (model.density_weight.data))
# load state to restart the optimizer
optimizer.load_state_dict(initial_state)
# must after loading the state
initialize_learning_rate(pos)
# increase iterations of the sub problem to slow down the search
model.Lsub_iteration = model.routability_Lsub_iteration
# reset best metric
best_metric[0] = None
best_pos[0] = None
#cur_metric = EvalMetrics.EvalMetrics(iteration)
#cur_metric.evaluate(placedb, {
# "hpwl" : self.op_collections.hpwl_op,
# "overflow" : self.op_collections.density_overflow_op,
# "route_utilization" : self.op_collections.route_utilization_map_op,
# "pin_utilization" : self.op_collections.pin_utilization_map_op,
# },
# pos)
#logging.info(cur_metric)
break
# gradually reduce gamma to tradeoff smoothness and accuracy
model.op_collections.update_gamma_op(Lgamma_step, Llambda_metrics[-1][-1].overflow)
model.op_collections.precondition_op.set_overflow(Llambda_metrics[-1][-1].overflow)
if Lgamma_stop_criterion(Lgamma_step, Lgamma_metrics):
break
# update learning rate
if optimizer_name.lower() in ["sgd", "adam", "sgd_momentum", "sgd_nesterov", "cg"]:
if 'learning_rate_decay' in global_place_params:
for param_group in optimizer.param_groups:
param_group['lr'] *= global_place_params['learning_rate_decay']
# in case of divergence, use the best metric
last_metric = all_metrics[-1][-1][-1]
if last_metric.overflow > max(params.stop_overflow, best_metric[0].overflow) and last_metric.hpwl > best_metric[0].hpwl:
self.pos[0].data.copy_(best_pos[0].data)
logging.error("possible DIVERGENCE detected, roll back to the best position recorded")
all_metrics.append([best_metric])
logging.info(best_metric[0])
logging.info("optimizer %s takes %.3f seconds" %
(optimizer_name, time.time() - tt))
# recover node size and pin offset for legalization, since node size is adjusted in global placement
if params.routability_opt_flag:
with torch.no_grad():
# convert lower left to centers
self.pos[0][:placedb.num_movable_nodes].add_(self.data_collections.node_size_x[:placedb.num_movable_nodes] / 2)
self.pos[0][placedb.num_nodes:placedb.num_nodes + placedb.num_movable_nodes].add_(self.data_collections.node_size_y[:placedb.num_movable_nodes] / 2)
self.data_collections.node_size_x.copy_(self.data_collections.original_node_size_x)
self.data_collections.node_size_y.copy_(self.data_collections.original_node_size_y)
# use fixed centers as the anchor
self.pos[0][:placedb.num_movable_nodes].sub_(self.data_collections.node_size_x[:placedb.num_movable_nodes] / 2)
self.pos[0][placedb.num_nodes:placedb.num_nodes + placedb.num_movable_nodes].sub_(self.data_collections.node_size_y[:placedb.num_movable_nodes] / 2)
self.data_collections.pin_offset_x.copy_(self.data_collections.original_pin_offset_x)
self.data_collections.pin_offset_y.copy_(self.data_collections.original_pin_offset_y)
else:
cur_metric = EvalMetrics.EvalMetrics(iteration)
all_metrics.append(cur_metric)
cur_metric.evaluate(placedb, {"hpwl": self.op_collections.hpwl_op}, self.pos[0])
logging.info(cur_metric)
# dump global placement solution for legalization
if params.dump_global_place_solution_flag:
self.dump(params, placedb, self.pos[0].cpu(),
"%s.lg.pklz" % (params.design_name()))
# plot placement
if params.plot_flag:
self.plot(params, placedb, iteration,
self.pos[0].data.clone().cpu().numpy())
# legalization
if params.legalize_flag:
assert len(placedb.regions) == 0, "FENCE REGIONS are not supported in legalization yet"
tt = time.time()
self.pos[0].data.copy_(self.op_collections.legalize_op(self.pos[0]))
logging.info("legalization takes %.3f seconds" % (time.time() - tt))
cur_metric = EvalMetrics.EvalMetrics(iteration)
all_metrics.append(cur_metric)
cur_metric.evaluate(placedb, {"hpwl": self.op_collections.hpwl_op}, self.pos[0])
logging.info(cur_metric)
iteration += 1
# plot placement
if params.plot_flag:
self.plot(params, placedb, iteration,
self.pos[0].data.clone().cpu().numpy())
# dump legalization solution for detailed placement
if params.dump_legalize_solution_flag:
self.dump(params, placedb, self.pos[0].cpu(),
"%s.dp.pklz" % (params.design_name()))
# detailed placement
if params.detailed_place_flag:
tt = time.time()
self.pos[0].data.copy_(self.op_collections.detailed_place_op(self.pos[0]))
logging.info("detailed placement takes %.3f seconds" % (time.time() - tt))
cur_metric = EvalMetrics.EvalMetrics(iteration)
all_metrics.append(cur_metric)
cur_metric.evaluate(placedb, {"hpwl": self.op_collections.hpwl_op}, self.pos[0])
logging.info(cur_metric)
iteration += 1
# save results
cur_pos = self.pos[0].data.clone().cpu().numpy()
# apply solution
placedb.apply(
params, cur_pos[0:placedb.num_movable_nodes],
cur_pos[placedb.num_nodes:placedb.num_nodes + placedb.num_movable_nodes])
# plot placement
if params.plot_flag:
self.plot(params, placedb, iteration, cur_pos)
return all_metrics