onednn-src 0.1.13

Source of oneAPI Deep Neural Network Library (oneDNN)
Documentation
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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
/*******************************************************************************
 * Copyright 2022 Intel Corporation
 *
 * 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 <memory>
#include <string>
#include <vector>

#include "oneapi/dnnl/dnnl.hpp"

#include "graph/interface/c_types_map.hpp"
#include "graph/interface/value.hpp"

#include "graph/backend/dnnl/common.hpp"
#include "graph/backend/dnnl/layout_propagator.hpp"
#include "graph/backend/dnnl/op_executable.hpp"

#include "graph/interface/shape_infer.hpp"

namespace dnnl {
namespace impl {
namespace graph {
namespace dnnl_impl {
#define VCHECK_LAYOUT_PROPAGATOR(cond, status, msg, ...) \
    VCONDCHECK(graph, create, check, layout_propagator, (cond), status, msg, \
            ##__VA_ARGS__);

using op_t = op_t;
using op_ptr = std::shared_ptr<op_t>;
using value_ptr = std::shared_ptr<value_t>;
using ltw = logical_tensor_wrapper_t;

status_t insert_reorder_before(op_ptr &op, size_t offset,
        const dnnl::memory::desc &opt_mdesc, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    value_ptr in_val = op->get_input_value(offset);
    const logical_tensor_t &in_lt = in_val->get_logical_tensor();
    // just return if real input layout is the same as optimal layout or
    // input layout type is ANY
    if (make_dnnl_memory_desc(in_lt) == opt_mdesc || ltw(in_lt).is_any())
        return status;

    // create reorder op, connect it to graph and add it's scratchpad output
    auto reorder_op = std::make_shared<op_t>(op_kind::_reorder);
    rewriter.insert_op_before(reorder_op, op, offset);
    auto scratchpad_val = insert_empty_scratchpad(reorder_op);
    // set optimal layout to reorder's output
    auto reorder_out_val = reorder_op->get_output_value(0);
    status = fill_layout_info(reorder_out_val, opt_mdesc);
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder output");
    // fill shape info
    reorder_out_val->set_data_type(ltw(in_lt).data_type());
    reorder_out_val->set_dims(ltw(in_lt).vdims());

    // set layout info for scratchpad output
    const auto &pd = reorder_executable_t::create_desc(
            reorder_op, p_engine, pd_cache, fpmath, use_block_layout);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t insert_reorder_after(op_ptr &op, size_t offset,
        const dnnl::memory::desc &opt_mdesc, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    value_ptr out_val = op->get_output_value(offset);
    const logical_tensor_t &out_lt = out_val->get_logical_tensor();
    // just return if real output layout is the same as optimal layout or
    // output layout type is ANY
    if (make_dnnl_memory_desc(out_lt) == opt_mdesc || ltw(out_lt).is_any())
        return status;

    // create reorder op, connect it to graph and add it's scratchpad output
    auto reorder_op = std::make_shared<op_t>(op_kind::_reorder);
    rewriter.insert_op_after(reorder_op, op, offset);
    auto scratchpad_val = insert_empty_scratchpad(reorder_op);
    // set optimal layout to reorder's input
    auto reorder_in_val = reorder_op->get_input_value(0);
    status = fill_layout_info(reorder_in_val, opt_mdesc);
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder input");
    // fill shape info
    reorder_in_val->set_data_type(ltw(out_lt).data_type());
    reorder_in_val->set_dims(ltw(out_lt).vdims());

    // set layout info for scratchpad output
    const auto &pd = reorder_executable_t::create_desc(
            reorder_op, p_engine, pd_cache, fpmath, use_block_layout);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_conv(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    // always create pd using any format
    const auto &pd = conv_fwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    // insert reorders for conv's inputs
    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv src");
    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv weights");

    if (op->has_attr(op_attr::with_bias)
            && op->get_attr<bool>(op_attr::with_bias)) {
        insert_reorder_before(op, 2, pd.bias_desc(), p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
        value_ptr bias = op->get_input_value(2);
        status = fill_layout_info(bias, pd.bias_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder before conv bias");
    }

    if (!op->has_attr(op_attr::fusion_info)) {
        fusion_info_t fusion_info;
        op->set_attr<fusion_info_t>(op_attr::fusion_info, fusion_info);
    }
    const fusion_info_t &fusion_info
            = op->get_attr<fusion_info_t>(op_attr::fusion_info);

    if (fusion_info.has_post_dw_conv()) {
        const auto &dw_conv = fusion_info.get_post_dw_conv();
        auto dw_idx = dw_conv->get_unfused_input_indices();
        value_ptr dw_wei = op->get_input_value(dw_idx[0]);
        value_ptr dw_bias = nullptr;
        if (dw_conv->get_unfused_input_indices().size() > 1) {
            dw_bias = op->get_input_value(dw_idx[1]);
        }

        const auto &dw_wei_opt_mdesc = pd.query_md(query::exec_arg_md,
                DNNL_ARG_ATTR_POST_OP_DW | DNNL_ARG_WEIGHTS);
        insert_reorder_before(op, dw_idx[0], dw_wei_opt_mdesc, p_engine,
                pd_cache, fpmath, use_block_layout, rewriter);
        status = fill_layout_info(dw_wei, dw_wei_opt_mdesc);

        if (dw_conv->get_unfused_input_indices().size() > 1) {
            const auto &dw_bias_opt_mdesc = pd.query_md(query::exec_arg_md,
                    DNNL_ARG_ATTR_POST_OP_DW | DNNL_ARG_BIAS);
            insert_reorder_before(op, dw_idx[1], dw_bias_opt_mdesc, p_engine,
                    pd_cache, fpmath, use_block_layout, rewriter);
            status = fill_layout_info(dw_bias, dw_bias_opt_mdesc);
        }
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder before post_dw_conv");
    }

    if (fusion_info.has_post_binary()) {
        const auto &post_ops = fusion_info.get_post_ops();
        for (size_t i = 0; i < post_ops.size(); ++i) {
            if (!post_ops[i]->is_post_binary()) continue;
            const auto &binary = post_ops[i];
            std::vector<size_t> binary_idx
                    = binary->get_unfused_input_indices();
            if (binary_idx.empty()) continue;

            value_ptr binary_unfused_src = op->get_input_value(binary_idx[0]);
            const auto &binary_unfused_src_opt_mdesc
                    = pd.query_md(query::exec_arg_md,
                            DNNL_ARG_SRC_1
                                    | DNNL_ARG_ATTR_MULTIPLE_POST_OP(
                                            static_cast<int>(i)));
            insert_reorder_before(op, binary_idx[0],
                    binary_unfused_src_opt_mdesc, p_engine, pd_cache, fpmath,
                    use_block_layout, rewriter);
            status = fill_layout_info(
                    binary_unfused_src, binary_unfused_src_opt_mdesc);

            VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                    "failed to fill layout info for reorder before "
                    "conv post_binary");
        }
    }
    // insert a reorder if output layout is different from output optimal layout
    // 1) output layout is opaque
    // 2) output is any, directly set optimal layout
    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after conv dst");

    // fill scratchpads dimensions and data type to scratchpad value_t
    // according to op schema, scratchpad must be be second output
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_deconv(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = deconv_fwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    // insert reorders for deconv's inputs
    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv src");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv weights");

    if (op->has_attr(op_attr::with_bias)
            && op->get_attr<bool>(op_attr::with_bias)) {
        insert_reorder_before(op, 2, pd.bias_desc(), p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
        value_ptr bias = op->get_input_value(2);
        status = fill_layout_info(bias, pd.bias_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder before deconv bias");
    }
    // insert a reorder if output layout is different from output optimal layout
    // 1) output layout is opaque
    // 2) output is any, directly set optimal layout
    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after deconv dst");

    // fill scratchpads dimensions and data type to scratchpad value_t
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_deconv_bwd_data(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    // always create pd using any format
    const auto &pd = deconv_bwd_data_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    // insert reorders for inputs
    insert_reorder_before(op, 0, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(0);
    status_t status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv_bwd_data "
            "diff_dst");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv_bwd_data "
            "weights");

    // insert a reorder if output layout is different from output optimal layout
    // 1) output layout is opaque
    // 2) output is any, directly set optimal layout
    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after deconv_bwd_data "
            "diff_src");

    // fill scratchpads dimensions and data type to scratchpad value_t
    // according to op schema, scratchpad must be be second output
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_deconv_bwd_weights(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = deconv_bwd_weights_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv_bwd_weights "
            "src");

    insert_reorder_before(op, 1, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(1);
    status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before deconv_bwd_weights "
            "diff_dst");

    insert_reorder_after(op, 0, pd.diff_weights_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr diff_weights = op->get_output_value(0);
    status = fill_layout_info(diff_weights, pd.diff_weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after deconv_bwd_weights "
            "diff_weights");

    // fill scratchpads dimensions and data type to scratchpad value_t
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_eltwise(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    // When input's layout is specified (opaque or strided),
    // we can propagate it to output.
    const auto &pd = eltwise_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after eltwise dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_eltwise_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = eltwise_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    // to hit an optimized kernel, input/output of forward and both diff_dst and
    // diff_src should use the same memory format. Primitive is created based on
    // a backward data and here we are conditionally aligning forward data
    // format.
    auto opt_desc = (op->has_attr(op_attr::use_dst)
                            && op->get_attr<bool>(op_attr::use_dst))
            ? pd.dst_desc()
            : pd.src_desc();
    insert_reorder_before(op, 0, opt_desc, p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr data = op->get_input_value(0);
    status_t status = fill_layout_info(data, opt_desc);
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before eltwise_bwd "
            "inputs 0");

    insert_reorder_before(op, 1, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(1);
    status = fill_layout_info(diff_dst, opt_desc);
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before eltwise_bwd "
            "diff_dst");
    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after eltwise_bwd "
            "diff_src");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_binary(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    using ltw = logical_tensor_wrapper_t;
    status_t status = status::success;

    // if with zero dimension, the binary op will take no effect, we just
    // complete the layout propagation process by using dummy dst md.
    if (ltw(op->get_input_logical_tensor(0)).has_zero_dim()
            || ltw(op->get_input_logical_tensor(1)).has_zero_dim()) {
        value_ptr dst = op->get_output_value(0);
        status = fill_layout_info(dst,
                to_ncx_format(
                        make_dnnl_memory_desc(dst->get_logical_tensor())));
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for binary logical tensor with 0 "
                "dims");

        return fill_layout_info(op->get_output_value(1), dnnl::memory::desc {});
    }

    const auto &pd = binary_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after binary dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_concat(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    const auto &pd = concat_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    for (size_t i = 0; i < op->num_inputs(); ++i) {
        insert_reorder_before(op, i, pd.src_desc(static_cast<int>(i)), p_engine,
                pd_cache, fpmath, use_block_layout, rewriter);
        status = fill_layout_info(
                op->get_input_value(i), pd.src_desc(static_cast<int>(i)));
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder before concat "
                "src");
    }

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    status = fill_layout_info(op->get_output_value(0), pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after concat dst");

    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_shuffle(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = shuffle_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    value_ptr src = op->get_input_value(0);
    value_ptr dst = op->get_output_value(0);

    VCHECK_LAYOUT_PROPAGATOR(!ltw(src->get_logical_tensor()).is_any(),
            status::invalid_arguments, "shuffle's src layout can't be any");

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after shuffle dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_matmul(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    using ltw = logical_tensor_wrapper_t;
    status_t status = status::success;

    // if with zero dimension, the matmul op will take no effect, we just
    // complete the layout propagation process by using dummy dst md.
    if (ltw(op->get_input_logical_tensor(0)).has_zero_dim()
            || ltw(op->get_input_logical_tensor(1)).has_zero_dim()) {
        value_ptr dst = op->get_output_value(0);
        status = fill_layout_info(dst,
                to_ncx_format(
                        make_dnnl_memory_desc(dst->get_logical_tensor())));
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for matmul 0 dims dst");

        return fill_layout_info(op->get_output_value(1), dnnl::memory::desc {});
    }

    const auto &pd = matmul_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    // insert reorders for matmul's inputs
    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before matmul src 0");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before matmul src 1");

    if (op->has_attr(op_attr::with_bias)
            && op->get_attr<bool>(op_attr::with_bias)) {
        insert_reorder_before(op, 2, pd.bias_desc(), p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
        value_ptr bias = op->get_input_value(2);
        status = fill_layout_info(bias, pd.bias_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder before matmul bias");
    }
    // insert a reorder if output layout is different from output optimal layout
    // 1) output layout is opaque
    // 2) output is any, directly set optimal layout
    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after matmul dst");

    // fill scratchpads dimensions and data type to scratchpad value_t
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_pool(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = pool_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after pool dst");

    // make scratchpad as pool's last output
    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after pool scratchpad");

    if (op->has_attr(op_attr::is_training)
            && op->get_attr<bool>(op_attr::is_training)) {
        value_ptr workspace_val = op->get_output_value(2);
        const memory::desc &ws_md = pd.workspace_desc();
        workspace_val->set_dims(ws_md.get_dims());
        workspace_val->set_data_type(
                static_cast<data_type_t>(ws_md.get_data_type()));
        status = fill_layout_info(workspace_val, ws_md);
    }
    return status;
}

status_t layout_propagator_for_pool_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = pool_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(0);
    status_t status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before pool_bwd "
            "diff_dst");

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after pool_bwd diff_src");

    // make scratchpad as pool's last output
    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_batchnorm(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = batchnorm_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before batchnorm src");

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after batchnorm dst");

    if (op->get_attr<bool>(op_attr::is_training)) {
        value_ptr running_mean = op->get_output_value(1);
        value_ptr running_variance = op->get_output_value(2);
        value_ptr batch_mean = op->get_output_value(3);
        value_ptr batch_variance = op->get_output_value(4);

        status = fill_layout_info(running_mean, pd.mean_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm running_mean");
        status = fill_layout_info(running_variance, pd.variance_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm running_variance");
        status = fill_layout_info(batch_mean, pd.mean_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm batch_mean");
        status = fill_layout_info(batch_variance, pd.variance_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm batch_variance");
    }

    size_t scratchpad_index = op->num_outputs() - 1;

    // if batchnorm's prop_kind is forward_training and fused with ReLU, it will
    // have a workspace output
    if (op->has_attr(op_attr::fuse_relu)
            && op->get_attr<bool>(op_attr::fuse_relu)) {
        scratchpad_index = op->num_outputs() - 2;
        value_ptr workspace_val = op->get_output_value(op->num_outputs() - 1);
        status = fill_layout_info(workspace_val, pd.workspace_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm workspace");
    }

    value_ptr scratchpad_val = op->get_output_value(scratchpad_index);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());

    return status;
}

status_t layout_propagator_for_batchnorm_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = batchnorm_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before batchnorm_bwd "
            "src");

    insert_reorder_before(op, 1, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(1);
    status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before batchnorm_bwd "
            "diff_dst");

    insert_reorder_before(op, 2, pd.mean_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr mean = op->get_input_value(2);
    status = fill_layout_info(mean, pd.mean_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before batchnorm_bwd "
            "mean");

    insert_reorder_before(op, 3, pd.variance_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr var = op->get_input_value(3);
    status = fill_layout_info(var, pd.variance_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before batchnorm_bwd "
            "virance");

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after batchnorm_bwd "
            "diff_src");

    if (op->num_outputs() > 2) {
        value_ptr diff_gamma = op->get_output_value(1);
        value_ptr diff_beta = op->get_output_value(2);

        status = fill_layout_info(diff_gamma, pd.diff_weights_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm_bwd diff_gamma");
        status = fill_layout_info(diff_beta, pd.diff_weights_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for batchnorm_bwd diff_beta");
    }

    value_ptr scratchpad_val = op->get_output_values().back();
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_prelu(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = prelu_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before prelu src");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before prelu weights");

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after prelu dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    // make scratchpad as prelu's last output
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_prelu_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = prelu_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before prelu_bwd src");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before prelu_bwd "
            "weights");

    value_ptr diff_dst = op->get_input_value(2);
    status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for prelu_bwd diff_dst");

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after prelu_bwd "
            "diff_src");

    insert_reorder_after(op, 1, pd.diff_weights_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr diff_wei = op->get_output_value(1);
    status = fill_layout_info(diff_wei, pd.diff_weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after prelu_bwd "
            "diff_weights");

    value_ptr scratchpad_val = op->get_output_value(2);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_layernorm(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = layernorm_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before layernorm src");

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after layernorm dst");

    if (op->num_outputs() > 2) {
        // keep_stats is true
        value_ptr mean = op->get_output_value(1);
        value_ptr variance = op->get_output_value(2);
        status = fill_layout_info(mean, pd.mean_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for layernorm mean");
        status = fill_layout_info(variance, pd.variance_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for layernorm variance");
    }

    // scratchpad is layernorm's last output
    value_ptr scratchpad_val = op->get_output_values().back();
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_layernorm_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = layernorm_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    size_t in_index {0};
    insert_reorder_before(op, in_index, pd.src_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr src = op->get_input_value(in_index++);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before layernorm_bwd "
            "src");

    insert_reorder_before(op, in_index, pd.diff_dst_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(in_index++);
    status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before layernorm_bwd "
            "diff_dst");

    insert_reorder_before(op, in_index, pd.mean_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr mean = op->get_input_value(in_index++);
    status = fill_layout_info(mean, pd.mean_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before layernorm_bwd "
            "mean");

    insert_reorder_before(op, in_index, pd.variance_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr var = op->get_input_value(in_index++);
    status = fill_layout_info(var, pd.variance_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before layernorm_bwd "
            "variance");

    size_t out_index {0};
    insert_reorder_after(op, out_index, pd.diff_src_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(out_index++);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after layernorm_bwd "
            "diff_src");

    const bool use_affine = op->get_attr<bool>(op_attr::use_affine);
    if (use_affine) {
        const auto &diff_scale_opt_mdesc
                = pd.query_md(query::exec_arg_md, DNNL_ARG_DIFF_SCALE);
        insert_reorder_after(op, out_index, diff_scale_opt_mdesc, p_engine,
                pd_cache, fpmath, use_block_layout, rewriter);
        value_ptr diff_scale = op->get_output_value(out_index++);
        status = fill_layout_info(diff_scale, diff_scale_opt_mdesc);
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder after layernorm_bwd "
                "diff_scale");

        const auto &diff_shift_opt_mdesc
                = pd.query_md(query::exec_arg_md, DNNL_ARG_DIFF_SHIFT);
        insert_reorder_after(op, out_index, diff_shift_opt_mdesc, p_engine,
                pd_cache, fpmath, use_block_layout, rewriter);
        value_ptr diff_shift = op->get_output_value(out_index++);
        status = fill_layout_info(diff_shift, diff_shift_opt_mdesc);
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder after layernorm_bwd "
                "diff_shift");
    }
    auto scratchpad_val = op->get_output_value(op->num_outputs() - 1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_permute(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    std::shared_ptr<value_t> src, dst;
    src = op->get_input_value(0);
    dst = op->get_output_value(0);

    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    auto perm = op->get_attr<std::vector<int64_t>>(op_attr::permutation);
    if (!ltw(in_lt).is_any() && ltw(out_lt).is_any()) {
        dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);

        auto int32_perm = dnnl_impl::utils::cast_to_int32(perm);
        dnnl::memory::desc out_md = in_md.permute_axes(int32_perm);
        status = fill_layout_info(dst, out_md);
    } else if (!ltw(out_lt).is_any() && ltw(in_lt).is_any()) {
        dnnl::memory::desc out_md = make_dnnl_memory_desc(out_lt);
        std::vector<int32_t> inverse_perm(perm.size(), -1);
        for (size_t i = 0; i < perm.size(); i++) {
            inverse_perm[static_cast<size_t>(perm[i])]
                    = static_cast<int32_t>(i);
        }
        dnnl::memory::desc in_md = out_md.permute_axes(inverse_perm);
        status = fill_layout_info(src, in_md);
    } else if (!ltw(in_lt).is_any() && !ltw(out_lt).is_any()) {
        // case `conv (opaque) -> permute -> output (strided)` or
        // case `input (strided) -> permute -> conv (opaque)`
        dnnl::memory::desc out_md = make_dnnl_memory_desc(out_lt);
        std::vector<int32_t> inverse_perm(perm.size(), -1);
        for (size_t i = 0; i < perm.size(); i++) {
            inverse_perm[static_cast<size_t>(perm[i])]
                    = static_cast<int32_t>(i);
        }
        dnnl::memory::desc tmp_in_md = out_md.permute_axes(inverse_perm);

        // if the input md derived from output md is different from the real
        // input mem desc, just insert a reorder before the op
        if (make_dnnl_memory_desc(in_lt) != tmp_in_md)
            insert_reorder_before(op, 0, tmp_in_md, p_engine, pd_cache, fpmath,
                    use_block_layout, rewriter);
    }
    return status;
}

status_t layout_propagator_for_to_group(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);

    status_t status = status::success;
    std::shared_ptr<value_t> src, dst;
    src = op->get_input_value(0);
    dst = op->get_output_value(0);
    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    if (!ltw(in_lt).is_any() && ltw(out_lt).is_any()) {
        dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
        dnnl::memory::desc out_md;
        auto groups = op->get_attr<int64_t>(op_attr::groups);
        // avoid dividing by zero at below.
        if (groups == 0) return status::invalid_shape;
        if (op->has_attr(op_attr::is_convtranspose)
                && op->get_attr<bool>(op_attr::is_convtranspose)) {
            auto permuted_weight = transpose(in_md, 0, 1);
            auto permuted_group_weight = to_grouped(permuted_weight, groups);
            out_md = transpose(permuted_group_weight, 1, 2);
        } else {
            out_md = to_grouped(in_md, groups);
        }
        status = fill_layout_info(dst, out_md);
    }
    return status;
}

status_t layout_propagator_for_from_group(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    const auto get_dst_md
            = [](const dnnl::memory::desc &src_md,
                      bool is_convtranspose) -> dnnl::memory::desc {
        if (is_convtranspose) {
            auto permuted_dst = transpose(src_md, 1, 2);
            auto permuted_dst_no_groups = from_grouped(permuted_dst);
            return !permuted_dst_no_groups
                    ? permuted_dst_no_groups
                    : transpose(permuted_dst_no_groups, 0, 1);
        } else {
            return from_grouped(src_md);
        }
    };
    const auto get_strides = [](const dnnl::memory::desc &src_md,
                                     bool is_convtranspose) -> dims {
        if (is_convtranspose) {
            // chain of (transpose -> from_grouped -> transpose) requires
            // such permuted strides, otherwise reshape will fail
            auto strides
                    = get_dense_strides(transpose(src_md, 0, 1).get_dims());
            std::swap(strides[0], strides[1]);
            return strides;
        } else {
            return get_dense_strides(src_md.get_dims());
        }
    };

    value_ptr src = op->get_input_value(0);
    value_ptr dst = op->get_output_value(0);
    auto src_lt = src->get_logical_tensor();
    auto dst_lt = dst->get_logical_tensor();

    if (ltw(src_lt).is_any()) return status;

    const bool is_convtranspose = op->has_attr(op_attr::is_convtranspose)
            ? op->get_attr<bool>(op_attr::is_convtranspose)
            : false;
    const auto src_md = make_dnnl_memory_desc(src_lt);
    dnnl::memory::desc inferred_dst_md = get_dst_md(src_md, is_convtranspose);
    // from_grouped uses the 'allow_empty' option when reshaping, so if reshape
    // will not succeed (e.g. padding exists inside dims we want to join),
    // inferred_dst_md will be an empty memory descriptor.
    if (!inferred_dst_md) {
        dnnl::memory::desc strided_dst_md(src_md.get_dims(),
                src_md.get_data_type(), get_strides(src_md, is_convtranspose));
        insert_reorder_before(op, 0, strided_dst_md, p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
        inferred_dst_md = get_dst_md(strided_dst_md, is_convtranspose);
    }

    if (ltw(dst_lt).is_any()) {
        status = fill_layout_info(dst, inferred_dst_md);
    } else {
        insert_reorder_after(op, 0, inferred_dst_md, p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
    }
    return status;
}

static status_t layout_propagator_for_reshape_like_ops(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter, const dnnl::memory::dims &target_dims) {
    status_t status = status::success;
    std::shared_ptr<value_t> src, dst;
    src = op->get_input_value(0);
    dst = op->get_output_value(0);
    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    VCHECK_LAYOUT_PROPAGATOR(!ltw(in_lt).is_any(), status::invalid_arguments,
            "input layout must be specified for reshape_like");

    if (target_dims.empty()) {
        // scalar, just set empty strides to make the dst strided
        dst->set_strides({});
        return status::success;
    }

    if (ltw(out_lt).is_any()) {
        dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
        dnnl::memory::desc out_md
                = in_md.reshape(target_dims, /* allow empty */ true);

        // out_md will be empty if the in_md is not reshape-able. We need to
        // reorder the in_md first and then reshape the reordered reshape-able
        // md.
        if (!out_md) {
            dnnl::memory::desc reshapable_md(in_md.get_dims(),
                    in_md.get_data_type(), get_ncx_format(in_md.get_ndims()));
            insert_reorder_before(op, 0, reshapable_md, p_engine, pd_cache,
                    fpmath, use_block_layout, rewriter);
            out_md = reshapable_md.reshape(target_dims);
        }

        status = fill_layout_info(dst, out_md);
    } else if (ltw(out_lt).is_strided()) {
        dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
        dnnl::memory::desc out_md = make_dnnl_memory_desc(out_lt);
        // check if the out_md is reshape-able
        dnnl::memory::desc expected_in_md
                = out_md.reshape(in_md.get_dims(), /* allow empty */ true);
        if (expected_in_md) {
            // If the out_md is reshape-able, the expected_in_md must be
            // reshape-able too. Then we just need to check if the real in_md
            // has same layout as the expected_in_md, and insert only one
            // possible reorder if needed.
            if (expected_in_md != in_md) {
                insert_reorder_before(op, 0, expected_in_md, p_engine, pd_cache,
                        fpmath, use_block_layout, rewriter);
            }
            // finally, we have a chain of: in_md -> (optional reorder) ->
            // expected_in_md -> reshape -> out_md
        } else {
            // Check if the in_md is reshape-able.
            dnnl::memory::desc reshaped_in_md
                    = in_md.reshape(target_dims, /* allow empty */ true);
            if (!reshaped_in_md) {
                dnnl::memory::desc reshapable_md(in_md.get_dims(),
                        in_md.get_data_type(),
                        get_ncx_format(in_md.get_ndims()));
                insert_reorder_before(op, 0, reshapable_md, p_engine, pd_cache,
                        fpmath, use_block_layout, rewriter);
                reshaped_in_md = reshapable_md.reshape(target_dims);
            }
            // If the reshaped_in_md is not same as the specified out_md, we
            // insert reorder
            if (reshaped_in_md != out_md) {
                insert_reorder_after(op, 0, reshaped_in_md, p_engine, pd_cache,
                        fpmath, use_block_layout, rewriter);
            }
            // finally, we have a chain of: in_md -> (optional reorder) ->
            // reshapable_md -> reshape -> reshaped_in_md -> (optional reorder)
            // -> out_md. The optional reorder will only occurs when both in_md
            // and out_md are not reshapable.
        }
    }
    return status;
}

status_t layout_propagator_for_reshape(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    auto out_lt = op->get_output_value(0)->get_logical_tensor();
    auto target_dims = ltw(out_lt).vdims();
    status_t status = layout_propagator_for_reshape_like_ops(op, p_engine,
            pd_cache, fpmath, use_block_layout, rewriter, target_dims);
    return status;
}

status_t layout_propagator_for_transpose(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    status_t status = status::success;
    std::shared_ptr<value_t> src, dst;
    src = op->get_input_value(0);
    dst = op->get_output_value(0);
    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    VCHECK_LAYOUT_PROPAGATOR(!ltw(in_lt).is_any(), status::invalid_arguments,
            "layout of transpose src can't be any layout");

    std::vector<int64_t> order
            = op->get_attr<std::vector<int64_t>>(op_attr::order);
    // if order < 0, convert it to positive order
    if (!order.empty()) {
        for (int64_t &axis : order) {
            if (axis < 0) axis += ltw(in_lt).ndims();
        }
    } else {
        // FIXME(xx) handle this case
        VCHECK_LAYOUT_PROPAGATOR(false, status::unimplemented,
                "transpose with empty order is not supported");
    }

    /// The order in spec op is used as:
    /// for (i = 0; i < ndims(); i++)
    ///     new_shape[i] = org_shape[order[i]];
    ///
    /// The axes for permute_axes function is used as:
    /// for (i = 0; i < ndims(); i++)
    ///     new_shape[axes[i]] = org_shape[i];
    ///
    /// So, we need to convert the order to axes
    std::vector<int> axes(order.size(), -1);
    for (size_t i = 0; i < order.size(); i++) {
        size_t new_shape_idx = i;
        size_t org_shape_idx = order[i];
        axes[org_shape_idx] = static_cast<int>(new_shape_idx);
    }

    // calculate the expected transposed layout by permuting the md
    dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
    dnnl::memory::desc expected_out_md = in_md.permute_axes(axes);
    if (ltw(out_lt).is_any()) {
        status = fill_layout_info(dst, expected_out_md);
    } else {
        // if the output layout is specified, we need to check if it's matched
        // with the expected out layout. If not, we should insert a reorder op
        // to convert the transposed layout to the specified one.
        dnnl::memory::desc out_md = make_dnnl_memory_desc(out_lt);
        if (expected_out_md != out_md) {
            insert_reorder_after(op, 0, expected_out_md, p_engine, pd_cache,
                    fpmath, use_block_layout, rewriter);
        }
    }
    return status;
}

status_t layout_propagator_for_unsqueeze(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(rewriter);

    status_t status = status::success;
    value_ptr src = op->get_input_value(0);
    value_ptr dst = op->get_output_value(0);
    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    if (!ltw(in_lt).is_any() && ltw(out_lt).is_any()) {
        dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
        // 'out_lt' shape should be known at this stage
        status = fill_layout_info(dst, in_md.reshape(ltw(out_lt).vdims()));
    }
    return status;
}

status_t layout_propagator_for_squeeze(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    auto out_lt = op->get_output_value(0)->get_logical_tensor();
    auto target_dims = ltw(out_lt).vdims();
    status_t status = layout_propagator_for_reshape_like_ops(op, p_engine,
            pd_cache, fpmath, use_block_layout, rewriter, target_dims);
    return status;
}

status_t layout_propagator_for_reorder(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(rewriter);

    status_t status = status::success;
    std::shared_ptr<value_t> src, dst;
    src = op->get_input_value(0);
    dst = op->get_output_value(0);
    auto in_lt = src->get_logical_tensor();
    auto out_lt = dst->get_logical_tensor();

    if (!ltw(in_lt).is_any() && ltw(out_lt).is_any()) {
        VCHECK_LAYOUT_PROPAGATOR(!op->has_attr(op_attr::change_layout)
                        || !op->get_attr<bool>(op_attr::change_layout),
                status::invalid_arguments,
                "layout of dnnl_reorder input and output must be known "
                "if it changes layout");
        // for logical tensor with opaque layout, make_dnnl_memory_desc
        // cannot help manually modify the logical tensor. The previously
        // created memory will be quired according to logic tensor id.
        // So data_type of quired memory may be not same as the logical tensor.
        // We used to be able to change the data type manually in oneDNN
        // v2.7, but we can't modify the data type in this way anymore
        // since v3.0.
        auto out_md = make_dnnl_memory_desc(in_lt);
        if (in_lt.data_type != out_lt.data_type) {
            auto format_tag = md2fmt_tag_str(out_md.get());
            const auto &dims = out_md.get_dims();
            dnnl_memory_desc_t tmp_md;
            dnnl_memory_desc_create_with_string_tag(&tmp_md,
                    static_cast<int>(dims.size()), dims.data(),
                    static_cast<dnnl_data_type_t>(out_lt.data_type),
                    format_tag.data());
            status = fill_layout_info(dst, tmp_md);
        } else {
            status = fill_layout_info(dst, out_md);
        }
    } else if (!ltw(out_lt).is_any() && ltw(in_lt).is_any()) {
        VCHECK_LAYOUT_PROPAGATOR(!op->has_attr(op_attr::change_layout)
                        || !op->get_attr<bool>(op_attr::change_layout),
                status::invalid_arguments,
                "layout of dnnl_reorder input and output must be known "
                "if it changes layout");
        out_lt.data_type = ltw(in_lt).data_type();
        auto in_md = make_dnnl_memory_desc(out_lt);
        status = fill_layout_info(src, in_md);
    }
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder");

    // set layout info for scratchpad output
    if (op->num_outputs() == 1) { insert_empty_scratchpad(op); }

    const auto &pd = reorder_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_mul_scales(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    return layout_propagator_for_reorder(
            op, p_engine, pd_cache, fpmath, use_block_layout, rewriter);
}

status_t layout_propagator_for_bn_folding(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(rewriter);

    status_t status = status::success;
    // skip the scratchpad
    for (size_t i = 0; i < op->num_outputs() - 1; i++) {
        auto in_lt = op->get_input_logical_tensor(i);
        auto out_lt = op->get_output_logical_tensor(i);
        if (!ltw(in_lt).is_any() && ltw(out_lt).is_any()) {
            dnnl::memory::desc in_md = make_dnnl_memory_desc(in_lt);
            auto dst = op->get_output_value(i);
            status = fill_layout_info(dst, in_md);
            VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                    "failed to fill layout info for bn_folding dst");
        }
    }

    auto pd = bn_folding_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);
    // scratchpad is bn_folding's last inputs
    auto val = op->get_output_value(2);
    status = fill_layout_info(val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_conv_bwd_data(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = conv_bwd_data_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(0);
    status_t status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv_bwd_data "
            "diff_dst");

    insert_reorder_before(op, 1, pd.weights_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr wei = op->get_input_value(1);
    status = fill_layout_info(wei, pd.weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv_bwd_data "
            "weights");

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after conv_bwd_data "
            "diff_src");

    // fill scratchpads dimensions and data type to scratchpad value_t
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_conv_bwd_weights(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = conv_bwd_weights_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr src = op->get_input_value(0);
    status_t status = fill_layout_info(src, pd.src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv_bwd_weights "
            "src");

    insert_reorder_before(op, 1, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(1);
    status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before conv_bwd_weights "
            "diff_dst");

    insert_reorder_after(op, 0, pd.diff_weights_desc(), p_engine, pd_cache,
            fpmath, use_block_layout, rewriter);
    value_ptr diff_weights = op->get_output_value(0);
    status = fill_layout_info(diff_weights, pd.diff_weights_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after conv_bwd_weights "
            "diff_weights");

    // fill scratchpads dimensions and data type to scratchpad value_t
    auto scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc = pd.scratchpad_desc();
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

status_t layout_propagator_for_resampling(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = resampling_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after interpolate "
            "dst");

    // make scratchpad as resampling's last output
    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_resampling_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = resampling_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status_t status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after interpolate_bwd "
            "diff_src");

    auto scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_sum(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    value_ptr dst = op->get_output_value(0);
    bool input_has_any_format = false;
    for (const auto &in_val : op->get_input_values()) {
        if (ltw(in_val->get_logical_tensor()).is_any()) {
            input_has_any_format = true;
            break;
        }
    }

    MAYBE_UNUSED(input_has_any_format);
    assertm(!input_has_any_format,
            "input format of sum primitive cannot be any");
    VCHECK_LAYOUT_PROPAGATOR(!input_has_any_format, status::invalid_arguments,
            "input format of sum primitive cannot be any ");

    const auto &pd = sum_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    if (ltw(dst->get_logical_tensor()).is_any()) {
        insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
        dst = op->get_output_value(0);
        status_t status = fill_layout_info(dst, pd.dst_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for reorder after sum dst");
    }

    // scratchpad is dnnl_sum's last output
    value_ptr scratchpad_val = op->get_output_values().back();
    status_t status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_softmax(op_ptr &op, const dnnl::engine &p_engine,
        pd_cache_t &pd_cache, const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    value_ptr src = op->get_input_value(0);
    VCHECK_LAYOUT_PROPAGATOR(!ltw(src->get_logical_tensor()).is_any(),
            status::invalid_arguments,
            "layout of softmax/logsoftmax src can't be any");

    const auto &pd = softmax_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after softmax dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_softmax_bwd(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    value_ptr dst = op->get_input_value(1);
    VCHECK_LAYOUT_PROPAGATOR(!ltw(dst->get_logical_tensor()).is_any(),
            status::invalid_arguments,
            "layout of softmax/logsoftmax bwd dst can't be any");

    const auto &pd = softmax_bwd_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_before(op, 0, pd.diff_dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_dst = op->get_input_value(0);
    status_t status = fill_layout_info(diff_dst, pd.diff_dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder before softmax_bwd "
            "diff_dst");

    insert_reorder_after(op, 0, pd.diff_src_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr diff_src = op->get_output_value(0);
    status = fill_layout_info(diff_src, pd.diff_src_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after softmax_bwd "
            "diff_src");

    // according to op schema, scratchpad must be be second output
    auto scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_reduction(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    value_ptr src = op->get_input_value(0);
    VCHECK_LAYOUT_PROPAGATOR(!ltw(src->get_logical_tensor()).is_any(),
            status::invalid_arguments, "layout of reduction src can't be any");
    const auto &pd = reduction_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after reduction dst");

    value_ptr scratchpad_val = op->get_output_value(1);
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_constant_filler(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(op);
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    return status::success;
}

status_t layout_propagator_for_sub_zps(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(op);
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    assertm(false,
            "dnnl_sub_zps op is only for fusion purpose, we shouldn't do "
            "layout propagation for it");
    return status::invalid_graph_op;
}

status_t layout_propagator_for_add_zps(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(op);
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    assertm(false,
            "dnnl_add_zps op is only for fusion purpose, we shouldn't do "
            "layout propagation for it");
    return status::invalid_graph_op;
}

status_t layout_propagator_for_gen_index(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    auto src_md = make_dnnl_memory_desc(op->get_input_logical_tensor(0));
    if (!is_plain(src_md)) {
        src_md = dnnl::memory::desc(src_md.get_dims(), src_md.get_data_type(),
                dnnl::memory::format_tag::abcd);
        insert_reorder_before(op, 0, src_md, p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
    }
    value_ptr dst_val = op->get_output_value(0);
    status_t status = fill_layout_info(dst_val, src_md);
    return status;
}

status_t layout_propagator_for_identity(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    logical_tensor_t dst_lt = op->get_input_logical_tensor(0);
    VCHECK_LAYOUT_PROPAGATOR(!ltw(dst_lt).is_any(), status::invalid_arguments,
            "layout of identity dst can't be any");
    auto dst_md = make_dnnl_memory_desc(dst_lt);
    auto src_md = make_dnnl_memory_desc(op->get_input_logical_tensor(0));
    if (src_md != dst_md) {
        insert_reorder_before(op, 0, dst_md, p_engine, pd_cache, fpmath,
                use_block_layout, rewriter);
    }
    return status::success;
}

status_t layout_propagator_for_groupnorm(op_ptr &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    const auto &pd = groupnorm_executable_t::create_desc(
            op, p_engine, pd_cache, fpmath, use_block_layout);

    insert_reorder_after(op, 0, pd.dst_desc(), p_engine, pd_cache, fpmath,
            use_block_layout, rewriter);
    value_ptr dst = op->get_output_value(0);
    status_t status = fill_layout_info(dst, pd.dst_desc());
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for reorder after groupnorm dst");

    if (op->num_outputs() > 2) {
        // keep_stats is true
        value_ptr mean = op->get_output_value(1);
        value_ptr variance = op->get_output_value(2);
        status = fill_layout_info(mean, pd.mean_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for groupnorm mean");
        status = fill_layout_info(variance, pd.variance_desc());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for groupnorm variance");
    }

    // scratchpad is groupnorm's last output
    value_ptr scratchpad_val = op->get_output_values().back();
    status = fill_layout_info(scratchpad_val, pd.scratchpad_desc());
    return status;
}

status_t layout_propagator_for_mask(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    auto src_md = make_dnnl_memory_desc(op->get_input_logical_tensor(0));
    value_ptr dst_val = op->get_output_value(0);
    status_t status = fill_layout_info(dst_val, src_md);
    return status;
}

status_t layout_propagator_for_sdpa(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);

    size_t output_idx = 0;
    value_ptr dst_val = op->get_output_value(output_idx++);
    const logical_tensor_t &out_lt = dst_val->get_logical_tensor();

    dnnl::memory::desc expected_md;
    if (ltw(out_lt).is_any()) {
        // For GQA, we need to check the layout of the dnnl_reshape output
        // following dnnl_sdpa, which is given by the user.
        if (!dst_val->get_consumers().empty()) {
            const auto &consumer_op = dst_val->get_consumers()[0].get_op();
            const logical_tensor_t &consumer_out
                    = consumer_op.get_output_logical_tensor(0);
            if (consumer_op.get_kind() == op_kind::_reshape
                    && ltw(consumer_out).ndims() == 5
                    && ltw(consumer_out).is_strided()) {
                const auto &ori_strides = ltw(consumer_out).vstrides();
                std::vector<dim_t> strides = {ori_strides[0], ori_strides[2],
                        ori_strides[3], ori_strides[4]};
                expected_md = {ltw(out_lt).vdims(),
                        static_cast<dnnl::memory::data_type>(
                                ltw(out_lt).data_type()),
                        strides};
            } else {
                // Set default output layout format for sdpa as acbd if user
                // doesn't specify the layout since no reorder will be required.
                expected_md = {ltw(out_lt).vdims(),
                        static_cast<dnnl::memory::data_type>(
                                ltw(out_lt).data_type()),
                        dnnl::memory::format_tag::acbd};
            }
        } else {
            expected_md = {ltw(out_lt).vdims(),
                    static_cast<dnnl::memory::data_type>(
                            ltw(out_lt).data_type()),
                    dnnl::memory::format_tag::acbd};
        }
    } else {
        expected_md = make_dnnl_memory_desc(out_lt);
    }
    status_t status = fill_layout_info(dst_val, expected_md);

    // fill scratchpads dimensions and data type to scratchpad value_t
    value_ptr scratchpad_val = op->get_output_value(output_idx++);
    const memory::desc scratchpad_desc;
    status = fill_layout_info(scratchpad_val, scratchpad_desc);

    if (op->get_attr<bool>(op_attr::is_training)) {
        value_ptr stats_val = op->get_output_value(output_idx);
        const logical_tensor_t &stats_lt = stats_val->get_logical_tensor();
        dnnl::memory::desc stats_md;
        // For GQA, we need to check the layout of the dnnl_reshape output
        // following dnnl_sdpa, which is given by the user.
        if (!stats_val->get_consumers().empty()) {
            const auto &consumer_op = stats_val->get_consumers()[0].get_op();
            const logical_tensor_t &consumer_out
                    = consumer_op.get_output_logical_tensor(0);
            if (consumer_op.get_kind() == op_kind::_reshape
                    && ltw(consumer_out).ndims() == 5
                    && ltw(consumer_out).is_strided()) {
                const auto &ori_strides = ltw(consumer_out).vstrides();
                std::vector<dim_t> strides = {ori_strides[0], ori_strides[2],
                        ori_strides[3], ori_strides[4]};
                stats_md = {ltw(stats_lt).vdims(),
                        static_cast<dnnl::memory::data_type>(
                                ltw(stats_lt).data_type()),
                        strides};
            } else {
                // Set default output layout format for sdpa as acbd if user
                // doesn't specify the layout since no reorder will be required.
                stats_md = {ltw(stats_lt).vdims(),
                        static_cast<dnnl::memory::data_type>(
                                ltw(out_lt).data_type()),
                        dnnl::memory::format_tag::acbd};
            }
        } else {
            expected_md = {ltw(out_lt).vdims(),
                    static_cast<dnnl::memory::data_type>(
                            ltw(out_lt).data_type()),
                    dnnl::memory::format_tag::acbd};
        }

        status = fill_layout_info(stats_val, stats_md);
    }
    return status;
}

status_t layout_propagator_for_sdpa_bwd(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(pd_cache);
    UNUSED(use_block_layout);
    UNUSED(rewriter);

    // Helper: derive a memory desc for a diff output from the corresponding
    // forward input logical tensor. If the input layout is already fixed, reuse
    // it; otherwise fall back to the canonical acbd format used by sdpa.
    auto get_md_for_diff = [](const logical_tensor_t &lt) {
        if (!ltw(lt).is_any()) return make_dnnl_memory_desc(lt);
        return dnnl::memory::desc {ltw(lt).vdims(),
                static_cast<dnnl::memory::data_type>(ltw(lt).data_type()),
                dnnl::memory::format_tag::acbd};
    };

    status_t status = status::success;
    size_t output_idx = 0;

    // diff_query (output 0): propagate layout from query (input 0)
    value_ptr diff_query_val = op->get_output_value(output_idx++);
    const logical_tensor_t &query_lt = op->get_input_logical_tensor(0);
    status = fill_layout_info(diff_query_val, get_md_for_diff(query_lt));
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for sdpa_bwd diff_query");

    // diff_key (output 1): propagate layout from key (input 1)
    value_ptr diff_key_val = op->get_output_value(output_idx++);
    const logical_tensor_t &key_lt = op->get_input_logical_tensor(1);
    status = fill_layout_info(diff_key_val, get_md_for_diff(key_lt));
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for sdpa_bwd diff_key");

    // diff_value (output 2): propagate layout from value (input 2)
    value_ptr diff_value_val = op->get_output_value(output_idx++);
    const logical_tensor_t &value_lt = op->get_input_logical_tensor(2);
    status = fill_layout_info(diff_value_val, get_md_for_diff(value_lt));
    VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
            "failed to fill layout info for sdpa_bwd diff_value");

    // scratchpad (output 3): create the pd to get the real scratchpad size
    {
        const bool with_scale = op->get_attr<bool>(op_attr::with_scale);
        const auto mask_type = static_cast<attn_mask_type_t>(
                op->get_attr<int64_t>(op_attr::mask_type));
        const bool is_invert_scale = op->has_attr(op_attr::is_invert_scale)
                ? op->get_attr<bool>(op_attr::is_invert_scale)
                : false;
        const bool with_explicit_mask = mask_type == attn_mask_type::buffer;

        auto md_q = make_dnnl_memory_desc(op->get_input_logical_tensor(0));
        auto md_k = make_dnnl_memory_desc(op->get_input_logical_tensor(1));
        auto md_v = make_dnnl_memory_desc(op->get_input_logical_tensor(2));
        auto md_dst = make_dnnl_memory_desc(op->get_input_logical_tensor(3));
        auto md_diff_dst
                = make_dnnl_memory_desc(op->get_input_logical_tensor(5));
        auto md_diff_q = get_md_for_diff(op->get_input_logical_tensor(0));
        auto md_diff_k = get_md_for_diff(op->get_input_logical_tensor(1));
        auto md_diff_v = get_md_for_diff(op->get_input_logical_tensor(2));

        dnnl::memory::desc md_scale, md_attn_mask, md_dS;
        size_t idx = 6;
        if (with_scale)
            md_scale = make_dnnl_memory_desc(
                    op->get_input_logical_tensor(idx++));
        if (with_explicit_mask) {
            md_attn_mask = make_dnnl_memory_desc(
                    op->get_input_logical_tensor(idx++));
            if (op->num_outputs() > 4)
                md_dS = make_dnnl_memory_desc(op->get_output_logical_tensor(4));
        }

        const auto &sdpa_fusion_info = op->has_attr(op_attr::fusion_info)
                ? op->get_attr<fusion_info_t>(op_attr::fusion_info)
                : fusion_info_t();
        dnnl::primitive_attr attr, qk_attr, vs_attr;
        if (op->has_attr(op_attr::fusion_info)) {
            qk_attr = make_dnnl_sdpa_primitive_attr(
                    op, sdpa_fusion_info, attr_type_t::QK);
            vs_attr = make_dnnl_sdpa_primitive_attr(
                    op, sdpa_fusion_info, attr_type_t::VS);
        }
        qk_attr.set_accumulation_mode(str2accumulation_mode(
                op->get_attr<std::string>(op_attr::qk_acc_mode)));
        vs_attr.set_accumulation_mode(str2accumulation_mode(
                op->get_attr<std::string>(op_attr::vs_acc_mode)));
        attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
        attr.set_fpmath_mode(static_cast<dnnl::fpmath_mode>(fpmath.mode_));

        dim_t kv_head_number = op->get_input_logical_tensor(1).dims[1];
        const alg_kind_t softmax_alg = alg_kind::softmax_accurate_inf_as_zero;

        std::shared_ptr<primitive_desc_t> hint_fwd_pd;
        status = create_sdpa_pd(hint_fwd_pd, p_engine.get(), md_q.get(),
                md_k.get(), md_v.get(), md_dst.get(), md_attn_mask.get(),
                md_scale.get(), is_invert_scale, kv_head_number, mask_type,
                softmax_alg, impl::prop_kind::forward_training, attr.get(),
                qk_attr.get(), vs_attr.get());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to create hint fwd pd for sdpa_bwd scratchpad");

        std::shared_ptr<primitive_desc_t> sdpa_bwd_pd;
        status = create_sdpa_pd(sdpa_bwd_pd, p_engine.get(), md_q.get(),
                md_k.get(), md_v.get(), md_dst.get(), md_diff_q.get(),
                md_diff_k.get(), md_diff_v.get(), md_diff_dst.get(),
                md_dS.get(), md_attn_mask.get(), md_scale.get(),
                is_invert_scale, kv_head_number, mask_type, softmax_alg,
                attr.get(), hint_fwd_pd.get(), qk_attr.get(), vs_attr.get());
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to create pd for sdpa_bwd scratchpad");

        value_ptr scratchpad_val = op->get_output_value(output_idx++);
        dnnl_memory_desc_t cloned_md = nullptr;
        dnnl_memory_desc_clone(&cloned_md, sdpa_bwd_pd->scratchpad_md());
        dnnl::memory::desc scratchpad_desc;
        scratchpad_desc.reset(cloned_md);
        status = fill_layout_info(scratchpad_val, scratchpad_desc);
    }

    // diff_mask (output 4, optional)
    if (op->num_outputs() > output_idx) {
        value_ptr diff_mask_val = op->get_output_value(output_idx);
        const logical_tensor_t &diff_mask_lt
                = diff_mask_val->get_logical_tensor();
        status = fill_layout_info(
                diff_mask_val, make_dnnl_memory_desc(diff_mask_lt));
        VCHECK_LAYOUT_PROPAGATOR(status == status::success, status,
                "failed to fill layout info for sdpa_bwd diff_mask");
    }

    return status;
}

status_t layout_propagator_for_host_scalar(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    // no need to do layout propagation for host scalar
    // as its output is always strided
    UNUSED(op);
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);
    return status::success;
}

status_t layout_propagator_for_gated_mlp(std::shared_ptr<op_t> &op,
        const dnnl::engine &p_engine, pd_cache_t &pd_cache,
        const fpmath_t &fpmath, bool use_block_layout,
        subgraph_rewriter_t &rewriter) {
    UNUSED(p_engine);
    UNUSED(pd_cache);
    UNUSED(fpmath);
    UNUSED(use_block_layout);
    UNUSED(rewriter);

    value_ptr dst_val = op->get_output_value(0);
    const logical_tensor_t &dst_lt = dst_val->get_logical_tensor();

    dnnl::memory::desc expected_md;
    if (ltw(dst_lt).is_any()) {
        const auto tag = get_ncx_format(ltw(dst_lt).ndims());
        expected_md = {ltw(dst_lt).vdims(),
                static_cast<dnnl::memory::data_type>(ltw(dst_lt).data_type()),
                tag};
    } else {
        expected_md = make_dnnl_memory_desc(dst_lt);
    }
    status_t status = fill_layout_info(dst_val, expected_md);

    // fill scratchpads dimensions and data type to scratchpad value_t
    value_ptr scratchpad_val = op->get_output_value(1);
    const memory::desc scratchpad_desc;
    status = fill_layout_info(scratchpad_val, scratchpad_desc);
    return status;
}

} // namespace dnnl_impl
} // namespace graph
} // namespace impl
} // namespace dnnl