forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt.cpp
More file actions
2308 lines (1941 loc) · 118 KB
/
opt.cpp
File metadata and controls
2308 lines (1941 loc) · 118 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 (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "opt.hpp"
#include "../../logging.hpp"
#include "../../util.hpp"
#include "openvino/op/ops.hpp"
#include "openvino/op/util/op_types.hpp"
#include "openvino/pass/pattern/op/label.hpp" // any_input
#include "openvino/pass/pattern/op/optional.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "openvino/util/common_util.hpp"
namespace ov {
namespace npuw {
namespace patterns {
namespace opt {
void Context::permute(const PPtr& orig_param, const Context::Axes& order) {
closures_to_permute[orig_param] = order;
const auto& orig_shape = orig_param->get_shape();
ov::Shape tw_shape;
for (const auto& axis : order) {
tw_shape.push_back(orig_shape[axis]);
}
orig_param->set_partial_shape(tw_shape);
orig_param->validate_and_infer_types();
}
void Context::to_f16(const PPtr& orig_param) {
closures_to_f16.insert(orig_param);
orig_param->set_element_type(ov::element::f16);
orig_param->validate_and_infer_types();
}
void Context::register_parallel_matmul(const O& multiply, std::size_t axis, DQParMM&& mm) {
par_dq_mms[std::make_pair(multiply, axis)].push_back(std::move(mm));
}
Context::PPtr Context::concat(ov::ParameterVector&& v, std::size_t dim) {
// Sanity check dimensions - all dims other tham dim must match
std::size_t sum = 0u;
const auto& first = v.front();
const auto& first_shape = first->get_shape();
for (auto&& p : v) {
const auto& this_shape = p->get_shape();
NPUW_ASSERT(first_shape.size() == this_shape.size());
for (std::size_t d = 0; d < first_shape.size(); d++) {
if (d != dim) {
NPUW_ASSERT(first_shape[d] == this_shape[d]);
} else {
sum += this_shape[d];
}
}
NPUW_ASSERT(first->get_element_type() == p->get_element_type());
}
auto out_shape = first_shape;
out_shape[dim] = sum;
auto new_param = std::make_shared<ov::op::v0::Parameter>(first->get_element_type(), out_shape);
params_to_concat[new_param] = {std::move(v), dim};
return new_param;
}
Context::PPtr Context::unpack(const Context::PPtr& w,
const Context::PPtr& z,
const Context::PPtr& s,
ov::element::Type type) {
const auto& w_shape = w->get_shape();
const auto& s_shape = s->get_shape();
Context::PPtr new_param;
if (w_shape.size() == 3 && s_shape.size() == 3) {
// Assume already reshaped tensor (as it does with unpack)
ov::Shape new_shape = {w_shape[0], w_shape[1] * w_shape[2]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else if (w_shape.size() == 2 && s_shape.size() == 2) {
new_param = std::make_shared<ov::op::v0::Parameter>(type, w_shape);
} else {
NPUW_ASSERT(false && "Yet unsupported combination");
}
NPUW_ASSERT(new_param);
params_to_unpack[new_param] = {w, z, s};
return new_param;
}
Context::PPtr Context::unpack(const Context::PPtr& w, const Context::PPtr& s, ov::element::Type type) {
const auto& w_shape = w->get_shape();
const auto& s_shape = s->get_shape();
Context::PPtr new_param;
if (w_shape.size() == 3 && s_shape.size() == 3) {
// Assume already reshaped tensor (as it does with unpack)
ov::Shape new_shape = {w_shape[0], w_shape[1] * w_shape[2]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else if (w_shape.size() == 2 && s_shape.size() == 2) {
new_param = std::make_shared<ov::op::v0::Parameter>(type, w_shape);
} else {
NPUW_ASSERT(false && "Yet unsupported combination");
}
NPUW_ASSERT(new_param);
params_to_unpack[new_param] = {w, {}, s};
return new_param;
}
Context::PPtr Context::gather_cb4(const Context::PPtr& w, const ov::Tensor& t, ov::element::Type type) {
const auto& w_shape = w->get_shape();
Context::PPtr new_param;
if (w_shape.size() == 2) {
new_param = std::make_shared<ov::op::v0::Parameter>(type, w_shape);
} else {
NPUW_ASSERT(false && "Yet unsupported combination");
}
NPUW_ASSERT(new_param);
params_to_nf4_gather[new_param] = {w, t};
return new_param;
}
Context::PPtr Context::host_gather(const Context::PPtr& w, const Context::PPtr& ids) {
const auto& w_shape = w->get_shape();
const auto& ids_shape = ids->get_shape();
NPUW_ASSERT(w_shape.size() == 2);
NPUW_ASSERT(ids_shape.size() == 2);
NPUW_ASSERT(ids_shape[0] == 1);
ov::Shape new_shape = {1, ids_shape[1], w_shape[1]};
auto new_param = std::make_shared<ov::op::v0::Parameter>(w->get_element_type(), new_shape);
params_to_gather = Gather{new_param, w, ids};
return new_param;
}
Context::PPtr Context::host_gather_unpack_quant(const Context::PPtr& ids,
const Context::PPtr& w,
const Context::PPtr& z,
const Context::PPtr& s,
ov::element::Type type) {
const auto& w_shape = w->get_shape();
const auto& ids_shape = ids->get_shape();
NPUW_ASSERT(ids_shape.size() == 2);
NPUW_ASSERT(ids_shape[0] == 1);
Context::PPtr new_param;
if (s) {
const auto& s_shape = s->get_shape();
if (w_shape.size() == 3 && s_shape.size() == 3) {
ov::Shape new_shape = {1, ids_shape[1], w_shape[1] * w_shape[2]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else if (w_shape.size() == 2 && s_shape.size() == 2) {
ov::Shape new_shape = {1, ids_shape[1], w_shape[1]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else {
NPUW_ASSERT(false && "Yet unsupported combination");
}
} else {
// No scale, no zerop
if (w_shape.size() == 3) {
ov::Shape new_shape = {1, ids_shape[1], w_shape[1] * w_shape[2]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else if (w_shape.size() == 2) {
ov::Shape new_shape = {1, ids_shape[1], w_shape[1]};
new_param = std::make_shared<ov::op::v0::Parameter>(type, new_shape);
} else {
NPUW_ASSERT(false && "Yet unsupported combination");
}
}
NPUW_ASSERT(new_param);
params_to_quant_gather_unpack = QuantizedGather{};
params_to_quant_gather_unpack->params_to_runtime_unpack_gather[new_param] = {w, z, s};
params_to_quant_gather_unpack->pids = ids;
return new_param;
}
bool Context::found_host_gather_quant() const {
return params_to_quant_gather_unpack.has_value();
}
namespace opp = ov::pass::pattern;
namespace uat = ov::npuw::util::at;
// FROM:
// ???(Act) ------------------------------------------------------------>
// Param(W) -------> (Reshape) -> to(f16/f32) -> Multiply -> (to(f32)) -> MatMul
// Param/Const(S) -> (Reshape) -> (to(f32)) --->
//
// TO:
// ???(Act) --------------------> to(f16/f32) ->
// Param(W) -------> (Reshape) -> to(f16/f32) -> MatMul -> Multiply -> (to(f32))
// Param/Const(S) -> (Reshape) -> (to(f32)) -> Reshape -->
//
DQMatMulCWi::DQMatMulCWi(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::any_input();
auto reshapew = opp::optional<ov::op::v1::Reshape>({qweight, opp::any_input()});
auto reshapec = opp::optional<ov::op::v1::Reshape>({qcoeff, opp::any_input()});
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({reshapew});
auto qcvtc = opp::optional<ov::op::v0::Convert>({reshapec->output(0)});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcvtc});
auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls->output(0)});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qcvtm});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
auto qcoeff_shape = matched_node_qcoeff->output(0).get_shape();
if ((ov::element::i4 == matched_qweight->get_element_type() ||
ov::element::i8 == matched_qweight->get_element_type() ||
ov::element::nf4 == matched_qweight->get_element_type()) &&
(ov::op::util::is_parameter(matched_node_qcoeff) || ov::op::util::is_constant(matched_node_qcoeff)) &&
qcoeff_shape[1] == 1 && !matched_matmul->get_transpose_a() && matched_matmul->get_transpose_b()) {
auto matched_node_cvtw = node_to_output.at(qcvtw).get_node_shared_ptr();
auto matched_node_muls = node_to_output.at(qmuls).get_node_shared_ptr();
auto matched_node_mmi = node_to_output.at(qmmi).get_node_shared_ptr();
auto& matched_node_qcoeff_out = uat::_(node_to_output).at_or_at_or_at(qcvtc, reshapec, qcoeff);
auto& matched_node_muls_out = uat::_(node_to_output).at_or_at(qcvtm, qmuls);
if (!ctx.get().mm_dq_full) {
const auto& matm_mul_out_shape = matched_matmul->get_output_shape(0);
const auto& matm_mul_in_shape = matched_matmul->get_input_shape(1);
NPUW_ASSERT(matm_mul_out_shape.back() == matm_mul_in_shape.front());
NPUW_ASSERT(matched_matmul->get_transpose_b());
return false; // root hasn't changed
}
// Reconnect MatMul to read from Convert(W) directly.
// Note: ACT has to be converted too.
auto cvt_prec = matched_node_cvtw->output(0).get_element_type();
auto new_cvt_act = std::make_shared<ov::op::v0::Convert>(matched_node_mmi, cvt_prec);
matched_matmul->input(0).replace_source_output(new_cvt_act);
matched_matmul->input(1).replace_source_output(matched_node_cvtw);
// Store MatMul's readers
auto mm_readers = matched_matmul->output(0).get_target_inputs();
// Introduce a Reshape to alter Scale factor's shape
auto new_dims = std::vector<std::size_t>{qcoeff_shape[1], qcoeff_shape[0]};
auto new_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{2}, new_dims);
auto new_reshape = std::make_shared<ov::op::v1::Reshape>(matched_node_qcoeff_out, new_const, false);
// Reconnect Multiply's both inputs. Drop all outputs
matched_node_muls->input(0).replace_source_output(matched_matmul);
matched_node_muls->input(1).replace_source_output(new_reshape);
for (auto&& r : matched_node_muls->output(0).get_target_inputs()) {
matched_node_muls->output(0).remove_target_input(r);
}
// Reconnect Convert(M) to convert the Multiply's result (optional)
if (matched_node_muls_out != matched_node_muls) {
matched_node_muls_out.get_node()->input(0).replace_source_output(matched_node_muls);
}
// Reconnect MatMul's old readers to Convert(Multiply)
for (auto&& r : mm_readers) {
r.replace_source_output(matched_node_muls_out);
}
return true; // root has changed
}
return false; // root hasn't changed
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulCWi"), std::move(callback));
}
// FROM:
// ???(Act) ----------------------------------------------->
// Param(W) -------> to(f16/f32) -> Multiply -> Transpose -> MatMul
// Param/Const(S) ---------------->
//
// TO:
// ???(Act) ---------------------------------->
// Param(W) -------> to(f16/f32) -> Multiply -> MatMul (Transpose Attributes)
// Param/Const(S) ---------------->
//
DQMatMulCWi_Transpose::DQMatMulCWi_Transpose(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qtrans = opp::wrap_type<ov::op::v1::Transpose>({qmuls, opp::any_input()});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qtrans});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_node_transpose = node_to_output.at(qtrans).get_node_shared_ptr();
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
const auto& tr_in_shape = matched_node_transpose->input(0).get_shape();
const auto& tr_out_shape = matched_node_transpose->output(0).get_shape();
if ((ov::element::i4 == matched_qweight->get_element_type() ||
ov::element::i8 == matched_qweight->get_element_type() ||
ov::element::nf4 == matched_qweight->get_element_type()) &&
!matched_matmul->get_transpose_a() && !matched_matmul->get_transpose_b() && tr_in_shape.size() == 2 &&
tr_out_shape.size() == 2 && tr_in_shape[0] == tr_out_shape[1] && tr_in_shape[1] == tr_out_shape[0]) {
auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
matched_matmul->input(1).replace_source_output(matched_node_qmuls);
matched_matmul->set_transpose_b(true);
return true; // root has changed
}
return false; // root hasn't changed
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulCWi_Transpose"), std::move(callback));
}
// 1 token case (generate)
//
// FROM:
// ???(Act) -------------------------------------------->
// Param(W) -> Convert(f16|f32) -> Multiply -> Reshape -> MatMul
// Param(S) --------------------->
//
// WHERE (example):
// Act: [ 1, 1, 4096]
// W: [32,128,11008]
// S: [32, 1,11008]
// [1, 1 ,128] x
// TO: [1,11K,128]T =
// [32,1,128] [1, 1 ,11K] [32,1,11K]
// ???(Act) -> Reshape > Split(/32) ->[to(f16) -> ]}
// Param(W*) -----------> Split(/32) ->[to(f16) -> MatMul]} Concat v
// Param(S) ---------------------------------------------> Multiply
// Reshape(1,a,b,c)
// ReduceSum(1)
// Reshape(a,b,c)
// to(f32)
// WHERE:
// W* : [32,11008,128]
DQMatMulGQi::DQMatMulGQi(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qmuls, opp::any_input()});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qreshp});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_node_qreshp = node_to_output.at(qreshp).get_node_shared_ptr();
auto matched_out_mmi = node_to_output.at(qmmi);
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qcoeff);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
auto qweight_shape = matched_qweight->output(0).get_shape();
auto qcoeff_shape = matched_qcoeff->output(0).get_shape();
auto act_shape = matched_out_mmi.get_shape();
auto out_shape = matched_node_matmul->output(0).get_shape();
if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&
(ov::element::f32 == matched_qcoeff->get_element_type() ||
ov::element::f16 == matched_qcoeff->get_element_type()) &&
qcoeff_shape.size() == 3 && act_shape.size() == 3 && act_shape[1] == 1 && // single-token case
qcoeff_shape[0] == qweight_shape[0] && qcoeff_shape[1] == 1 && qcoeff_shape[2] == qweight_shape[2] &&
!matched_matmul->get_transpose_a() && !matched_matmul->get_transpose_b()) {
if (!ctx.get().mm_dq_full) {
// Transpose weight and coeff
ctx.get().permute(matched_qweight, {0, 2, 1});
ctx.get().permute(matched_qcoeff, {0, 2, 1});
// Add Transpose and insert it
std::vector<std::size_t> new_transpose_order = {1, 0, 2};
auto new_transpose_order_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, new_transpose_order);
auto new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qmuls, new_transpose_order_c);
matched_node_qreshp->input(0).replace_source_output(new_transpose);
matched_node_qreshp->validate_and_infer_types();
// Change Reshape's shape
std::vector<std::size_t> transposed_shape = {qweight_shape[2], qweight_shape[0] * qweight_shape[1]};
auto transposed_shape_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{2}, transposed_shape);
matched_node_qreshp->input(1).replace_source_output(transposed_shape_c);
matched_node_qreshp->validate_and_infer_types();
matched_matmul->set_transpose_b(true);
matched_matmul->validate_and_infer_types();
const auto& matm_mul_out_shape = matched_matmul->get_output_shape(0);
const auto& matm_mul_in_shape = matched_matmul->get_input_shape(1);
NPUW_ASSERT(matm_mul_out_shape.back() == matm_mul_in_shape.front());
return false; // root hasn't changed
}
// Mark W closure to transpose, and transpose the respective parameter
ctx.get().permute(matched_qweight, {0, 2, 1});
// Mark S closure to be lowered fo f16
if (ov::element::f32 == matched_qcoeff->get_element_type()) {
ctx.get().to_f16(matched_qcoeff);
}
// Reshape the Act to group format
const auto NSPLIT = qweight_shape[0];
std::vector<std::size_t> rshp_act_v = {NSPLIT, act_shape[1], act_shape[2] / NSPLIT};
auto rshp_act_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, rshp_act_v);
auto rshp_act = std::make_shared<ov::op::v1::Reshape>(matched_out_mmi, rshp_act_c, false);
// Split Act and W, and S tensors by NSPLIT
auto split_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 0);
auto split_a = std::make_shared<ov::op::v1::Split>(rshp_act, split_axis, NSPLIT);
auto split_w = std::make_shared<ov::op::v1::Split>(matched_qweight, split_axis, NSPLIT);
// Do the CW MM for every split
std::vector<std::shared_ptr<ov::Node>> to_concat;
for (std::size_t i = 0; i < NSPLIT; i++) {
auto a_f16 = std::make_shared<ov::op::v0::Convert>(split_a->output(i), ov::element::f16);
auto w_f16 = std::make_shared<ov::op::v0::Convert>(split_w->output(i), ov::element::f16);
auto m_f16 = std::make_shared<ov::op::v0::MatMul>(a_f16, w_f16, false, true);
to_concat.push_back(m_f16);
}
// Now concat and scale the result
auto concat = std::make_shared<ov::op::v0::Concat>(to_concat, 0);
auto s_f16 = std::make_shared<ov::op::v1::Multiply>(concat, matched_qcoeff);
// Now reshape to a better shape, ReduceSum, and reshape to the right size again
std::vector<std::size_t> rshp_ccat_v = {1, NSPLIT, 1, qweight_shape[2]};
auto rshp_ccat_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, rshp_ccat_v);
auto rshp_ccat = std::make_shared<ov::op::v1::Reshape>(s_f16, rshp_ccat_c, false);
auto reduce_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
auto reduce = std::make_shared<ov::op::v1::ReduceSum>(rshp_ccat, reduce_axis, true);
auto rshp_out_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, out_shape);
auto rshp_out = std::make_shared<ov::op::v1::Reshape>(reduce, rshp_out_c, false);
// Convert the result to f32 to maintain the graph contracts. FIXME should be avoided
auto out = std::make_shared<ov::op::v0::Convert>(rshp_out, ov::element::f32);
// Now.. Reconnect the matmul readers to the new output (reducesum)
for (auto&& r : matched_matmul->output(0).get_target_inputs()) {
r.replace_source_output(out);
}
return true; // root has changed
}
return false; // did nothing here
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulGQi"), std::move(callback));
}
// FROM:
// ???(Act) -------------------------------------------------------->
// Param(W) -> Convert(f16) -> Multiply -> Reshape -> Convert(f32) -> MatMul
// Param(S) ----------------->
//
// WHERE (example):
// Act: [ 1, 1,2048]
// W: [512,16, 128]
// S: [512,16, 1]
// [1, 1,128] x
// TO: [1,512,128]T =
// [16,1,128] [1, 1,512] [16,1,512]
// ???(Act) -> Reshape > Split(/16) ->[to(f16) -> ]}
// Param(W*) -----------> Split(/16) ->[to(f16) -> MatMul >]} Concat
// v
// Param(S*) ---------------------------------------------> Multiply
// Reshape(1,16,1,512)
// ReduceSum(1)
// Reshape( 1,1,512)
// to(f32)
// WHERE:
// W* : [16,512,128]
// S* : [16, 1,512]
DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls->output(0)});
auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qcvtm, opp::any_input()});
auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qcvtr});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
std::shared_ptr<Node> matched_node_qcvtm = nullptr;
if (node_to_output.count(qcvtm)) {
matched_node_qcvtm = node_to_output.at(qcvtm).get_node_shared_ptr();
}
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_node_qreshp = node_to_output.at(qreshp).get_node_shared_ptr();
auto matched_out_mmi = node_to_output.at(qmmi);
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qcoeff);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
auto qweight_shape = matched_qweight->output(0).get_shape();
auto qcoeff_shape = matched_qcoeff->output(0).get_shape();
auto act_shape = matched_out_mmi.get_shape();
auto out_shape = matched_node_matmul->output(0).get_shape();
if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&
ov::element::f16 == matched_qcoeff->get_element_type() && qcoeff_shape.size() == 3 &&
act_shape.size() == 3 && act_shape[0] == 1 && act_shape[1] == 1 && qcoeff_shape[0] == qweight_shape[0] &&
qcoeff_shape[2] == 1 && qcoeff_shape[1] == qweight_shape[1] && !matched_matmul->get_transpose_a() &&
matched_matmul->get_transpose_b()) {
if (!ctx.get().mm_dq_full) {
// Transpose weight and coeff
ctx.get().permute(matched_qweight, {1, 0, 2});
ctx.get().permute(matched_qcoeff, {1, 0, 2});
// Add Transpose and insert it
std::vector<std::size_t> new_transpose_order = {1, 0, 2};
auto new_transpose_order_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, new_transpose_order);
auto new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qmuls, new_transpose_order_c);
if (matched_node_qcvtm) {
new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qcvtm, new_transpose_order_c);
}
matched_node_qreshp->input(0).replace_source_output(new_transpose);
matched_node_qreshp->validate_and_infer_types();
matched_matmul->validate_and_infer_types();
const auto& matm_mul_out_shape = matched_matmul->get_output_shape(0);
const auto& matm_mul_in_shape = matched_matmul->get_input_shape(1);
NPUW_ASSERT(matm_mul_out_shape.back() == matm_mul_in_shape.front());
NPUW_ASSERT(matched_matmul->get_transpose_b());
return false; // root hasn't changed
}
// Mark W closure to transpose, and transpose the respective parameter
ctx.get().permute(matched_qweight, {1, 0, 2});
// Also transpose S, but in a different way (see diagram above)
ctx.get().permute(matched_qcoeff, {1, 2, 0});
// Reshape the Act to group format
const auto NSPLIT = qweight_shape[1];
std::vector<std::size_t> rshp_act_v = {NSPLIT, 1, act_shape[2] / NSPLIT};
auto rshp_act_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, rshp_act_v);
auto rshp_act = std::make_shared<ov::op::v1::Reshape>(matched_out_mmi, rshp_act_c, false);
// Split Act and W, and S tensors by NSPLIT
auto split_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 0);
auto split_a = std::make_shared<ov::op::v1::Split>(rshp_act, split_axis, NSPLIT);
auto split_w = std::make_shared<ov::op::v1::Split>(matched_qweight, split_axis, NSPLIT);
// Do the CW MM for every split
std::vector<std::shared_ptr<ov::Node>> to_concat;
for (std::size_t i = 0; i < NSPLIT; i++) {
auto a_f16 = std::make_shared<ov::op::v0::Convert>(split_a->output(i), ov::element::f16);
auto w_f16 = std::make_shared<ov::op::v0::Convert>(split_w->output(i), ov::element::f16);
auto m_f16 = std::make_shared<ov::op::v0::MatMul>(a_f16, w_f16, false, true);
to_concat.push_back(m_f16);
}
// Now concat and scale the result
auto concat = std::make_shared<ov::op::v0::Concat>(to_concat, 0);
auto scaled = std::make_shared<ov::op::v1::Multiply>(concat, matched_qcoeff);
// Now reshape to a better shape, ReduceSum, and reshape to the right size again
std::vector<std::size_t> rshp_ccat_v = {1, NSPLIT, 1, qweight_shape[0]};
auto rshp_ccat_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, rshp_ccat_v);
auto rshp_ccat = std::make_shared<ov::op::v1::Reshape>(scaled, rshp_ccat_c, false);
auto reduce_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
// Make reduceSum not to keep axis because then it will convert to poolings in compiler.
// Otherwise reduceSum will convert to the convolution which is less efficient than poolings.
auto reduce = std::make_shared<ov::op::v1::ReduceSum>(rshp_ccat, reduce_axis, false);
auto rshp_out_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, out_shape);
auto rshp_out = std::make_shared<ov::op::v1::Reshape>(reduce, rshp_out_c, false);
// Convert the result to f32 to maintain the graph contracts if required.
std::shared_ptr<ov::Node> out = rshp_out;
if (matched_matmul->get_element_type() == ov::element::f32) {
out = std::make_shared<ov::op::v0::Convert>(rshp_out, ov::element::f32);
}
// Now.. Reconnect the matmul readers to the new output (reducesum)
for (auto&& r : matched_matmul->output(0).get_target_inputs()) {
r.replace_source_output(out);
}
return true; // root has changed
}
return false; // did nothing here
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulGQ2i"), std::move(callback));
}
// N token case (prompt)
//
// FROM:
// ???(Act) -------------------------------------------->
// Param(W) -> Convert(f16|f32) -> Multiply -> Reshape -> MatMul
// Param(S) --------------------->
//
// WHERE (example):
// Act: [ 1, N, 4096]
// W: [32,128,11008]
// S: [32, 1,11008]
// [1, N ,128] x
// TO: [1,11K,128]T =
// [N,32,128] [1,N,128] [1, N ,11K] [32,N,11K]
// ???(Act) -> Reshape > Split(/32) ->[to(f16) -> Reshape -> ]}
// Param(W*) -----------> Split(/32) ->[to(f16) ------------> MatMul v ]} 32xAdd
// Param(S) -------------Split(/32) ->[--------------------> Multiply ]} v
// to(f32)
// WHERE:
// W* : [32,11008,128]
DQMatMulGQiP::DQMatMulGQiP(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qmuls, opp::any_input()});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qreshp});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_node_qreshp = node_to_output.at(qreshp).get_node_shared_ptr();
auto matched_out_mmi = node_to_output.at(qmmi);
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qcoeff);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
auto qweight_shape = matched_qweight->output(0).get_shape();
auto qcoeff_shape = matched_qcoeff->output(0).get_shape();
auto act_shape = matched_out_mmi.get_shape();
if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&
(ov::element::f32 == matched_qcoeff->get_element_type() ||
ov::element::f16 == matched_qcoeff->get_element_type()) &&
qcoeff_shape.size() == 3 && act_shape.size() == 3 && act_shape[1] > 1 && // multi-token case
qcoeff_shape[0] == qweight_shape[0] && qcoeff_shape[1] == 1 && qcoeff_shape[2] == qweight_shape[2] &&
!matched_matmul->get_transpose_a() && !matched_matmul->get_transpose_b()) {
if (!ctx.get().mm_dq_full) {
// Transpose weight and coeff
ctx.get().permute(matched_qweight, {0, 2, 1});
ctx.get().permute(matched_qcoeff, {0, 2, 1});
// Add Transpose and insert it
std::vector<std::size_t> new_transpose_order = {1, 0, 2};
auto new_transpose_order_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, new_transpose_order);
auto new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qmuls, new_transpose_order_c);
matched_node_qreshp->input(0).replace_source_output(new_transpose);
matched_node_qreshp->validate_and_infer_types();
// // Change Reshape's shape
std::vector<std::size_t> transposed_shape = {qweight_shape[2], qweight_shape[0] * qweight_shape[1]};
auto transposed_shape_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{2}, transposed_shape);
matched_node_qreshp->input(1).replace_source_output(transposed_shape_c);
matched_node_qreshp->validate_and_infer_types();
matched_matmul->set_transpose_b(true);
matched_matmul->validate_and_infer_types();
const auto& matm_mul_out_shape = matched_matmul->get_output_shape(0);
const auto& matm_mul_in_shape = matched_matmul->get_input_shape(1);
NPUW_ASSERT(matm_mul_out_shape.back() == matm_mul_in_shape.front());
return false; // root hasn't changed
}
// Mark W closure to transpose, and transpose the respective parameter
ctx.get().permute(matched_qweight, {0, 2, 1});
// Mark S closure to be lowered fo f16
if (ov::element::f32 == matched_qcoeff->get_element_type()) {
ctx.get().to_f16(matched_qcoeff);
}
// Reshape the Act to group format
const auto NSPLIT = qweight_shape[0];
std::vector<std::size_t> rshp_act_v = {act_shape[1], NSPLIT, act_shape[2] / NSPLIT};
auto rshp_act_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, rshp_act_v);
auto rshp_act = std::make_shared<ov::op::v1::Reshape>(matched_out_mmi, rshp_act_c, false);
// Split Act and W, and S tensors by NSPLIT
auto split_axis_a = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
auto split_a = std::make_shared<ov::op::v1::Split>(rshp_act, split_axis_a, NSPLIT);
auto split_axis_w = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 0);
auto split_w = std::make_shared<ov::op::v1::Split>(matched_qweight, split_axis_w, NSPLIT);
auto split_s = std::make_shared<ov::op::v1::Split>(matched_qcoeff, split_axis_w, NSPLIT);
std::vector<std::size_t> r_a_v = {1, act_shape[1], act_shape[2] / NSPLIT};
auto r_a_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, r_a_v);
// Do the CW MM for every split
std::vector<std::shared_ptr<ov::Node>> to_concat;
for (std::size_t i = 0; i < NSPLIT; i++) {
auto a_f16 = std::make_shared<ov::op::v0::Convert>(split_a->output(i), ov::element::f16);
auto r_f16 = std::make_shared<ov::op::v1::Reshape>(a_f16, r_a_c, false);
auto w_f16 = std::make_shared<ov::op::v0::Convert>(split_w->output(i), ov::element::f16);
auto m_f16 = std::make_shared<ov::op::v0::MatMul>(r_f16, w_f16, false, true);
auto s_f16 = std::make_shared<ov::op::v1::Multiply>(m_f16, split_s->output(i));
to_concat.push_back(s_f16);
}
// Reduce via Add
std::vector<ov::Output<ov::Node>> reduce;
reduce.push_back(std::make_shared<ov::op::v1::Add>(to_concat[0], to_concat[1]));
for (std::size_t i = 1; i < NSPLIT - 1; i++) {
reduce.push_back(std::make_shared<ov::op::v1::Add>(reduce[i - 1], to_concat[i + 1]));
}
// Convert the result to f32 to maintain the graph contracts. FIXME should be avoided
auto out = std::make_shared<ov::op::v0::Convert>(reduce.back(), ov::element::f32);
// Now.. Reconnect the matmul readers to the new output (reducesum)
for (auto&& r : matched_matmul->output(0).get_target_inputs()) {
r.replace_source_output(out);
}
return true; // root has changed
}
return false; // did nothing here
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulGQiP"), std::move(callback));
}
// N token case (prompt)
//
// FROM:
// ???(Act) ------------------------------------------------------->
// Param(W) -> Convert(f16|f32) -> Multiply -> Reshape -> Convert -> MatMul
// Param(S) --------------------->
//
// WHERE (example):
// Act: [ 1, N,4096]
// W: [11008,32, 128]
// S: [11008,32, 1]
// [1, N ,128] x
// TO: [1,11K,128]T =
// [N,32,128] [1,N,128] [1, N ,11K] [32,N,11K]
// ???(Act) -> Reshape > Split(/32) ->[to(f16) - Reshape -> ]}
// Param(W*) -----------> Split(/32) ->[to(f16) -----------> MatMul v ]} 32xAdd
// Param(S*) -----------> Split(/32) ->[-------------------> Multiply ]} v
// to(f32)
// WHERE:
// W* : [32,11008, 128]
// S* : [32, 1,11008]
DQMatMulGQ2iP::DQMatMulGQ2iP(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qcvtm = opp::optional<ov::op::v0::Convert>({qmuls->output(0)});
auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qcvtm, opp::any_input()});
auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});
auto qmmi = opp::any_input();
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qcvtr});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
std::shared_ptr<Node> matched_node_qcvtm = nullptr;
if (node_to_output.count(qcvtm)) {
matched_node_qcvtm = node_to_output.at(qcvtm).get_node_shared_ptr();
}
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_node_qreshp = node_to_output.at(qreshp).get_node_shared_ptr();
auto matched_out_mmi = node_to_output.at(qmmi);
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qcoeff);
auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);
auto qweight_shape = matched_qweight->output(0).get_shape();
auto qcoeff_shape = matched_qcoeff->output(0).get_shape();
auto act_shape = matched_out_mmi.get_shape();
const auto just_one = [](std::size_t a, std::size_t b) {
return (a == 1 && b > 1) || (a > 1 && b == 1);
};
if (ov::element::i4 == matched_qweight->get_element_type() && qweight_shape.size() == 3 &&
ov::element::f16 == matched_qcoeff->get_element_type() && qcoeff_shape.size() == 3 &&
act_shape.size() == 3 && just_one(act_shape[0], act_shape[1]) && // multi-token case
qcoeff_shape[0] == qweight_shape[0] && qcoeff_shape[1] == qweight_shape[1] && qcoeff_shape[2] == 1 &&
!matched_matmul->get_transpose_a() && matched_matmul->get_transpose_b()) {
if (!ctx.get().mm_dq_full) {
// Transpose weight and coeff
ctx.get().permute(matched_qweight, {1, 0, 2});
ctx.get().permute(matched_qcoeff, {1, 0, 2});
// Add Transpose and insert it
std::vector<std::size_t> new_transpose_order = {1, 0, 2};
auto new_transpose_order_c =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, new_transpose_order);
auto new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qmuls, new_transpose_order_c);
if (matched_node_qcvtm) {
new_transpose = std::make_shared<ov::op::v1::Transpose>(matched_node_qcvtm, new_transpose_order_c);
}
matched_node_qreshp->input(0).replace_source_output(new_transpose);
matched_node_qreshp->validate_and_infer_types();
matched_matmul->validate_and_infer_types();
const auto& matm_mul_out_shape = matched_matmul->get_output_shape(0);
const auto& matm_mul_in_shape = matched_matmul->get_input_shape(1);
NPUW_ASSERT(matm_mul_out_shape.back() == matm_mul_in_shape.front());
NPUW_ASSERT(matched_matmul->get_transpose_b());
return false; // root hasn't changed
}
// Mark W closure to transpose, and transpose the respective parameter
ctx.get().permute(matched_qweight, {1, 0, 2});
// Also transpose S, but in a different way (see diagram above)
ctx.get().permute(matched_qcoeff, {1, 2, 0});
// Select proper activation shape
std::size_t act_dim = act_shape[0] > act_shape[1] ? 0 : 1;
// Reshape the Act to group format
const auto NSPLIT = qweight_shape[1];
std::vector<std::size_t> rshp_act_v = {act_shape[act_dim], NSPLIT, act_shape[2] / NSPLIT};
auto rshp_act_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, rshp_act_v);
auto rshp_act = std::make_shared<ov::op::v1::Reshape>(matched_out_mmi, rshp_act_c, false);
// Split Act and W, and S tensors by NSPLIT
auto split_axis_a = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
auto split_a = std::make_shared<ov::op::v1::Split>(rshp_act, split_axis_a, NSPLIT);
auto split_axis_w = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 0);
auto split_w = std::make_shared<ov::op::v1::Split>(matched_qweight, split_axis_w, NSPLIT);
auto split_s = std::make_shared<ov::op::v1::Split>(matched_qcoeff, split_axis_w, NSPLIT);
std::vector<std::size_t> r_a_v = {1, act_shape[act_dim], act_shape[2] / NSPLIT};
auto r_a_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, r_a_v);
// Do the CW MM for every split
std::vector<std::shared_ptr<ov::Node>> to_concat;
for (std::size_t i = 0; i < NSPLIT; i++) {
auto a_f16 = std::make_shared<ov::op::v0::Convert>(split_a->output(i), ov::element::f16);
auto r_f16 = std::make_shared<ov::op::v1::Reshape>(a_f16, r_a_c, false);
auto w_f16 = std::make_shared<ov::op::v0::Convert>(split_w->output(i), ov::element::f16);
auto m_f16 = std::make_shared<ov::op::v0::MatMul>(r_f16, w_f16, false, true);
auto s_f16 = std::make_shared<ov::op::v1::Multiply>(m_f16, split_s->output(i));
to_concat.push_back(s_f16);
}
// Reduce via Add
std::vector<ov::Output<ov::Node>> reduce;
reduce.push_back(std::make_shared<ov::op::v1::Add>(to_concat[0], to_concat[1]));
for (std::size_t i = 1; i < NSPLIT - 1; i++) {
reduce.push_back(std::make_shared<ov::op::v1::Add>(reduce[i - 1], to_concat[i + 1]));
}
ov::Output<ov::Node> out = reduce.back();
if (matched_matmul->output(0).get_element_type() == ov::element::f32) {
// Convert the result to f32 to maintain the graph contracts, if needed
out = std::make_shared<ov::op::v0::Convert>(out, ov::element::f32);
}
if (act_shape[0] > act_shape[1]) {
std::vector<std::size_t> new_out_size = {act_shape[0], act_shape[1], qweight_shape[0]};
auto new_out_shape =
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, new_out_size);
out = std::make_shared<ov::op::v1::Reshape>(out, new_out_shape, false);
}
// Now.. Reconnect the matmul readers to the new output (reducesum)
for (auto&& r : matched_matmul->output(0).get_target_inputs()) {
r.replace_source_output(out);
}
return true; // root has changed
}
return false; // did nothing here
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulGQ2iP"), std::move(callback));
}
////////////////////////////////////////////////////////////////////////////////
// Parallel matmuls
// Identifies this pattern
//
// Multiply -----------------------------------> MatMul
// Param(W) -> to(f32) -> Multiply -> Reshape ->
// Param(S) ------------>
DQParMMGQ::DQParMMGQ(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qmuls, opp::any_input()});
auto qmmi = opp::wrap_type<ov::op::v1::Multiply>({opp::any_input(), opp::any_input()});
auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});
auto qcvtm = opp::optional<ov::op::v0::Convert>({qmmi->output(0)});
auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qcvtm, qcvtr});
// Note: Use [=] to make sure the above objects stay alive in the callback
auto callback = [=](ov::pass::pattern::Matcher& m) {
auto& node_to_output = m.get_pattern_value_map();
auto w_param =
std::static_pointer_cast<ov::op::v0::Parameter>(node_to_output.at(qweight).get_node_shared_ptr());
auto s_param = std::static_pointer_cast<ov::op::v0::Parameter>(node_to_output.at(qcoeff).get_node_shared_ptr());
auto matmul = std::static_pointer_cast<ov::op::v0::MatMul>(node_to_output.at(qmm).get_node_shared_ptr());
auto qmmi_shape = node_to_output.at(qmm).get_shape();
if (qmmi_shape.size() != 3 || qmmi_shape[0] != 1) {
// Not handling such cases
return false;
}
if (!matmul->get_transpose_a() && !matmul->get_transpose_b()) {
ctx.get().register_parallel_matmul(
node_to_output.at(qmmi),
2,
Context::DQParMM{std::move(w_param), std::move(s_param), std::move(matmul)});
} else if (!matmul->get_transpose_a() && matmul->get_transpose_b()) {
ctx.get().register_parallel_matmul(
node_to_output.at(qmmi),
0,
Context::DQParMM{std::move(w_param), std::move(s_param), std::move(matmul)});
}
return false; // no change here
};
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQParMMGQ"), std::move(callback));
}
void mergeParallelMatMuls(const std::shared_ptr<ov::Model>& m, Context& ctx) {
for (auto&& mul_to_mms : ctx.par_dq_mms) {
auto& parallel_matmuls = mul_to_mms.second;
if (parallel_matmuls.size() < 2) {
continue;
}
ov::Output<ov::Node> orig_multiply;
std::size_t axis_to_concat = -1;
std::tie(orig_multiply, axis_to_concat) = mul_to_mms.first;
const ov::Shape orig_act_shape = orig_multiply.get_shape();
if (!util::is_set(axis_to_concat, ctx.pmm_dims)) {
LOG_VERB("Parallel MatMuls found, but fusion over dim " << axis_to_concat << " is not enabled");
continue;
}
const auto& first_w = parallel_matmuls[0].w;
const auto& first_s = parallel_matmuls[0].s;
ov::ParameterVector old_w, old_s;
bool all_ok = true;
for (auto&& dqmm : parallel_matmuls) {
if (first_w->get_shape().size() != dqmm.w->get_shape().size() ||
first_s->get_shape().size() != dqmm.s->get_shape().size() ||
dqmm.w->get_shape().size() != dqmm.s->get_shape().size()) {
all_ok = false;
break;