-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_sp_halo_exchange_conv.py
1092 lines (887 loc) · 33.7 KB
/
benchmark_sp_halo_exchange_conv.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
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023, The Ohio State University. All rights reserved.
# The MPI4DL software package is developed by the team members of
# The Ohio State University's Network-Based Computing Laboratory (NBCL),
# headed by Professor Dhabaleswar K. (DK) Panda.
#
#
# 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 torch
import torch.nn as nn
import torch.distributed as dist
import numpy as np
import os
import time
import math
import argparse
def get_parser():
parser = argparse.ArgumentParser(
description="Halo exchange benchmark",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--fp16-allreduce",
action="store_true",
default=False,
help="use fp16 compression during allreduce",
)
parser.add_argument("--image-size", type=int, default=8, help="Full image size")
parser.add_argument("--batch-size", type=int, default=1, help="input batch size")
parser.add_argument("--halo-len", type=int, default=1, help="halo length")
parser.add_argument("--warmup", type=int, default=10, help="warmups")
parser.add_argument("--iterations", type=int, default=100, help="Iterations")
parser.add_argument(
"--in-channels", type=int, default=1, help="number of input channels"
)
parser.add_argument(
"--out-channels", type=int, default=256, help="number of output channels"
)
parser.add_argument(
"--enable-val-recv-tensors",
action="store_true",
default=False,
help="Enable validation of recv tensors",
)
parser.add_argument(
"--enable-val-conv",
action="store_true",
default=False,
help="Enable validation of convolution",
)
parser.add_argument(
"--enable-val-small-conv",
action="store_true",
default=False,
help="Enable validation of sequential small validation with large",
)
parser.add_argument(
"--enable-deterministic",
action="store_true",
default=False,
help="Enable deterministic behaviour of cuDNN and PyTorch ",
)
parser.add_argument(
"--enable-one-h-dim-kernel",
action="store_true",
default=False,
help="Set dimension (height) of kernel to 1",
)
parser.add_argument(
"--enable-one-w-dim-kernel",
action="store_true",
default=False,
help="Set dimension (width) of kernel to 1",
)
parser.add_argument(
"--num-spatial-parts",
type=int,
default="4",
help="Number of partitions in spatial parallelism",
)
parser.add_argument(
"--slice-method",
type=str,
default="square",
help="Slice method (square, vertical, and horizontal)",
)
parser.add_argument("--CPU", action="store_true", default=False, help="Run on CPU")
return parser
parser_obj = get_parser()
args = parser_obj.parse_args()
ENABLE_VAL_RECV_TENSORS = args.enable_val_recv_tensors
ENABLE_VAL_CONV = args.enable_val_conv
ENABLE_VAL_SMALL_CONV = args.enable_val_small_conv
warmup = args.warmup
if args.enable_deterministic:
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.set_deterministic(True)
if args.CPU:
dev = "cpu"
else:
dev = "cuda"
class halo_bench_pt2pt(nn.Conv2d):
def __init__(
self,
local_rank,
comm_size,
kernel_size,
num_spatial_parts,
in_channels,
out_channels,
slice_method="square",
):
# slice_method: "vertical" "horizontal" "square"
self.slice_method = slice_method
self.local_rank = local_rank
self.comm_size = comm_size
self.spatial_local_rank = local_rank
# number of parts in one image
self.num_spatial_parts = num_spatial_parts
self.kernel_size = kernel_size
self.halo_len_height = int((kernel_size[0] - 1) / 2)
self.halo_len_width = int((kernel_size[1] - 1) / 2)
# self.halo_len = halo_len
self.shapes_recv = None
self.recv_tensors = []
self.dev = dev
self.get_neighbours()
self.get_neighbours_rank()
self.set_tags()
self.get_index_locations()
super(halo_bench_pt2pt, self).__init__(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
padding_mode="zeros",
)
def set_tags(self):
self.send_tag = [100, 200, 300, 400, 500, 600, 700, 800, 900]
self.recv_tag = [900, 800, 700, 600, 500, 400, 300, 200, 100]
def get_neighbours_rank(self):
self.rank_neighbours = []
if self.slice_method == "square":
# 0 1 2
# 2 3 4
# 5 6 7
total_rows = int(math.sqrt(self.comm_size))
total_cols = int(math.sqrt(self.comm_size))
# top_left will be (total_cols + 1) away from current rank
top_left = -(total_cols + 1)
top = -total_cols
top_right = -(total_cols - 1)
left = -1
right = +1
bottom_left = total_cols - 1
bottom = total_cols
bottom_right = total_cols + 1
rank_offset = [
top_left,
top,
top_right,
left,
0,
right,
bottom_left,
bottom,
bottom_right,
]
elif self.slice_method == "vertical":
rank_offset = [0, 0, 0, -1, 0, +1, 0, 0, 0]
elif self.slice_method == "horizontal":
rank_offset = [0, -1, 0, 0, 0, 0, 0, +1, 0]
for i in range(9):
if self.neighbours[i] == 1:
self.rank_neighbours.append(self.local_rank + rank_offset[i])
else:
self.rank_neighbours.append(-1)
def set_neighbours_based_on_kernel_size(self):
if self.kernel_size[0] == 1:
self.neighbours[0] = 0
self.neighbours[1] = 0
self.neighbours[2] = 0
self.neighbours[6] = 0
self.neighbours[7] = 0
self.neighbours[8] = 0
if self.kernel_size[1] == 1:
self.neighbours[0] = 0
self.neighbours[3] = 0
self.neighbours[6] = 0
self.neighbours[2] = 0
self.neighbours[5] = 0
self.neighbours[8] = 0
def get_neighbours(self):
if self.spatial_local_rank < self.num_spatial_parts:
self.ENABLE_SPATIAL = True
else:
self.ENABLE_SPATIAL = False
self.neighbours = None
return
self.spatial_rank = self.spatial_local_rank
# Neighbour
# 0 1 2
# 3 4 5
# 6 7 8
if self.slice_method == "square":
self.neighbours = []
total_rows = int(math.sqrt(self.num_spatial_parts))
total_cols = int(math.sqrt(self.num_spatial_parts))
# current rank position in matrix of total_rows * total_cols
row = self.local_rank / total_rows
col = self.local_rank % total_cols
dir = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 0],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
]
for d in dir:
neighbour_row = row + d[0]
neighbour_col = col + d[1]
if neighbour_row == row and neighbour_col == col:
self.neighbours.append(0)
elif (
neighbour_row < 0
or neighbour_row >= total_rows
or neighbour_col < 0
or neighbour_col >= total_cols
):
self.neighbours.append(0)
else:
self.neighbours.append(1)
elif self.slice_method == "vertical":
if self.spatial_rank == 0:
self.neighbours = [0, 0, 0, 0, 0, 1, 0, 0, 0]
elif self.spatial_rank == self.num_spatial_parts - 1:
self.neighbours = [0, 0, 0, 1, 0, 0, 0, 0, 0]
else:
self.neighbours = [0, 0, 0, 1, 0, 1, 0, 0, 0]
elif self.slice_method == "horizontal":
if self.spatial_rank == 0:
self.neighbours = [0, 0, 0, 0, 0, 0, 0, 1, 0]
elif self.spatial_rank == self.num_spatial_parts - 1:
self.neighbours = [0, 1, 0, 0, 0, 0, 0, 0, 0]
else:
self.neighbours = [0, 1, 0, 0, 0, 0, 0, 1, 0]
self.set_neighbours_based_on_kernel_size()
def get_index_locations(self):
locations_recv = []
locations_recv.append(
[[None, self.halo_len_height], [None, self.halo_len_width]]
) # 1
locations_recv.append(
[
[None, self.halo_len_height],
[
self.halo_len_width,
-self.halo_len_width if self.halo_len_width else None,
],
]
) # 2
locations_recv.append(
[[None, self.halo_len_height], [-self.halo_len_width, None]]
) # 3
locations_recv.append(
[
[
self.halo_len_height,
-self.halo_len_height if self.halo_len_height else None,
],
[None, self.halo_len_width],
]
) # 4
locations_recv.append([[None, None], [None, None]]) # 5
locations_recv.append(
[
[
self.halo_len_height,
-self.halo_len_height if self.halo_len_height else None,
],
[-self.halo_len_width, None],
]
) # 6
locations_recv.append(
[[-self.halo_len_height, None], [None, self.halo_len_width]]
) # 7
locations_recv.append(
[
[-self.halo_len_height, None],
[
self.halo_len_width,
-self.halo_len_width if self.halo_len_width else None,
],
]
) # 8
locations_recv.append(
[[-self.halo_len_height, None], [-self.halo_len_width, None]]
) # 9
self.locations_recv = locations_recv
locations_send = []
locations_send.append(
[
[self.halo_len_height, 2 * self.halo_len_height],
[self.halo_len_width, 2 * self.halo_len_width],
]
) # 1
locations_send.append(
[
[self.halo_len_height, 2 * self.halo_len_height],
[
self.halo_len_width,
-self.halo_len_width if self.halo_len_width else None,
],
]
) # 2
locations_send.append(
[
[self.halo_len_height, 2 * self.halo_len_height],
[-2 * self.halo_len_width, -1 * self.halo_len_width],
]
) # 3
locations_send.append(
[
[
self.halo_len_height,
-self.halo_len_height if self.halo_len_height else None,
],
[self.halo_len_width, 2 * self.halo_len_width],
]
) # 4
locations_send.append([[None, None], [None, None]]) # 5
locations_send.append(
[
[
self.halo_len_height,
-self.halo_len_height if self.halo_len_height else None,
],
[-2 * self.halo_len_width, -1 * self.halo_len_width],
]
) # 6
locations_send.append(
[
[-2 * self.halo_len_height, -1 * self.halo_len_height],
[self.halo_len_width, 2 * self.halo_len_width],
]
) # 7
locations_send.append(
[
[-2 * self.halo_len_height, -1 * self.halo_len_height],
[
self.halo_len_width,
-self.halo_len_width if self.halo_len_width else None,
],
]
) # 8
locations_send.append(
[
[-2 * self.halo_len_height, -1 * self.halo_len_height],
[-2 * self.halo_len_width, -1 * self.halo_len_width],
]
) # 9
self.locations_send = locations_send
def get_shapes_recv(self, shapes):
shapes_recv = []
shapes_recv.append([self.halo_len_height, self.halo_len_width]) # 1
shapes_recv.append(
[self.halo_len_height, shapes[3] - 2 * self.halo_len_width]
) # 2
shapes_recv.append([self.halo_len_height, self.halo_len_width]) # 3
shapes_recv.append(
[shapes[2] - 2 * self.halo_len_height, self.halo_len_width]
) # 4
shapes_recv.append([None, None]) # 5
shapes_recv.append(
[shapes[2] - 2 * self.halo_len_height, self.halo_len_width]
) # 6
shapes_recv.append([self.halo_len_height, self.halo_len_width]) # 7
shapes_recv.append(
[self.halo_len_height, shapes[3] - 2 * self.halo_len_width]
) # 8
shapes_recv.append([self.halo_len_height, self.halo_len_width]) # 9
return shapes_recv
def start_halo_exchange(self, halo_input):
req = []
for i in range(9):
if self.neighbours[i] == 1:
temp = (
halo_input[
:,
:,
self.locations_send[i][0][0] : self.locations_send[i][0][1],
self.locations_send[i][1][0] : self.locations_send[i][1][1],
]
.clone()
.detach()
)
if self.dev == "cuda":
torch.cuda.synchronize()
temp_req = dist.isend(
temp, self.rank_neighbours[i], tag=self.send_tag[i]
)
req.append(temp_req)
self.send_tag[i] += 1
self.recv_tensors = []
shapes = halo_input.shape
self.halo_input_shape = shapes
if self.shapes_recv == None:
self.shapes_recv = self.get_shapes_recv(shapes)
for i in range(9):
if self.neighbours[i] == 1:
temp_tensor = torch.zeros(
shapes[0],
shapes[1],
self.shapes_recv[i][0],
self.shapes_recv[i][1],
dtype=torch.float,
device=self.dev,
)
"""
Synchronization is necessary at this point as all GPU operations
in PyTorch are asynchronous. MPI copy operation is not under
PyTorch therefore it can start before pytorch finishes
initilization of tensor with zeros.
It will lead to data corruption
Spent 1 week on this issue (data validation)
KEEP THIS IN MIND
"""
if self.dev == "cuda":
torch.cuda.synchronize()
temp_req = dist.irecv(
tensor=temp_tensor,
src=self.rank_neighbours[i],
tag=self.recv_tag[i],
)
req.append(temp_req)
self.recv_tag[i] += 1
self.recv_tensors.append(temp_tensor)
else:
self.recv_tensors.append([])
return req
def end_halo_exchange(self, reqs):
for req in reqs:
req.wait()
def copy_halo_exchange_values(self, halo_input):
for i in range(9):
if self.neighbours[i] == 1:
halo_input[
:,
:,
self.locations_recv[i][0][0] : self.locations_recv[i][0][1],
self.locations_recv[i][1][0] : self.locations_recv[i][1][1],
] = self.recv_tensors[i]
def run(self, tensor):
reqs = self.start_halo_exchange(tensor)
self.end_halo_exchange(reqs)
self.copy_halo_exchange_values(tensor)
# torch.cuda.synchronize()
res_final = super(halo_bench_pt2pt, self).forward(tensor)
return tensor, res_final
def env2int(env_list, default=-1):
for e in env_list:
val = int(os.environ.get(e, -1))
if val >= 0:
return val
return default
def initialize_cuda():
my_local_rank = env2int(
["MPI_LOCALRANKID", "OMPI_COMM_WORLD_LOCAL_RANK", "MV2_COMM_WORLD_LOCAL_RANK"],
0,
)
os.environ["CUDA_VISIBLE_DEVICES"] = str(my_local_rank % 4)
torch.cuda.init()
def init_comm(backend="mpi"):
"""Initialize the distributed environment."""
dist.init_process_group(backend)
size = dist.get_world_size()
rank = dist.get_rank()
return size, rank
def create_input_vertical(kernel_size, halo_len, image_size, comm_size, rank):
image_height_local = int(image_size[0])
image_width_local = int(image_size[1] / comm_size)
halo_len_height = int((kernel_size[0] - 1) / 2)
halo_len_width = int((kernel_size[1] - 1) / 2)
np_x = np.asarray(
list(
range(0, args.batch_size * args.in_channels * image_size[0] * image_size[1])
),
dtype=np.float32,
)
np_x.resize(args.batch_size, args.in_channels, image_size[0], image_size[1])
pad_width = [
(0, 0),
(0, 0),
(halo_len_height, halo_len_height),
(halo_len_width, halo_len_width),
]
expected_output = np.pad(np_x, pad_width=pad_width, mode="constant")
expected_out_width = image_width_local + 2 * halo_len_width
expected_out_height = image_height_local + 2 * halo_len_height
start_left = rank * image_width_local
end_right = (rank + 1) * image_width_local + 2 * halo_len_width
if rank == comm_size - 1:
# In case of odd number of GPUs, partition size will be uneven and last
# rank will receive remaining image
expected_output = expected_output[:, :, :, start_left:]
else:
expected_output = expected_output[:, :, :, start_left:end_right]
start_left_i = rank * image_width_local
end_right_i = (rank + 1) * image_width_local
if rank == comm_size - 1:
# In case of odd number of GPUs, partition size will be uneven and last
# rank will receive remaining image
input_local = np_x[:, :, :, start_left_i:]
else:
input_local = np_x[:, :, :, start_left_i:end_right_i]
input_tensor_local = torch.tensor(input_local, dtype=torch.float, device=dev)
pads = nn.ZeroPad2d(
(halo_len_width, halo_len_width, halo_len_height, halo_len_height)
)
input_tensor_local = pads(input_tensor_local)
return input_tensor_local, expected_output
def create_input_horizontal(kernel_size, halo_len, image_size, comm_size, rank):
image_height_local = int(image_size[0] / comm_size)
image_width_local = int(image_size[1])
halo_len_height = int((kernel_size[0] - 1) / 2)
halo_len_width = int((kernel_size[1] - 1) / 2)
np_x = np.asarray(
list(
range(0, args.batch_size * args.in_channels * image_size[0] * image_size[1])
),
dtype=np.float32,
)
np_x.resize(args.batch_size, args.in_channels, image_size[0], image_size[1])
pad_width = [
(0, 0),
(0, 0),
(halo_len_height, halo_len_height),
(halo_len_width, halo_len_width),
]
expected_output = np.pad(np_x, pad_width=pad_width, mode="constant")
expected_out_width = image_width_local + 2 * halo_len_width
expected_out_height = image_height_local + 2 * halo_len_height
start_top = rank * image_height_local
end_bottom = (rank + 1) * image_height_local + 2 * halo_len_height
if rank == comm_size - 1:
# In case of odd number of GPUs, partition size will be uneven and last
# rank will receive remaining image
expected_output = expected_output[:, :, start_top:, :]
else:
expected_output = expected_output[:, :, start_top:end_bottom, :]
start_top_i = rank * image_height_local
end_bottom_i = (rank + 1) * image_height_local
if rank == comm_size - 1:
# In case of odd number of GPUs, partition size will be uneven and last
# rank will receive remaining image
input_local = np_x[:, :, start_top_i:, :]
else:
input_local = np_x[:, :, start_top_i:end_bottom_i, :]
input_tensor_local = torch.tensor(input_local, dtype=torch.float, device=dev)
pads = nn.ZeroPad2d(
(halo_len_width, halo_len_width, halo_len_height, halo_len_height)
)
input_tensor_local = pads(input_tensor_local)
return input_tensor_local, expected_output
def create_input_square(kernel_size, halo_len, image_size, comm_size, rank):
image_height_local = int(image_size[0] / math.sqrt(comm_size))
image_width_local = int(image_size[1] / math.sqrt(comm_size))
halo_len_height = int((kernel_size[0] - 1) / 2)
halo_len_width = int((kernel_size[1] - 1) / 2)
np_x = np.asarray(
list(
range(0, args.batch_size * args.in_channels * image_size[0] * image_size[1])
),
dtype=np.float32,
)
np_x.resize(args.batch_size, args.in_channels, image_size[0], image_size[1])
pad_width = [
(0, 0),
(0, 0),
(halo_len_height, halo_len_height),
(halo_len_width, halo_len_width),
]
expected_output = np.pad(np_x, pad_width=pad_width, mode="constant")
print(f"Overall Expected output shape {expected_output.shape}")
expected_out_width = image_width_local + 2 * halo_len_width
expected_out_height = image_height_local + 2 * halo_len_height
total_rows = int(math.sqrt(comm_size))
total_cols = int(math.sqrt(comm_size))
# position of rank in matrix math.sqrt(comm_size) * math.sqrt(comm_size)
row = int(rank / total_rows)
col = int(rank % total_cols)
e_left_idx = col * image_width_local
e_right_idx = (col + 1) * image_width_local + 2 * halo_len_width
e_top_idx = row * image_height_local
e_bottom_idx = (row + 1) * image_height_local + 2 * halo_len_height
expected_output = expected_output[
:, :, e_top_idx:e_bottom_idx, e_left_idx:e_right_idx
]
left_idx = col * image_width_local
right_idx = (col + 1) * image_width_local
top_idx = row * image_height_local
bottom_idx = (row + 1) * image_height_local
input_local = np_x[:, :, top_idx:bottom_idx, left_idx:right_idx]
input_tensor_local = torch.tensor(input_local, dtype=torch.float, device=dev)
pads = nn.ZeroPad2d(
(halo_len_width, halo_len_width, halo_len_height, halo_len_height)
)
input_tensor_local = pads(input_tensor_local)
return input_tensor_local, expected_output
def test_output_square(image_size, output, expected_output, rank, size, mode="CONV"):
# only padding == halo_len case is supported
image_height_local = int(image_size[0] / math.sqrt(size))
image_width_local = int(image_size[1] / math.sqrt(size))
expected_out_width = image_width_local
expected_out_height = image_height_local
total_rows = int(math.sqrt(size))
total_cols = int(math.sqrt(size))
row = int(rank / total_rows)
col = int(rank % total_cols)
e_left_idx = col * expected_out_width
e_right_idx = (col + 1) * expected_out_width
e_top_idx = row * expected_out_height
e_bottom_idx = (row + 1) * expected_out_height
expected_output = expected_output[
:, :, e_top_idx:e_bottom_idx, e_left_idx:e_right_idx
]
expected_output = expected_output.detach().cpu().numpy()
output = output.detach().cpu().numpy()
if np.equal(output, expected_output).all():
print(f"{mode} : Validation passed for rank: {rank}")
else:
print(f"{mode} : Validation failed for rank: {rank}")
def test_output_vertical(image_size, output, expected_output, rank, size, mode="CONV"):
# only padding == halo_len case is supported
image_height_local = int(image_size[0] / (size))
image_width_local = int(image_size[1] / (size))
expected_out_width = image_width_local
expected_out_height = image_height_local
start_left = rank * image_width_local
end_right = (rank + 1) * image_width_local
if rank == size - 1:
# In case of odd number of GPUs, partition size will be uneven and last
# rank will receive remaining image
expected_output = expected_output[:, :, :, start_left:]
else:
expected_output = expected_output[:, :, :, start_left:end_right]
expected_output = expected_output.detach().cpu().numpy()
output = output.detach().cpu().numpy()
if np.equal(output, expected_output).all():
print(f"{mode} : Validation passed for rank: {rank}")
else:
print(f"{mode} : Validation failed for rank: {rank}")
def test_output_horizontal(
image_size, output, expected_output, rank, size, mode="CONV"
):
# only padding == halo_len case is supported
image_height_local = int(image_size[0] / (size))
image_width_local = int(image_size[1] / (size))
expected_out_width = image_width_local
expected_out_height = image_height_local
start_top = rank * image_height_local
end_bottom = (rank + 1) * image_height_local
if rank == size - 1:
expected_output = expected_output[:, :, start_top:, :]
else:
expected_output = expected_output[:, :, start_top:end_bottom, :]
expected_output = expected_output.detach().cpu().numpy()
output = output.detach().cpu().numpy()
if np.equal(output, expected_output).all():
print(f"{mode} : Validation passed for rank: {rank}")
else:
print(f"{mode} : Validation failed for rank: {rank}")
def test_output_recv(output, expected_output, rank):
np_out = output.to("cpu").numpy()
if np.equal(np_out, expected_output).all():
print(f"Validation passed for rank: {rank}")
else:
uneq = np.not_equal(np_out.astype("int"), expected_output.astype("int"))
print(
f"Rank : {rank} => Received : {np_out[uneq]} Expected : {expected_output[uneq]}"
)
print(f"Validation failed for rank: {rank}")
halo_len = args.halo_len
iterations = args.iterations
kernel_size = [2 * halo_len + 1, 2 * halo_len + 1]
if args.enable_one_h_dim_kernel:
kernel_size[0] = 1
if args.enable_one_w_dim_kernel:
kernel_size[1] = 1
halo_len_height = int((kernel_size[0] - 1) / 2)
halo_len_width = int((kernel_size[1] - 1) / 2)
initialize_cuda()
size, rank = init_comm()
image_size = (args.image_size, args.image_size)
if args.slice_method == "vertical":
input_tensor_local, expected_output_recv = create_input_vertical(
kernel_size=kernel_size,
halo_len=halo_len,
image_size=image_size,
comm_size=size,
rank=rank,
)
elif args.slice_method == "horizontal":
input_tensor_local, expected_output_recv = create_input_horizontal(
kernel_size=kernel_size,
halo_len=halo_len,
image_size=image_size,
comm_size=size,
rank=rank,
)
elif args.slice_method == "square":
input_tensor_local, expected_output_recv = create_input_square(
kernel_size=kernel_size,
halo_len=halo_len,
image_size=image_size,
comm_size=size,
rank=rank,
)
print(
f"Size of input:{input_tensor_local.shape} Size of Output:{expected_output_recv.shape}"
)
b_pt2pt = halo_bench_pt2pt(
local_rank=rank,
comm_size=size,
kernel_size=kernel_size,
num_spatial_parts=args.num_spatial_parts,
in_channels=args.in_channels,
out_channels=args.out_channels,
slice_method=args.slice_method,
)
if ENABLE_VAL_CONV or ENABLE_VAL_SMALL_CONV:
b_pt2pt.weight.data.fill_(1.0)
b_pt2pt.bias.data.fill_(1.0)
if dev == "cuda":
# transmit to cuda
b_pt2pt.cuda()
for i in range(warmup):
recv, y = b_pt2pt.run(input_tensor_local)
# Time event
if dev == "cuda":
start_event = torch.cuda.Event(enable_timing=True, blocking=True)
end_event = torch.cuda.Event(enable_timing=True, blocking=True)
start_event.record()
else:
start_time = time.time()
# Run benchmarking for spatial conv
for i in range(iterations):
recv, y = b_pt2pt.run(input_tensor_local)
if dev == "cuda":
torch.cuda.synchronize()
output = y
if dev == "cuda":
end_event.record()
torch.cuda.synchronize()
t = start_event.elapsed_time(end_event)
else:
t = (time.time() - start_time) * 1000
print(f"Rank: {rank} Time taken (ms): {(t / iterations)}")
if ENABLE_VAL_RECV_TENSORS:
test_output_recv(recv, expected_output_recv, rank)
"""
Sequential processing of large input
"""
# create input for sequential processing
input_seq = np.asarray(
list(range(0, args.batch_size * args.in_channels * image_size[0] * image_size[1])),
dtype=np.float32,
)
input_seq.resize(args.batch_size, args.in_channels, image_size[0], image_size[1])
input_tensor_seq = torch.tensor(input_seq, dtype=torch.float, device=dev)
conv_seq = nn.Conv2d(
args.in_channels,
args.out_channels,
kernel_size=kernel_size,
stride=1,
padding=(halo_len_height, halo_len_width),
dilation=1,
groups=1,
bias=True,
padding_mode="zeros",
)
if ENABLE_VAL_CONV or ENABLE_VAL_SMALL_CONV:
conv_seq.weight.data.fill_(1.0)
conv_seq.bias.data.fill_(1.0)
if dev == "cuda":
conv_seq = conv_seq.cuda()
torch.cuda.synchronize()
# warmup iterations
for i in range(warmup):
y = conv_seq.forward(input_tensor_seq)
if dev == "cuda":
start_event_seq = torch.cuda.Event(enable_timing=True, blocking=True)
end_event_seq = torch.cuda.Event(enable_timing=True, blocking=True)
start_event_seq.record()
else:
start_time = time.time()