forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Fbgemm.h
1491 lines (1334 loc) · 41.8 KB
/
Fbgemm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
/**
* Top level include file for FBGEMM.
*/
#include <cassert>
#include <cmath>
#include <limits>
#include <memory>
#include <type_traits>
#include "./ConvUtils.h"
#include "./FbgemmBuild.h"
#include "./FbgemmEmbedding.h"
#include "./FbgemmI8DepthwiseAvx2.h"
#include "./FbgemmI8DirectconvAvx2.h"
#include "./FbgemmI8Spmdm.h"
#include "./QuantUtilsAvx2.h"
#include "./Types.h"
#include "./Utils.h"
// Turning on this option will print out time breakdown of each stage (e.g.,
// input packing, the main GEMM kernel, each output processing pipeline).
// Please note that currently this option won't report accurate timing if
// multiple threads are used.
// #define FBGEMM_MEASURE_TIME_BREAKDOWN
#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
#include <chrono>
#include <iostream>
extern double packing_time;
extern double computing_time;
extern double kernel_time;
extern double postprocessing_time;
extern double run_time;
#endif
namespace fbgemm {
/**
* @brief Templatized struct for packing parameters for A and B matrices.
*
* @tparam T input type
* @tparam accT the type used for accumulation
* @tparam instSet anyarch/avx2/avx512
* @tparam int8Type an auxiliary template parameter to specialize for 8-bit
* input types.
*/
template <
typename T,
typename accT,
inst_set_t instSet,
typename int8Type = void>
struct PackingTraits;
// type specialized implementation in an include file
#include "./PackingTraits-inl.h"
/**
* @brief Base class for packing matrices for higher GEMM performance.
*
* Matrix is tiled into blockRows() * blockCols() blocks.
* Each block is with size blockRowSize() * blockColSize().
* This class is designed using CRTP
* (https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern)
*
* @tparam PT actual packing type, e.g., PackAWithRowOffset
*/
template <typename PT, typename inpType, typename accType = std::int32_t>
class PackMatrix {
public:
PackMatrix() = delete; // no default constructor
PackMatrix(const PackMatrix&) = delete; // no copy
PackMatrix& operator==(const PackMatrix&) = delete; // no copy
PackMatrix(PackMatrix&&) = delete; // no move
PackMatrix& operator==(PackMatrix&& rhs) noexcept = delete; // no move
/**
* @param rows total number of rows in the matrix
* (packed rows can be less than rows).
* @param cols total number of columns in the matrix
* @param pmat A buffer to contain the packed matrix.
* If nullptr, a buffer owned by PackMatrix will be allocated
* internally to contain the packed matrix.
* For non-constant matrices like activation matrices, the client
* code may want to pass a pre-allocated pmat to avoid the
* overhead of internal memory allocation everytime a PackMatrix
* is constructed. The client code can query how big patm should
* be with packedBufferSize function.
* @param groups when groups > 1, we compute groups number of GEMMs each
* multiplies A.rows by A.cols/A.groups matrix with
* B.rows/B.groups by B.cols matrix (in conventional BLAS
* terminology, this is a batched GEMM but we use the name group
* to follow deep learning terminology). The result matrix has
* dimension A.rows by B.cols*B.groups .
* A.groups must be same as B.groups, A.groups must divide
* A.cols, and B.groups must divide B.rows and C.cols.
*/
PackMatrix(
std::int32_t rows,
std::int32_t cols,
inpType* pmat,
int groups = 1,
const BlockingFactors* params = nullptr);
/**
* @return true usually when the matrix is constant matrix (e.g., weight
* matrices) that can be prepacked
*/
bool isPrePacked() const {
return static_cast<const PT*>(this)->isPrePacked();
}
/**
* @return true if this is the first input matrix in GEMM (i.e., A in C = A *
* B)
*/
static constexpr bool isA() {
return PT::isA();
}
/**
* @brief The size of the buffer used for packing (The size is in number of
* elements).
*
* rows and cols are only used for fully packing, i.e., for B matrix. The
* client code can use this function to query how big the buffer used for
* packing should be.
*/
static int packedBufferSize(
int rows = 0,
int cols = 0,
const BlockingFactors* params = nullptr);
FBGEMM_PUSH_WARNING_AND_DISABLE("-Winfinite-recursion")
/**
* @return Pointer to a buffer containing row offset results. Some packing
* objects fuse row offset computation for later requantization step.
*/
std::int32_t* getRowOffsetBuffer() const {
return static_cast<const PT*>(this)->getRowOffsetBuffer();
}
FBGEMM_POP_WARNING
FBGEMM_PUSH_WARNING_AND_DISABLE("-Winfinite-recursion")
/**
* @brief When k loop is also tiled/blocked, this function is used to check if
* have executed computations for the last k block so that we can perform
* post-GEMM operations.
*/
bool isThisLastKBlock(int block_id) const {
return static_cast<const PT*>(this)->isThisLastKBlock(block_id);
}
FBGEMM_POP_WARNING
/**
* @brief Actual packing of a block of the source matrix in pmat buffer.
*/
void pack(const block_type_t& block) {
static_cast<PT*>(this)->pack(block);
}
std::int32_t numRows() const {
return nrows_;
}
std::int32_t numCols() const {
return ncols_;
}
/**
* @return The number of rows in each block
*/
std::int32_t blockRowSize() const {
return brow_;
}
/**
* @return The number of columns in each block
*/
std::int32_t blockColSize() const {
return bcol_;
}
/**
* @return The number of blocks along rows
*/
std::int32_t blockRows() const {
return nbrow_;
}
/**
* @return The number of blocks along columns
*/
std::int32_t blockCols() const {
return nbcol_;
}
/**
* @return The number of the rows in the currently packed block of a matrix.
* For pre-packed (i.e., fully-packed), it's equal to the total number
* of rows.
*/
std::int32_t numPackedRows() const {
return packedBlock_.row_size;
}
/**
* @return The number of columns in the currently packed block of a matrix.
* For pre-packed (i.e., fully-packed), it's equal to the number of
* columns.
*/
std::int32_t numPackedCols() const {
return packedBlock_.col_size;
}
/**
* @return The first row of the block we're working on.
*/
std::int32_t packedRowStart() const {
return packedBlock_.row_start;
}
/**
* @return The first column of the block we're working on.
*/
std::int32_t packedColStart() const {
return packedBlock_.col_start;
}
/**
* @return The beginning of (rowBlockNum, colBlockNum)th block
*/
inpType* getBuf(std::int32_t rowBlockNum = 0, std::int32_t colBlockNum = 0) {
return buf_ + blockRowSize() * blockColSize() * rowBlockNum +
blockRowSize() * blockColSize() * blockCols() * colBlockNum;
}
/**
* @brief Print the packed block.
*/
void printPackedMatrix(std::string name) {
static_cast<PT*>(this)->printPackedMatrix(name);
}
/**
* @return The number of rows in the last row block.
*/
std::int32_t lastBrow() const {
return last_brow_;
}
/**
* @return The number of columns in the last column block.
*/
std::int32_t lastBcol() const {
return last_bcol_;
}
int numGroups() const {
return G_;
}
/**
* @return True if the last column block has fewer columns than the block
* size.
*/
bool isThereColRemainder() const {
return last_bcol_ != blockColSize();
}
virtual ~PackMatrix() {
if (bufAllocatedHere_) {
fbgemmAlignedFree(buf_);
}
}
protected:
/**
* Set which block we're packing
*/
void packedBlock(const block_type_t& block) {
packedBlock_ = block;
nbrow_ = (numPackedRows() + blockRowSize() - 1) / blockRowSize();
nbcol_ = (numPackedCols() + blockColSize() - 1) / blockColSize();
last_brow_ = ((numPackedRows() % blockRowSize()) == 0)
? blockRowSize()
: (numPackedRows() % blockRowSize());
last_bcol_ = ((numPackedCols() % blockColSize()) == 0)
? blockColSize()
: (numPackedCols() % blockColSize());
}
inpType* buf_;
std::int32_t brow_; ///< the number of rows in each block
std::int32_t bcol_; ///< the number of columns in each block
std::int32_t nbrow_; ///< the number of blocks along rows
std::int32_t nbcol_; ///< the number of blocks along columns
bool bufAllocatedHere_{false};
const BlockingFactors*
blocking_params; ///< MCB, KCB, NCB, MR, NR, NR_MIN, ROW_INTERLEAVE;
private:
std::int32_t nrows_, ncols_;
int G_;
block_type_t packedBlock_; ///< The block in the source matrix just packed
std::int32_t last_brow_, last_bcol_;
};
/**
* @brief Matrix packed for the first input matrix in GEMM (usually
* activation). The source matrix is already quantized. Default
* accumulation type is int32.
*/
template <typename T, typename accT = std::int32_t>
class FBGEMM_API PackAMatrix final
: public PackMatrix<PackAMatrix<T, accT>, T, accT> {
public:
using This = PackAMatrix<T, accT>;
using BaseType = PackMatrix<This, T, accT>;
using inpType = T;
using accType = accT;
PackAMatrix() = delete; // no default constructor
PackAMatrix(
matrix_op_t trans,
std::int32_t nRow,
std::int32_t nCol,
const inpType* smat,
std::int32_t ld,
inpType* pmat = nullptr,
int groups = 1,
const BlockingFactors* params = nullptr);
/**
* Activation matrices are not constant so cannot amortize the cost of
* pre-packing.
*/
bool isPrePacked() const {
return false;
}
/**
* @return True if this is used as A matrix.
*/
static constexpr bool isA() {
return true;
}
/**
* @return A pointer to the row offset buffer. There is no row offset buffer
* calculations with this packing class, hence, it returns nullptr.
*/
std::int32_t* getRowOffsetBuffer() const {
return nullptr;
}
/**
* @return Offset of the element in the packed matrix that was at (i, j) in
* the source matrix.
*/
std::int32_t addr(std::int32_t i, std::int32_t j) const;
/**
* @brief Packs a block of source matrix into pmat buffer.
*/
void pack(const block_type_t& block);
/**
* @brief Print the packed block.
*/
void printPackedMatrix(std::string name);
private:
matrix_op_t trans_;
const T* smat_;
std::int32_t ld_;
std::int32_t row_interleave_B_;
};
/**
* @brief Matrix packed for the second input matrix in GEMM (usually weight).
* The source matrix is already quantized. Default accumulation
* type is int32.
*/
template <typename T, typename accT = std::int32_t>
class FBGEMM_API PackBMatrix final
: public PackMatrix<PackBMatrix<T, accT>, T, accT> {
public:
using This = PackBMatrix<T, accT>;
using BaseType = PackMatrix<This, T, accT>;
using inpType = T;
using accType = accT;
PackBMatrix() = delete; // no default constructor
/**
* @param groups if > 1 and trans == NoTranspose, smat is nRow x nCol with
* groups are vertically concatenated: each group is
* (nRow / groups) x nCol .
* if > 1 and trans == Transpose, smat is (nCol * groups) x
* (nRow / groups) with groups are horizontally concatenated:
* each group is nCol x (nRow / groups) . Each group is
* transposed and vertically concatenated to match with the
* NoTranspose case.
*/
PackBMatrix(
matrix_op_t trans,
std::int32_t nRow,
std::int32_t nCol,
const inpType* smat,
std::int32_t ld,
inpType* pmat = nullptr,
int groups = 1,
const BlockingFactors* params = nullptr);
/**
* Weight matrices are usually constant so worth pre-packing.
*/
bool isPrePacked() const {
return true;
}
/**
* @return True if to be used as A matrix, False otherwise.
*/
static constexpr bool isA() {
return false;
}
/**
* @brief When k loop is also tiled/blocked, this function is used to check if
* have executed computations for the last k block so that we can perform
* post-GEMM operations.
*/
bool isThisLastKBlock(int block_id) const {
return (BaseType::blockRows() - 1) == block_id;
}
/**
* @return Offset of the element in the packed matrix that was at (i, j) in
* the source matrix.
*/
std::int32_t addr(std::int32_t i, std::int32_t j) const;
/**
* @brief Packs a block of source matrix into pmat buffer. The blocking
* parameters are needed to compute the buffer size of each group.
* It will use default blocking parameters if params is not provided.
*/
void pack(const block_type_t& block, const BlockingFactors* params = nullptr);
/**
* @brief Print the packed block.
*/
void printPackedMatrix(
std::string name,
const BlockingFactors* params = nullptr);
/**
* @return true if meta information like matrix shape is the same.
*/
bool metaEquals(const PackBMatrix<T, accT>& that) const;
/**
* @return true if matrices are the same.
*/
bool equals(const PackBMatrix<T, accT>& that) const;
/**
* @brief Unpack pmat buffer to the origin_buf (Used for the serialization to
* recover weight matrix).
*/
void unpack(T* origin_buf, const BlockingFactors* params = nullptr);
~PackBMatrix() {}
private:
matrix_op_t trans_;
const T* smat_;
std::int32_t ld_;
std::int32_t row_interleave_;
/**
* @brief Internal function performing both pack & unpack
*/
void pack_unpack_(
const block_type_t& block,
T* unpack_buf,
T* pack_buf,
bool ispack,
const BlockingFactors* params = nullptr);
};
/**
* @brief Matrix packed for direct group convolution.
* The source matrix is already quantized. Default accumulation
* type is int32.
*/
template <typename T, typename accT = std::int32_t, int SPATIAL_DIM = 2>
class FBGEMM_API PackWeightMatrixForGConv {
public:
using This = PackWeightMatrixForGConv<T, accT, SPATIAL_DIM>;
using inpType = T;
using accType = accT;
PackWeightMatrixForGConv() = delete; // no default constructor
PackWeightMatrixForGConv(const PackWeightMatrixForGConv&) = delete; // no copy
PackWeightMatrixForGConv& operator==(const PackWeightMatrixForGConv&) =
delete; // no copy
PackWeightMatrixForGConv(PackWeightMatrixForGConv&&) = delete; // no move
PackWeightMatrixForGConv& operator==(PackWeightMatrixForGConv&&) =
delete; // no move
/**
* @param pmat if nullptr, a buffer is allocated and owned by this class.
*/
PackWeightMatrixForGConv(
matrix_op_t trans,
const conv_param_t<SPATIAL_DIM>& conv_param,
const inpType* sdata,
inpType* pdata = nullptr);
/**
* Number of groups we work at a time to fill the full simd width
* e.g., IC_PER_G = 4 and OC_PER_G = 4, we work on two groups at a time
* to fill the avx2 width of 256 bits.
*/
static int numOfGroupsTogether(const conv_param_t<SPATIAL_DIM>& conv_param);
/**
* @brief Packs a block of source matrix into pmat buffer.
*/
void pack();
/**
* @brief Unpacks a pmat buffer into source matrix.
*/
void unpack(T* origin_buf);
/**
* @brief Return packed data
*/
inpType* getBuf() {
return pdata_;
}
~PackWeightMatrixForGConv() {
if (bufAllocatedHere_) {
fbgemmAlignedFree(pdata_);
}
}
private:
matrix_op_t trans_;
const conv_param_t<SPATIAL_DIM> conv_param_;
const T* sdata_;
T* pdata_;
bool bufAllocatedHere_{false};
// Number of groups we work at a time to fill the full simd width
int GTogether_;
/**
* @brief Internal function performing both pack & unpack
*/
void pack_unpack_(const T* src, T* dst, bool ispack);
/**
* @brief Get the index of the unpacked data
*/
int unpacked_index_(int t, int r, int s, int k, int g, int c, bool tr);
/**
* @brief Get the index of the packed data
*/
int packed_index_(int t, int r, int s, int k, int g, int c);
};
/**
* @brief A container class to keep packed weight tensor for convolution.
* The source tensor should already be quantized.
*
* @tparam SPATIAL_DIM is equal to 2 for 2D convolutions and 3 for 3D
* convolutions. Default value is 2.
* @tparam T is the datatype for source tensor. Default value is int8.
* @tparam accT is the datatype to accumulate into. Default value is int32.
*/
template <
int SPATIAL_DIM = 2,
typename T = std::int8_t,
typename accT = std::int32_t>
class FBGEMM_API PackWeightsForConv {
public:
using This = PackWeightsForConv<SPATIAL_DIM, T, accT>;
using inpType = T;
using accType = accT;
PackWeightsForConv() = delete; // no default constructor
PackWeightsForConv(
const conv_param_t<SPATIAL_DIM>& conv_param,
const inpType* sdata,
const BlockingFactors* blocking_params = nullptr);
std::shared_ptr<PackBMatrix<T, accT>> getPackedWForIm2col() {
return W_im2col_packed_;
}
std::shared_ptr<PackedDepthWiseConvMatrix> getPackedWForDepthwise() {
return W_dw_packed_;
}
std::shared_ptr<PackedDirectConvMatrix> getPackedWForDirectconv() {
return W_dc_packed_;
}
std::shared_ptr<PackWeightMatrixForGConv<T, accT, SPATIAL_DIM>>
getPackedWForGroupwise() {
return W_gconv_packed_;
}
std::shared_ptr<PackBMatrix<T, accT>> getPackedWForPointwise() {
return W_pointwise_packed_;
}
int inputChannels() {
return conv_param_.IC;
}
int outputChannels() {
return conv_param_.OC;
}
std::array<int, SPATIAL_DIM> kernelDims() {
return conv_param_.K;
}
int groups() {
return conv_param_.G;
}
/**
* @brief Returns true if the packed weights would work for the given
* convolution parameters, and false otherwise
*/
bool isPackingCompliant(const conv_param_t<SPATIAL_DIM>& conv_p);
/**
* @brief Returns a string of mismatching parameters
*/
std::string mismatchingParams(const conv_param_t<SPATIAL_DIM>& conv_p);
/**
* @brief Unpack packed matric into origin_buf (Used for the serialization to
* recover weight matrix).
*/
void unpack(T* origin_buf);
private:
const conv_param_t<SPATIAL_DIM> conv_param_;
// Packed weights if we use im2col based convolution implementation
std::shared_ptr<PackBMatrix<T, accT>> W_im2col_packed_;
// Packed weights if we use depthwise convolution implementation
std::shared_ptr<PackedDepthWiseConvMatrix> W_dw_packed_;
// Packed weights if we use direct convolution implementation
std::shared_ptr<PackedDirectConvMatrix> W_dc_packed_;
// Packed weights if we use groupwise (small channels per group) convolution
// implementation
std::shared_ptr<PackWeightMatrixForGConv<T, accT, SPATIAL_DIM>>
W_gconv_packed_;
// Packed weights if we use direct gemm for pointwise convolution
std::shared_ptr<PackBMatrix<T, accT>> W_pointwise_packed_;
};
/**
* @brief Matrix packed for the first input matrix in GEMM (usually activation),
* and row offsets used for requantization is computed during packing.
* Im2col is fused with packing here. The source matrix is already
* quantized.
*/
template <typename T, typename accT = std::int32_t, int SPATIAL_DIM = 2>
class FBGEMM_API PackAWithIm2Col
: public PackMatrix<PackAWithIm2Col<T, accT, SPATIAL_DIM>, T, accT> {
public:
using This = PackAWithIm2Col<T, accT, SPATIAL_DIM>;
using BaseType = PackMatrix<This, T, accT>;
using inpType = T;
using accType = accT;
PackAWithIm2Col() = delete; // no default constructor
/**
* @param zero_pt the quantized value that maps to 0.0f floating-point number.
* @param row_offset If nullptr, this constructor internally allocates a
* buffer and owns it. Otherwise, this class doesn't own
* the buffer. The buffer will be populated when pack
* function is called.
* @param b_symmetric if true we skip row offset computation
*/
PackAWithIm2Col(
const conv_param_t<SPATIAL_DIM>& conv_param,
const T* sdata,
inpType* pmat = nullptr,
std::int32_t a_zero_pt = 0,
std::int32_t* row_offset = nullptr,
bool b_symmetric = false,
const BlockingFactors* params = nullptr);
/**
* Activation matrices are not constant so cannot amortize the cost of
* pre-packing.
*/
bool isPrePacked() const {
return false;
}
/**
* @return True if this is used as A matrix.
*/
static constexpr bool isA() {
return true;
}
/**
* @brief Packs a block of source matrix into pmat buffer.
*/
void pack(const block_type_t& block);
/**
* @return A pointer to the row offset buffer.
*/
std::int32_t* getRowOffsetBuffer() const {
return row_offset_;
}
/**
* @brief Print the packed block.
*/
void printPackedMatrix(std::string name);
/**
* @return Size of row offset buffer in number of elements
*/
static int rowOffsetBufferSize(const BlockingFactors* params = nullptr);
~PackAWithIm2Col() {
if (rowOffsetAllocatedHere) {
fbgemmAlignedFree(row_offset_);
}
}
private:
const conv_param_t<SPATIAL_DIM> conv_p_;
const T* sdata_;
std::int32_t a_zero_pt_;
std::int32_t* row_offset_{nullptr};
bool rowOffsetAllocatedHere{false};
std::int32_t row_interleave_B_;
};
/**
* @brief Matrix packed for the first input matrix in GEMM (usually activation),
* and row offsets used for requantization is computed during packing.
* The source matrix is already quantized.
*/
template <typename T, typename accT = std::int32_t>
class FBGEMM_API PackAWithRowOffset final
: public PackMatrix<PackAWithRowOffset<T, accT>, T, accT> {
public:
using This = PackAWithRowOffset<T, accT>;
using BaseType = PackMatrix<This, T, accT>;
using inpType = T;
using accType = accT;
PackAWithRowOffset() = delete; // no default constructor
/**
* @param row_offset If nullptr, this constructor internally allocates a
* buffer and owns it. Otherwise, this class doesn't own
* the buffer. The buffer will be populated when pack
* function is called.
*/
PackAWithRowOffset(
matrix_op_t trans,
std::uint32_t nRow,
std::uint32_t nCol,
const T* smat,
std::uint32_t ld,
inpType* pmat = nullptr,
int groups = 1,
std::int32_t* row_offset = nullptr,
const BlockingFactors* params = nullptr);
/**
* Activation matrices are not constant so cannot amortize the cost of
* pre-packing.
*/
bool isPrePacked() const {
return false;
}
/**
* @return True if this is used as A matrix.
*/
static constexpr bool isA() {
return true;
}
/**
* @return Offset of the element in the packed matrix that was at (i, j) in
* the source matrix
*/
std::int32_t addr(std::int32_t i, std::int32_t j) const;
/**
* @brief Packs a block of source matrix into pmat buffer.
*/
void pack(const block_type_t& block);
/**
* @return A pointer to the row offset buffer.
*/
std::int32_t* getRowOffsetBuffer() const {
return row_offset_;
}
/**
* @brief Print the packed block.
*/
void printPackedMatrix(std::string name);
/**
* @return size of row offset buffer in number of elements
*/
static int rowOffsetBufferSize(const BlockingFactors* params = nullptr);
~PackAWithRowOffset() {
if (rowOffsetAllocatedHere) {
fbgemmAlignedFree(row_offset_);
}
}
private:
matrix_op_t trans_;
const T* smat_;
std::uint32_t ld_;
std::int32_t* row_offset_{nullptr};
bool rowOffsetAllocatedHere{false};
std::int32_t row_interleave_B_;
};
/**
* @brief Matrix packed for the first input matrix in GEMM (usually activation),
* and row offsets used for requantization is computed during packing.
* The source matrix is in fp32 and quantized during packing.
*/
template <typename T, typename accT = std::int32_t>
class FBGEMM_API PackAWithQuantRowOffset final
: public PackMatrix<PackAWithQuantRowOffset<T, accT>, T, accT> {
public:
using This = PackAWithQuantRowOffset<T, accT>;
using BaseType = PackMatrix<This, T, accT>;
using inpType = T;
using accType = accT;
PackAWithQuantRowOffset() = delete; // no default constructor
/**
* @param row_offset If nullptr, this constructor internally allocates a
* buffer and owns it. Otherwise, this class doesn't own
* the buffer. The buffer will be populated when pack
* function is called.
*/
PackAWithQuantRowOffset(
matrix_op_t trans,
std::int32_t nRow,
std::int32_t nCol,
const float* smat,
std::int32_t ld,
inpType* pmat = nullptr,
float scale = 1.0f,
std::int32_t zero_pt = 0,
int groups = 1,
std::int32_t* row_offset = nullptr,
const BlockingFactors* params = nullptr);
/**
* Activation matrices are not constant so cannot amortize the cost of
* pre-packing.
*/
bool isPrePacked() const {
return false;
}
/**
* @return True if this is used as A matrix.
*/
static constexpr bool isA() {
return true;
}
/**
* @return offset of the element in the packed matrix that was at (i, j) in
* the source matrix
*/
std::int32_t addr(std::int32_t i, std::int32_t j) const;
/**
* @brief Packs a block of source matrix into pmat buffer.
*/
void pack(const block_type_t& block);
/**
* @return A pointer to the row offset buffer.
*/
std::int32_t* getRowOffsetBuffer() const {
return row_offset_;
}
/**
* @brief Print the packed block.
*/
void printPackedMatrix(std::string name);
/**
* @return Size of row offset buffer in number of elements
*/
static int rowOffsetBufferSize(const BlockingFactors* params = nullptr);
~PackAWithQuantRowOffset() {
if (rowOffsetAllocatedHere) {
fbgemmAlignedFree(row_offset_);
}
}
private:
matrix_op_t trans_;
const float* smat_;
std::int32_t ld_;
float scale_;
std::int32_t zero_pt_;
std::int32_t* row_offset_{nullptr};
bool rowOffsetAllocatedHere{false};
std::int32_t row_interleave_B_;
};
/*
*
* Post Processing of outputs
*
*/
/**
* @brief Does nothing. NoOp. Used as the last operation in the output
* processing pipeline.
*
*/
template <typename outT = std::uint8_t, typename inT = std::uint8_t>
class FBGEMM_API DoNothing {
public:
using outType = outT;
using inpType = inT;
DoNothing() {}
template <inst_set_t instSet>
int f(
outType* /* unused */,
inpType* /* unused */,
const block_type_t& /* unused */,
int /* unused */,
int /* unused */) const {
return 0;
}
};
/**
* @brief Copy data pointed by inp ptr to out ptr when
* inp ptr and out ptr are not the same.
* inp buffer: row and column start points: (0, 0)
* output buffer: row and column start points:
* (block.row_start, block.col_start)
*
* This is the output processing stage that should passed when there is no
* requantization and output is required in the same format as internal buffer
* used for accumulation.
*/
template <
typename outT = std::int32_t,
typename inT = std::int32_t,
typename nextOPType = DoNothing<outT, outT>>
class FBGEMM_API memCopy {
public:
using outType = outT;
using inpType = inT;
explicit memCopy(nextOPType& nextop) : nextop_(nextop) {}