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
/*******************************************************************************
* Copyright 2020 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 <tuple>

#include "common/c_types_map.hpp"
#include "common/dnnl_thread.hpp"
#include "common/type_helpers.hpp"
#include "common/utils.hpp"

#include "cpu/cpu_primitive.hpp"
#include "cpu/scale_utils.hpp"

#include "cpu/x64/amx_tile_configure.hpp"
#include "cpu/x64/cpu_barrier.hpp"
#include "cpu/x64/injectors/jit_uni_binary_injector.hpp"
#include "cpu/x64/jit_brgemm_inner_product.hpp"
#include "cpu/x64/jit_transpose_utils.hpp"

namespace dnnl {
namespace impl {
namespace cpu {
namespace x64 {

using namespace dnnl::impl::cpu::x64::brgemm_inner_product_utils;
using namespace dnnl::impl::data_type;
using namespace dnnl::impl::format_tag;
using namespace dnnl::impl::memory_tracking::names;
using namespace dnnl::impl::status;
using namespace dnnl::impl::utils;

using namespace nstl;

#define get_blk_off(d, dt, ...) \
    (types::data_type_size((dt)) * (d).blk_off(__VA_ARGS__))

static size_t blk_off(const memory_desc_wrapper &mdw, dim_t n, dim_t c, dim_t d,
        dim_t h, dim_t w) {
    switch (mdw.ndims()) {
        case 5: return get_blk_off(mdw, mdw.data_type(), n, c, d, h, w);
        case 4: return get_blk_off(mdw, mdw.data_type(), n, c, h, w);
        case 3: return get_blk_off(mdw, mdw.data_type(), n, c, w);
        case 2: return get_blk_off(mdw, mdw.data_type(), n, c);
        default: assert(!"unsupported ndims"); return size_t(0);
    }
}

namespace {
template <typename ker_type>
void copy_data_chunk(ker_type &ker, char *tr_data, const char *data,
        int os_work, bool is_last_blk) {
    auto ctx = jit_brgemm_copy_to_coarse_t::ctx_t();
    ctx.data = (void *)data;
    ctx.tr_data = (void *)tr_data;
    ctx.os_work = os_work;
    ctx.last_row_blk = is_last_blk ? 1 : 0;
    (*ker)(&ctx);
}

} // namespace

template <cpu_isa_t isa>
status_t brgemm_inner_product_fwd_t<isa>::execute_forward(
        const exec_ctx_t &ctx) const {
    auto src = CTX_IN_MEM(const char *, DNNL_ARG_SRC);
    auto weights = CTX_IN_MEM(const char *, DNNL_ARG_WEIGHTS);
    auto bias = CTX_IN_MEM(const char *, DNNL_ARG_BIAS);
    auto dst = CTX_OUT_MEM(char *, DNNL_ARG_DST);
    const auto post_ops_binary_rhs_arg_vec
            = binary_injector::prepare_binary_args(
                    pd()->attr()->post_ops_, ctx);

    const auto &scratchpad = ctx.get_scratchpad_grantor();
    const memory_desc_wrapper src_d(pd()->src_md());
    const memory_desc_wrapper dst_d(pd()->dst_md());
    const memory_desc_wrapper weights_d(pd()->weights_md(0));

    const auto &jbgp = pd()->jbgp_;

    const void *src_scales
            = CTX_IN_MEM(const void *, DNNL_ARG_ATTR_SCALES | DNNL_ARG_SRC);
    const void *wei_scales
            = CTX_IN_MEM(const void *, DNNL_ARG_ATTR_SCALES | DNNL_ARG_WEIGHTS);
    const void *dst_scales
            = CTX_IN_MEM(const void *, DNNL_ARG_ATTR_SCALES | DNNL_ARG_DST);

    const size_t src_dt_size = types::data_type_size(jbgp.src_dt);
    const size_t bia_dt_size
            = jbgp.with_bias ? types::data_type_size(jbgp.bia_dt) : 0;
    const size_t acc_dt_size = types::data_type_size(jbgp.acc_dt);
    const size_t dst_dt_size = types::data_type_size(jbgp.dst_dt);

    auto addr_batch_global = scratchpad.template get<brgemm_batch_element_t>(
            key_brgemm_primitive_batch);
    auto a_buffer_global = (jbgp.use_buffer_a)
            ? scratchpad.template get<char>(key_brgemm_primitive_buffer_a)
            : nullptr;
    auto c_buffer_global = (jbgp.use_buffer)
            ? scratchpad.template get<char>(key_brgemm_primitive_buffer)
            : nullptr;
    const bool is_amx = jbgp.is_amx;
    auto wsp_tile_base = is_amx
            ? ctx.get_scratchpad_grantor().template get<char>(
                      key_conv_amx_tile_buffer)
            : nullptr;

    const int ic_chunks = div_up(jbgp.nb_ic, jbgp.nb_ic_blocking);

    const bool are_post_ops_applicable = one_of(true, jbgp.with_sum,
            jbgp.with_bias, jbgp.with_src_scales, jbgp.with_wei_scales,
            jbgp.with_dst_scales, jbgp.with_eltwise, jbgp.with_binary,
            jbgp.acc_dt != jbgp.dst_dt, jbgp.req_s8s8_compensation);
    const bool can_use_dst_as_acc_buffer
            = jbgp.acc_dt == jbgp.dst_dt && !jbgp.with_sum;
    const int acc_buffer_idx_shift = can_use_dst_as_acc_buffer;

    size_t offset = types::data_type_size(jbgp.wei_dt)
            * (weights_d.size() - weights_d.additional_buffer_size());
    auto compensation = jbgp.req_s8s8_compensation
            ? reinterpret_cast<const int32_t *>(&weights[offset])
            : nullptr;

    const dims_t ic_dims = {0, jbgp.ic_block, 0, 0, 0};
    const auto wei_ic_stride
            = types::data_type_size(jbgp.wei_dt) * weights_d.off_v(ic_dims);

    const auto ker = [= COMPAT_THIS_CAPTURE](int ithr_oc_mb, int nthr_oc_mb,
                             int ithr_ic, int osb, int osb_s, int ocb,
                             int ocb_s, int icc, int icc_s, int kd, int kh,
                             int kw, bool copy_buffer_a, int &prev_ker_idx,
                             const void *dst_scales_ptr) {
        const int cur_ocb = ocb + ocb_s;
        const int cur_osb = osb + osb_s;
        const int cur_icc = icc + icc_s;
        const int n = cur_osb * jbgp.os_block;
        const int ithr = nthr_oc_mb * ithr_ic + ithr_oc_mb;
        auto addr_batch = addr_batch_global + ithr * jbgp.adjusted_batch_size;

        const size_t a_buffer_osb_stride
                = src_dt_size * jbgp.LDA * jbgp.os_block;
        const size_t a_buffer_per_thr
                = a_buffer_osb_stride * jbgp.nb_os_blocking;
        auto a_buffer = (jbgp.use_buffer_a) ? a_buffer_global
                        + ithr * a_buffer_per_thr + osb * a_buffer_osb_stride
                                            : nullptr;

        const int oc = cur_ocb * jbgp.oc_block;
        const size_t dst_off = get_blk_off(dst_d, jbgp.dst_dt, n, oc);

        const bool force_use_dst_as_acc_buffer = can_use_dst_as_acc_buffer
                && jbgp.nthr_ic_b > 1 && ithr_ic == 0;
        const bool use_c_buffer
                = jbgp.use_buffer && !force_use_dst_as_acc_buffer;

        char *c_buffer = nullptr;
        if (use_c_buffer) {
            size_t c_buf_idx = 0;
            size_t c_buf_nrows = 0;
            size_t c_buf_walk = 0;

            if (jbgp.nthr_ic_b > 1) {
                c_buf_idx = ithr_ic - acc_buffer_idx_shift;
                c_buf_nrows = jbgp.mb;

                // NOTE: This trick only works because the leading dimension of
                // buffer and memory descriptor is same.
                c_buf_walk = dst_off / dst_dt_size;
            } else {
                using loop_order_t = jit_brgemm_ip_fwd_conf_t::loop_order_t;
                switch (jbgp.loop_order) {
                    case loop_order_t::osc_occ_osb_ocb_icc:
                        c_buf_idx = ithr;
                        c_buf_nrows = jbgp.M;

                        // Single small block buffer, no walk needed.
                        c_buf_walk = 0;
                        break;
                    case loop_order_t::osc_occ_icc_osb_ocb:
                        c_buf_idx = ithr;
                        c_buf_nrows = jbgp.os_block * jbgp.nb_os_blocking;

                        // Walk for each block in os/oc-chunk.
                        c_buf_walk = osb * jbgp.os_block * jbgp.LDC
                                + ocb * jbgp.oc_block;
                        break;
                    case loop_order_t::icc_osc_occ_osb_ocb:
                    case loop_order_t::icc_occ_osc_ocb_osb:
                        c_buf_idx = 0;
                        c_buf_nrows = jbgp.os;

                        // NOTE: This trick only works because the leading
                        // dimension of buffer and memory descriptor is same.
                        c_buf_walk = dst_off / dst_dt_size;
                        break;
                }
            }

            size_t c_buf_shift = c_buf_idx * c_buf_nrows * jbgp.LDC;
            c_buf_shift += c_buf_walk;
            size_t c_buf_off = acc_dt_size * c_buf_shift;
            c_buffer = c_buffer_global + c_buf_off;
        }

        char *wsp_tile = is_amx
                ? wsp_tile_base + ithr * jbgp.amx_buf_size_per_thread
                : nullptr;
        int icb = cur_icc * jbgp.nb_ic_blocking;
        int ic = icb * jbgp.ic_block;

        bool kernel_init = cur_icc == icc_s && everyone_is(0, kd, kh, kw);

        bool is_os_tail = (jbgp.mb - n < jbgp.os_block);
        bool is_oc_tail = (jbgp.oc - oc < jbgp.oc_block);
        bool is_last_ic_chunk = cur_icc == ic_chunks - 1;
        bool is_ic_tail = is_last_ic_chunk && jbgp.K_tail > 0;
        bool last_spatial_slice = true;
        last_spatial_slice &= kd == jbgp.kd - 1;
        last_spatial_slice &= kh == jbgp.kh - 1;
        last_spatial_slice &= kw == jbgp.kw - 1;

        const int remaining_ic_blks
                = (jbgp.use_buffer_a ? utils::rnd_up(jbgp.ic, jbgp.ic_block)
                                     : jbgp.ic)
                - ic;
        const int gemm_batch
                = nstl::min(jbgp.gemm_batch_size, remaining_ic_blks / jbgp.K);

        auto is_bs_tail = (gemm_batch != jbgp.gemm_batch_size);
        int brg_ker_idx = brgemm_inner_product_utils::get_brg_kernel_index(
                is_bs_tail, kernel_init, is_os_tail, is_oc_tail, false);
        auto brg_kernel = brg_kernels_[brg_ker_idx].get();
        const int ic_blocks_per_batch = jbgp.K / jbgp.ic_block;

        const dim_t wei_cur_ocb = blk_off(weights_d, cur_ocb, 0, kd, kh, kw);

        if (copy_buffer_a) {
            assert(!jbgp.is_bf32);
            auto src_ptr = src + blk_off(src_d, n, ic, kd, kh, kw);
            copy_data_chunk(copy_src_kernel_, a_buffer, src_ptr,
                    is_os_tail ? jbgp.mb - n : jbgp.os_block, is_last_ic_chunk);
        }
        if (gemm_batch > 0 && brg_kernel != nullptr) {
            brgemm_palettes_.maybe_tile_configure(
                    is_amx, prev_ker_idx, brg_ker_idx);
            for (int b = 0; b < gemm_batch; b++) {
                auto A_ptr = jbgp.use_buffer_a
                        ? (a_buffer + src_dt_size * b * jbgp.K)
                        : (src
                                  + blk_off(src_d, n, ic + b * jbgp.K, kd, kh,
                                          kw));
                addr_batch[b].ptr.A = A_ptr;
                const dim_t wei_offset = wei_cur_ocb
                        + wei_ic_stride * (icb + b * ic_blocks_per_batch);
                addr_batch[b].ptr.B = weights + wei_offset;
            }

            auto ptr_D = dst + dst_off;
            auto ptr_C = use_c_buffer ? c_buffer : ptr_D;

            if (jbgp.nthr_ic_b == 1 && are_post_ops_applicable
                    && is_last_ic_chunk && !is_ic_tail && last_spatial_slice) {
                void *scratch = is_amx
                        ? static_cast<void *>(wsp_tile)
                        : (jbgp.req_s8s8_compensation
                                          ? static_cast<void *>(
                                                    const_cast<int *>(
                                                            &compensation[oc]))
                                          : nullptr);
                auto ptr_bias
                        = jbgp.with_bias ? bias + bia_dt_size * oc : nullptr;
                const brgemm_post_ops_data_t post_ops_data {
                        static_cast<const void *>(ptr_bias),
                        post_ops_binary_rhs_arg_vec.data(),
                        static_cast<size_t>(oc), 0, dst, 0, nullptr, nullptr,
                        nullptr, false, 1, false, false, src_scales,
                        wei_scales ? static_cast<const char *>(wei_scales)
                                        + jbgp.is_oc_scale * oc * sizeof(float)
                                   : nullptr,
                        dst_scales_ptr};

                brgemm_kernel_execute_postops(brg_kernel, gemm_batch,
                        addr_batch, (void *)ptr_C, (void *)ptr_D, post_ops_data,
                        scratch);
            } else {
                brgemm_kernel_execute(brg_kernel, gemm_batch, addr_batch,
                        (void *)ptr_C, is_amx ? (void *)wsp_tile : nullptr);
            }
        }

        if (is_ic_tail) {
            assert(!jbgp.use_buffer_a);
            auto use_init_ker = (kernel_init && gemm_batch == 0);
            int brg_ker_ic_tail_idx
                    = brgemm_inner_product_utils::get_brg_kernel_index(
                            false, use_init_ker, is_os_tail, is_oc_tail, true);
            brgemm_palettes_.maybe_tile_configure(
                    is_amx, prev_ker_idx, brg_ker_ic_tail_idx);

            const dim_t ic_block
                    = static_cast<dim_t>(gemm_batch) * ic_blocks_per_batch;
            addr_batch[0].ptr.A = src
                    + blk_off(src_d, n, ic + ic_block * jbgp.ic_block, kd, kh,
                            kw);
            const dim_t wei_offset
                    = wei_cur_ocb + wei_ic_stride * (icb + ic_block);
            addr_batch[0].ptr.B = weights + wei_offset;

            auto brg_kernel_ic_tail = brg_kernels_[brg_ker_ic_tail_idx].get();
            auto ptr_D = dst + dst_off;
            auto ptr_C = use_c_buffer ? c_buffer : ptr_D;
            if (jbgp.nthr_ic_b == 1 && are_post_ops_applicable
                    && last_spatial_slice) {
                void *scratch = is_amx
                        ? static_cast<void *>(wsp_tile)
                        : (jbgp.req_s8s8_compensation
                                          ? static_cast<void *>(
                                                    const_cast<int *>(
                                                            &compensation[oc]))
                                          : nullptr);
                auto ptr_bias
                        = jbgp.with_bias ? bias + bia_dt_size * oc : nullptr;
                const brgemm_post_ops_data_t post_ops_data {
                        static_cast<const void *>(ptr_bias),
                        post_ops_binary_rhs_arg_vec.data(),
                        static_cast<size_t>(oc), 0, dst, 0, nullptr, nullptr,
                        nullptr, false, 1, false, false, src_scales,
                        wei_scales ? static_cast<const char *>(wei_scales)
                                        + jbgp.is_oc_scale * oc * sizeof(float)
                                   : nullptr,
                        dst_scales_ptr};

                brgemm_kernel_execute_postops(brg_kernel_ic_tail, 1, addr_batch,
                        (void *)ptr_C, (void *)ptr_D, post_ops_data, scratch);
            } else {
                brgemm_kernel_execute(brg_kernel_ic_tail, 1, addr_batch,
                        (void *)ptr_C, is_amx ? (void *)wsp_tile : nullptr);
            }
        }
    };

    const int os_chunks = div_up(jbgp.nb_os, jbgp.nb_os_blocking);
    const int oc_chunks = div_up(jbgp.nb_oc, jbgp.nb_oc_blocking);
    const int work_amount = oc_chunks * os_chunks;

    const auto init_thr_groups
            = [=](const int ithr, const int nthr, int &nthr_ic, int &nthr_oc_mb,
                      int &ithr_ic, int &ithr_oc_mb) {
        nthr_ic = jbgp.nthr_ic_b <= nthr ? jbgp.nthr_ic_b : 1;
        nthr_oc_mb = nthr / nthr_ic;
        ithr_ic = ithr / nthr_oc_mb;
        ithr_oc_mb = ithr % nthr_oc_mb;
        if (ithr_oc_mb >= work_amount || ithr_ic >= ic_chunks
                || ithr >= rnd_dn(nthr, nthr_ic))
            return false;
        return true;
    };

    // If work_amount == 1 we limit num_threads to 1 as parallel(1, ...) does
    // not create parallel section at all. We do not limit num_threads
    // for 1 < work_amount < dnnl_get_max_threads() case to avoid potential
    // overhead on spawning different number of OMP threads from layer to layer.
    const int num_threads
            = work_amount == 1 && jbgp.nthr_ic_b <= 1 ? 1 : jbgp.nthr;
    parallel(num_threads, [=](const int ithr, const int nthr) {
        int nthr_ic {1}, nthr_oc_mb {1}, ithr_ic {0}, ithr_oc_mb {0};
        bool ok = init_thr_groups(
                ithr, nthr, nthr_ic, nthr_oc_mb, ithr_ic, ithr_oc_mb);
        if (!ok) return;

        int start {0}, end {0};
        balance211(work_amount, nthr_oc_mb, ithr_oc_mb, start, end);

        int icc_start {0}, icc_end {ic_chunks};
        if (nthr_ic > 1)
            balance211(ic_chunks, nthr_ic, ithr_ic, icc_start, icc_end);

        float *dst_scales_inv_ptr = nullptr;
        if (jbgp.with_dst_scales) {
            const float *dst_scales_ptr
                    = static_cast<const float *>(dst_scales);
            dst_scales_inv_ptr
                    = scratchpad.template get<float>(key_iprod_dst_scales)
                    + ithr;
            dst_scales_inv_ptr[0] = 1.f / dst_scales_ptr[0];
        }

        const int icc_work = icc_end - icc_start;

        const auto order = jbgp.loop_order;

        // For keeping track of last tile configuration used.
        int prev_ker_idx = -1;

        int icc {0}, occ {0}, osc {0};
        int work {start};
        using loop_order_t = jit_brgemm_ip_fwd_conf_t::loop_order_t;

        switch (order) {
            case loop_order_t::osc_occ_icc_osb_ocb:
            case loop_order_t::osc_occ_osb_ocb_icc:
            case loop_order_t::icc_osc_occ_osb_ocb:
                nd_iterator_init(work, osc, os_chunks, occ, oc_chunks);
                break;
            case loop_order_t::icc_occ_osc_ocb_osb:
                nd_iterator_init(work, occ, oc_chunks, osc, os_chunks);
                break;
        }
        while (work < end) {
            int ocb_s = occ * jbgp.nb_oc_blocking;
            int ocb_e = nstl::min(ocb_s + jbgp.nb_oc_blocking, jbgp.nb_oc);
            int ocb_work = ocb_e - ocb_s;

            int osb_s = osc * jbgp.nb_os_blocking;
            int osb_e = nstl::min(osb_s + jbgp.nb_os_blocking, jbgp.nb_os);
            int osb_work = osb_e - osb_s;

            // Each thread runs the below loops:
            int loop_start = 0, loop_end = 0;
            switch (order) {
                case loop_order_t::osc_occ_icc_osb_ocb:
                case loop_order_t::osc_occ_osb_ocb_icc:
                    loop_end = icc_work * osb_work * ocb_work;
                    break;
                case loop_order_t::icc_occ_osc_ocb_osb:
                case loop_order_t::icc_osc_occ_osb_ocb:
                    loop_end = osb_work * ocb_work;
                    break;
            }

            int osb = 0, ocb = 0;
            switch (order) {
                case loop_order_t::osc_occ_icc_osb_ocb:
                    nd_iterator_init(
                            0, icc, icc_work, osb, osb_work, ocb, ocb_work);
                    break;
                case loop_order_t::osc_occ_osb_ocb_icc:
                    nd_iterator_init(
                            0, osb, osb_work, ocb, ocb_work, icc, icc_work);
                    break;
                case loop_order_t::icc_osc_occ_osb_ocb:
                    nd_iterator_init(0, osb, osb_work, ocb, ocb_work);
                    break;
                case loop_order_t::icc_occ_osc_ocb_osb:
                    nd_iterator_init(0, ocb, ocb_work, osb, osb_work);
                    break;
            }

            while (loop_start < loop_end) {
                const bool ocb_inner_most
                        = order == loop_order_t::osc_occ_icc_osb_ocb;

                const bool copy_buffer_a = jbgp.use_buffer_a
                        && IMPLICATION(ocb_inner_most, ocb == 0);

                for_(int kd = 0; kd < jbgp.kd; kd++)
                for_(int kh = 0; kh < jbgp.kh; kh++)
                for (int kw = 0; kw < jbgp.kw; kw++) {
                    ker(ithr_oc_mb, nthr_oc_mb, ithr_ic, osb, osb_s, ocb, ocb_s,
                            icc, icc_start, kd, kh, kw, copy_buffer_a,
                            prev_ker_idx, dst_scales_inv_ptr);
                }

                ++loop_start;
                switch (order) {
                    case loop_order_t::osc_occ_icc_osb_ocb:
                        nd_iterator_step(
                                icc, icc_work, osb, osb_work, ocb, ocb_work);
                        break;
                    case loop_order_t::osc_occ_osb_ocb_icc:
                        nd_iterator_step(
                                osb, osb_work, ocb, ocb_work, icc, icc_work);
                        break;
                    case loop_order_t::icc_osc_occ_osb_ocb:
                        nd_iterator_step(osb, osb_work, ocb, ocb_work);
                        break;
                    case loop_order_t::icc_occ_osc_ocb_osb:
                        nd_iterator_step(ocb, ocb_work, osb, osb_work);
                        break;
                }
            }

            ++work;
            switch (order) {
                case loop_order_t::osc_occ_icc_osb_ocb:
                case loop_order_t::osc_occ_osb_ocb_icc:
                    nd_iterator_step(osc, os_chunks, occ, oc_chunks);
                    break;
                case loop_order_t::icc_osc_occ_osb_ocb:
                    nd_iterator_step(osc, os_chunks, occ, oc_chunks);
                    if (work == end) {
                        icc++;
                        if (icc < icc_work) {
                            work = start;
                            nd_iterator_init(
                                    work, osc, os_chunks, occ, oc_chunks);
                        }
                    }
                    break;
                case loop_order_t::icc_occ_osc_ocb_osb:
                    nd_iterator_step(occ, oc_chunks, osc, os_chunks);
                    if (work == end) {
                        icc++;
                        if (icc < icc_work) {
                            work = start;
                            nd_iterator_init(
                                    work, occ, oc_chunks, osc, os_chunks);
                        }
                    }
                    break;
            }
        }
        if (is_amx) amx_tile_release();
    });

    if (jbgp.nthr_ic_b > 1) {
        const auto get_dst_reduced_off = [=](int ithr_ic, int osb, int ocb) {
            assert(jbgp.nthr_ic_b > 1);
            int os = osb * jbgp.os_block;
            int oc = ocb * jbgp.oc_block;
            // use accumulation data type size for buffer offset computation
            const size_t dst_off = get_blk_off(dst_d, jbgp.acc_dt, os, oc);
            if (ithr_ic == 0) return dst_off;
            assert(ithr_ic > 0);
            // shift buffer idx if dst is used as accumulation buffer
            const int ic_buf_idx = ithr_ic - acc_buffer_idx_shift;
            return dst_off + (acc_dt_size * jbgp.mb * jbgp.LDC * ic_buf_idx);
        };

        parallel(num_threads,
                [= COMPAT_THIS_CAPTURE](const int ithr, const int nthr) {
            int nthr_ic {1}, nthr_oc_mb {1}, ithr_ic {0}, ithr_oc_mb {0};
            bool ok = init_thr_groups(
                    ithr, nthr, nthr_ic, nthr_oc_mb, ithr_ic, ithr_oc_mb);
            if (!ok) return;

            int ocmb_start {0}, ocmb_end {0};
            int start {0}, end {0};
            balance211(
                    work_amount, nthr_oc_mb, ithr_oc_mb, ocmb_start, ocmb_end);
            balance211(ocmb_end - ocmb_start, nthr_ic, ithr_ic, start, end);

            int prev_ker_idx = -1;

            int occ {0}, osc {0};
            nd_iterator_init(
                    ocmb_start + start, osc, os_chunks, occ, oc_chunks);
            while (start < end) {
                int ocb_s = occ * jbgp.nb_oc_blocking;
                int ocb_e = nstl::min(ocb_s + jbgp.nb_oc_blocking, jbgp.nb_oc);

                int osb_s = osc * jbgp.nb_os_blocking;
                int osb_e = nstl::min(osb_s + jbgp.nb_os_blocking, jbgp.nb_os);

                for (int osb = osb_s; osb < osb_e; ++osb) {
                    int cur_os_block = nstl::min(
                            jbgp.os - osb * jbgp.os_block, jbgp.os_block);
                    const bool is_os_tail = cur_os_block < jbgp.os_block;
                    const int cur_oc_chunk_size
                            = nstl::min(jbgp.LDC, ocb_e * jbgp.oc_block)
                            - ocb_s * jbgp.oc_block;
                    char *dst_reduced
                            = (can_use_dst_as_acc_buffer ? dst
                                                         : c_buffer_global)
                            + get_dst_reduced_off(0, osb, ocb_s);
                    const size_t os_offset = jbgp.LDC * acc_dt_size;
                    for (int ic_buf = 0; ic_buf < nthr_ic - 1; ++ic_buf) {
                        const char *c_buffer = c_buffer_global
                                + get_dst_reduced_off(ic_buf + 1, osb, ocb_s);
                        for (int os = 0; os < cur_os_block; ++os) {
                            acc_ker_->accumulate(
                                    (float *)(dst_reduced + os * os_offset),
                                    (float *)(c_buffer + os * os_offset),
                                    cur_oc_chunk_size);
                        }
                    }
                    if (are_post_ops_applicable) {
                        for (int ocb = ocb_s; ocb < ocb_e; ++ocb) {
                            const bool is_oc_tail
                                    = (jbgp.oc - ocb * jbgp.oc_block
                                            < jbgp.oc_block);
                            const int brg_ker_idx = brgemm_inner_product_utils::
                                    get_brg_kernel_index(false, false,
                                            is_os_tail, is_oc_tail, false);
                            brgemm_palettes_.maybe_tile_configure(
                                    is_amx, prev_ker_idx, brg_ker_idx);
                            const auto brg_kernel
                                    = brg_kernels_[brg_ker_idx].get();
                            const int os = osb * jbgp.os_block;
                            const int oc = ocb * jbgp.oc_block;
                            const auto ptr_bias = jbgp.with_bias
                                    ? bias + bia_dt_size * oc
                                    : nullptr;
                            auto ptr_D = dst
                                    + get_blk_off(dst_d, jbgp.dst_dt, os, oc);
                            auto ptr_C = can_use_dst_as_acc_buffer
                                    ? ptr_D
                                    : c_buffer_global
                                            + get_dst_reduced_off(0, osb, ocb);

                            char *wsp_tile = is_amx ? wsp_tile_base
                                            + ithr * jbgp.amx_buf_size_per_thread
                                                    : nullptr;

                            void *scratch = is_amx
                                    ? static_cast<void *>(wsp_tile)
                                    : (jbgp.req_s8s8_compensation
                                                      ? static_cast<
                                                                void *>(const_cast<
                                                                int *>(
                                                                &compensation
                                                                        [oc]))
                                                      : nullptr);

                            const brgemm_post_ops_data_t post_ops_data {
                                    static_cast<const void *>(ptr_bias),
                                    post_ops_binary_rhs_arg_vec.data(),
                                    static_cast<size_t>(oc), 0, dst, 0, nullptr,
                                    nullptr, nullptr, true /* skip_accm */, 1,
                                    false, false, src_scales,
                                    wei_scales ? static_cast<const char *>(
                                                         wei_scales)
                                                    + jbgp.is_oc_scale * oc
                                                            * sizeof(float)
                                               : nullptr,
                                    dst_scales};

                            brgemm_kernel_execute_postops(brg_kernel, 0,
                                    nullptr, (void *)ptr_C, (void *)ptr_D,
                                    post_ops_data, scratch);
                        }
                    }
                }
                ++start;
                nd_iterator_step(osc, os_chunks, occ, oc_chunks);
            }
        });
    }

    return status::success;
}

template struct brgemm_inner_product_fwd_t<avx2>;
template struct brgemm_inner_product_fwd_t<avx2_vnni>;
template struct brgemm_inner_product_fwd_t<avx2_vnni_2>;
template struct brgemm_inner_product_fwd_t<avx512_core>;
template struct brgemm_inner_product_fwd_t<avx512_core_bf16>;
template struct brgemm_inner_product_fwd_t<avx512_core_vnni>;
template struct brgemm_inner_product_fwd_t<avx512_core_amx>;
template struct brgemm_inner_product_fwd_t<avx512_core_fp16>;
template struct brgemm_inner_product_fwd_t<avx512_core_amx_fp16>;
template struct brgemm_inner_product_fwd_t<avx10_2>;
template struct brgemm_inner_product_fwd_t<avx10_2_amx_2>;

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_data_t<isa>::execute_backward_data(
        const exec_ctx_t &ctx) const {

    auto diff_dst_ = CTX_IN_MEM(const char *, DNNL_ARG_DIFF_DST);
    auto weights_ = CTX_IN_MEM(const char *, DNNL_ARG_WEIGHTS);
    auto diff_src_ = CTX_OUT_MEM(char *, DNNL_ARG_DIFF_SRC);

    auto diff_src = const_cast<char *>(diff_src_);
    auto weights = const_cast<char *>(weights_);
    auto diff_dst = const_cast<char *>(diff_dst_);

    const memory_desc_wrapper diff_src_d(pd()->diff_src_md());
    const memory_desc_wrapper diff_dst_d(pd()->diff_dst_md());
    const memory_desc_wrapper weights_d(pd()->weights_md(0));

    const auto &jbgp = pd()->jbgp_;

    const bool is_f32 = everyone_is(f32, jbgp.src_dt, jbgp.wei_dt, jbgp.dst_dt);
    const bool is_bf16 = everyone_is(bf16, jbgp.wei_dt, jbgp.dst_dt);
    const bool is_f16 = everyone_is(f16, jbgp.wei_dt, jbgp.dst_dt);
    const bool is_f32_out = jbgp.src_dt == f32;
    const bool is_amx = jbgp.is_amx;
    const size_t buf_dt_size = types::data_type_size(
            isa == avx512_core_fp16 ? f32 : jbgp.wei_dt);

    const dim_t wei_dt_size = types::data_type_size(jbgp.wei_dt);

    const auto &scratchpad = ctx.get_scratchpad_grantor();
    brgemm_batch_element_t *addr_batch_global
            = scratchpad.template get<brgemm_batch_element_t>(
                    key_brgemm_primitive_batch);
    char *c_buffer_global = (jbgp.use_buffer)
            ? scratchpad.template get<char>(key_brgemm_primitive_buffer)
            : nullptr;
    char *b_buffer_global = jbgp.use_buffer_b
            ? scratchpad.template get<char>(key_brgemm_primitive_buffer_b)
            : nullptr;
    char *a_buffer_global = jbgp.use_buffer_a
            ? scratchpad.template get<char>(key_brgemm_primitive_buffer_a)
            : nullptr;
    auto wsp_tile_base = is_amx
            ? ctx.get_scratchpad_grantor().template get<char>(
                      key_conv_amx_tile_buffer)
            : nullptr;

    const dim_t acc_dt_sz = types::data_type_size(jbgp.acc_dt);
    const dim_t src_dt_sz = types::data_type_size(jbgp.src_dt);

    const int oc_chunks = div_up(jbgp.nb_oc, jbgp.nb_oc_blocking);
    const int os_chunks = div_up(jbgp.nb_os, jbgp.nb_os_blocking);
    const int work_amount = jbgp.nb_ic * jbgp.ks() * os_chunks;
    const int num_threads
            = work_amount == 1 && jbgp.nthr_oc_b <= 1 ? 1 : jbgp.nthr;

    const auto get_weights_ptr
            = [=](int icb, int ocb, int kd = 0, int kh = 0, int kw = 0) {
        int fwd_ic_block
                = (is_amx && !jbgp.is_bf32) ? 2 * jbgp.simd_w : jbgp.simd_w;
        int fwd_oc_block = jbgp.get_weights_oc_block();
        int ic = icb * jbgp.ic_block;
        int oc = ocb * jbgp.oc_block;

        int fwd_icb = ic / fwd_ic_block;
        int fwd_ocb = oc / fwd_oc_block;
        int fwd_icb_simd = ic % fwd_ic_block;
        int fwd_ocb_simd = oc % fwd_oc_block;

        char *ptr_wei_local
                = weights + blk_off(weights_d, fwd_ocb, fwd_icb, kd, kh, kw);

        int blk_sz = is_bf16 || (is_f16 && isa != avx512_core_fp16) ? 2 : 1;

        return ptr_wei_local
                + wei_dt_size
                * (rnd_dn(fwd_icb_simd, blk_sz) * fwd_oc_block
                        + blk_sz * fwd_ocb_simd);
    };

    const auto transform_b_chunk
            = [= COMPAT_THIS_CAPTURE](char *tr_wei, const char *wei,
                      int trans_batch, int current_N, int current_K) {
        auto ctx = jit_brgemm_trans_wei_t::ctx_t();
        ctx.src = (void *)wei;
        ctx.tr_src = (void *)tr_wei;
        ctx.current_gemm_batch = trans_batch;
        ctx.current_N = current_N;
        ctx.current_K = current_K;
        (*trans_B_kernel_)(&ctx);
    };

    const auto ker = [= COMPAT_THIS_CAPTURE](int ithr_ic_mb, int nthr_ic_mb,
                             int ithr_oc, int nthr_oc, int n, int icb, int occ,
                             int kd, int kh, int kw, bool do_init,
                             bool do_b_transpose, int &prev_ker_idx) {
        const int ithr = nthr_ic_mb * ithr_oc + ithr_ic_mb;
        brgemm_batch_element_t *addr_batch
                = addr_batch_global + ithr * jbgp.adjusted_batch_size;

        const int ic = icb * jbgp.ic_block;
        const int ocb = occ * jbgp.nb_oc_blocking;
        const int oc = ocb * jbgp.oc_block;
        const size_t dsrc_off = blk_off(diff_src_d, n, ic, kd, kh, kw);
        const int adj_buffers = (jbgp.src_dt == f32) ? 1 : 0;
        const size_t c_buf_shift = jbgp.nthr_oc_b > 1
                ? (ithr_oc - adj_buffers)
                        * static_cast<size_t>(jbgp.mb * jbgp.LDC)
                : ithr * static_cast<size_t>(jbgp.LDC * jbgp.M);
        const size_t c_buf_off
                = types::data_type_size(jbgp.acc_dt) * c_buf_shift
                + (jbgp.nthr_oc_b > 1 ? acc_dt_sz * dsrc_off / src_dt_sz : 0);
        bool use_c_buf = false;
        if (is_f32_out && jbgp.use_buffer) {
            use_c_buf = (jbgp.nthr_oc_b == 1 || ithr_oc > 0);
        } else if (!is_f32_out && jbgp.use_buffer) {
            if (jbgp.nthr_oc_b > 1)
                use_c_buf = true;
            else
                use_c_buf = (jbgp.nthr_oc_b == 1 || ithr_oc > 0);
        }

        const size_t a_buffer_size_per_thr
                = jbgp.os_block * jbgp.LDA * types::data_type_size(jbgp.dst_dt);
        char *c_buffer = use_c_buf ? c_buffer_global + c_buf_off : nullptr;
        char *a_buffer = jbgp.use_buffer_a
                ? a_buffer_global + ithr * a_buffer_size_per_thr
                : diff_dst;
        char *wsp_tile = is_amx
                ? wsp_tile_base + ithr * jbgp.amx_buf_size_per_thread
                : nullptr;

        bool kernel_init = do_init;

        const bool is_os_tail = (jbgp.mb - n < jbgp.os_block);
        const bool is_ic_tail = (jbgp.ic - ic < jbgp.ic_block);
        const bool is_last_oc_chunk = occ == oc_chunks - 1;
        const bool is_oc_tail = is_last_oc_chunk && jbgp.K_tail > 0;

        const int rnd_oc
                = rnd_up(jbgp.oc, jbgp.use_buffer_a ? jbgp.oc_block : 1);
        const int nb_oc_b
                = nstl::min((rnd_oc - oc) / jbgp.oc_block, jbgp.nb_oc_blocking);

        auto is_bs_tail = (nb_oc_b != jbgp.nb_oc_blocking);
        const int brg_ker_idx
                = brgemm_inner_product_utils::get_brg_kernel_index(
                        is_bs_tail, kernel_init, is_os_tail, is_ic_tail, false);
        auto brg_kernel = brg_kernels_[brg_ker_idx].get();

        const int size_B = jbgp.LDB * rnd_up(jbgp.K, 2);

        const size_t b_buf_shift = jbgp.global_b_transpose
                ? icb * jbgp.nb_oc + ocb
                : ithr * jbgp.gemm_batch_size;
        const size_t b_buf_off = buf_dt_size * b_buf_shift * size_B;
        char *b_buffer = b_buffer_global + b_buf_off;

        char *ptr_D = diff_src + dsrc_off;
        char *ptr_C = use_c_buf ? c_buffer : ptr_D;

        if (jbgp.use_buffer_a)
            copy_data_chunk(copy_diff_dst_kernel_, a_buffer,
                    diff_dst + get_blk_off(diff_dst_d, jbgp.dst_dt, n, oc),
                    is_os_tail ? jbgp.os - n : jbgp.os_block, is_last_oc_chunk);

        if (nb_oc_b > 0 && brg_kernel != nullptr) {
            brgemm_palettes_.maybe_tile_configure(
                    is_amx, prev_ker_idx, brg_ker_idx);
            for (int b = 0; b < nb_oc_b; b++) {
                addr_batch[b].ptr.A = jbgp.use_buffer_a ? a_buffer
                                + b * jbgp.oc_block
                                        * types::data_type_size(jbgp.dst_dt)
                                                        : diff_dst
                                + get_blk_off(diff_dst_d, jbgp.dst_dt, n,
                                        oc + b * jbgp.oc_block);
                addr_batch[b].ptr.B = b_buffer + buf_dt_size * (b * size_B);
                if (!jbgp.global_b_transpose && do_b_transpose)
                    transform_b_chunk((char *)addr_batch[b].ptr.B,
                            get_weights_ptr(icb, ocb + b, kd, kh, kw), 1,
                            is_ic_tail ? jbgp.ic % jbgp.ic_block
                                       : jbgp.ic_block,
                            jbgp.oc_block);
            }

            if (jbgp.use_buffer && (jbgp.nthr_oc_b <= 1 || num_threads == 1)
                    && is_last_oc_chunk && !is_oc_tail) {
                void *scratch
                        = is_amx ? static_cast<void *>(wsp_tile) : nullptr;
                const brgemm_post_ops_data_t empty_po_data {};
                brgemm_kernel_execute_postops(brg_kernel, nb_oc_b, addr_batch,
                        (void *)c_buffer, (void *)ptr_D, empty_po_data,
                        scratch);

            } else {
                brgemm_kernel_execute(brg_kernel, nb_oc_b, addr_batch,
                        (void *)ptr_C, is_amx ? (void *)wsp_tile : nullptr);
            }
        }
        if (is_oc_tail) {
            assert(!jbgp.use_buffer_a);
            auto use_init_ker = (kernel_init && nb_oc_b == 0);
            const int brg_kernel_oc_tail_idx
                    = brgemm_inner_product_utils::get_brg_kernel_index(
                            false, use_init_ker, is_os_tail, is_ic_tail, true);
            brgemm_palettes_.maybe_tile_configure(
                    is_amx, prev_ker_idx, brg_kernel_oc_tail_idx);

            const int oc_block = nb_oc_b;
            addr_batch[0].ptr.A = diff_dst
                    + get_blk_off(diff_dst_d, jbgp.dst_dt, n,
                            oc + oc_block * jbgp.oc_block);
            addr_batch[0].ptr.B = b_buffer + buf_dt_size * (oc_block * size_B);
            if (!jbgp.global_b_transpose && do_b_transpose) {
                transform_b_chunk((char *)addr_batch[0].ptr.B,
                        get_weights_ptr(icb, ocb + oc_block, kd, kh, kw), 1,
                        is_ic_tail ? jbgp.ic % jbgp.ic_block : jbgp.ic_block,
                        jbgp.K_tail);
            }

            auto brg_kernel_oc_tail
                    = brg_kernels_[brg_kernel_oc_tail_idx].get();
            if (jbgp.use_buffer && jbgp.nthr_oc_b <= 1) {
                void *scratch
                        = is_amx ? static_cast<void *>(wsp_tile) : nullptr;
                const brgemm_post_ops_data_t empty_po_data {};
                brgemm_kernel_execute_postops(brg_kernel_oc_tail, 1, addr_batch,
                        (void *)c_buffer, (void *)ptr_D, empty_po_data,
                        scratch);

            } else {
                brgemm_kernel_execute(brg_kernel_oc_tail, 1, addr_batch,
                        (void *)ptr_C, is_amx ? (void *)wsp_tile : nullptr);
            }
        }
    };

    if (jbgp.global_b_transpose && jbgp.use_buffer_b) {
        assert(jbgp.nthr_oc_b == 1
                && "No global B transpose support for oc-reduction.");
        assert(!jbgp.has_spatial_dims()
                && "No global B transpose support for spatial dims.");

        parallel(num_threads, [=](const int ithr, const int nthr) {
            int start {0}, end {0};
            int max_ch_block = nstl::max(jbgp.ic_block, jbgp.oc_block);
            int ic_chunk_sz = max_ch_block / jbgp.ic_block;
            int oc_chunk_sz = max_ch_block / jbgp.oc_block;
            int nc_ic = div_up(jbgp.nb_ic, ic_chunk_sz);
            int nc_oc = div_up(jbgp.nb_oc, oc_chunk_sz);
            int transp_work_amount = nc_ic * nc_oc;
            balance211(transp_work_amount, nthr, ithr, start, end);
            int icc, occ;
            nd_iterator_init(start, icc, nc_ic, occ, nc_oc);
            while (start < end) {
                int icb_start = icc * ic_chunk_sz;
                int icb_end = nstl::min((icc + 1) * ic_chunk_sz, jbgp.nb_ic);
                int ocb_start = occ * oc_chunk_sz;
                int ocb_end = nstl::min((occ + 1) * oc_chunk_sz, jbgp.nb_oc);
                for_(int icb = icb_start; icb < icb_end; icb++)
                for (int ocb = ocb_start; ocb < ocb_end; ocb++) {
                    int ic = icb * jbgp.ic_block;
                    int oc = ocb * jbgp.oc_block;
                    bool is_ic_tail = (jbgp.ic - ic < jbgp.ic_block);
                    bool is_oc_tail = (jbgp.oc - oc < jbgp.oc_block);
                    const int size_B = jbgp.LDB * rnd_up(jbgp.K, 2);
                    char *b_buffer = b_buffer_global
                            + buf_dt_size
                                    * ((dim_t)icb * jbgp.nb_oc * size_B
                                            + (dim_t)ocb * size_B);

                    transform_b_chunk(b_buffer, get_weights_ptr(icb, ocb), 1,
                            is_ic_tail ? jbgp.ic % jbgp.ic_block
                                       : jbgp.ic_block,
                            is_oc_tail ? jbgp.oc % jbgp.oc_block
                                       : jbgp.oc_block);
                }
                ++start;
                nd_iterator_step(icc, nc_ic, occ, nc_oc);
            }
        });
    }

    parallel(num_threads, [=](const int ithr, const int nthr) {
        const int nthr_oc = jbgp.nthr_oc_b <= nthr ? jbgp.nthr_oc_b : 1;
        const int nthr_ic_mb = nthr / nthr_oc;
        const int ithr_ic_mb = ithr % nthr_ic_mb;
        const int ithr_oc = ithr / nthr_ic_mb;
        if (ithr_ic_mb >= work_amount || ithr_oc >= oc_chunks
                || ithr >= rnd_dn(nthr, nthr_oc))
            return;

        int start {0}, end {0};
        balance211(work_amount, nthr_ic_mb, ithr_ic_mb, start, end);
        int occ_start {0}, occ_end {oc_chunks};
        if (nthr_oc > 1)
            balance211(oc_chunks, nthr_oc, ithr_oc, occ_start, occ_end);

        // For keeping track of last tile configuration used.
        int prev_ker_idx = -1;

        int icb {0}, osc {0}, work {start};
        int kd {0}, kh {0}, kw {0};
        nd_iterator_init(work, osc, os_chunks, kd, jbgp.kd, kh, jbgp.kh, kw,
                jbgp.kw, icb, jbgp.nb_ic);
        while (work < end) {
            const int nb_os_blocking
                    = nstl::min(jbgp.nb_os - osc * jbgp.nb_os_blocking,
                            jbgp.nb_os_blocking);
            const int occ_work = occ_end - occ_start;
            const int loop_iteration = nb_os_blocking * occ_work;

            for (int iter = 0; iter < loop_iteration; ++iter) {
                int osb = 0, occ = occ_start;
                if (jbgp.use_buffer || !is_f32) {
                    osb += iter / occ_work;
                    occ += iter % occ_work;
                } else {
                    occ += iter / nb_os_blocking;
                    osb += iter % nb_os_blocking;
                }
                int n = (osc * jbgp.nb_os_blocking + osb) * jbgp.os_block;
                ker(ithr_ic_mb, nthr_ic_mb, ithr_oc, nthr_oc, n, icb, occ, kd,
                        kh, kw, occ == occ_start, osb == 0 || occ_work > 1,
                        prev_ker_idx);
            }
            ++work;
            nd_iterator_step(osc, os_chunks, kd, jbgp.kd, kh, jbgp.kh, kw,
                    jbgp.kw, icb, jbgp.nb_ic);
        }
        if (is_amx) amx_tile_release();
    });

    if (jbgp.nthr_oc_b > 1) {
        parallel(num_threads,
                [= COMPAT_THIS_CAPTURE](const int ithr, const int nthr) {
            const int nthr_oc = jbgp.nthr_oc_b <= nthr ? jbgp.nthr_oc_b : 1;
            const int n_oc_bufs = nstl::min(oc_chunks, nthr_oc);
            if (n_oc_bufs <= 1) return;

            const int dsrc_elems = jbgp.LDC * jbgp.os;
            const int reduce_chunk_size = 64;
            int start {0}, end {0};
            balance211(div_up(dsrc_elems, reduce_chunk_size), nthr, ithr, start,
                    end);
            const dim_t reduce_start = start * reduce_chunk_size;
            const dim_t reduce_finish
                    = nstl::min(end * reduce_chunk_size, dsrc_elems);
            if (reduce_finish <= reduce_start) return;
            const dim_t elems_to_reduce = reduce_finish - reduce_start;

            char *dsrc_reduced = diff_src + src_dt_sz * reduce_start;
            char *c_buffer_start = c_buffer_global + acc_dt_sz * reduce_start;

            float *out_buffer = is_f32_out
                    ? reinterpret_cast<float *>(dsrc_reduced)
                    : reinterpret_cast<float *>(c_buffer_start);
            int oc_buf_idx = !is_f32_out;
            int oc_buf_end = is_f32_out;
            int n_accs = n_oc_bufs - oc_buf_end;
            for (int oc_buf = oc_buf_idx; oc_buf < n_accs; oc_buf++) {
                const dim_t c_buf_offt = acc_dt_sz
                        * (oc_buf * jbgp.os * jbgp.LDC + reduce_start);
                char *c_buffer = c_buffer_global + c_buf_offt;

                acc_ker_->accumulate((float *)out_buffer, (float *)c_buffer,
                        elems_to_reduce);
                if (!is_f32_out && oc_buf == n_accs - 1) {
                    if (is_bf16) {
                        cvt_float_to_bfloat16((bfloat16_t *)dsrc_reduced,
                                (const float *)out_buffer, elems_to_reduce);
                    } else if (is_f16) {
                        cvt_float_to_float16((float16_t *)dsrc_reduced,
                                (const float *)out_buffer, elems_to_reduce);
                    }
                }
            }
        });
    }
}

template struct brgemm_inner_product_bwd_data_t<avx2>;
template struct brgemm_inner_product_bwd_data_t<avx512_core>;
template struct brgemm_inner_product_bwd_data_t<avx512_core_amx>;
template struct brgemm_inner_product_bwd_data_t<avx512_core_bf16>;
template struct brgemm_inner_product_bwd_data_t<avx512_core_amx_fp16>;
template struct brgemm_inner_product_bwd_data_t<avx512_core_fp16>;
template struct brgemm_inner_product_bwd_data_t<avx10_2>;

template <cpu_isa_t isa>
struct brgemm_inner_product_bwd_weights_t<isa>::thread_info_t {
    const char *src;
    const char *diff_dst;
    char *diff_weights;
    char *diff_bias;

    const memory_tracking::grantor_t &scratchpad;

    char *buffer_c = nullptr;
    char *buffer_bias = nullptr;
    char *wsp_tile_base = nullptr;

    int ithr;
    int ithr_sp_ic_c, ithr_oc_c, ithr_os_c;
    int nthr;
    int nthr_sp_ic_c, nthr_oc_c, nthr_os_c;

    int os_c_start = 0, os_c_end = 0, os_c_work;
    int oc_c_start = 0, oc_c_end = 0, oc_c_work;
    int sp_ic_c_start = 0, sp_ic_c_end = 0, sp_ic_c_work;
    simple_barrier::ctx_t *barrier_ctx;

    thread_info_t(const brgemm_inner_product_bwd_weights_t *self,
            const exec_ctx_t &ctx, int ithr)
        : src(CTX_IN_MEM(const char *, DNNL_ARG_SRC))
        , diff_dst(CTX_IN_MEM(const char *, DNNL_ARG_DIFF_DST))
        , diff_weights(CTX_OUT_MEM(char *, DNNL_ARG_DIFF_WEIGHTS))
        , diff_bias(CTX_OUT_MEM(char *, DNNL_ARG_DIFF_BIAS))
        , scratchpad(ctx.get_scratchpad_grantor())
        , ithr(ithr)
        , buffer_a_(scratchpad.template get<char>(
                  key_brgemm_primitive_buffer_a)) {

        const auto &jbgp = self->pd()->jbgp_;

        const bool is_amx = jbgp.is_amx;

        buffer_c = (jbgp.use_buffer)
                ? scratchpad.template get<char>(key_brgemm_primitive_buffer)
                : nullptr;

        buffer_bias = (jbgp.with_bias
                              && (jbgp.bia_dt != data_type::f32
                                      || jbgp.nthr_mb > 1))
                ? scratchpad.template get<char>(key_iprod_bias_bf16_convert_wsp)
                : nullptr;

        buffer_b_ = jbgp.use_buffer_b
                ? scratchpad.template get<char>(key_brgemm_primitive_buffer_b)
                : nullptr;

        thread_local_input_buffers_ = jbgp.local_buffers_for_input_tensors;
        int ic_chunks = div_up(jbgp.nb_ic, jbgp.nb_ic_blocking);
        int os_chunks = div_up(jbgp.nb_os, jbgp.nb_os_blocking);
        int sp_ic_chunks = ic_chunks * jbgp.ks();
        const size_t os_chunks_per_thr = div_up(os_chunks, jbgp.nthr_mb);
        const size_t num_os_chunks_per_thread
                = thread_local_input_buffers_ ? 1 : os_chunks_per_thr;

        nb_ic_blocking_ = jbgp.nb_ic_blocking;
        nb_oc_blocking_ = jbgp.nb_oc_blocking;
        ic_chunks_ = ic_chunks;

        if (jbgp.use_buffer_a) {
            const size_t dt_sz = buf_dt_size(jbgp.src_dt, jbgp.isa);
            const size_t sp_ic_chunks_per_thr
                    = div_up(sp_ic_chunks, jbgp.nthr_ic_b);
            const size_t num_sp_ic_chunks_per_thread
                    = thread_local_input_buffers_ ? 1 : sp_ic_chunks_per_thr;
            sp_ic_chunks_per_thread_ = num_sp_ic_chunks_per_thread;
            const size_t block_A_size = dt_sz * jbgp.LDA * jbgp.M;
            const size_t os_chunk_A_buffer
                    = jbgp.gemm_batch_size * block_A_size;
            const size_t ic_os_chunk_A_buffer
                    = jbgp.nb_ic_blocking * os_chunk_A_buffer;

            buffer_a_icb_shift_ = os_chunk_A_buffer;
            buffer_a_osb_shift_ = block_A_size;
            buffer_a_osc_shift_ = thread_local_input_buffers_
                    ? 0
                    : sp_ic_chunks_per_thr * ic_os_chunk_A_buffer;
            const size_t buffer_a_thread_shift = num_sp_ic_chunks_per_thread
                    * num_os_chunks_per_thread * ic_os_chunk_A_buffer;

            buffer_a_ = buffer_a_ + ithr * buffer_a_thread_shift;
        }

        if (jbgp.use_buffer_b) {
            const auto buf_dt = jbgp.dst_dt == f16 && isa == avx512_core_fp16
                    ? data_type::f32
                    : jbgp.dst_dt;
            const size_t dt_sz = buf_dt_size(jbgp.dst_dt, jbgp.isa);
            assert(types::data_type_size(buf_dt) == dt_sz);
            const size_t block_B_size = dt_sz * jbgp.LDB * jbgp.K;
            const size_t os_chunk_B_buffer
                    = jbgp.gemm_batch_size * block_B_size;
            buffer_b_ocb_shift_ = dt_sz * jbgp.oc_block
                    * data_type_vnni_granularity(buf_dt);
            buffer_b_osb_shift_ = block_B_size;
            buffer_b_osc_shift_
                    = thread_local_input_buffers_ ? 0 : os_chunk_B_buffer;

            const size_t buffer_b_thread_shift
                    = num_os_chunks_per_thread * os_chunk_B_buffer;

            buffer_b_ = buffer_b_ + ithr * buffer_b_thread_shift;
        }

        wsp_tile_base = is_amx
                ? ctx.get_scratchpad_grantor().template get<char>(
                          key_conv_amx_tile_buffer)
                : nullptr;

        nthr = jbgp.nthr;
        nthr_sp_ic_c = jbgp.nthr_ic_b;
        nthr_oc_c = jbgp.nthr_oc_b;
        nthr_os_c = jbgp.nthr_mb;

        // Set thread ID per dimension.
        nd_iterator_init(ithr, ithr_os_c, nthr_os_c, ithr_oc_c, nthr_oc_c,
                ithr_sp_ic_c, nthr_sp_ic_c);

        int oc_chunks = div_up(jbgp.nb_oc, jbgp.nb_oc_blocking);

        /* reduction dimension */
        balance211(os_chunks, nthr_os_c, ithr_os_c, os_c_start, os_c_end);
        os_c_work = os_c_end - os_c_start;

        balance211(oc_chunks, nthr_oc_c, ithr_oc_c, oc_c_start, oc_c_end);
        oc_c_work = oc_c_end - oc_c_start;

        balance211(sp_ic_chunks, nthr_sp_ic_c, ithr_sp_ic_c, sp_ic_c_start,
                sp_ic_c_end);
        sp_ic_c_work = sp_ic_c_end - sp_ic_c_start;

        if (dnnl_thr_syncable())
            barrier_ctx = scratchpad.template get<simple_barrier::ctx_t>(
                    key_conv_wei_bia_reduction_bctx);
    }

    char *get_buffer_a_ptr(int sp_icb, int osc) const {
        if (!buffer_a_) return (char *)nullptr;

        int icb_idx = sp_icb % (sp_ic_chunks_per_thread_ * nb_ic_blocking_);
        int osc_idx = thread_local_input_buffers_ ? 0 : osc - os_c_start;

        return buffer_a_ + osc_idx * buffer_a_osc_shift_
                + icb_idx * buffer_a_icb_shift_;
    }

    char *get_buffer_b_ptr(int ocb, int osc) const {
        if (!buffer_b_) return (char *)nullptr;

        const int ocb_idx = ocb % nb_oc_blocking_;
        const int osc_idx = thread_local_input_buffers_ ? 0 : osc - os_c_start;

        return buffer_b_ + osc_idx * buffer_b_osc_shift_
                + ocb_idx * buffer_b_ocb_shift_;
    }

    size_t get_buffer_a_osb_shift() const { return buffer_a_osb_shift_; }
    size_t get_buffer_b_osb_shift() const { return buffer_b_osb_shift_; }

    int ic_chunks() const { return ic_chunks_; }

private:
    char *buffer_a_ = nullptr;
    char *buffer_b_ = nullptr;

    bool thread_local_input_buffers_ = false;
    int nb_ic_blocking_ = 1;
    int nb_oc_blocking_ = 1;
    size_t buffer_a_icb_shift_ = 0;
    size_t buffer_a_osc_shift_ = 0;
    size_t buffer_a_osb_shift_ = 0;
    size_t buffer_b_ocb_shift_ = 0;
    size_t buffer_b_osc_shift_ = 0;
    size_t buffer_b_osb_shift_ = 0;

    int ic_chunks_ = 0;
    int sp_ic_chunks_per_thread_ = 0;
};

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<isa>::transform_matrix_a_chunk(
        char *tr_src, const char *src, int trans_batch, int current_m,
        int current_k) const {
    auto ctx = jit_brgemm_trans_src_t::ctx_t();
    ctx.src = (void *)src;
    ctx.tr_src = (void *)tr_src;
    ctx.current_gemm_batch = trans_batch;
    ctx.current_M = current_m;
    ctx.current_K = current_k;
    (*trans_A_kernel_)(&ctx);
}

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<isa>::transform_matrix_b_chunk(
        char *tr_diff_dst, const char *diff_dst, int trans_batch,
        int current_col_size, int current_row_size) const {
    auto ctx = jit_brgemm_trans_to_vnni_t::ctx_t();
    ctx.src = (void *)diff_dst;
    ctx.tr_src = (void *)tr_diff_dst;
    ctx.current_gemm_batch = trans_batch;
    ctx.current_col_size = current_col_size;
    ctx.current_row_size = current_row_size;
    (*trans_B_kernel_)(&ctx);
}

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<isa>::transpose_matrix_c_chunk(
        const thread_info_t *ti, const dim_t ocb, const dim_t icb, int oc_size,
        int ic_size, dim_t kd, dim_t kh, dim_t kw, bool is_reduction) const {
    const auto &jbgp = pd()->jbgp_;

    if (jbgp.is_amx) {
        auto p = jit_amx_ip_trans_diff_wei_t::ctx_t();

        // Note: This assumes AxB{inner_blocking} weights memory format.
        const dim_t ext_nb_ic = div_up(jbgp.ic, ext_ic_block_);
        const dim_t ext_block_nelems
                = static_cast<dim_t>(ext_ic_block_) * ext_oc_block_;
        const dim_t n_sp_slices = jbgp.ks();

        dim_t ext_icb = icb * (jbgp.ic_block / ext_ic_block_);
        dim_t ext_ocb = ocb * (jbgp.oc_block / ext_oc_block_);
        dim_t sp_slice = kd * jbgp.kh * jbgp.kw + kh * jbgp.kw + kw;

        dim_t icb_shift = ext_icb * ext_block_nelems;
        dim_t ocb_shift = ext_ocb * ext_nb_ic * ext_block_nelems * n_sp_slices;
        dim_t sp_slice_shift = sp_slice * ext_nb_ic * ext_block_nelems;
        dim_t out_shift = sp_slice_shift + ocb_shift + icb_shift;

        size_t out_offset = types::data_type_size(jbgp.wei_dt) * out_shift;

        p.src = get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw, 0);
        p.dst = (void *)(ti->diff_weights + out_offset);

        p.last_ic_block = (jbgp.ic <= ext_ic_block_
                || (jbgp.nb_ic > 1 && icb == jbgp.nb_ic - 1));
        p.last_oc_block = (jbgp.oc <= ext_oc_block_
                || (jbgp.nb_oc > 1 && ocb == jbgp.nb_oc - 1));
        (*diff_wei_trans_kernel_)(&p);
    } else {
        auto ctx = jit_brgemm_trans_to_vnni_t::ctx_t();
        ctx.src = (void *)(get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw, 0));

        const memory_desc_wrapper diff_weights_d(pd()->diff_weights_md(0));
        size_t wei_off = blk_off(diff_weights_d, ocb, icb, kd, kh, kw);
        ctx.tr_src = (void *)(ti->diff_weights + wei_off);

        ctx.current_gemm_batch = 1;
        ctx.current_col_size = oc_size;
        ctx.current_row_size = ic_size;
        (*trans_C_kernel_)(&ctx);
    }
}

template <cpu_isa_t isa>
char *brgemm_inner_product_bwd_weights_t<isa>::get_wei_acc_ptr(
        const thread_info_t *ti, dim_t ocb, dim_t icb, dim_t kd, dim_t kh,
        dim_t kw, int reduction_buf_idx) const {
    const auto &jbgp = pd()->jbgp_;

    const int reduction_buf_start_idx = jbgp.wei_dt == f32;
    // reduction_buf_idx argument allows manually set up required reduction
    // buffer index, required for reduction and transform diff_weights parts.
    // It has value -1 by default. If reduction_buf_idx < 0 then ti->ithr_os_c
    // is used for calculation of the current reduction index.
    const int buf_idx = reduction_buf_idx >= 0
            ? reduction_buf_idx
            : (ti->ithr_os_c - reduction_buf_start_idx);
    const size_t acc_dt_size = types::data_type_size(jbgp.acc_dt);

    if ((jbgp.nthr_mb > 1 && buf_idx < 0)
            || (jbgp.wei_dt == jbgp.acc_dt && reduction_buf_idx < 0
                    && ti->ithr_os_c == 0)) {
        MAYBE_UNUSED(reduction_buf_idx);
        const int icb_scale = (!jbgp.is_amx || jbgp.wei_dt == jbgp.acc_dt)
                ? jbgp.ic_block / jbgp.simd_w
                : 1;
        const memory_desc_wrapper diff_weights_d(pd()->diff_weights_md(0));
        size_t dwei_off
                = blk_off(diff_weights_d, ocb, icb * icb_scale, kd, kh, kw);
        return (char *)ti->diff_weights + dwei_off;
    }

    if (!jbgp.use_buffer) return nullptr;

    const int ocb_l = ocb % jbgp.nb_oc_blocking;
    const int icb_l = icb % jbgp.nb_ic_blocking;

    if (jbgp.nthr_mb > 1 || jbgp.harness == harness_mb_reduction) {
        const size_t icc = icb / jbgp.nb_ic_blocking;
        const size_t occ = ocb / jbgp.nb_oc_blocking;
        const size_t num_ic_chunks = div_up(jbgp.nb_ic, jbgp.nb_ic_blocking);
        const size_t num_oc_chunks = div_up(jbgp.nb_oc, jbgp.nb_oc_blocking);
        const size_t block_size = acc_dt_size * jbgp.ic_block * jbgp.oc_block;
        const size_t chunk_size
                = block_size * jbgp.nb_ic_blocking * jbgp.nb_oc_blocking;
        const size_t sp_slice_size = num_ic_chunks * num_oc_chunks * chunk_size;
        const size_t n_sp_slices = jbgp.ks();
        const size_t reduction_buf_size = n_sp_slices * sp_slice_size;
        const size_t reduction_buf_shift = reduction_buf_size * buf_idx;
        const size_t sp_slice_idx = kd * jbgp.kh * jbgp.kw + kh * jbgp.kw + kw;
        return ti->buffer_c + reduction_buf_shift + sp_slice_idx * sp_slice_size
                + (occ * num_ic_chunks + icc) * chunk_size
                + (ocb_l * jbgp.nb_ic_blocking + icb_l) * block_size;
    } else if (jbgp.nthr_mb == 1) {
        MAYBE_UNUSED(reduction_buf_idx);
        const size_t blk_size = acc_dt_size * jbgp.ic_block * jbgp.oc_block;
        const size_t buf_size_per_thread
                = blk_size * jbgp.nb_ic_blocking * jbgp.nb_oc_blocking;
        const size_t offset_within_thread_buf
                = blk_size * (jbgp.nb_ic_blocking * ocb_l + icb_l);
        const size_t offset
                = ti->ithr * buf_size_per_thread + offset_within_thread_buf;
        return ti->buffer_c + offset;
    }

    assert(!"unsupported case");
    return nullptr;
}

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<isa>::compute_diff_weights_and_bias(
        const thread_info_t *ti) const {
    auto diff_dst = const_cast<char *>(ti->diff_dst);
    auto diff_bias = ti->diff_bias;

    const memory_desc_wrapper diff_dst_d(pd()->diff_dst_md());

    const auto &jbgp = pd()->jbgp_;

    const size_t bia_dt_size
            = jbgp.with_bias ? types::data_type_size(jbgp.bia_dt) : 0;
    const size_t acc_dt_size = types::data_type_size(jbgp.acc_dt);

    const int oc_chunk_sz = jbgp.oc_block * jbgp.nb_oc_blocking;

    brgemm_batch_element_t *addr_batch_global
            = ti->scratchpad.template get<brgemm_batch_element_t>(
                    key_brgemm_primitive_batch);

    const bool is_bf16 = jbgp.wei_dt == bf16 || jbgp.is_bf32;
    const bool is_amx_bf16 = is_bf16 && isa == avx512_core_amx;
    char *wsp_tile_global = (is_amx_bf16) ? ti->wsp_tile_base : nullptr;
    int os_chunks = utils::div_up(jbgp.nb_os, jbgp.nb_os_blocking);

    const auto get_bia_acc_ptr = [&](int oc) {
        const int reduction_buf_start_idx = jbgp.bia_dt == f32;
        if (jbgp.bia_dt != data_type::f32
                || (jbgp.nthr_mb > 1
                        && ti->ithr_os_c >= reduction_buf_start_idx)) {
            return ti->buffer_bias
                    + acc_dt_size * (ti->ithr_os_c - reduction_buf_start_idx)
                    * jbgp.oc
                    + acc_dt_size * oc;
        } else {
            return ti->diff_bias + bia_dt_size * oc;
        }
    };

    const auto a_buf_osb_shift = ti->get_buffer_a_osb_shift();
    const auto b_buf_osb_shift = ti->get_buffer_b_osb_shift();

    const auto ker
            = [&](const int sp_icc, const int osc, const int icc, const int occ,
                      const int icb_i, const int ocb_i, const int osc_prev,
                      const int sp_icc_prev, const int occ_prev, int kd, int kh,
                      int kw, int &prev_ker_idx) {
        brgemm_batch_element_t *addr_batch
                = addr_batch_global + ti->ithr * jbgp.adjusted_batch_size;
        char *wsp_tile = is_amx_bf16
                ? wsp_tile_global + ti->ithr * jbgp.amx_buf_size_per_thread
                : nullptr;
        int icb = icb_i + icc * jbgp.nb_ic_blocking;
        int ocb = ocb_i + occ * jbgp.nb_oc_blocking;
        int ic = icb * jbgp.ic_block;
        int oc = ocb * jbgp.oc_block;
        int osb = osc * jbgp.nb_os_blocking;
        int n = osb * jbgp.os_block;

        // Base buffer pointers for the current kernel iteration,
        // x_buf_osb_shift values are used for shifting wrt osb iter variable
        int sp_icb = sp_icc * jbgp.nb_ic_blocking + icb_i;
        char *a_buffer = ti->get_buffer_a_ptr(sp_icb, osc);
        char *b_buffer = ti->get_buffer_b_ptr(ocb, osc);

        bool kernel_init = (osc == ti->os_c_start);

        auto nb_os_b
                = nstl::min((jbgp.mb - n) / jbgp.os_block, jbgp.nb_os_blocking);

        bool is_bs_tail = nb_os_b != jbgp.nb_os_blocking;
        bool is_os_tail = is_bs_tail && (jbgp.mb - n) % jbgp.os_block != 0;
        bool is_ic_tail = jbgp.ic - ic < jbgp.ic_block;
        bool is_oc_tail = jbgp.oc - oc < jbgp.oc_block;
        const int oc_chunk_tail = jbgp.oc % oc_chunk_sz;
        const bool is_last_oc_chunk = jbgp.oc - oc < oc_chunk_sz;
        const int curr_oc_chunk_sz = oc_chunk_tail > 0 && is_last_oc_chunk
                ? oc_chunk_tail
                : oc_chunk_sz;

        const bool transform_weights = jbgp.wei_dt != jbgp.acc_dt
                && (jbgp.nthr_mb == 1 || os_chunks == 1)
                && osc == (os_chunks - 1);
        const bool transform_b = jbgp.local_buffers_for_input_tensors
                ? jbgp.use_buffer_b && icb % jbgp.nb_ic_blocking == 0
                        && ocb % jbgp.nb_oc_blocking == 0
                        && IMPLICATION(osc_prev == osc,
                                occ_prev != ocb / jbgp.nb_oc_blocking)
                : jbgp.use_buffer_b && icb % jbgp.nb_ic_blocking == 0
                        && sp_icc == ti->sp_ic_c_start
                        && ocb % jbgp.nb_oc_blocking == 0;
        const bool transform_a = jbgp.local_buffers_for_input_tensors
                ? jbgp.use_buffer_a && ocb % jbgp.nb_oc_blocking == 0
                        && IMPLICATION(osc_prev == osc, sp_icc_prev != sp_icc)
                : jbgp.use_buffer_a
                        && ocb == ti->oc_c_start * jbgp.nb_oc_blocking;

        const int brg_ker_idx
                = brgemm_inner_product_utils::get_brg_kernel_index(
                        is_bs_tail, kernel_init, is_ic_tail, is_oc_tail, false);
        auto brg_kernel = brg_kernels_[brg_ker_idx].get();

        if (kernel_init && (is_ic_tail || is_oc_tail)) {
            // Due to big_ic_blk_ok optimization, the ic_block can be larger
            // than simd_w. last_ic_chunk makes sure we do not overflow.
            bool is_last_spatial_slice = true;
            is_last_spatial_slice &= kd == jbgp.kd - 1;
            is_last_spatial_slice &= kh == jbgp.kh - 1;
            is_last_spatial_slice &= kw == jbgp.kw - 1;

            int last_ic_chunk = jbgp.ic_block;
            if (is_last_spatial_slice)
                last_ic_chunk = nstl::min(
                        jbgp.ic_block, rnd_up(jbgp.ic - ic, jbgp.simd_w));

            utils::array_set(get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw), 0,
                    types::data_type_size(jbgp.acc_dt) * jbgp.oc_block
                            * last_ic_chunk);
        }

        bool is_1st_sp_slice = kd == 0 && kh == 0 && kw == 0;
        bool do_bias = jbgp.with_bias && icb == 0 && is_1st_sp_slice;

        if (nb_os_b > 0 && brg_kernel != nullptr) {
            brgemm_palettes_.maybe_tile_configure(
                    jbgp.is_amx, prev_ker_idx, brg_ker_idx);
            if (transform_a) {
                const memory_desc_wrapper src_d(pd()->src_md());
                auto src_ptr = ti->src + blk_off(src_d, n, ic, kd, kh, kw);

                transform_matrix_a_chunk(a_buffer, src_ptr, nb_os_b,
                        is_ic_tail ? jbgp.ic % jbgp.ic_block : jbgp.ic_block,
                        jbgp.os_block);
            }

            if (transform_b) {
                auto diff_dst_ptr = diff_dst
                        + types::data_type_size(jbgp.dst_dt)
                                * diff_dst_d.blk_off(n, oc);
                transform_matrix_b_chunk(b_buffer, diff_dst_ptr, nb_os_b,
                        curr_oc_chunk_sz, jbgp.os_block);
            }

            for (int os_block = 0; os_block < nb_os_b; os_block++) {
                auto a_ptr = a_buffer + os_block * a_buf_osb_shift;
                addr_batch[os_block].ptr.A = a_ptr;
                auto diff_dst_ptr = diff_dst
                        + types::data_type_size(jbgp.dst_dt)
                                * diff_dst_d.blk_off(
                                        n + os_block * jbgp.os_block, oc);
                if (jbgp.use_buffer_b) {
                    auto b_ptr = b_buffer + os_block * b_buf_osb_shift;
                    addr_batch[os_block].ptr.B = b_ptr;
                } else {
                    addr_batch[os_block].ptr.B = diff_dst_ptr;
                }
                if (do_bias) {
                    brgemm_kernel_diff_bias_t p;
                    auto bias_ptr = diff_bias + bia_dt_size * oc;
                    p.ptr_diff_dst = (void *)addr_batch[os_block].ptr.B;
                    p.ptr_diff_bias_acc = (void *)get_bia_acc_ptr(oc);
                    p.ptr_diff_bias = (void *)bias_ptr;
                    bool is_first = kernel_init && os_block == 0;
                    bool is_last = (jbgp.nthr_mb == 1 || os_chunks == 1)
                            && osc == os_chunks - 1 && os_block == nb_os_b - 1
                            && !is_os_tail;
                    p.flags = 0 | (is_first ? FLAG_REDUCE_FIRST : 0)
                            | (is_last ? FLAG_REDUCE_LAST : 0);

                    (*kernels_db_[false][is_oc_tail])(&p);
                }
            }
            brgemm_kernel_execute(brg_kernel, nb_os_b, addr_batch,
                    (void *)get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw),
                    wsp_tile);
        }

        if (is_os_tail) {
            auto use_init_ker = (kernel_init && nb_os_b == 0);
            const int brg_ker_idx_os_tail
                    = brgemm_inner_product_utils::get_brg_kernel_index(
                            false, use_init_ker, is_ic_tail, is_oc_tail, true);
            auto brg_kernel_os_tail = brg_kernels_[brg_ker_idx_os_tail].get();
            if (brg_kernel_os_tail != nullptr)
                brgemm_palettes_.maybe_tile_configure(
                        jbgp.is_amx, prev_ker_idx, brg_ker_idx_os_tail);
            int os_block = nb_os_b;
            auto a_ptr = a_buffer + os_block * a_buf_osb_shift;

            if (transform_a) {
                const memory_desc_wrapper src_d(pd()->src_md());
                auto src_ptr = ti->src
                        + blk_off(src_d, n + os_block * jbgp.os_block, ic, kd,
                                kh, kw);
                transform_matrix_a_chunk(a_ptr, src_ptr, 1,
                        is_ic_tail ? jbgp.ic % jbgp.ic_block : jbgp.ic_block,
                        jbgp.mb % jbgp.os_block);
            }

            addr_batch[0].ptr.A = a_ptr;
            auto diff_dst_ptr = diff_dst
                    + types::data_type_size(jbgp.dst_dt)
                            * diff_dst_d.blk_off(
                                    n + os_block * jbgp.os_block, oc);
            if (jbgp.use_buffer_b) {
                auto b_ptr = b_buffer + os_block * b_buf_osb_shift;

                if (transform_b)
                    transform_matrix_b_chunk(b_ptr, diff_dst_ptr, 1,
                            curr_oc_chunk_sz, jbgp.mb % jbgp.os_block);
                addr_batch[0].ptr.B = b_ptr;
            } else {
                addr_batch[0].ptr.B = diff_dst_ptr;
            }

            if (do_bias) {
                brgemm_kernel_diff_bias_t p;
                auto bias_ptr = diff_bias + bia_dt_size * oc;
                p.ptr_diff_dst = (void *)addr_batch[0].ptr.B;
                p.ptr_diff_bias_acc = (void *)get_bia_acc_ptr(oc);
                p.ptr_diff_bias = (void *)bias_ptr;
                bool is_first = kernel_init && os_block == 0;
                bool is_last = (jbgp.nthr_mb == 1 || os_chunks == 1)
                        && osc == os_chunks - 1;
                p.flags = 0 | (is_first ? FLAG_REDUCE_FIRST : 0)
                        | (is_last ? FLAG_REDUCE_LAST : 0);

                (*kernels_db_[true][is_oc_tail])(&p);
            }

            if (brg_kernel_os_tail != nullptr) {
                brgemm_kernel_execute(brg_kernel_os_tail, 1, addr_batch,
                        (void *)get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw),
                        wsp_tile);
            }
        }

        if (transform_weights) {
            transpose_matrix_c_chunk(ti, ocb, icb,
                    is_oc_tail ? jbgp.oc % jbgp.oc_block : jbgp.oc_block,
                    is_ic_tail ? jbgp.ic % jbgp.ic_block : jbgp.ic_block, kd,
                    kh, kw);
        }
    };

    const auto occ_work = (ti->oc_c_end - ti->oc_c_start);
    const auto sp_icc_work = (ti->sp_ic_c_end - ti->sp_ic_c_start);
    const auto osc_work = (ti->os_c_end - ti->os_c_start);
    const auto loop_end = occ_work * sp_icc_work * osc_work;
    int occ_idx = 0, sp_icc_idx = 0, osc_idx = 0;

    auto loop_idx = 0;

    using loop_order_t = jit_brgemm_ip_bwd_w_conf_t::loop_order_t;
    switch (jbgp.loop_order) {
        case loop_order_t::osc_icc_occ:
            nd_iterator_init(loop_idx, osc_idx, osc_work, sp_icc_idx,
                    sp_icc_work, occ_idx, occ_work);
            break;
        case loop_order_t::osc_occ_icc:
            nd_iterator_init(loop_idx, osc_idx, osc_work, occ_idx, occ_work,
                    sp_icc_idx, sp_icc_work);
            break;
        case loop_order_t::occ_icc_osc:
            nd_iterator_init(loop_idx, occ_idx, occ_work, sp_icc_idx,
                    sp_icc_work, osc_idx, osc_work);
    };

    int prev_ker_idx = -1;
    int osc_prev = -1, sp_icc_prev = -1, occ_prev = -1;
    while (loop_idx < loop_end) {
        const int occ = ti->oc_c_start + occ_idx;
        const int sp_icc = ti->sp_ic_c_start + sp_icc_idx;
        const int osc = ti->os_c_start + osc_idx;
        const int ic_chunks = ti->ic_chunks();

        // Unsquash spatial and input channels chunks index.
        int kd = 0, kh = 0, kw = 0, icc = 0;
        nd_iterator_init(
                sp_icc, kd, jbgp.kd, kh, jbgp.kh, kw, jbgp.kw, icc, ic_chunks);

        const int ocb_work = nstl::min(
                jbgp.nb_oc_blocking, jbgp.nb_oc - occ * jbgp.nb_oc_blocking);
        const int icb_work = nstl::min(
                jbgp.nb_ic_blocking, jbgp.nb_ic - icc * jbgp.nb_ic_blocking);

        for_(int ocb_i = 0; ocb_i < ocb_work; ocb_i++)
        for (int icb_i = 0; icb_i < icb_work; icb_i++) {
            ker(sp_icc, osc, icc, occ, icb_i, ocb_i, osc_prev, sp_icc_prev,
                    occ_prev, kd, kh, kw, prev_ker_idx);
        }
        osc_prev = osc;
        sp_icc_prev = icc;
        occ_prev = occ;

        ++loop_idx;

        switch (jbgp.loop_order) {
            case loop_order_t::osc_icc_occ:
                nd_iterator_step(osc_idx, osc_work, sp_icc_idx, sp_icc_work,
                        occ_idx, occ_work);
                break;
            case loop_order_t::osc_occ_icc:
                nd_iterator_step(osc_idx, osc_work, occ_idx, occ_work,
                        sp_icc_idx, sp_icc_work);
                break;
            case loop_order_t::occ_icc_osc:
                nd_iterator_step(occ_idx, occ_work, sp_icc_idx, sp_icc_work,
                        osc_idx, osc_work);
        };
    }
    if (jbgp.is_amx) amx_tile_release();
}

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<
        isa>::reduce_and_convert_diff_weights_and_bias(const thread_info_t *ti)
        const {
    const auto &jbgp = pd()->jbgp_;

    if (dnnl_thr_syncable() && jbgp.nthr > 1)
        simple_barrier::barrier(ti->barrier_ctx, jbgp.nthr);
    if (ti->nthr_os_c == 1) return;

    const bool is_f32_out = jbgp.wei_dt == data_type::f32;
    const int icb_scale = is_f32_out ? jbgp.ic_block / jbgp.simd_w : 1;

    const int icb_work = nstl::min(ti->sp_ic_c_work * jbgp.nb_ic_blocking,
            jbgp.nb_ic - ti->sp_ic_c_start * jbgp.nb_ic_blocking);
    const int ocb_work = nstl::min(ti->oc_c_work * jbgp.nb_oc_blocking,
            jbgp.nb_oc - ti->oc_c_start * jbgp.nb_oc_blocking);
    const int work = ocb_work * icb_work * jbgp.ks();

    int os_chunks = utils::div_up(jbgp.nb_os, jbgp.nb_os_blocking);
    int reduce_buffers = nstl::min(ti->nthr_os_c, os_chunks);
    int reduce_buf_idx_start = !is_f32_out;
    int reduce_buf_idx_end = reduce_buffers - is_f32_out;

    int start = 0, end = 0;
    balance211(work, ti->nthr_os_c, ti->ithr_os_c, start, end);
    if (start == end) return;

    int icb_l = 0, ocb_l = 0, kd = 0, kh = 0, kw = 0;
    const int acc_size = jbgp.ic_block * jbgp.oc_block;

    for (int ir = reduce_buf_idx_start; ir < reduce_buf_idx_end; ++ir) {
        int counter = start;
        nd_iterator_init(start, kd, jbgp.kd, kh, jbgp.kh, kw, jbgp.kw, ocb_l,
                ocb_work, icb_l, icb_work);
        while (counter < end) {
            const int ocb = ti->oc_c_start * jbgp.nb_oc_blocking + ocb_l;
            const int icb = ti->sp_ic_c_start * jbgp.nb_ic_blocking + icb_l;
            char *wei_to_reduce = get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw, ir);
            const memory_desc_wrapper diff_weights_d(pd()->diff_weights_md(0));
            char *wei_reduced = !is_f32_out
                    ? get_wei_acc_ptr(ti, ocb, icb, kd, kh, kw, 0)
                    : ti->diff_weights
                            + blk_off(diff_weights_d, ocb, icb * icb_scale, kd,
                                    kh, kw);
            acc_ker_->accumulate(
                    (float *)(wei_reduced), (float *)(wei_to_reduce), acc_size);
            if (!is_f32_out && ir + 1 == reduce_buf_idx_end) {
                transpose_matrix_c_chunk(ti, ocb, icb * icb_scale,
                        jbgp.oc_block, jbgp.ic_block, kd, kh, kw, true);
            }
            ++counter;
            nd_iterator_step(kd, jbgp.kd, kh, jbgp.kh, kw, jbgp.kw, ocb_l,
                    ocb_work, icb_l, icb_work);
        }
    }

    if (jbgp.with_bias && ti->ithr_sp_ic_c == 0 && ti->sp_ic_c_work > 0
            && ti->ithr_os_c == 0 && ti->os_c_work > 0 && ti->oc_c_work > 0) {
        const bool is_f32_bias = jbgp.bia_dt == data_type::f32;
        float *bias_reduced = is_f32_bias ? (float *)ti->diff_bias
                                          : (float *)ti->buffer_bias;
        int reduce_buf_idx_start = !is_f32_bias;
        int reduce_buf_idx_end = reduce_buffers - 1;
        int oc_chunk_size = jbgp.nb_oc_blocking * jbgp.oc_block;
        int oc = ti->oc_c_start * oc_chunk_size;
        int acc_size = nstl::min(ti->oc_c_work * oc_chunk_size, jbgp.oc - oc);

        int ir = reduce_buf_idx_start;
        for (; ir < reduce_buf_idx_end; ++ir) {
            float *bias_to_reduce = (float *)ti->buffer_bias + ir * jbgp.oc;
            acc_ker_->accumulate(
                    &bias_reduced[oc], &bias_to_reduce[oc], acc_size);
        }

        if (!is_f32_bias) {
            float *bias_to_reduce = (float *)ti->buffer_bias + ir * jbgp.oc;
            switch (jbgp.bia_dt) {
                case data_type::bf16:
                    add_floats_and_cvt_to_bfloat16(
                            (bfloat16_t *)(ti->diff_bias) + oc,
                            &bias_reduced[oc], &bias_to_reduce[oc], acc_size);
                    break;
                case data_type::f16:
                    add_floats_and_cvt_to_float16(
                            (float16_t *)(ti->diff_bias) + oc,
                            &bias_reduced[oc], &bias_to_reduce[oc], acc_size);
                    break;
                default: assert(!"invalid data type");
            }
        }
    }
}

template <cpu_isa_t isa>
void brgemm_inner_product_bwd_weights_t<isa>::execute_backward_weights(
        const exec_ctx_t &ctx) const {
    const auto &jbgp = pd()->jbgp_;

    if (dnnl_thr_syncable() && jbgp.nthr > 1) {
        const auto &scratchpad = ctx.get_scratchpad_grantor();
        simple_barrier::ctx_init(scratchpad.template get<simple_barrier::ctx_t>(
                key_conv_wei_bia_reduction_bctx));
    }

    parallel(
            jbgp.nthr, [= COMPAT_THIS_CAPTURE](const int ithr, const int nthr) {
        thread_info_t thread_info(this, ctx, ithr);
        compute_diff_weights_and_bias(&thread_info);

        if (dnnl_thr_syncable()) {
            reduce_and_convert_diff_weights_and_bias(&thread_info);
        }
    });

    if (!dnnl_thr_syncable()) {
        parallel(jbgp.nthr,
                [= COMPAT_THIS_CAPTURE](const int ithr, const int nthr) {
            thread_info_t thread_info(this, ctx, ithr);
            reduce_and_convert_diff_weights_and_bias(&thread_info);
        });
    }
}

template struct brgemm_inner_product_bwd_weights_t<avx512_core_amx_fp16>;
template struct brgemm_inner_product_bwd_weights_t<avx512_core_fp16>;
template struct brgemm_inner_product_bwd_weights_t<avx512_core_amx>;
template struct brgemm_inner_product_bwd_weights_t<avx512_core_bf16>;
template struct brgemm_inner_product_bwd_weights_t<avx512_core>;
template struct brgemm_inner_product_bwd_weights_t<avx2>;
template struct brgemm_inner_product_bwd_weights_t<avx10_2>;

} // namespace x64
} // namespace cpu
} // namespace impl
} // namespace dnnl

// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s