-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathfused.cu
More file actions
2084 lines (2063 loc) · 89.1 KB
/
fused.cu
File metadata and controls
2084 lines (2063 loc) · 89.1 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
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 CMU, Facebook, LANL, MIT, NVIDIA, and Stanford (alphabetical)
*
* 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.
*/
#include "flexflow/accessor.h"
#include "flexflow/ffconst_utils.h"
#include "flexflow/model.h"
#include "flexflow/ops/add_bias_residual_layer_norm.h"
#include "flexflow/ops/batch_norm.h"
#include "flexflow/ops/element_unary.h"
#include "flexflow/ops/embedding.h"
#include "flexflow/ops/flat.h"
#include "flexflow/ops/fused.h"
#include "flexflow/ops/inc_multihead_self_attention.h"
#include "flexflow/ops/kernels/batch_matmul_kernels.h"
#include "flexflow/ops/kernels/concat_kernels.h"
#include "flexflow/ops/kernels/conv_2d_kernels.h"
#include "flexflow/ops/kernels/dropout_kernels.h"
#include "flexflow/ops/kernels/element_binary_kernels.h"
#include "flexflow/ops/kernels/embedding_kernels.h"
#include "flexflow/ops/kernels/flat_kernels.h"
#include "flexflow/ops/kernels/linear_kernels.h"
#include "flexflow/ops/kernels/lora_linear_kernels.h"
#include "flexflow/ops/kernels/pool_2d_kernels.h"
#include "flexflow/ops/kernels/reshape_kernels.h"
#include "flexflow/ops/kernels/residual_rms_norm_kernels.h"
#include "flexflow/ops/kernels/rms_norm_kernels.h"
#include "flexflow/ops/kernels/softmax_kernels.h"
#include "flexflow/ops/kernels/transpose_kernels.h"
#include "flexflow/ops/layer_norm.h"
#include "flexflow/ops/residual_layer_norm.h"
#include "flexflow/ops/sigmoid_silu_multi.h"
#include "flexflow/ops/spec_inc_multihead_self_attention.h"
#include "flexflow/ops/tree_inc_multihead_self_attention.h"
#include "flexflow/parallel_ops/kernels/allreduce_kernels.h"
#include "flexflow/parallel_ops/kernels/parallel_identity_kernels.h"
#include "flexflow/utils/cuda_helper.h"
namespace FlexFlow {
// declare Legion names
using Legion::Context;
using Legion::coord_t;
using Legion::Domain;
using Legion::Future;
using Legion::LogicalPartition;
using Legion::LogicalRegion;
using Legion::Memory;
using Legion::PhysicalRegion;
using Legion::Runtime;
using Legion::Task;
OpMeta *FusedOp::init_task(Task const *task,
std::vector<PhysicalRegion> const ®ions,
Context ctx,
Runtime *runtime) {
FusedOp const *fused = (FusedOp *)task->args;
FusedOpMeta const *metas = (FusedOpMeta *)task->local_args;
FusedOpMeta *local_meta = new FusedOpMeta();
memcpy(local_meta, metas, sizeof(FusedOpMeta));
local_meta->fused_op = (FusedOp *)malloc(sizeof(FusedOp));
memcpy(static_cast<void *>(local_meta->fused_op),
static_cast<void const *>(fused),
sizeof(FusedOp));
return ((OpMeta *)local_meta);
}
/*
regions[...](I): inputs
regions[...](I): weights
regions[...](O): outputs
*/
__host__ void
FusedOp::inference_task(Task const *task,
std::vector<PhysicalRegion> const ®ions,
Context ctx,
Runtime *runtime) {
// const FusedOp* fused = (FusedOp*) task->args;
FusedOpMeta const *metas = *((FusedOpMeta **)task->local_args);
FusedOp const *fused = metas->fused_op;
BatchConfig const *bc = BatchConfig::from_future(task->futures[0]);
// Return if no active tokens
if (bc->num_tokens == 0) {
return;
}
assert(metas->numOperators == fused->numOperators);
assert(regions.size() == task->regions.size());
bool softmax_grad_additional_region =
(fused->op_op_type[fused->numOperators - 1] == OP_SOFTMAX);
assert((int)regions.size() == fused->numInputs + fused->numWeights +
fused->numOutputs +
softmax_grad_additional_region);
std::vector<GenericTensorAccessorR> input_accessor;
std::vector<GenericTensorAccessorR> weight_accessor;
std::vector<GenericTensorAccessorW> output_accessor;
assert(fused->numInputs <= MAX_NUM_INPUTS);
for (int i = 0; i < fused->numInputs; i++) {
input_accessor.push_back(
helperGetGenericTensorAccessorRO(fused->input_data_types[i],
regions[i],
task->regions[i],
FID_DATA,
ctx,
runtime));
}
int roff = fused->numInputs;
assert(fused->numWeights <= MAX_NUM_WEIGHTS);
for (int i = 0; i < fused->numWeights; i++) {
weight_accessor.push_back(
helperGetGenericTensorAccessorRO(fused->weight_data_types[i],
regions[i + roff],
task->regions[i + roff],
FID_DATA,
ctx,
runtime));
}
roff += fused->numWeights;
assert(fused->numOutputs <= MAX_NUM_OUTPUTS);
for (int i = 0; i < fused->numOutputs; i++) {
output_accessor.push_back(
helperGetGenericTensorAccessorWO(fused->output_data_types[i],
regions[i + roff],
task->regions[i + roff],
FID_DATA,
ctx,
runtime));
}
roff += fused->numOutputs;
// Assert that all meta share the same dnn/blas handler
int start = 0;
for (start = 0; start < fused->numOperators; start++) {
if (metas->meta[start] != NULL) {
break;
}
}
for (int op = start + 1; op < fused->numOperators; op++) {
if (metas->meta[op] != NULL) {
assert(metas->meta[start]->handle.blas == metas->meta[op]->handle.blas);
assert(metas->meta[start]->handle.dnn == metas->meta[op]->handle.dnn);
}
}
int ioff = 0, woff = 0, ooff = 0;
for (int op = 0; op < fused->numOperators; op++) {
std::vector<GenericTensorAccessorR> my_input_accessor;
std::vector<GenericTensorAccessorR> my_weight_accessor;
std::vector<GenericTensorAccessorW> my_output_accessor;
for (int i = 0; i < fused->op_num_inputs[op]; i++) {
int my_off = fused->op_input_idx[i + ioff];
if (fused->op_input_source[i + ioff] == SOURCE_INPUT) {
my_input_accessor.push_back(input_accessor[my_off]);
} else if (fused->op_input_source[i + ioff] == SOURCE_OUTPUT) {
my_input_accessor.push_back(output_accessor[my_off]);
} else {
assert(false);
}
}
for (int i = 0; i < fused->op_num_weights[op]; i++) {
assert(fused->op_weight_source[i + woff] == SOURCE_WEIGHT);
my_weight_accessor.push_back(
weight_accessor[fused->op_weight_idx[i + woff]]);
}
for (int i = 0; i < fused->op_num_outputs[op]; i++) {
int my_off = fused->op_output_idx[i + ooff];
assert(fused->op_output_source[i + ooff] == SOURCE_OUTPUT);
my_output_accessor.push_back(output_accessor[my_off]);
}
switch (fused->op_op_type[op]) {
case OP_CONCAT: {
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
ConcatMeta *m = (ConcatMeta *)metas->meta[op];
int num_inputs = fused->op_num_inputs[op];
Kernels::Concat::forward_kernel_wrapper(m,
my_output_accessor[0],
my_input_accessor.data(),
num_inputs,
m->legion_axis);
break;
}
case OP_BATCHNORM: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_accessor[0].domain.get_dim() == 5);
assert(my_output_accessor[0].domain.get_dim() == 5);
assert(my_weight_accessor[0].domain.get_dim() == 2);
assert(my_weight_accessor[1].domain.get_dim() == 2);
BatchNormMeta *m = (BatchNormMeta *)metas->meta[op];
BatchNorm::forward_kernel(m,
my_input_accessor[0].get_float_ptr(),
my_output_accessor[0].get_float_ptr(),
my_weight_accessor[0].get_float_ptr(),
my_weight_accessor[1].get_float_ptr());
break;
}
case OP_LINEAR: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
Domain kernel_domain = my_weight_accessor[0].domain;
int in_dim = kernel_domain.hi()[0] - kernel_domain.lo()[0] + 1;
int out_dim = kernel_domain.hi()[1] - kernel_domain.lo()[1] + 1;
int batch_size = my_input_accessor[0].domain.get_volume() / in_dim;
assert(my_output_accessor[0].domain.get_volume() ==
out_dim * batch_size);
assert(my_input_accessor[0].domain.get_volume() == in_dim * batch_size);
void const *bias_ptr = nullptr;
LinearMeta *m = (LinearMeta *)metas->meta[op];
if (fused->op_num_weights[op] == 2) {
assert(my_weight_accessor[1].domain.get_volume() == out_dim);
if (!m->add_bias_only_once || task->index_point.point_data[0] == 0) {
bias_ptr = my_weight_accessor[1].ptr;
}
} else {
assert(fused->op_num_weights[op] == 1);
}
assert(m->input_type[0] == my_input_accessor[0].data_type);
assert(m->input_type[0] == my_output_accessor[0].data_type);
batch_size = bc->num_active_infr_tokens();
Kernels::Linear::forward_kernel_wrapper(m,
my_input_accessor[0].ptr,
my_output_accessor[0].ptr,
my_weight_accessor[0].ptr,
bias_ptr,
in_dim,
out_dim,
batch_size);
break;
}
case OP_LORA: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_outputs[op] == 1);
Domain input_domain = my_input_accessor[0].domain;
Domain output_domain = my_output_accessor[0].domain;
int in_dim = input_domain.hi()[0] - input_domain.lo()[0] + 1;
int out_dim = output_domain.hi()[0] - output_domain.lo()[0] + 1;
int batch_size = my_input_accessor[0].domain.get_volume() / in_dim;
assert(my_output_accessor[0].domain.get_volume() ==
out_dim * batch_size);
assert(my_input_accessor[0].domain.get_volume() == in_dim * batch_size);
LoraLinearMeta *m = (LoraLinearMeta *)metas->meta[op];
assert(m->input_type[0] == my_input_accessor[0].data_type);
assert(m->output_type[0] == my_output_accessor[0].data_type);
// Assert that the output and the second input are at the same place
// since we ``inplace'' the output for LoRA
assert(my_input_accessor[1].ptr == my_output_accessor[0].ptr);
Kernels::LoraLinear::inference_kernel_wrapper(
m, bc, my_input_accessor[0], my_output_accessor[0]);
break;
}
case OP_BATCHMATMUL: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
Domain out_domain = my_output_accessor[0].domain;
Domain a_domain = my_input_accessor[0].domain;
Domain b_domain = my_input_accessor[1].domain;
int m = b_domain.hi()[0] - b_domain.lo()[0] + 1;
assert(m == out_domain.hi()[0] - out_domain.lo()[0] + 1);
int n = a_domain.hi()[1] - a_domain.lo()[1] + 1;
assert(n == out_domain.hi()[1] - out_domain.lo()[1] + 1);
int k = a_domain.hi()[0] - a_domain.lo()[0] + 1;
assert(k == b_domain.hi()[1] - b_domain.lo()[1] + 1);
assert(a_domain.get_dim() == b_domain.get_dim());
assert(a_domain.get_dim() == out_domain.get_dim());
int batch = 1;
for (int i = 2; i < a_domain.get_dim(); i++) {
int dim_size = a_domain.hi()[i] - a_domain.lo()[i] + 1;
assert(dim_size == b_domain.hi()[i] - b_domain.lo()[i] + 1);
assert(dim_size == out_domain.hi()[i] - out_domain.lo()[i] + 1);
batch *= dim_size;
}
BatchMatmulMeta *meta = (BatchMatmulMeta *)metas->meta[op];
Kernels::BatchMatmul::forward_kernel_wrapper(
meta,
my_output_accessor[0].get_float_ptr(),
my_input_accessor[0].get_float_ptr(),
my_input_accessor[1].get_float_ptr(),
(float const *)nullptr,
m,
n,
k,
batch,
meta->a_seq_length_dim,
meta->b_seq_length_dim,
fused->iter_config.seq_length);
break;
}
case OP_EW_ADD:
case OP_EW_SUB:
case OP_EW_MUL:
case OP_EW_DIV:
case OP_EW_MAX:
case OP_EW_MIN: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_accessor[0].domain == my_input_accessor[1].domain);
assert(my_input_accessor[0].domain == my_output_accessor[0].domain);
ElementBinaryMeta *m = (ElementBinaryMeta *)metas->meta[op];
Kernels::ElementBinary::forward_kernel_wrapper(m,
my_input_accessor[0],
my_input_accessor[1],
my_output_accessor[0]);
break;
}
case OP_EMBEDDING: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_weights[op] == 1);
assert(fused->op_num_outputs[op] == 1);
EmbeddingMeta *m = (EmbeddingMeta *)metas->meta[op];
if (m->aggr == AGGR_MODE_NONE) {
// assert(kernel_domain.get_dim() == 2);
assert(my_input_accessor[0].domain.get_dim() + 1 ==
my_output_accessor[0].domain.get_dim());
for (size_t i = 0; i < my_input_accessor[0].domain.get_dim(); i++) {
assert(my_input_accessor[0].domain.hi()[i] ==
my_output_accessor[0].domain.hi()[i + 1]);
assert(my_input_accessor[0].domain.lo()[i] ==
my_output_accessor[0].domain.lo()[i + 1]);
}
assert(my_weight_accessor[0].domain.hi()[0] -
my_weight_accessor[0].domain.lo()[0] ==
my_output_accessor[0].domain.hi()[0] -
my_output_accessor[0].domain.lo()[0]);
} else {
assert(my_input_accessor[0].domain.get_dim() ==
my_output_accessor[0].domain.get_dim());
for (size_t i = 1; i < my_input_accessor[0].domain.get_dim(); i++) {
assert(my_input_accessor[0].domain.hi()[i] ==
my_output_accessor[0].domain.hi()[i]);
assert(my_input_accessor[0].domain.lo()[i] ==
my_output_accessor[0].domain.lo()[i]);
}
assert(my_weight_accessor[0].domain.hi()[0] -
my_weight_accessor[0].domain.lo()[0] ==
my_output_accessor[0].domain.hi()[0] -
my_output_accessor[0].domain.lo()[0]);
}
int in_dim, out_dim, effective_batch_size;
if (m->aggr == AGGR_MODE_NONE) {
in_dim = 1;
out_dim = my_output_accessor[0].domain.hi()[0] -
my_output_accessor[0].domain.lo()[0] + 1;
effective_batch_size =
my_output_accessor[0].domain.get_volume() / out_dim;
assert(effective_batch_size * in_dim ==
my_input_accessor[0].domain.get_volume());
} else {
assert(m->aggr == AGGR_MODE_AVG || m->aggr == AGGR_MODE_SUM);
in_dim = my_input_accessor[0].domain.hi()[0] -
my_input_accessor[0].domain.lo()[0] + 1;
out_dim = my_output_accessor[0].domain.hi()[0] -
my_output_accessor[0].domain.lo()[0] + 1;
effective_batch_size =
my_output_accessor[0].domain.get_volume() / out_dim;
assert(effective_batch_size * in_dim ==
my_input_accessor[0].domain.get_volume());
}
assert(my_input_accessor[0].data_type == DT_INT32 ||
my_input_accessor[0].data_type == DT_INT64);
Kernels::Embedding::forward_kernel_wrapper(m,
my_input_accessor[0],
my_output_accessor[0],
my_weight_accessor[0],
in_dim,
out_dim,
effective_batch_size);
break;
}
case OP_GELU:
case OP_RELU:
case OP_SIGMOID:
case OP_TANH:
case OP_ELU:
case OP_SCALAR_TRUE_DIV: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_accessor[0].domain == my_output_accessor[0].domain);
ElementUnaryMeta *m = (ElementUnaryMeta *)metas->meta[op];
if (m->data_type == DT_HALF) {
ElementUnary::forward_kernel_wrapper(
m,
my_input_accessor[0].get_half_ptr(),
my_output_accessor[0].get_half_ptr(),
my_input_accessor[0].domain.get_volume());
} else if (m->data_type == DT_FLOAT) {
ElementUnary::forward_kernel_wrapper(
m,
my_input_accessor[0].get_float_ptr(),
my_output_accessor[0].get_float_ptr(),
my_input_accessor[0].domain.get_volume());
} else {
assert(false && "Unsupported data type in ElementUnary forward");
}
break;
}
case OP_RMS_NORM: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_weights[op] == 1);
assert(fused->op_num_outputs[op] == 1);
RMSNormMeta *m = (RMSNormMeta *)metas->meta[op];
Kernels::RMSNorm::inference_kernel_wrapper(m,
bc,
my_input_accessor[0],
my_weight_accessor[0],
my_output_accessor[0]);
break;
}
case OP_RESIDUAL_RMS_NORM: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_weights[op] == 1);
assert(fused->op_num_outputs[op] == 2);
ResidualRMSNormMeta *m = (ResidualRMSNormMeta *)metas->meta[op];
Kernels::ResidualRMSNorm::inference_kernel_wrapper(
m,
bc,
my_input_accessor[0],
my_input_accessor[1],
my_weight_accessor[0],
my_output_accessor[0],
my_output_accessor[1]);
break;
}
case OP_INC_MULTIHEAD_SELF_ATTENTION: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
IncMultiHeadSelfAttentionMeta *m =
(IncMultiHeadSelfAttentionMeta *)metas->meta[op];
assert(fused->op_num_weights[op] ==
(1 + (int)(*m->qkv_bias || *m->final_bias)));
GenericTensorAccessorR biases;
if (*m->qkv_bias || *m->final_bias) {
assert(fused->op_num_weights[op] == 2);
biases = my_weight_accessor[1];
}
IncMultiHeadSelfAttention::inference_kernel_wrapper(
m,
bc,
task->index_point.point_data[0],
my_input_accessor[0],
my_weight_accessor[0],
my_output_accessor[0],
biases);
break;
}
case OP_TREE_INC_MULTIHEAD_SELF_ATTENTION: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
TreeIncMultiHeadSelfAttentionMeta *m =
(TreeIncMultiHeadSelfAttentionMeta *)metas->meta[op];
TreeVerifyBatchConfig const &tree_bc =
Future(task->futures[0]).get_result<TreeVerifyBatchConfig>();
assert(fused->op_num_weights[op] ==
(1 + (int)(*m->qkv_bias || *m->final_bias)));
GenericTensorAccessorR biases;
if (*m->qkv_bias || *m->final_bias) {
assert(fused->op_num_weights[op] == 2);
biases = my_weight_accessor[1];
}
TreeIncMultiHeadSelfAttention::inference_kernel_wrapper(
m,
&tree_bc,
task->index_point.point_data[0],
my_input_accessor[0],
my_weight_accessor[0],
my_output_accessor[0],
biases);
break;
}
case OP_SPEC_INC_MULTIHEAD_SELF_ATTENTION: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
SpecIncMultiHeadSelfAttentionMeta const *m =
(SpecIncMultiHeadSelfAttentionMeta *)metas->meta[op];
// BeamSearchBatchConfig const *beam_bc =
// (BeamSearchBatchConfig *)task->args;
BeamSearchBatchConfig const &beam_bc =
Future(task->futures[0]).get_result<BeamSearchBatchConfig>();
assert(fused->op_num_weights[op] ==
(1 + (int)(*m->qkv_bias || *m->final_bias)));
GenericTensorAccessorR biases;
if (*m->qkv_bias || *m->final_bias) {
assert(fused->op_num_weights[op] == 2);
biases = my_weight_accessor[1];
}
SpecIncMultiHeadSelfAttention::inference_kernel_wrapper(
m,
&beam_bc,
task->index_point.point_data[0],
my_input_accessor[0],
my_weight_accessor[0],
my_output_accessor[0],
biases);
break;
}
case OP_LAYERNORM: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
LayerNormMeta const *m = (LayerNormMeta *)metas->meta[op];
if (m->elementwise_affine) {
assert(fused->op_num_weights[op] == 1 + (int)(m->use_bias));
}
GenericTensorAccessorR gamma, beta;
if (m->elementwise_affine) {
gamma = my_weight_accessor[0];
if (m->use_bias) {
beta = my_weight_accessor[1];
}
}
LayerNorm::forward_kernel_wrapper(
m, my_input_accessor[0], my_output_accessor[0], gamma, beta);
break;
}
case OP_RESIDUAL_LAYERNORM: {
assert(fused->op_num_outputs[op] == 2);
ResidualLayerNormMeta *m = (ResidualLayerNormMeta *)metas->meta[op];
if (m->use_two_residuals) {
assert(fused->op_num_inputs[op] == 3);
} else {
assert(fused->op_num_inputs[op] == 2);
}
if (!m->elementwise_affine) {
assert(fused->op_num_weights[op] == 0);
} else {
if (!m->use_bias) {
assert(fused->op_num_weights[op] == 1); // weight
} else {
assert(fused->op_num_weights[op] == 2); // weight + bias
}
}
GenericTensorAccessorR residual2;
if (m->use_two_residuals) {
residual2 = my_input_accessor[2];
}
GenericTensorAccessorR gamma, beta;
if (m->elementwise_affine) {
gamma = my_weight_accessor[0];
if (m->use_bias) {
beta = my_weight_accessor[1];
}
}
ResidualLayerNorm::inference_kernel_wrapper(m,
bc,
my_input_accessor[0],
my_input_accessor[1],
residual2,
my_output_accessor[0],
my_output_accessor[1],
gamma,
beta);
break;
}
case OP_ADD_BIAS_RESIDUAL_LAYERNORM: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_outputs[op] == 2);
AddBiasResidualLayerNormMeta *m =
(AddBiasResidualLayerNormMeta *)metas->meta[op];
if (!m->elementwise_affine) {
assert(fused->op_num_weights[op] == 1); // attn bias
} else {
if (!m->use_bias) {
assert(fused->op_num_weights[op] == 2); // attn bias + weight
} else {
assert(fused->op_num_weights[op] == 3); // attn bias + weight + bias
}
}
GenericTensorAccessorR gamma, beta;
if (m->elementwise_affine) {
gamma = my_weight_accessor[1];
if (m->use_bias) {
beta = my_weight_accessor[2];
}
}
AddBiasResidualLayerNorm::inference_kernel_wrapper(
m,
bc,
my_input_accessor[0],
my_weight_accessor[0],
my_input_accessor[1],
my_output_accessor[0],
my_output_accessor[1],
gamma,
beta);
break;
}
case OP_SIGMOID_SILU_MULTI: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_outputs[op] == 1);
SigmoidSiluMultiMeta *m = (SigmoidSiluMultiMeta *)metas->meta[op];
SigmoidSiluMulti::inference_kernel_wrapper(m,
bc,
my_input_accessor[0],
my_input_accessor[1],
my_output_accessor[0]);
break;
}
case OP_SOFTMAX: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_accessor[0].domain.get_volume() ==
my_output_accessor[0].domain.get_volume());
if (op == fused->numOperators - 1) { // if this is the final operator
output_accessor[fused->numOutputs] = helperGetGenericTensorAccessorWO(
fused->output_data_types[fused->numOutputs - 1],
regions[roff],
task->regions[roff],
FID_DATA,
ctx,
runtime);
}
SoftmaxMeta *m = (SoftmaxMeta *)metas->meta[op];
Kernels::Softmax::inference_kernel_wrapper(
m,
bc,
(op == fused->numOperators - 1),
my_input_accessor[0],
my_output_accessor[0],
output_accessor[fused->numOutputs]);
break;
}
case OP_ALLREDUCE: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
AllReduceMeta const *m = (AllReduceMeta *)metas->meta[op];
Kernels::AllReduce::inference_kernel_wrapper(
m, bc, my_input_accessor[0], my_output_accessor[0]);
break;
}
case OP_PARALLEL_IDENTITY: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
ParallelIdentityMeta const *m = (ParallelIdentityMeta *)metas->meta[op];
Kernels::ParallelIdentity::inference_kernel_wrapper(
m, bc, my_input_accessor[0], my_output_accessor[0]);
break;
}
default: {
fprintf(stderr,
"Fusion currently does not support type = %d\n",
fused->op_op_type[op]);
assert(false && "Fusion currently does not support type");
}
}
if (metas->meta[op]->inference_debugging &&
!(fused->op_op_type[op] == OP_ALLREDUCE ||
fused->op_op_type[op] == OP_PARALLEL_IDENTITY ||
fused->op_op_type[op] == OP_REPLICATE ||
fused->op_op_type[op] == OP_REPARTITION ||
fused->op_op_type[op] == OP_COMBINE)) {
std::vector<GenericTensorAccessorR> input_accessors_to_save;
std::vector<GenericTensorAccessorR> weight_accessors_to_save;
std::vector<GenericTensorAccessorR> output_accessors_to_save;
for (int i = 0; i < fused->op_num_inputs[op]; i++) {
input_accessors_to_save.push_back(my_input_accessor[i]);
}
for (int i = 0; i < fused->op_num_weights[op]; i++) {
weight_accessors_to_save.push_back(my_weight_accessor[i]);
}
for (int i = 0; i < fused->op_num_outputs[op]; i++) {
output_accessors_to_save.push_back(my_output_accessor[i]);
}
assert(task->index_point.get_dim() == 1);
int shard_id = task->index_point.point_data[0];
FusedOp::save_inference_tensors_to_file(metas->meta[op],
shard_id,
bc,
input_accessors_to_save,
weight_accessors_to_save,
output_accessors_to_save);
}
ioff += fused->op_num_inputs[op];
woff += fused->op_num_weights[op];
ooff += fused->op_num_outputs[op];
}
// for (int i = 0; i < fused->numOutputs; i++)
// print_tensor<float>(output_ptr[i], output_domain[i].get_volume(),
// "[Fused:forward:output]");
}
/*
regions[...](I): inputs
regions[...](I): weights
regions[...](O): outputs
*/
__host__ void FusedOp::peft_bwd_task(Task const *task,
std::vector<PhysicalRegion> const ®ions,
Context ctx,
Runtime *runtime) {
// const FusedOp* fused = (FusedOp*) task->args;
FusedOpMeta *metas = *((FusedOpMeta **)task->local_args);
FusedOp const *fused = metas->fused_op;
// BatchConfig const *bc = (BatchConfig *)task->args;
BatchConfig const *bc = BatchConfig::from_future(task->futures[0]);
// Return if no active PEFT bwd tokens
if (bc->num_active_peft_tokens() == 0) {
return;
}
assert(metas->numOperators == fused->numOperators);
assert(regions.size() == task->regions.size());
assert((int)regions.size() ==
fused->numInputs + fused->numWeights + fused->numOutputs);
// Domain input_domain[MAX_NUM_INPUTS];
// Domain weight_domain[MAX_NUM_WEIGHTS];
// Domain output_domain[MAX_NUM_OUTPUTS];
GenericTensorAccessorW input_grad_accessor[MAX_NUM_INPUTS];
GenericTensorAccessorR weight_accessor[MAX_NUM_WEIGHTS];
GenericTensorAccessorW output_grad_accessor[MAX_NUM_OUTPUTS];
assert(fused->numInputs <= MAX_NUM_INPUTS);
for (int i = 0; i < fused->numInputs; i++) {
// input_domain[i] = runtime->get_index_space_domain(
// ctx, task->regions[i].region.get_index_space());
input_grad_accessor[i] =
helperGetGenericTensorAccessorRW(fused->input_data_types[i],
regions[i],
task->regions[i],
FID_DATA,
ctx,
runtime);
}
int roff = fused->numInputs;
assert(fused->numWeights <= MAX_NUM_WEIGHTS);
for (int i = 0; i < fused->numWeights; i++) {
// weight_domain[i] = runtime->get_index_space_domain(
// ctx, task->regions[i + roff].region.get_index_space());
weight_accessor[i] =
helperGetGenericTensorAccessorRO(fused->weight_data_types[i],
regions[i + roff],
task->regions[i + roff],
FID_DATA,
ctx,
runtime);
}
roff += fused->numWeights;
assert(fused->numOutputs <= MAX_NUM_OUTPUTS);
for (int i = 0; i < fused->numOutputs; i++) {
// output_domain[i] = runtime->get_index_space_domain(
// ctx, task->regions[i + roff].region.get_index_space());
output_grad_accessor[i] =
helperGetGenericTensorAccessorRW(fused->output_data_types[i],
regions[i + roff],
task->regions[i + roff],
FID_DATA,
ctx,
runtime);
}
// Assert that all meta share the same dnn/blas handler
int start = 0;
for (start = 0; start < fused->numOperators; start++) {
if (metas->meta[start] != NULL) {
break;
}
}
for (int op = start + 1; op < fused->numOperators; op++) {
if (metas->meta[op] != NULL) {
assert(metas->meta[start]->handle.blas == metas->meta[op]->handle.blas);
assert(metas->meta[start]->handle.dnn == metas->meta[op]->handle.dnn);
}
}
int ioff = 0, woff = 0, ooff = 0;
// Domain my_id[MAX_NUM_INPUTS];
// Domain my_wd[MAX_NUM_WEIGHTS];
// Domain my_od[MAX_NUM_OUTPUTS];
GenericTensorAccessorW my_input_grad_accessor[MAX_NUM_INPUTS];
GenericTensorAccessorR my_weight_accessor[MAX_NUM_WEIGHTS];
GenericTensorAccessorW my_output_grad_accessor[MAX_NUM_OUTPUTS];
// Do backpropagation in the reverse ordering
for (int op = 0; op < fused->numOperators; op++) {
ioff += fused->op_num_inputs[op];
woff += fused->op_num_weights[op];
ooff += fused->op_num_outputs[op];
}
for (int op = fused->numOperators - 1; op >= 0; op--) {
#if 0
std::cout << get_operator_type_name(fused->op_op_type[op]) << std::endl;
#endif
ioff -= fused->op_num_inputs[op];
woff -= fused->op_num_weights[op];
ooff -= fused->op_num_outputs[op];
for (int i = 0; i < fused->op_num_inputs[op]; i++) {
int my_off = fused->op_input_idx[i + ioff];
if (fused->op_input_source[i + ioff] == SOURCE_INPUT) {
// my_id[i] = input_domain[my_off];
my_input_grad_accessor[i] = input_grad_accessor[my_off];
#if 0
printf("\tmy_input_grad_accessor[%i] = input_grad_accessor[%i]\n", i, my_off);
#endif
} else if (fused->op_input_source[i + ioff] == SOURCE_OUTPUT) {
// my_id[i] = output_domain[my_off];
my_input_grad_accessor[i] = output_grad_accessor[my_off];
#if 0
printf("\tmy_input_grad_accessor[%i] = output_grad_accessor[%i]\n", i, my_off);
#endif
} else {
assert(false);
}
}
for (int i = 0; i < fused->op_num_weights[op]; i++) {
assert(fused->op_weight_source[i + woff] == SOURCE_WEIGHT);
// my_wd[i] = weight_domain[fused->op_weight_idx[i + woff]];
// my_wp[i] = weight_ptr[fused->op_weight_idx[i + woff]];
my_weight_accessor[i] = weight_accessor[fused->op_weight_idx[i + woff]];
}
for (int i = 0; i < fused->op_num_outputs[op]; i++) {
int my_off = fused->op_output_idx[i + ooff];
assert(fused->op_output_source[i + ooff] == SOURCE_OUTPUT);
// my_od[i] = output_domain[fused->op_output_idx[i + ooff]];
// my_op[i] = output_ptr[fused->op_output_idx[i + ooff]];
my_output_grad_accessor[i] = output_grad_accessor[my_off];
#if 0
printf("\tmy_output_grad_accessor[%i] = output_grad_accessor[%i]\n", i, my_off);
#endif
}
switch (fused->op_op_type[op]) {
case OP_CONCAT: {
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
// TODO: implement this
assert(false);
// ConcatMeta *m = (ConcatMeta *)metas->meta[op];
// int num_inputs = fused->op_num_inputs[op];
// Kernels::Concat::peft_bwd_kernel_wrapper(m,
// my_output_accessor[0],
// my_input_accessor,
// num_inputs,
// m->legion_axis);
break;
}
case OP_BATCHNORM: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_grad_accessor[0].domain.get_dim() == 5);
assert(my_output_grad_accessor[0].domain.get_dim() == 5);
assert(my_weight_accessor[0].domain.get_dim() == 2);
assert(my_weight_accessor[1].domain.get_dim() == 2);
// TODO: implement this
assert(false);
// BatchNormMeta *m = (BatchNormMeta *)metas->meta[op];
// BatchNorm::peft_bwd_kernel_kernel(
// m,
// my_input_accessor[0].get_float_ptr(),
// my_output_accessor[0].get_float_ptr(),
// my_weight_accessor[0].get_float_ptr(),
// my_weight_accessor[1].get_float_ptr());
break;
}
case OP_LINEAR: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_outputs[op] == 1);
Domain kernel_domain = my_weight_accessor[0].domain;
int in_dim = kernel_domain.hi()[0] - kernel_domain.lo()[0] + 1;
int out_dim = kernel_domain.hi()[1] - kernel_domain.lo()[1] + 1;
int batch_size = my_input_grad_accessor[0].domain.get_volume() / in_dim;
assert(my_output_grad_accessor[0].domain.get_volume() ==
out_dim * batch_size);
assert(my_input_grad_accessor[0].domain.get_volume() ==
in_dim * batch_size);
LinearMeta *m = (LinearMeta *)metas->meta[op];
assert(m->input_type[0] == my_input_grad_accessor[0].data_type);
assert(m->input_type[0] == my_output_grad_accessor[0].data_type);
int num_infr_tokens = bc->num_active_infr_tokens();
int num_peft_tokens = bc->num_active_peft_tokens();
Kernels::Linear::peft_bwd_kernel_wrapper(m,
my_input_grad_accessor[0].ptr,
my_output_grad_accessor[0].ptr,
my_weight_accessor[0].ptr,
in_dim,
out_dim,
num_infr_tokens,
num_peft_tokens);
break;
}
case OP_LORA: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_outputs[op] == 1);
Domain input_domain = my_input_grad_accessor[0].domain;
Domain output_domain = my_output_grad_accessor[0].domain;
int in_dim = input_domain.hi()[0] - input_domain.lo()[0] + 1;
int out_dim = output_domain.hi()[0] - output_domain.lo()[0] + 1;
int batch_size = my_input_grad_accessor[0].domain.get_volume() / in_dim;
assert(my_output_grad_accessor[0].domain.get_volume() ==
out_dim * batch_size);
assert(my_input_grad_accessor[0].domain.get_volume() ==
in_dim * batch_size);
LoraLinearMeta *m = (LoraLinearMeta *)metas->meta[op];
assert(m->input_type[0] == my_input_grad_accessor[0].data_type);
assert(m->output_type[0] == my_output_grad_accessor[0].data_type);
// Assert that the output and the second input are at the same place
// since we ``inplace'' the output for LoRA
assert(my_input_grad_accessor[1].ptr == my_output_grad_accessor[0].ptr);
Kernels::LoraLinear::peft_bwd_kernel_wrapper(
m, bc, my_input_grad_accessor[0], my_output_grad_accessor[0]);
break;
}
case OP_BATCHMATMUL: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
Domain out_domain = my_output_grad_accessor[0].domain;
Domain a_domain = my_input_grad_accessor[0].domain;
Domain b_domain = my_input_grad_accessor[1].domain;
int m = b_domain.hi()[0] - b_domain.lo()[0] + 1;
assert(m == out_domain.hi()[0] - out_domain.lo()[0] + 1);
int n = a_domain.hi()[1] - a_domain.lo()[1] + 1;
assert(n == out_domain.hi()[1] - out_domain.lo()[1] + 1);
int k = a_domain.hi()[0] - a_domain.lo()[0] + 1;
assert(k == b_domain.hi()[1] - b_domain.lo()[1] + 1);
assert(a_domain.get_dim() == b_domain.get_dim());
assert(a_domain.get_dim() == out_domain.get_dim());
int batch = 1;
for (int i = 2; i < a_domain.get_dim(); i++) {
int dim_size = a_domain.hi()[i] - a_domain.lo()[i] + 1;
assert(dim_size == b_domain.hi()[i] - b_domain.lo()[i] + 1);
assert(dim_size == out_domain.hi()[i] - out_domain.lo()[i] + 1);
batch *= dim_size;
}
// TODO: implement me
assert(false);
// BatchMatmulMeta *meta = (BatchMatmulMeta *)metas->meta[op];
// Kernels::BatchMatmul::backward_kernel_wrapper(
// meta,
// my_output_accessor[0].get_float_ptr(),
// my_input_accessor[0].get_float_ptr(),
// my_input_accessor[1].get_float_ptr(),
// (float const *)nullptr,
// m,
// n,
// k,
// batch,
// meta->a_seq_length_dim,
// meta->b_seq_length_dim,
// fused->iter_config.seq_length);
break;
}
case OP_EW_ADD:
case OP_EW_SUB:
case OP_EW_MUL:
case OP_EW_DIV:
case OP_EW_MAX:
case OP_EW_MIN: {
assert(fused->op_num_inputs[op] == 2);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_grad_accessor[0].domain ==
my_input_grad_accessor[1].domain);
assert(my_input_grad_accessor[0].domain ==
my_output_grad_accessor[0].domain);
// ElementBinaryMeta *m = (ElementBinaryMeta *)metas->meta[op];
// Kernels::ElementBinary::forward_kernel_wrapper(m,
// my_input_accessor[0],
// my_input_accessor[1],
// my_output_accessor[0]);
break;
}
case OP_EMBEDDING: {
// Currently assume the Embedding layer cannot be finetuned
// so we do nothing for embedding
break;
}
case OP_GELU:
case OP_RELU:
case OP_SIGMOID:
case OP_TANH:
case OP_ELU:
case OP_SCALAR_TRUE_DIV: {
assert(fused->op_num_inputs[op] == 1);
assert(fused->op_num_weights[op] == 0);
assert(fused->op_num_outputs[op] == 1);
assert(my_input_grad_accessor[0].domain ==
my_output_grad_accessor[0].domain);
// TODO: implement me
assert(false);
// ElementUnaryMeta *m = (ElementUnaryMeta *)metas->meta[op];
// if (m->data_type == DT_HALF) {
// ElementUnary::forward_kernel_wrapper(
// m,
// my_input_accessor[0].get_half_ptr(),
// my_output_accessor[0].get_half_ptr(),
// my_input_accessor[0].domain.get_volume());
// } else if (m->data_type == DT_FLOAT) {
// ElementUnary::forward_kernel_wrapper(
// m,
// my_input_accessor[0].get_float_ptr(),