forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransposeUtils.cc
90 lines (82 loc) · 2 KB
/
TransposeUtils.cc
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
/*
* 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.
*/
#define FBGEMM_EXPORTS
#include "./TransposeUtils.h"
#include <cstring>
#include "fbgemm/Utils.h"
namespace fbgemm {
template <typename T>
void transpose_ref(
unsigned M,
unsigned N,
const T* src,
unsigned ld_src,
T* dst,
unsigned ld_dst) {
for (unsigned j = 0; j < N; j++) {
for (unsigned i = 0; i < M; i++) {
dst[i + j * ld_dst] = src[i * ld_src + j];
}
} // for each output row
}
template <typename T>
void transpose_simd(
unsigned M,
unsigned N,
const T* src,
unsigned ld_src,
T* dst,
unsigned ld_dst) {
if (M == 0 || N == 0) {
return;
}
if ((M == 1 && ld_dst == 1) || (N == 1 && ld_src == 1)) {
if (dst != src) {
// sizeof must be first operand force dims promotion to OS-bitness type
memcpy(dst, src, sizeof(T) * M * N);
}
return;
}
static const auto iset = fbgemmInstructionSet();
// Run time CPU detection
if (isZmm(iset)) {
internal::transpose_avx512<T>(M, N, src, ld_src, dst, ld_dst);
} else if (isYmm(iset)) {
internal::transpose_avx2<T>(M, N, src, ld_src, dst, ld_dst);
} else {
transpose_ref<T>(M, N, src, ld_src, dst, ld_dst);
}
}
template void transpose_ref<float>(
unsigned M,
unsigned N,
const float* src,
unsigned ld_src,
float* dst,
unsigned ld_dst);
template void transpose_ref<uint8_t>(
unsigned M,
unsigned N,
const uint8_t* src,
unsigned ld_src,
uint8_t* dst,
unsigned ld_dst);
template FBGEMM_API void transpose_simd<float>(
unsigned M,
unsigned N,
const float* src,
unsigned ld_src,
float* dst,
unsigned ld_dst);
template FBGEMM_API void transpose_simd<uint8_t>(
unsigned M,
unsigned N,
const uint8_t* src,
unsigned ld_src,
uint8_t* dst,
unsigned ld_dst);
} // namespace fbgemm