Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Optimize Transpose_16x16 used in attention (#33354)

Chen Xue6195e87a675
diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp
index 484652e9300..caacc5948c8 100644
--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp
@@ -67,39 +67,21 @@ template <typename T1, typename T2>
CM_INLINE void Transpose_16x16(matrix_ref<T1, 16, 16> in,
matrix_ref<T2, 16, 16> out) {
matrix<T2, 16, 16> bBuf;
- bBuf.row(0) = in.template select<4, 1, 4, 4>(0, 0); // 0,4,8,c
- bBuf.row(1) = in.template select<4, 1, 4, 4>(4, 0); // 0,4,8,c
- bBuf.row(2) = in.template select<4, 1, 4, 4>(8, 0); // 0,4,8,c
- bBuf.row(3) = in.template select<4, 1, 4, 4>(12, 0); // 0,4,8,c
- bBuf.row(4) = in.template select<4, 1, 4, 4>(0, 1); // 1,5,9,d
- bBuf.row(5) = in.template select<4, 1, 4, 4>(4, 1); // 1,5,9,d
- bBuf.row(6) = in.template select<4, 1, 4, 4>(8, 1); // 1,5,9,d
- bBuf.row(7) = in.template select<4, 1, 4, 4>(12, 1); // 1,5,9,d
- bBuf.row(8) = in.template select<4, 1, 4, 4>(0, 2); // 2,6,a,e
- bBuf.row(9) = in.template select<4, 1, 4, 4>(4, 2); // 2,6,a,e
- bBuf.row(10) = in.template select<4, 1, 4, 4>(8, 2); // 2,6,a,e
- bBuf.row(11) = in.template select<4, 1, 4, 4>(12, 2); // 2,6,a,e
- bBuf.row(12) = in.template select<4, 1, 4, 4>(0, 3); // 3,7,b,f
- bBuf.row(13) = in.template select<4, 1, 4, 4>(4, 3); // 3,7,b,f
- bBuf.row(14) = in.template select<4, 1, 4, 4>(8, 3); // 3,7,b,f
- bBuf.row(15) = in.template select<4, 1, 4, 4>(12, 3); // 3,7,b,f
-
- out.row(0) = bBuf.template select<4, 1, 4, 4>(0, 0); // 0
- out.row(1) = bBuf.template select<4, 1, 4, 4>(4, 0); // 1
- out.row(2) = bBuf.template select<4, 1, 4, 4>(8, 0); // 2
- out.row(3) = bBuf.template select<4, 1, 4, 4>(12, 0); // 3
- out.row(4) = bBuf.template select<4, 1, 4, 4>(0, 1); // 4
- out.row(5) = bBuf.template select<4, 1, 4, 4>(4, 1); // 5
- out.row(6) = bBuf.template select<4, 1, 4, 4>(8, 1); // 6
- out.row(7) = bBuf.template select<4, 1, 4, 4>(12, 1); // 7
- out.row(8) = bBuf.template select<4, 1, 4, 4>(0, 2); // 8
- out.row(9) = bBuf.template select<4, 1, 4, 4>(4, 2); // 9
- out.row(10) = bBuf.template select<4, 1, 4, 4>(8, 2); // a
- out.row(11) = bBuf.template select<4, 1, 4, 4>(12, 2); // b
- out.row(12) = bBuf.template select<4, 1, 4, 4>(0, 3); // c
- out.row(13) = bBuf.template select<4, 1, 4, 4>(4, 3); // d
- out.row(14) = bBuf.template select<4, 1, 4, 4>(8, 3); // e
- out.row(15) = bBuf.template select<4, 1, 4, 4>(12, 3); // f
+ #pragma unroll
+ for (int k = 0; k < 16; k++) {
+ bBuf.row(k) = in.template select<2, 1, 8, 2>((k * 2) % 16, k / 8);
+ }
+ #pragma unroll
+ for (int k = 0; k < 16; k++) {
+ out.row(k) = bBuf.template select<2, 1, 8, 2>((k * 2) % 16, k / 8);
+ }
+ for (int k = 0; k < 16; k++) {
+ bBuf.row(k) = out.template select<2, 1, 8, 2>((k * 2) % 16, k / 8);
+ }
+ #pragma unroll
+ for (int k = 0; k < 16; k++) {
+ out.row(k) = bBuf.template select<2, 1, 8, 2>((k * 2) % 16, k / 8);
+ }
}
template <typename T1, typename T2>