Elffuss BraidHub

kikocis/ncnn public

master · 5.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#if NCNN_RUNTIME_CPU && NCNN_AVX512BF16 && __AVX512F__ && !__AVX512BF16__
16void cast_fp32_to_bf16_sse_avx512bf16(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
17void cast_bf16_to_fp32_sse_avx512bf16(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
18#endif
19
20#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVX512BF16__
21void cast_fp32_to_bf16_sse_avx2(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
22void cast_bf16_to_fp32_sse_avx2(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
23#endif
24
25static void cast_fp32_to_bf16_sse(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
26{
27#if NCNN_RUNTIME_CPU && NCNN_AVX512BF16 && __AVX512F__ && !__AVX512BF16__
28 if (ncnn::cpu_support_x86_avx512_bf16())
29 {
30 cast_fp32_to_bf16_sse_avx512bf16(bottom_blob, top_blob, opt);
31 return;
32 }
33#endif
34
35#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVX512BF16__
36 if (ncnn::cpu_support_x86_avx2())
37 {
38 cast_fp32_to_bf16_sse_avx2(bottom_blob, top_blob, opt);
39 return;
40 }
41#endif
42
43 const int w = bottom_blob.w;
44 const int h = bottom_blob.h;
45 const int d = bottom_blob.d;
46 const int channels = bottom_blob.c;
47 const int elempack = bottom_blob.elempack;
48
49 const int size = w * h * d * elempack;
50
51 #pragma omp parallel for num_threads(opt.num_threads)
52 for (int q = 0; q < channels; q++)
53 {
54 const float* ptr = bottom_blob.channel(q);
55 unsigned short* outptr = top_blob.channel(q);
56
57 int i = 0;
58#if __SSE2__
59#if __AVX__
60#if __AVX512F__
61 for (; i + 31 < size; i += 32)
62 {
63 _mm512_storeu_si512((__m512i*)outptr, float2bfloat_avx512(_mm512_loadu_ps(ptr), _mm512_loadu_ps(ptr + 16)));
64 ptr += 32;
65 outptr += 32;
66 }
67#endif // __AVX512F__
68 for (; i + 15 < size; i += 16)
69 {
70#if __AVX512F__
71 _mm256_storeu_si256((__m256i*)outptr, float2bfloat_avx512(_mm512_loadu_ps(ptr)));
72#else
73 _mm256_storeu_si256((__m256i*)outptr, float2bfloat_avx(_mm256_loadu_ps(ptr), _mm256_loadu_ps(ptr + 8)));
74#endif
75 ptr += 16;
76 outptr += 16;
77 }
78#endif // __AVX__
79 for (; i + 7 < size; i += 8)
80 {
81#if __AVX__
82 _mm_store_si128((__m128i*)outptr, float2bfloat_avx(_mm256_loadu_ps(ptr)));
83#else
84 _mm_store_si128((__m128i*)outptr, float2bfloat_sse(_mm_loadu_ps(ptr), _mm_loadu_ps(ptr + 4)));
85#endif
86 ptr += 8;
87 outptr += 8;
88 }
89#endif // __SSE2__
90 for (; i < size; i++)
91 {
92 *outptr++ = float32_to_bfloat16(*ptr++);
93 }
94 }
95}
96
97static void cast_bf16_to_fp32_sse(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
98{
99#if NCNN_AVX512BF16 && __AVX512F__ && !__AVX512BF16__
100 if (ncnn::cpu_support_x86_avx512_bf16())
101 {
102 cast_bf16_to_fp32_sse_avx512bf16(bottom_blob, top_blob, opt);
103 return;
104 }
105#endif
106
107#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVX512BF16__
108 if (ncnn::cpu_support_x86_avx2())
109 {
110 cast_bf16_to_fp32_sse_avx2(bottom_blob, top_blob, opt);
111 return;
112 }
113#endif
114
115 const int w = bottom_blob.w;
116 const int h = bottom_blob.h;
117 const int d = bottom_blob.d;
118 const int channels = bottom_blob.c;
119 const int elempack = bottom_blob.elempack;
120
121 const int size = w * h * d * elempack;
122
123 #pragma omp parallel for num_threads(opt.num_threads)
124 for (int q = 0; q < channels; q++)
125 {
126 const unsigned short* ptr = bottom_blob.channel(q);
127 float* outptr = top_blob.channel(q);
128
129 int i = 0;
130#if __SSE2__
131#if __AVX__
132#if __AVX512F__
133 for (; i + 15 < size; i += 16)
134 {
135 _mm512_storeu_ps(outptr, bfloat2float_avx512(_mm256_loadu_si256((const __m256i*)ptr)));
136 ptr += 16;
137 outptr += 16;
138 }
139#endif // __AVX512F__
140 for (; i + 7 < size; i += 8)
141 {
142 _mm256_storeu_ps(outptr, bfloat2float_avx(_mm_loadu_si128((const __m128i*)ptr)));
143 ptr += 8;
144 outptr += 8;
145 }
146#endif // __AVX__
147 for (; i + 3 < size; i += 4)
148 {
149 _mm_storeu_ps(outptr, bfloat2float_sse(_mm_loadl_epi64((const __m128i*)ptr)));
150 ptr += 4;
151 outptr += 4;
152 }
153#endif // __SSE2__
154 for (; i < size; i++)
155 {
156 *outptr++ = bfloat16_to_float32(*ptr++);
157 }
158 }
159}