Elffuss BraidHub

kikocis/ncnn public

master · 4.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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#include "cast_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19#endif // __riscv_vector
20
21namespace ncnn {
22
23Cast_riscv::Cast_riscv()
24{
25#if __riscv_vector
26 support_packing = true;
27#if __riscv_zfh
28 support_fp16_storage = true;
29#endif
30#endif // __riscv_vector
31}
32
33int Cast_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
34{
35 if (type_from == type_to)
36 {
37 top_blob = bottom_blob;
38 return 0;
39 }
40
41 int w = bottom_blob.w;
42 int h = bottom_blob.h;
43 int d = bottom_blob.d;
44 int channels = bottom_blob.c;
45 int dims = bottom_blob.dims;
46 size_t elemsize = bottom_blob.elemsize;
47 int elempack = bottom_blob.elempack;
48
49 size_t out_elemsize = elemsize;
50 if (type_to == 1)
51 {
52 // float32
53 out_elemsize = 4 * elempack;
54 }
55 else if (type_to == 2)
56 {
57 // float16
58 out_elemsize = 2 * elempack;
59 }
60 else if (type_to == 3)
61 {
62 // int8
63 out_elemsize = elempack;
64 }
65 else if (type_to == 4)
66 {
67 // bfloat16
68 out_elemsize = 2 * elempack;
69 }
70
71 if (dims == 1)
72 {
73 top_blob.create(w, out_elemsize, elempack, opt.blob_allocator);
74 }
75 else if (dims == 2)
76 {
77 top_blob.create(w, h, out_elemsize, elempack, opt.blob_allocator);
78 }
79 else if (dims == 3)
80 {
81 top_blob.create(w, h, channels, out_elemsize, elempack, opt.blob_allocator);
82 }
83 else if (dims == 4)
84 {
85 top_blob.create(w, h, d, channels, out_elemsize, elempack, opt.blob_allocator);
86 }
87 if (top_blob.empty())
88 return -100;
89
90 int size = w * h * d * elempack;
91
92#if __riscv_vector && __riscv_zfh
93 if (type_from == 1 && type_to == 2)
94 {
95 #pragma omp parallel for num_threads(opt.num_threads)
96 for (int q = 0; q < channels; q++)
97 {
98 const float* ptr = bottom_blob.channel(q);
99 __fp16* outptr = top_blob.channel(q);
100
101 int n = size;
102 while (n > 0)
103 {
104 size_t vl = vsetvl_e32m8(n);
105
106 vfloat32m8_t _p = vle32_v_f32m8(ptr, vl);
107 vfloat16m4_t _outp = vfncvt_f_f_w_f16m4(_p, vl);
108 vse16_v_f16m4(outptr, _outp, vl);
109
110 ptr += vl;
111 outptr += vl;
112 n -= vl;
113 }
114 }
115 }
116
117 if (type_from == 2 && type_to == 1)
118 {
119 #pragma omp parallel for num_threads(opt.num_threads)
120 for (int q = 0; q < channels; q++)
121 {
122 const __fp16* ptr = bottom_blob.channel(q);
123 float* outptr = top_blob.channel(q);
124
125 int n = size;
126 while (n > 0)
127 {
128 size_t vl = vsetvl_e16m4(n);
129
130 vfloat16m4_t _p = vle16_v_f16m4(ptr, vl);
131 vfloat32m8_t _outp = vfwcvt_f_f_v_f32m8(_p, vl);
132 vse32_v_f32m8(outptr, _outp, vl);
133
134 ptr += vl;
135 outptr += vl;
136 n -= vl;
137 }
138 }
139 }
140#endif // __riscv_vector && __riscv_zfh
141
142 if (type_from == 3 && type_to == 1)
143 {
144 #pragma omp parallel for num_threads(opt.num_threads)
145 for (int q = 0; q < channels; q++)
146 {
147 const signed char* ptr = bottom_blob.channel(q);
148 float* outptr = top_blob.channel(q);
149
150 for (int i = 0; i < size; i++)
151 {
152 outptr[i] = (float)ptr[i];
153 }
154 }
155 }
156
157 // TODO more cast type
158 return Cast::forward(bottom_blob, top_blob, opt);
159}
160
161} // namespace ncnn