module_store.py 34.3 KB
Newer Older
sunzhq2's avatar
init  
sunzhq2 committed
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# Copyright 2023 ByteDance and/or its affiliates.
#
# 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 math
import random

import torch
import torch.distributed as dist

def gemm_compute_size(input_shapes, torch_dtype):
    # input_shapes: [[M, K], [K, N]]
    a_shape, b_shape = input_shapes
    M, _ = a_shape
    _, N = b_shape
    d_shape = [M, N]

    # get element_size and dtype_size
    input_element_num = sum([math.prod(shape) for shape in [a_shape, b_shape]])
    output_element_num = sum([math.prod(shape) for shape in [d_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()

    input_tensor_size = dtype_size * input_element_num
    if torch_dtype == torch.int8:
        output_tensor_size = 4 * output_element_num
    else:
        output_tensor_size = dtype_size * output_element_num
    batch_size = M
    tensor_size = input_tensor_size + output_tensor_size
    return (batch_size, tensor_size, input_tensor_size, output_tensor_size)

def gemm_create_tensors(input_shapes, torch_dtype, xpu_device):
    # input_shapes: [[M, K], [K, N]]
    a_shape, b_shape = input_shapes
    M, _ = a_shape
    _, N = b_shape
    d_shape = [M, N]

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)
    b_tensor = torch.randint(0, 7, b_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    d_tensor = torch.randint(0, 7, d_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor, b_tensor, d_tensor]






def batch_gemm_compute_size(input_shapes, torch_dtype):
    # input_shapes: [[bs, M, K], [bs, K, N]]
    a_shape, b_shape = input_shapes
    bs, M, _ = a_shape
    bs, _, N = b_shape
    d_shape = [bs, M, N]

    # get element_size and dtype_size
    input_element_num = sum([math.prod(shape) for shape in [a_shape, b_shape]])
    output_element_num = sum([math.prod(shape) for shape in [d_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()

    input_tensor_size = dtype_size * input_element_num
    if torch_dtype == torch.int8:
        output_tensor_size = 4 * output_element_num
    else:
        output_tensor_size = dtype_size * output_element_num
    batch_size = bs
    tensor_size = input_tensor_size + output_tensor_size
    return (batch_size, tensor_size, input_tensor_size, output_tensor_size)

def batch_gemm_create_tensors(input_shapes, torch_dtype, xpu_device):
    # input_shapes: [[bs, M, K], [bs, K, N]]
    a_shape, b_shape = input_shapes
    bs, M, _ = a_shape
    bs, _, N = b_shape
    d_shape = [bs, M, N]

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)
    b_tensor = torch.randint(0, 7, b_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    d_tensor = torch.randint(0, 7, d_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor, b_tensor, d_tensor]



def group_gemm_compute_size(input_shapes, torch_dtype):
    """
    [
        [[M1, K1], [K1, N1]], 
        [[M2, K2], [K2, N2]]
    ]
    """

    input_tensor_size = 0
    output_tensor_size = 0

    for problem_shape in input_shapes:
        a_shape, b_shape = problem_shape
        M, _ = a_shape
        _, N = b_shape
        d_shape = [M, N]
        
        # get element_size and dtype_size
        input_element_num = sum([math.prod(shape) for shape in [a_shape, b_shape]])
        output_element_num = sum([math.prod(shape) for shape in [d_shape]])
        dtype_size = torch.tensor([], dtype=torch_dtype).element_size()

        input_tensor_size += dtype_size * input_element_num
        if torch_dtype == torch.int8:
            output_tensor_size += 4 * output_element_num
        else:
            output_tensor_size += dtype_size * output_element_num
        batch_size = 1
        tensor_size = input_tensor_size + output_tensor_size

    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def group_gemm_create_tensors(input_shapes, torch_dtype, xpu_device):
    """
    [
        [[M1, K1], [K1, N1]],
        [[M2, K2], [K2, N2]]
    ]
    """
    left_tensors = []
    right_tensors = []
    output_tensors = []

    for problem_shape in input_shapes:
        a_shape, b_shape = problem_shape
        M, _ = a_shape
        _, N = b_shape
        d_shape = [M, N]

        # create input tensors
        left_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)
        right_tensor = torch.randint(0, 7, b_shape, dtype=torch_dtype, device=xpu_device)

        # create output tensors
        output_tensor = torch.randint(0, 7, d_shape, dtype=torch_dtype, device=xpu_device)

        left_tensors.append(left_tensor)
        right_tensors.append(right_tensor)
        output_tensors.append(output_tensor)

    return [left_tensors, right_tensors, output_tensors]



def sin_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    c_shape = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    batch_size = c_shape[0]
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def sin_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    c_shape = a_shape

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor, c_tensor]


def cast_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    c_shape = a_shape
    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])

    if torch_dtype == torch.float32:
        dst_torch_dtype = torch.bfloat16
    elif torch_dtype == torch.bfloat16 or torch_dtype == torch.float16:
        dst_torch_dtype = torch.float32
    elif torch_dtype == torch.int8:
        dst_torch_dtype = torch.int32
    else:
        dst_torch_dtype = torch_dtype

    src_dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    dst_dtype_size = torch.tensor([], dtype=dst_torch_dtype).element_size()

    input_tensor_size = src_dtype_size * input_element_num
    output_tensor_size = dst_dtype_size * output_element_num

    batch_size = c_shape[0]
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def cast_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    c_shape = a_shape

    if torch_dtype == torch.float32:
        dst_torch_dtype = torch.bfloat16
    elif torch_dtype == torch.bfloat16 or torch_dtype == torch.float16:
        dst_torch_dtype = torch.float32
    elif torch_dtype == torch.int8:
        dst_torch_dtype = torch.int32
    else:
        dst_torch_dtype = torch_dtype  

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=dst_torch_dtype, device=xpu_device)

    return [a_tensor, c_tensor]


def swiglu_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    input_tensor_shape = [batch_size, hidden_size]
    output_tensor_shape = [batch_size, hidden_size]

    input_element_num = sum([math.prod(shape) for shape in [input_tensor_shape]])
    output_element_num = sum([math.prod(shape) for shape in [output_tensor_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size


def swiglu_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    input_tensor_shape = [batch_size, hidden_size]
    output_tensor_shape = [batch_size, hidden_size]

    # create input tensors
    input_tensor = torch.randint(0, 7, input_tensor_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    output_tensor = torch.randint(0, 7, output_tensor_shape, dtype=torch_dtype, device=xpu_device)

    return [input_tensor, output_tensor]



def add_compute_size(input_shapes, torch_dtype):
    a_shape, b_shape = input_shapes
    c_shape = a_shape
    batch_size, hidden_size = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape, b_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()

    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def add_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, b_shape = input_shapes
    c_shape = a_shape

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)
    b_tensor = torch.randint(0, 7, b_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor, b_tensor, c_tensor]


def layer_norm_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape
    w_shape = a_shape[-1:]

    input_element_num = sum([math.prod(shape) for shape in [a_shape, w_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def layer_norm_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape
    w_shape = a_shape[-1:]

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)

    # create weight tensors
    w_tensor = torch.randint(0, 7, w_shape, dtype=torch_dtype, device=xpu_device)

    return [a_tensor, c_tensor, w_tensor]



def softmax_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()

    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def softmax_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor, c_tensor]



def reduce_sum_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = [batch_size, 1]

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size

    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def reduce_sum_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = [batch_size, 1]

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)

    return [a_tensor, c_tensor]


def reduce_min_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    values_shape = [batch_size, 1]
    indices_shape = [batch_size, 1]
    
    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    values_element_num = sum([math.prod(shape) for shape in [values_shape]])
    indices_element_num = sum([math.prod(shape) for shape in [indices_shape]])
    
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    indices_dtype_size = torch.tensor([], dtype=torch.int64).element_size()

    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * values_element_num + indices_dtype_size * indices_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def reduce_min_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    values_shape = [batch_size, 1]
    indices_shape = [batch_size, 1]

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    values_tensor = torch.randint(0, 7, values_shape, dtype=torch_dtype, device=xpu_device)
    indices_tensor = torch.randint(0, 7, indices_shape, dtype=torch.int64, device=xpu_device)

    return [a_tensor, values_tensor, indices_tensor]




def index_add_compute_size(input_shapes, torch_dtype):
    # src_tensor -->(index_tensor) dst_tensor
    dst_shape, src_shape = input_shapes

    src_batch_size = src_shape[0]
    dst_batch_size = dst_shape[0]

    index_shape = [src_batch_size]

    
    src_element_num = sum([math.prod(shape) for shape in [src_shape]])
    index_element_num = sum([math.prod(shape) for shape in [index_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    index_dtype_size = torch.tensor([], dtype=torch.int64).element_size()

    src_tensor_size = dtype_size * src_element_num
    index_tensor_size = index_dtype_size * index_element_num


    input_tensor_size = 2 * src_tensor_size + index_tensor_size
    output_tensor_size = src_tensor_size
    tensor_size = input_tensor_size + output_tensor_size

    return src_batch_size, tensor_size, input_tensor_size, output_tensor_size


def index_add_create_tensors(input_shapes, torch_dtype, xpu_device):
    # src_tensor -->(index_tensor) dst_tensor
    dst_shape, src_shape = input_shapes

    src_batch_size = src_shape[0]
    dst_batch_size = dst_shape[0]

    index_shape = [src_batch_size]

    # create output tensors
    dst_tensor = torch.randint(0, 7, dst_shape, dtype=torch_dtype, device=xpu_device)

    # create input tensors
    src_tensor = torch.randint(0, 7, src_shape, dtype=torch_dtype, device=xpu_device)
    index_tensor = torch.randint(0, dst_batch_size, index_shape, dtype=torch.int64, device=xpu_device)
    
    return [dst_tensor, src_tensor, index_tensor]


def sort_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])
    indice_element_num  = output_element_num

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    indice_dtype_size = torch.tensor([], dtype=torch.int64).element_size()

    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num + indice_dtype_size * indice_element_num
    tensor_size = input_tensor_size + output_tensor_size

    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def sort_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    # create input tensors
    a_tensor = torch.randint(0, 7, a_shape, dtype=torch_dtype, device=xpu_device)

    # create output tensors
    c_tensor = torch.randint(0, 7, c_shape, dtype=torch_dtype, device=xpu_device)
    indice_tensor = torch.randint(0, 7, c_shape, dtype=torch.int64, device=xpu_device)
    return [a_tensor, c_tensor, indice_tensor]


def unique_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [c_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    indice_dtype_size = torch.tensor([], dtype=torch.int64).element_size()

    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num + indice_dtype_size * output_element_num
    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def unique_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    c_shape = a_shape

    # create input tensors
    torch.manual_seed(1)
    a_tensor = torch.randint(0, 1024, a_shape, dtype=torch_dtype, device="cpu").to(device=xpu_device)

    # create output tensors
    c_tensor = torch.empty(c_shape, dtype=torch_dtype, device=xpu_device)
    count_tensor = torch.empty(c_shape, dtype=torch.int64, device=xpu_device)
    return [a_tensor, c_tensor, count_tensor]




def scatter_compute_size(input_shapes, torch_dtype):
    tensor_shape = input_shapes[0]
    batch_size, hidden_size = tensor_shape
    index_shape = [batch_size]

    input_element_num = sum([math.prod(shape) for shape in [tensor_shape]])
    output_element_num = sum([math.prod(shape) for shape in [tensor_shape]])
    index_element_num = sum([math.prod(shape) for shape in [index_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    index_dtype_size = torch.tensor([], dtype=torch.int64).element_size()

    input_element_num = dtype_size * input_element_num + index_dtype_size * index_element_num
    output_element_num = dtype_size * output_element_num
    tensor_size = input_element_num + output_element_num
    return batch_size, tensor_size, input_element_num, output_element_num

def scatter_create_tensors(input_shapes, torch_dtype, xpu_device):
    tensor_shape = input_shapes[0]
    batch_size, hidden_size = tensor_shape
    index_shape = [batch_size]

    # create output tensors
    dst_tensor = torch.randint(0, 7, tensor_shape, dtype=torch_dtype, device=xpu_device)

    # create input tensors
    src_tensor = torch.randint(0, 7, tensor_shape, dtype=torch_dtype, device=xpu_device)
    index = [i for i in range(batch_size)]
    random.shuffle(index)
    index_tensor = torch.tensor(index, dtype=torch.int64, device=xpu_device)
    index_tensor = index_tensor.reshape(-1, 1).expand(-1, hidden_size)

    return [dst_tensor, src_tensor, index_tensor]








def host2device_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    output_element_num = sum([math.prod(shape) for shape in [a_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    output_tensor_size = dtype_size * output_element_num

    tensor_size = output_tensor_size
    return batch_size, tensor_size, 0, output_tensor_size

def host2device_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    host_tensor = torch.empty(a_shape, dtype=torch_dtype, device="cpu").pin_memory()
    device_tensor = torch.empty(a_shape, dtype=torch_dtype, device=xpu_device)
    
    return [host_tensor, device_tensor]


def allreduce_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    a_tensor = torch.zeros(a_shape, dtype=torch_dtype, device=xpu_device)
    return [a_tensor]


def allgather_compute_size(input_shapes, torch_dtype):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape

    output_element_num = sum([math.prod(shape) for shape in [a_shape]])
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    output_tensor_size = dtype_size * output_element_num
    tensor_size = output_tensor_size
    return batch_size, tensor_size, 0, output_tensor_size



def allgather_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, = input_shapes
    batch_size, hidden_size = a_shape
    
    world_size = dist.get_world_size()
    tensor = torch.empty([batch_size, hidden_size], dtype=torch_dtype, device=xpu_device)
    tensors = list(torch.chunk(tensor, world_size, dim=0))

    return [tensors]


def alltoall_compute_size(input_shapes, torch_dtype):
    a_shape, b_shape = input_shapes
    batch_size, hidden_size = a_shape

    world_size = dist.get_world_size()
    output_element_num = sum([math.prod(shape) for shape in [a_shape]]) * 2
    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    output_tensor_size = dtype_size * output_element_num
    tensor_size = output_tensor_size
    return batch_size, tensor_size, 0, output_tensor_size


def alltoall_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, b_shape = input_shapes
    batch_size, hidden_size = a_shape

    world_size = dist.get_world_size()
    input_tensor = torch.empty([batch_size, hidden_size], dtype=torch_dtype, device=xpu_device)
    input_tensors = list(torch.chunk(input_tensor, world_size, dim=0))

    output_tensor = torch.empty([batch_size, hidden_size], dtype=torch_dtype, device=xpu_device)
    output_tensors = list(torch.chunk(output_tensor, world_size, dim=0))

    return [input_tensors, output_tensors]
    

def p2p_compute_size(input_shapes, torch_dtype):
    a_shape, b_shape = input_shapes
    batch_size, hidden_size = a_shape

    input_element_num = sum([math.prod(shape) for shape in [a_shape]])
    output_element_num = sum([math.prod(shape) for shape in [b_shape]])

    dtype_size = torch.tensor([], dtype=torch_dtype).element_size()
    input_tensor_size = dtype_size * input_element_num
    output_tensor_size = dtype_size * output_element_num

    tensor_size = input_tensor_size + output_tensor_size
    return batch_size, tensor_size, input_tensor_size, output_tensor_size

def p2p_create_tensors(input_shapes, torch_dtype, xpu_device):
    a_shape, b_shape = input_shapes
    batch_size, hidden_size = a_shape

    a_tensor = torch.empty(a_shape, dtype=torch_dtype, device=xpu_device)
    b_tensor = torch.empty(b_shape, dtype=torch_dtype, device=xpu_device)

    return [a_tensor, b_tensor]



"""
gemm ops
"""
class GemmOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_d):
        compute_dtype = input_tensor_a.dtype
        if compute_dtype in [torch.float32, torch.float16, torch.bfloat16]:
            torch.mm(input_tensor_a, input_tensor_b, out=input_tensor_d)
        else:
            raise Exception(f"GemmOp with dtype {compute_dtype} is not implemented")

class BatchGemmOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_d):
        compute_dtype = input_tensor_a.dtype
        if compute_dtype in [torch.float32, torch.float16, torch.bfloat16]:
            torch.bmm(input_tensor_a, input_tensor_b, out=input_tensor_d)
        else:
            raise Exception(f"BatchGemmOp with dtype {compute_dtype} is not implemented")

class GroupGemmOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_d):
        compute_dtype = input_tensor_a[0].dtype
        for a, b, d in zip(input_tensor_a, input_tensor_b, input_tensor_d):
            if compute_dtype in [torch.float32, torch.float16, torch.bfloat16]:
                torch.mm(a, b, out=d)
            else:
                raise Exception(f"GroupGemmOp with dtype {compute_dtype} is not implemented")



"""
unary ops
"""
class SinOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.sin(input_tensor, out=output_tensor)

class CosOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.cos(input_tensor, out=output_tensor)

class ExpOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.exp(input_tensor, out=output_tensor)

class ExponentialOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        input_tensor.exponential_()

class LogOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.log(input_tensor, out=output_tensor)

class SqrtOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.sqrt(input_tensor, out=output_tensor)

class CastOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        output_tensor = input_tensor.to(output_tensor.dtype)

class SiluOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        output_tensor = torch.nn.functional.silu(input_tensor)

class GeluOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        output_tensor = torch.nn.functional.gelu(input_tensor)

class SwiGLUOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.mul(torch.nn.functional.silu(input_tensor), input_tensor, out=output_tensor)

"""
Binary ops
"""
class AddOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_c):
        torch.add(input_tensor_a, input_tensor_b, out=input_tensor_c)

class MulOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_c):
        torch.mul(input_tensor_a, input_tensor_b, out=input_tensor_c)

class SubOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_c):
        torch.sub(input_tensor_a, input_tensor_b, out=input_tensor_c)

class DivOp(torch.nn.Module):
    def forward(self, input_tensor_a, input_tensor_b, input_tensor_c):
        torch.div(input_tensor_a, input_tensor_b, out=input_tensor_c)



"""
reduction ops
"""
class LayerNormOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor, weight_tensor):
        output_tensor = torch.nn.functional.layer_norm(input_tensor, (input_tensor.shape[-1],), weight_tensor)

class SoftmaxOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        output_tensor = torch.nn.functional.softmax(input_tensor, dim=-1, dtype=output_tensor.dtype)

class ReduceSumOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor):
        torch.sum(input_tensor, dim=-1, keepdim=True, dtype=output_tensor.dtype, out=output_tensor)

class ReduceMinOp(torch.nn.Module):
    def forward(self, input_tensor, value_tensor, indice_tensor):
        torch.min(input_tensor, dim=-1, keepdim=True, out=(value_tensor, indice_tensor))

class ReduceMaxOp(torch.nn.Module):
    def forward(self, input_tensor, value_tensor, indice_tensor):
        torch.max(input_tensor, dim=-1, keepdim=True, out=(value_tensor, indice_tensor))



"""
index_ops
"""
class IndexAddOp(torch.nn.Module):
    def forward(self, dst_tensor, src_tensor, index_tensor):
        dst_tensor.index_add_(0, index_tensor, src_tensor)

class SortOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor, indice_tensor):
        torch.sort(input_tensor, dim=-1, out=(output_tensor, indice_tensor))

class UniqueOp(torch.nn.Module):
    def forward(self, input_tensor, output_tensor, count_tensor):
        output_tensor, count_tensor = torch.unique(
            input=input_tensor, 
            sorted=False, 
            return_counts=True, 
            return_inverse=False
        )

class ScatterOp(torch.nn.Module):
    def forward(self, dst_tensor, src_tensor, index_tensor):
        dst_tensor.scatter_(0, index_tensor, src_tensor)

class GatherOp(torch.nn.Module):
    def forward(self, dst_tensor, src_tensor, index_tensor):
        torch.gather(src_tensor, 0, index_tensor, out=dst_tensor)



"""
h2d_ops
"""
class Host2DeviceOp(torch.nn.Module):
    def forward(self, host_tensor, device_tensor):
        device_tensor.copy_(host_tensor)


class Device2HostOp(torch.nn.Module):
    def forward(self, host_tensor, device_tensor):
        host_tensor.copy_(device_tensor)



"""
communication ops
"""
class AllReduceOp(torch.nn.Module):
    def forward(self, input_tensor):
        dist.all_reduce(input_tensor, op=dist.ReduceOp.SUM)

class AllGatherOp(torch.nn.Module):
    def forward(self, input_tensors):
        dist.all_gather(input_tensors, input_tensors[dist.get_rank()])

class ReduceScatterOp(torch.nn.Module):
    def forward(self, input_tensors):
        dist.reduce_scatter(input_tensors[dist.get_rank()], input_tensors)

class AllToAllOp(torch.nn.Module):
    def forward(self, input_tensors, output_tensors):
        dist.all_to_all(output_tensors, input_tensors)


class BroadcastOp(torch.nn.Module):
    def forward(self, input_tensor):
        dist.broadcast(input_tensor, 0)


class P2POp(torch.nn.Module):
    def forward(self, send_tensor, recv_tensor):
        world_size = dist.get_world_size()
        rank = dist.get_rank()

        reqs = []
        if rank != world_size - 1:
            reqs.append(dist.isend(send_tensor, (rank + 1) % world_size))
        if rank != 0:
            reqs.append(dist.irecv(recv_tensor, (rank - 1 + world_size) % world_size))
        for req in reqs:
            req.wait()


op_registry = {
    # gemm ops
    "gemm": GemmOp(), 
    "gemv": GemmOp(),
    "batch_gemm": BatchGemmOp(),
    "group_gemm": GroupGemmOp(),

    # unary ops
    "sin": SinOp(),
    "cos": CosOp(),
    "exp": ExpOp(),
    "exponential": ExponentialOp(),
    "log": LogOp(),
    "sqrt": SqrtOp(),
    "cast": CastOp(),
    "silu": SiluOp(),
    "gelu": GeluOp(),
    "swiglu": SwiGLUOp(),

    # binary ops
    "add": AddOp(),
    "sub": SubOp(),
    "mul": MulOp(),
    "div": DivOp(),

    # reduction ops
    "layernorm": LayerNormOp(),
    "softmax": SoftmaxOp(),
    "reduce_sum": ReduceSumOp(),
    "reduce_max": ReduceMaxOp(),
    "reduce_min": ReduceMinOp(),

    # index_ops
    "index_add": IndexAddOp(),
    "sort": SortOp(),
    "unique": UniqueOp(),
    "scatter": ScatterOp(),
    "gather": GatherOp(),

    # h2d_ops
    "device2host": Device2HostOp(),
    "host2device": Host2DeviceOp(),

    # ccl ops
    "broadcast": BroadcastOp(),
    "allreduce": AllReduceOp(),
    "allgather": AllGatherOp(),
    "alltoall": AllToAllOp(),
    "reducescatter": ReduceScatterOp(),
    "p2p": P2POp(),
}


op_compute_size_funcs = {
    # gemm_ops
    "gemm": gemm_compute_size,
    "gemv": gemm_compute_size,
    "batch_gemm": batch_gemm_compute_size,
    "group_gemm": group_gemm_compute_size,

    # unary_ops
    "sin": sin_compute_size,
    "cos": sin_compute_size,
    "exp": sin_compute_size,
    "exponential": sin_compute_size,
    "log": sin_compute_size,
    "sqrt": sin_compute_size,
    "cast": cast_compute_size,
    "silu": sin_compute_size,
    "gelu": sin_compute_size,
    "swiglu": swiglu_compute_size,

    # binary_ops
    "add": add_compute_size,
    "mul": add_compute_size,
    "sub": add_compute_size,
    "div": add_compute_size,

    # reduction_ops
    "layernorm": layer_norm_compute_size,
    "softmax": softmax_compute_size,
    "reduce_sum": reduce_sum_compute_size,
    "reduce_min": reduce_min_compute_size,
    "reduce_max": reduce_min_compute_size,

    # index_ops
    "index_add": index_add_compute_size,
    "sort": sort_compute_size,
    "unique": unique_compute_size,
    "scatter": scatter_compute_size,
    "gather": scatter_compute_size,

    # h2d_ops
    "host2device": host2device_compute_size,
    "device2host": host2device_compute_size,

    # ccl_ops
    "broadcast": host2device_compute_size,
    "allreduce": host2device_compute_size,
    "allgather": allgather_compute_size,
    "alltoall": alltoall_compute_size,
    "reducescatter": allgather_compute_size, 
    "p2p": p2p_compute_size,
}

op_create_tensors_funcs = {
    # gemm ops
    "gemm": gemm_create_tensors,
    "gemv": gemm_create_tensors,
    "batch_gemm": batch_gemm_create_tensors,
    "group_gemm": group_gemm_create_tensors,

    # unary ops
    "sin": sin_create_tensors,
    "cos": sin_create_tensors,
    "exp": sin_create_tensors,
    "exponential": sin_create_tensors,
    "log": sin_create_tensors,
    "sqrt": sin_create_tensors,
    "cast": cast_create_tensors,
    "silu": sin_create_tensors,
    "gelu": sin_create_tensors,
    "swiglu": swiglu_create_tensors,

    # binary ops
    "add": add_create_tensors,
    "mul": add_create_tensors,
    "sub": add_create_tensors,
    "div": add_create_tensors,

    # reduction ops
    "layernorm": layer_norm_create_tensors,
    "softmax": softmax_create_tensors,
    "reduce_sum": reduce_sum_create_tensors,
    "reduce_min": reduce_min_create_tensors,
    "reduce_max": reduce_min_create_tensors,

    # index ops
    "index_add": index_add_create_tensors,
    "sort": sort_create_tensors,
    "unique": unique_create_tensors,
    "scatter": scatter_create_tensors,
    "gather": scatter_create_tensors,

    # h2d_ops
    "host2device": host2device_create_tensors,
    "device2host": host2device_create_tensors,

    # ccl_ops
    "broadcast": allreduce_create_tensors,
    "allreduce": allreduce_create_tensors,
    "allgather": allgather_create_tensors,
    "alltoall": alltoall_create_tensors,
    "reducescatter": allgather_create_tensors, 
    "p2p": p2p_create_tensors,
}