forked from partcleda/intern_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplacement.py
More file actions
975 lines (797 loc) · 34.7 KB
/
placement.py
File metadata and controls
975 lines (797 loc) · 34.7 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
"""
VLSI Cell Placement Optimization Challenge
==========================================
CHALLENGE OVERVIEW:
You are tasked with implementing a critical component of a chip placement optimizer.
Given a set of cells (circuit components) with fixed sizes and connectivity requirements,
you need to find positions for these cells that:
1. Minimize total wirelength (wiring cost between connected pins)
2. Eliminate all overlaps between cells
YOUR TASK:
Implement the `overlap_repulsion_loss()` function to prevent cells from overlapping.
The function must:
- Be differentiable (uses PyTorch operations for gradient descent)
- Detect when cells overlap in 2D space
- Apply increasing penalties for larger overlaps
- Work efficiently with vectorized operations
SUCCESS CRITERIA:
After running the optimizer with your implementation:
- overlap_count should be 0 (no overlapping cell pairs)
- total_overlap_area should be 0.0 (no overlap)
- wirelength should be minimized
- Visualization should show clean, non-overlapping placement
GETTING STARTED:
1. Read through the existing code to understand the data structures
2. Look at wirelength_attraction_loss() as a reference implementation
3. Implement overlap_repulsion_loss() following the TODO instructions
4. Run main() and check the overlap metrics in the output
5. Tune hyperparameters (lambda_overlap, lambda_wirelength) if needed
6. Generate visualization to verify your solution
BONUS CHALLENGES:
- Improve convergence speed by tuning learning rate or adding momentum
- Implement better initial placement strategy
- Add visualization of optimization progress over time
"""
import os
from enum import IntEnum
import torch
import torch.optim as optim
# Feature index enums for cleaner code access
class CellFeatureIdx(IntEnum):
"""Indices for cell feature tensor columns."""
AREA = 0
NUM_PINS = 1
X = 2
Y = 3
WIDTH = 4
HEIGHT = 5
class PinFeatureIdx(IntEnum):
"""Indices for pin feature tensor columns."""
CELL_IDX = 0
PIN_X = 1 # Relative to cell corner
PIN_Y = 2 # Relative to cell corner
X = 3 # Absolute position
Y = 4 # Absolute position
WIDTH = 5
HEIGHT = 6
# Configuration constants
# Macro parameters
MIN_MACRO_AREA = 100.0
MAX_MACRO_AREA = 10000.0
# Standard cell parameters (areas can be 1, 2, or 3)
STANDARD_CELL_AREAS = [1.0, 2.0, 3.0]
STANDARD_CELL_HEIGHT = 1.0
# Pin count parameters
MIN_STANDARD_CELL_PINS = 3
MAX_STANDARD_CELL_PINS = 6
# Output directory
OUTPUT_DIR = os.path.dirname(os.path.abspath(__file__))
# Training Hyperparameters
LR = 0.01
PARETO_NUM_EPOCHS = 5000
TRAINING_STAGES = {
"0": 32000,
"1": 8000,
"2": 20000,
"3": 10000,
"4": 10000,
"5": 30000,
"6": 50000,
"7": 15000
}
LAMBDA_WIRELENGTH = 100000000.0
LAMBDA_OVERLAP = 65000000.0
LAMBDA_PARETO = 1000.0 # 100.0
LAMBDA_CENTERING = 1.0
ALPHA_MANHATTAN = 0.1 # Smoothing parameter
COS_LR_MIN_FCTR = 0.01
# ======= SETUP =======
def generate_placement_input(num_macros, num_std_cells):
"""Generate synthetic placement input data.
Args:
num_macros: Number of macros to generate
num_std_cells: Number of standard cells to generate
Returns:
Tuple of (cell_features, pin_features, edge_list):
- cell_features: torch.Tensor of shape [N, 6] with columns [area, num_pins, x, y, width, height]
- pin_features: torch.Tensor of shape [total_pins, 7] with columns
[cell_instance_index, pin_x, pin_y, x, y, pin_width, pin_height]
- edge_list: torch.Tensor of shape [E, 2] with [src_pin_idx, tgt_pin_idx]
"""
total_cells = num_macros + num_std_cells
# Step 1: Generate macro areas (uniformly distributed between min and max)
macro_areas = (
torch.rand(num_macros) * (MAX_MACRO_AREA - MIN_MACRO_AREA) + MIN_MACRO_AREA
)
# Step 2: Generate standard cell areas (randomly pick from 1, 2, or 3)
std_cell_areas = torch.tensor(STANDARD_CELL_AREAS)[
torch.randint(0, len(STANDARD_CELL_AREAS), (num_std_cells,))
]
# Combine all areas
areas = torch.cat([macro_areas, std_cell_areas])
# Step 3: Calculate cell dimensions
# Macros are square
macro_widths = torch.sqrt(macro_areas)
macro_heights = torch.sqrt(macro_areas)
# Standard cells have fixed height = 1, width = area
std_cell_widths = std_cell_areas / STANDARD_CELL_HEIGHT
std_cell_heights = torch.full((num_std_cells,), STANDARD_CELL_HEIGHT)
# Combine dimensions
cell_widths = torch.cat([macro_widths, std_cell_widths])
cell_heights = torch.cat([macro_heights, std_cell_heights])
# Step 4: Calculate number of pins per cell
num_pins_per_cell = torch.zeros(total_cells, dtype=torch.int)
# Macros: between sqrt(area) and 2*sqrt(area) pins
for i in range(num_macros):
sqrt_area = int(torch.sqrt(macro_areas[i]).item())
num_pins_per_cell[i] = torch.randint(sqrt_area, 2 * sqrt_area + 1, (1,)).item()
# Standard cells: between 3 and 6 pins
num_pins_per_cell[num_macros:] = torch.randint(
MIN_STANDARD_CELL_PINS, MAX_STANDARD_CELL_PINS + 1, (num_std_cells,)
)
# Step 5: Create cell features tensor [area, num_pins, x, y, width, height]
cell_features = torch.zeros(total_cells, 6)
cell_features[:, CellFeatureIdx.AREA] = areas
cell_features[:, CellFeatureIdx.NUM_PINS] = num_pins_per_cell.float()
cell_features[:, CellFeatureIdx.X] = 0.0 # x position (initialized to 0)
cell_features[:, CellFeatureIdx.Y] = 0.0 # y position (initialized to 0)
cell_features[:, CellFeatureIdx.WIDTH] = cell_widths
cell_features[:, CellFeatureIdx.HEIGHT] = cell_heights
# Step 6: Generate pins for each cell
total_pins = num_pins_per_cell.sum().item()
pin_features = torch.zeros(total_pins, 7)
# Fixed pin size for all pins (square pins)
PIN_SIZE = 0.1 # All pins are 0.1 x 0.1
pin_idx = 0
for cell_idx in range(total_cells):
n_pins = num_pins_per_cell[cell_idx].item()
cell_width = cell_widths[cell_idx].item()
cell_height = cell_heights[cell_idx].item()
# Generate random pin positions within the cell
# Offset from edges to ensure pins are fully inside
margin = PIN_SIZE / 2
if cell_width > 2 * margin and cell_height > 2 * margin:
pin_x = torch.rand(n_pins) * (cell_width - 2 * margin) + margin
pin_y = torch.rand(n_pins) * (cell_height - 2 * margin) + margin
else:
# For very small cells, just center the pins
pin_x = torch.full((n_pins,), cell_width / 2)
pin_y = torch.full((n_pins,), cell_height / 2)
# Fill pin features
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.CELL_IDX] = cell_idx
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.PIN_X] = (
pin_x # relative to cell
)
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.PIN_Y] = (
pin_y # relative to cell
)
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.X] = (
pin_x # absolute (same as relative initially)
)
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.Y] = (
pin_y # absolute (same as relative initially)
)
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.WIDTH] = PIN_SIZE
pin_features[pin_idx : pin_idx + n_pins, PinFeatureIdx.HEIGHT] = PIN_SIZE
pin_idx += n_pins
# Step 7: Generate edges with simple random connectivity
# Each pin connects to 1-3 random pins (preferring different cells)
edge_list = []
avg_edges_per_pin = 2.0
pin_to_cell = torch.zeros(total_pins, dtype=torch.long)
pin_idx = 0
for cell_idx, n_pins in enumerate(num_pins_per_cell):
pin_to_cell[pin_idx : pin_idx + n_pins] = cell_idx
pin_idx += n_pins
# Create adjacency set to avoid duplicate edges
adjacency = [set() for _ in range(total_pins)]
for pin_idx in range(total_pins):
pin_cell = pin_to_cell[pin_idx].item()
num_connections = torch.randint(1, 4, (1,)).item() # 1-3 connections per pin
# Try to connect to pins from different cells
for _ in range(num_connections):
# Random candidate
other_pin = torch.randint(0, total_pins, (1,)).item()
# Skip self-connections and existing connections
if other_pin == pin_idx or other_pin in adjacency[pin_idx]:
continue
# Add edge (always store smaller index first for consistency)
if pin_idx < other_pin:
edge_list.append([pin_idx, other_pin])
else:
edge_list.append([other_pin, pin_idx])
# Update adjacency
adjacency[pin_idx].add(other_pin)
adjacency[other_pin].add(pin_idx)
# Convert to tensor and remove duplicates
if edge_list:
edge_list = torch.tensor(edge_list, dtype=torch.long)
edge_list = torch.unique(edge_list, dim=0)
else:
edge_list = torch.zeros((0, 2), dtype=torch.long)
print(f"\nGenerated placement data:")
print(f" Total cells: {total_cells}")
print(f" Total pins: {total_pins}")
print(f" Total edges: {len(edge_list)}")
print(f" Average edges per pin: {2 * len(edge_list) / total_pins:.2f}")
return cell_features, pin_features, edge_list
# ======= OPTIMIZATION CODE (edit this part) =======
def wirelength_attraction_loss(cell_features, pin_features, edge_list):
"""Calculate loss based on total wirelength to minimize routing.
This is a REFERENCE IMPLEMENTATION showing how to write a differentiable loss function.
The loss computes the Manhattan distance between connected pins and minimizes
the total wirelength across all edges.
Args:
cell_features: [N, 6] tensor with [area, num_pins, x, y, width, height]
pin_features: [P, 7] tensor with pin information
edge_list: [E, 2] tensor with edges
Returns:
Scalar loss value
"""
# if edge_list.shape[0] == 0:
# return torch.tensor(0.0, requires_grad=True)
# Update absolute pin positions based on cell positions
cell_positions = cell_features[:, 2:4] # [N, 2]
cell_indices = pin_features[:, 0].long()
# Calculate absolute pin positions
pin_absolute_x = cell_positions[cell_indices, 0] + pin_features[:, 1]
pin_absolute_y = cell_positions[cell_indices, 1] + pin_features[:, 2]
# Get source and target pin positions for each edge
src_pins = edge_list[:, 0].long()
tgt_pins = edge_list[:, 1].long()
src_x = pin_absolute_x[src_pins]
src_y = pin_absolute_y[src_pins]
tgt_x = pin_absolute_x[tgt_pins]
tgt_y = pin_absolute_y[tgt_pins]
# Calculate smooth approximation of Manhattan distance
# Using log-sum-exp approximation for differentiability
dx = torch.abs(src_x - tgt_x)
dy = torch.abs(src_y - tgt_y)
# Smooth L1 distance with numerical stability
smooth_manhattan = ALPHA_MANHATTAN * torch.logsumexp(
torch.stack([dx / ALPHA_MANHATTAN, dy / ALPHA_MANHATTAN], dim=0), dim=0
)
# Total wirelength
total_wirelength = torch.sum(smooth_manhattan)
# Normalize by number of edges
total_wirelength = total_wirelength / edge_list.shape[0]
return total_wirelength
wirelength_attraction_loss_jit = torch.compile(wirelength_attraction_loss)
@torch.compile
def centering_regularization(cell_features, pin_features, edge_list):
"""Calculate regularization term to center the cells on the chip."""
x = cell_features[:, CellFeatureIdx.X]
y = cell_features[:, CellFeatureIdx.Y]
centering_reg = LAMBDA_CENTERING * (torch.sum((x**2 + y**2)**0.5) / x.shape[0])
return centering_reg
@torch.compile
def overlap_repulsion_loss(cell_features, pin_features, edge_list):
"""Calculate loss to prevent cell overlaps.
TODO: IMPLEMENT THIS FUNCTION
This is the main challenge. You need to implement a differentiable loss function
that penalizes overlapping cells. The loss should:
1. Be zero when no cells overlap
2. Increase as overlap area increases
3. Use only differentiable PyTorch operations (no if statements on tensors)
4. Work efficiently with vectorized operations
HINTS:
- Two axis-aligned rectangles overlap if they overlap in BOTH x and y dimensions
- For rectangles centered at (x1, y1) and (x2, y2) with widths (w1, w2) and heights (h1, h2):
* x-overlap occurs when |x1 - x2| < (w1 + w2) / 2
* y-overlap occurs when |y1 - y2| < (h1 + h2) / 2
- Use torch.relu() to compute positive overlaps: overlap_x = relu((w1+w2)/2 - |x1-x2|)
- Overlap area = overlap_x * overlap_y
- Consider all pairs of cells: use broadcasting with unsqueeze
- Use torch.triu() to avoid counting each pair twice (only consider i < j)
- Normalize the loss appropriately (by number of pairs or total area)
RECOMMENDED APPROACH:
1. Extract positions, widths, heights from cell_features
2. Compute all pairwise distances using broadcasting:
positions_i = positions.unsqueeze(1) # [N, 1, 2]
positions_j = positions.unsqueeze(0) # [1, N, 2]
distances = positions_i - positions_j # [N, N, 2]
3. Calculate minimum separation distances for each pair
4. Use relu to get positive overlap amounts
5. Multiply overlaps in x and y to get overlap areas
6. Mask to only consider upper triangle (i < j)
7. Sum and normalize
Args:
cell_features: [N, 6] tensor with [area, num_pins, x, y, width, height]
pin_features: [P, 7] tensor with pin information (not used here)
edge_list: [E, 2] tensor with edges (not used here)
Returns:
Scalar loss value (should be 0 when no overlaps exist)
"""
def calculate_overlap_1d(x_or_y, w_or_h):
overlap = (w_or_h.unsqueeze(0) + w_or_h.unsqueeze(1)) / 2.
overlap = overlap - torch.abs(x_or_y.unsqueeze(0) - x_or_y.unsqueeze(1))
overlap = torch.relu(overlap)
overlap = torch.triu(overlap, diagonal=1)
return overlap
# N = cell_features.shape[0]
# if N <= 1:
# return torch.tensor(0.0, requires_grad=True)
# areas = cell_features[:, CellFeatureIdx.AREA] # = areas
# num_pins = cell_features[:, CellFeatureIdx.NUM_PINS] = num_pins_per_cell.float()
x = cell_features[:, CellFeatureIdx.X]
y = cell_features[:, CellFeatureIdx.Y]
w = cell_features[:, CellFeatureIdx.WIDTH]
h = cell_features[:, CellFeatureIdx.HEIGHT]
overlap_x = calculate_overlap_1d(x, w)
overlap_y = calculate_overlap_1d(y, h)
loss = overlap_x * overlap_y
# loss = torch.mean(loss)
num_non_zero = (loss != 0).sum().clamp(min=1)
loss = loss.sum() / num_non_zero
return loss
def train_placement(
cell_features,
pin_features,
edge_list,
num_epochs=None,
lr=LR,
lambda_wirelength=LAMBDA_WIRELENGTH,
lambda_overlap=LAMBDA_OVERLAP,
verbose=True,
log_interval=100,
):
"""Train the placement optimization using gradient descent.
Args:
cell_features: [N, 6] tensor with cell properties
pin_features: [P, 7] tensor with pin properties
edge_list: [E, 2] tensor with edge connectivity
num_epochs: Number of optimization iterations
lr: Learning rate for Adam optimizer
lambda_wirelength: Weight for wirelength loss
lambda_overlap: Weight for overlap loss
verbose: Whether to print progress
log_interval: How often to print progress
Returns:
Dictionary with:
- final_cell_features: Optimized cell positions
- initial_cell_features: Original cell positions (for comparison)
- loss_history: Loss values over time
"""
N = cell_features.shape[0]
def pareto_alternating_optimization(training_stage, epoch_curr):
"""Alternate objectives to find an optimal solution within the Pareto front."""
nonlocal scheduler
if (epoch_curr // PARETO_NUM_EPOCHS) % 2 == 0:
lambda_overlap_final = LAMBDA_PARETO if training_stage in {0, 2, 6} else lambda_overlap
lambda_wirelength_final = lambda_wirelength
else:
lambda_overlap_final = lambda_overlap
lambda_wirelength_final = LAMBDA_PARETO if training_stage in {0, 2, 6} else lambda_wirelength
if scheduler is not None:
scheduler = None
return lambda_overlap_final, lambda_wirelength_final
def overlap_optimization(training_stage):
"""Prioritize overlap objective."""
nonlocal scheduler
lambda_overlap_final = lambda_overlap
lambda_wirelength_final = 0.
if scheduler is None and N >= 25 and training_stage in {5, 7}:
for pg in optimizer.param_groups:
pg['lr'] = lr
scheduler = optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=get_stage_epochs(N, str(training_stage), TRAINING_STAGES), eta_min=lr * COS_LR_MIN_FCTR
)
return lambda_overlap_final, lambda_wirelength_final
def get_stage_epochs(N, training_stage, training_stages):
num_epochs_stage = training_stages[training_stage]
training_stage = int(training_stage)
if training_stage == 1:
if N < 50:
num_epochs_stage = 1500
elif N < 150:
num_epochs_stage = 4000
elif N < 250:
num_epochs_stage = 6000
elif training_stage in {3, 5, 7}:
if N < 150:
num_epochs_stage = 1000
elif N < 250:
num_epochs_stage = 2000
return num_epochs_stage
def get_cumulative_epochs(N, training_stages):
epoch_stages = set()
num_epochs_tot = 0
for training_stage in training_stages.keys():
num_epochs_tot += get_stage_epochs(N, training_stage, training_stages)
epoch_stages.add(num_epochs_tot)
return num_epochs_tot, epoch_stages
# @torch.compile
# def freeze_adam_state(optimizer, params):
# for p in params:
# # p.grad.zero_()
# p.grad = None
# state = optimizer.state[p]
# if state:
# state['exp_avg'].zero_()
# state['exp_avg_sq'].zero_()
# if 'max_exp_avg_sq' in state:
# state['max_exp_avg_sq'].zero_()
# Clone features and create learnable positions
cell_features = cell_features.clone()
initial_cell_features = cell_features.clone()
# Make only cell positions require gradients
cell_positions = cell_features[:, 2:4].clone().detach()
cell_positions.requires_grad_(True)
# Create optimizer
optimizer = optim.Adam([cell_positions], lr=lr) #, foreach=False, fused=False)
# optimizer = optim.SGD([cell_positions], lr=lr)
scheduler = None
# scheduler = optim.lr_scheduler.CosineAnnealingLR(
# optimizer, T_max=num_epochs, eta_min=lr*0.3
# )
# Initialize training stage and epochs
training_stage = 0
epoch_curr = 0
epoch_stages = None
if num_epochs is None:
num_epochs, epoch_stages = get_cumulative_epochs(N, TRAINING_STAGES)
# Initialize macro position freezing
freeze_macros = freeze_std_cells = False
areas = cell_features[:, CellFeatureIdx.AREA]
idx_macros = (areas >= MIN_MACRO_AREA) & (areas < MAX_MACRO_AREA)
idx_std_cells = torch.isin(areas, torch.tensor(STANDARD_CELL_AREAS))
# Track loss history
loss_history = {
"total_loss": [],
"wirelength_loss": [],
"overlap_loss": [],
}
# Training loop
for epoch in range(num_epochs):
optimizer.zero_grad()
# Create cell_features with current positions
cell_features_current = cell_features.clone()
cell_features_current[:, 2:4] = cell_positions
# Update training stage and macro position freezing
if epoch in epoch_stages:
training_stage += 1
if training_stage == 3 or training_stage >= 5:
freeze_macros = False
if training_stage >= 6:
freeze_std_cells = True
elif training_stage == 2 or training_stage == 4:
freeze_macros = True
epoch_curr = 0
# Set training hyperparams and optimization method
if training_stage % 2 == 0:
lambda_overlap_final, lambda_wirelength_final = pareto_alternating_optimization(training_stage, epoch_curr)
else:
lambda_overlap_final, lambda_wirelength_final = overlap_optimization(training_stage)
# Calculate losses
overlap_loss = overlap_repulsion_loss(cell_features_current, pin_features, edge_list)
wl_loss = wirelength_attraction_loss_jit(cell_features_current, pin_features, edge_list)
# Stage 0 should also regularize for centering the cells on the chip,
# as this leads to an easier manifold to optimize, and it reduces wirelength on average.
if training_stage in {0, 1}:
wl_loss += centering_regularization(cell_features_current, pin_features, edge_list)
# Combined loss
total_loss = lambda_wirelength_final * wl_loss + lambda_overlap_final * overlap_loss
# Backward pass
total_loss.backward()
# Gradient clipping to prevent extreme updates
torch.nn.utils.clip_grad_norm_([cell_positions], max_norm=2.5)
# Freeze appropriate cell positions based on training stage
if freeze_macros:
cell_positions.grad[idx_macros] = 0.
elif freeze_std_cells:
cell_positions[idx_std_cells].grad = None
# Update positions
optimizer.step()
if scheduler is not None:
scheduler.step()
# Record losses
if verbose and (epoch % log_interval == 0 or epoch == num_epochs - 1):
loss_history["total_loss"].append(total_loss.item())
loss_history["wirelength_loss"].append(wl_loss.item())
loss_history["overlap_loss"].append(overlap_loss.item())
# Log progress
if verbose and (epoch % log_interval == 0 or epoch == num_epochs - 1):
print(f"Epoch {epoch}/{num_epochs}:")
print(f" Total Loss: {total_loss.item():.6f}")
print(f" Wirelength Loss: {wl_loss.item():.6f}")
print(f" Overlap Loss: {overlap_loss.item():.6f}")
epoch_curr += 1
# Create final cell features
final_cell_features = cell_features.clone()
final_cell_features[:, 2:4] = cell_positions.detach()
return {
"final_cell_features": final_cell_features,
"initial_cell_features": initial_cell_features,
"loss_history": loss_history,
}
# ======= FINAL EVALUATION CODE (Don't edit this part) =======
def calculate_overlap_metrics(cell_features):
"""Calculate ground truth overlap statistics (non-differentiable).
This function provides exact overlap measurements for evaluation and reporting.
Unlike the loss function, this does NOT need to be differentiable.
Args:
cell_features: [N, 6] tensor with [area, num_pins, x, y, width, height]
Returns:
Dictionary with:
- overlap_count: number of overlapping cell pairs (int)
- total_overlap_area: sum of all overlap areas (float)
- max_overlap_area: largest single overlap area (float)
- overlap_percentage: percentage of total area that overlaps (float)
"""
N = cell_features.shape[0]
if N <= 1:
return {
"overlap_count": 0,
"total_overlap_area": 0.0,
"max_overlap_area": 0.0,
"overlap_percentage": 0.0,
}
# Extract cell properties
positions = cell_features[:, 2:4].detach().numpy() # [N, 2]
widths = cell_features[:, 4].detach().numpy() # [N]
heights = cell_features[:, 5].detach().numpy() # [N]
areas = cell_features[:, 0].detach().numpy() # [N]
overlap_count = 0
total_overlap_area = 0.0
max_overlap_area = 0.0
overlap_areas = []
# Check all pairs
for i in range(N):
for j in range(i + 1, N):
# Calculate center-to-center distances
dx = abs(positions[i, 0] - positions[j, 0])
dy = abs(positions[i, 1] - positions[j, 1])
# Minimum separation for non-overlap
min_sep_x = (widths[i] + widths[j]) / 2
min_sep_y = (heights[i] + heights[j]) / 2
# Calculate overlap amounts
overlap_x = max(0, min_sep_x - dx)
overlap_y = max(0, min_sep_y - dy)
# Overlap occurs only if both x and y overlap
if overlap_x > 0 and overlap_y > 0:
overlap_area = overlap_x * overlap_y
overlap_count += 1
total_overlap_area += overlap_area
max_overlap_area = max(max_overlap_area, overlap_area)
overlap_areas.append(overlap_area)
# Calculate percentage of total area
total_area = sum(areas)
overlap_percentage = (overlap_count / N * 100) if total_area > 0 else 0.0
return {
"overlap_count": overlap_count,
"total_overlap_area": total_overlap_area,
"max_overlap_area": max_overlap_area,
"overlap_percentage": overlap_percentage,
}
def calculate_cells_with_overlaps(cell_features):
"""Calculate number of cells involved in at least one overlap.
This metric matches the test suite evaluation criteria.
Args:
cell_features: [N, 6] tensor with cell properties
Returns:
Set of cell indices that have overlaps with other cells
"""
N = cell_features.shape[0]
if N <= 1:
return set()
# Extract cell properties
positions = cell_features[:, 2:4].detach().numpy()
widths = cell_features[:, 4].detach().numpy()
heights = cell_features[:, 5].detach().numpy()
cells_with_overlaps = set()
# Check all pairs
for i in range(N):
for j in range(i + 1, N):
# Calculate center-to-center distances
dx = abs(positions[i, 0] - positions[j, 0])
dy = abs(positions[i, 1] - positions[j, 1])
# Minimum separation for non-overlap
min_sep_x = (widths[i] + widths[j]) / 2
min_sep_y = (heights[i] + heights[j]) / 2
# Calculate overlap amounts
overlap_x = max(0, min_sep_x - dx)
overlap_y = max(0, min_sep_y - dy)
# Overlap occurs only if both x and y overlap
if overlap_x > 0 and overlap_y > 0:
cells_with_overlaps.add(i)
cells_with_overlaps.add(j)
return cells_with_overlaps
def calculate_normalized_metrics(cell_features, pin_features, edge_list):
"""Calculate normalized overlap and wirelength metrics for test suite.
These metrics match the evaluation criteria in the test suite.
Args:
cell_features: [N, 6] tensor with cell properties
pin_features: [P, 7] tensor with pin properties
edge_list: [E, 2] tensor with edge connectivity
Returns:
Dictionary with:
- overlap_ratio: (num cells with overlaps / total cells)
- normalized_wl: (wirelength / num nets) / sqrt(total area)
- num_cells_with_overlaps: number of unique cells involved in overlaps
- total_cells: total number of cells
- num_nets: number of nets (edges)
"""
N = cell_features.shape[0]
# Calculate overlap metric: num cells with overlaps / total cells
cells_with_overlaps = calculate_cells_with_overlaps(cell_features)
num_cells_with_overlaps = len(cells_with_overlaps)
overlap_ratio = num_cells_with_overlaps / N if N > 0 else 0.0
# Calculate wirelength metric: (wirelength / num nets) / sqrt(total area)
if edge_list.shape[0] == 0:
normalized_wl = 0.0
num_nets = 0
else:
# Calculate total wirelength using the loss function (unnormalized)
wl_loss = wirelength_attraction_loss(cell_features, pin_features, edge_list)
total_wirelength = wl_loss.item() * edge_list.shape[0] # Undo normalization
# Calculate total area
total_area = cell_features[:, 0].sum().item()
num_nets = edge_list.shape[0]
# Normalize: (wirelength / net) / sqrt(area)
# This gives a dimensionless quality metric independent of design size
normalized_wl = (total_wirelength / num_nets) / (total_area ** 0.5) if total_area > 0 else 0.0
return {
"overlap_ratio": overlap_ratio,
"normalized_wl": normalized_wl,
"num_cells_with_overlaps": num_cells_with_overlaps,
"total_cells": N,
"num_nets": num_nets,
}
def plot_placement(
initial_cell_features,
final_cell_features,
pin_features,
edge_list,
filename="placement_result.png",
):
"""Create side-by-side visualization of initial vs final placement.
Args:
initial_cell_features: Initial cell positions and properties
final_cell_features: Optimized cell positions and properties
pin_features: Pin information
edge_list: Edge connectivity
filename: Output filename for the plot
"""
try:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
# Plot both initial and final placements
for ax, cell_features, title in [
(ax1, initial_cell_features, "Initial Placement"),
(ax2, final_cell_features, "Final Placement"),
]:
N = cell_features.shape[0]
positions = cell_features[:, 2:4].detach().numpy()
widths = cell_features[:, 4].detach().numpy()
heights = cell_features[:, 5].detach().numpy()
# Draw cells
for i in range(N):
x = positions[i, 0] - widths[i] / 2
y = positions[i, 1] - heights[i] / 2
rect = Rectangle(
(x, y),
widths[i],
heights[i],
fill=True,
facecolor="lightblue",
edgecolor="darkblue",
linewidth=0.5,
alpha=0.7,
)
ax.add_patch(rect)
# Calculate and display overlap metrics
metrics = calculate_overlap_metrics(cell_features)
ax.set_aspect("equal")
ax.grid(True, alpha=0.3)
ax.set_title(
f"{title}\n"
f"Overlaps: {metrics['overlap_count']}, "
f"Total Overlap Area: {metrics['total_overlap_area']:.2f}",
fontsize=12,
)
# Set axis limits with margin
all_x = positions[:, 0]
all_y = positions[:, 1]
margin = 10
ax.set_xlim(all_x.min() - margin, all_x.max() + margin)
ax.set_ylim(all_y.min() - margin, all_y.max() + margin)
plt.tight_layout()
output_path = os.path.join(OUTPUT_DIR, filename)
plt.savefig(output_path, dpi=150, bbox_inches="tight")
plt.close()
except ImportError as e:
print(f"Could not create visualization: {e}")
print("Install matplotlib to enable visualization: pip install matplotlib")
# ======= MAIN FUNCTION =======
def main():
"""Main function demonstrating the placement optimization challenge."""
print("=" * 70)
print("VLSI CELL PLACEMENT OPTIMIZATION CHALLENGE")
print("=" * 70)
print("\nObjective: Implement overlap_repulsion_loss() to eliminate cell overlaps")
print("while minimizing wirelength.\n")
# Set random seed for reproducibility
torch.manual_seed(42)
# Generate placement problem
num_macros = 3
num_std_cells = 50
print(f"Generating placement problem:")
print(f" - {num_macros} macros")
print(f" - {num_std_cells} standard cells")
cell_features, pin_features, edge_list = generate_placement_input(
num_macros, num_std_cells
)
# Initialize positions with random spread to reduce initial overlaps
total_cells = cell_features.shape[0]
spread_radius = 30.0
angles = torch.rand(total_cells) * 2 * 3.14159
radii = torch.rand(total_cells) * spread_radius
cell_features[:, 2] = radii * torch.cos(angles)
cell_features[:, 3] = radii * torch.sin(angles)
# Calculate initial metrics
print("\n" + "=" * 70)
print("INITIAL STATE")
print("=" * 70)
initial_metrics = calculate_overlap_metrics(cell_features)
print(f"Overlap count: {initial_metrics['overlap_count']}")
print(f"Total overlap area: {initial_metrics['total_overlap_area']:.2f}")
print(f"Max overlap area: {initial_metrics['max_overlap_area']:.2f}")
print(f"Overlap percentage: {initial_metrics['overlap_percentage']:.2f}%")
# Run optimization
print("\n" + "=" * 70)
print("RUNNING OPTIMIZATION")
print("=" * 70)
result = train_placement(
cell_features,
pin_features,
edge_list,
verbose=True,
log_interval=200,
)
# Calculate final metrics (both detailed and normalized)
print("\n" + "=" * 70)
print("FINAL RESULTS")
print("=" * 70)
final_cell_features = result["final_cell_features"]
# Detailed metrics
final_metrics = calculate_overlap_metrics(final_cell_features)
print(f"Overlap count (pairs): {final_metrics['overlap_count']}")
print(f"Total overlap area: {final_metrics['total_overlap_area']:.2f}")
print(f"Max overlap area: {final_metrics['max_overlap_area']:.2f}")
# Normalized metrics (matching test suite)
print("\n" + "-" * 70)
print("TEST SUITE METRICS (for leaderboard)")
print("-" * 70)
normalized_metrics = calculate_normalized_metrics(
final_cell_features, pin_features, edge_list
)
print(f"Overlap Ratio: {normalized_metrics['overlap_ratio']:.4f} "
f"({normalized_metrics['num_cells_with_overlaps']}/{normalized_metrics['total_cells']} cells)")
print(f"Normalized Wirelength: {normalized_metrics['normalized_wl']:.4f}")
# Success check
print("\n" + "=" * 70)
print("SUCCESS CRITERIA")
print("=" * 70)
if normalized_metrics["num_cells_with_overlaps"] == 0:
print("✓ PASS: No overlapping cells!")
print("✓ PASS: Overlap ratio is 0.0")
print("\nCongratulations! Your implementation successfully eliminated all overlaps.")
print(f"Your normalized wirelength: {normalized_metrics['normalized_wl']:.4f}")
else:
print("✗ FAIL: Overlaps still exist")
print(f" Need to eliminate overlaps in {normalized_metrics['num_cells_with_overlaps']} cells")
print("\nSuggestions:")
print(" 1. Check your overlap_repulsion_loss() implementation")
print(" 2. Change lambdas (try increasing lambda_overlap)")
print(" 3. Change learning rate or number of epochs")
# Generate visualization
plot_placement(
result["initial_cell_features"],
result["final_cell_features"],
pin_features,
edge_list,
filename="placement_result.png",
)
if __name__ == "__main__":
main()