Elffuss BraidHub

kikocis/ncnn public

master · 3.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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 "dropout_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#endif // __ARM_NEON
20
21namespace ncnn {
22
23Dropout_arm::Dropout_arm()
24{
25#if __ARM_NEON
26 support_packing = true;
27#endif // __ARM_NEON
28}
29
30int Dropout_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
31{
32 if (scale == 1.f)
33 {
34 return 0;
35 }
36
37 int w = bottom_top_blob.w;
38 int h = bottom_top_blob.h;
39 int d = bottom_top_blob.d;
40 int channels = bottom_top_blob.c;
41 int elempack = bottom_top_blob.elempack;
42 int size = w * h * d * elempack;
43
44 #pragma omp parallel for num_threads(opt.num_threads)
45 for (int q = 0; q < channels; q++)
46 {
47 float* ptr = bottom_top_blob.channel(q);
48
49 int i = 0;
50#if __ARM_NEON
51 float32x4_t _scale = vdupq_n_f32(scale);
52 for (; i + 15 < size; i += 16)
53 {
54#if NCNN_GNU_INLINE_ASM
55#if __aarch64__
56 asm volatile(
57 "prfm pldl1keep, [%0, #512] \n"
58 "ld1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0] \n"
59 "fmul v0.4s, v0.4s, %2.4s \n"
60 "fmul v1.4s, v1.4s, %2.4s \n"
61 "fmul v2.4s, v2.4s, %2.4s \n"
62 "fmul v3.4s, v3.4s, %2.4s \n"
63 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
64 : "=r"(ptr) // %0
65 : "0"(ptr),
66 "w"(_scale) // %2
67 : "memory", "v0", "v1", "v2", "v3");
68#else // __aarch64__
69 asm volatile(
70 "pld [%0, #512] \n"
71 "vldm %0, {d0-d7} \n"
72 "vmul.f32 q0, q0, %q2 \n"
73 "vmul.f32 q1, q1, %q2 \n"
74 "vmul.f32 q2, q2, %q2 \n"
75 "vmul.f32 q3, q3, %q2 \n"
76 "vstm %0!, {d0-d7} \n"
77 : "=r"(ptr) // %0
78 : "0"(ptr),
79 "w"(_scale) // %2
80 : "memory", "q0", "q1", "q2", "q3");
81#endif // __aarch64__
82#else // NCNN_GNU_INLINE_ASM
83 float32x4_t _p0 = vld1q_f32(ptr);
84 float32x4_t _p1 = vld1q_f32(ptr + 4);
85 float32x4_t _p2 = vld1q_f32(ptr + 8);
86 float32x4_t _p3 = vld1q_f32(ptr + 12);
87 _p0 = vmulq_f32(_p0, _scale);
88 _p1 = vmulq_f32(_p1, _scale);
89 _p2 = vmulq_f32(_p2, _scale);
90 _p3 = vmulq_f32(_p3, _scale);
91 vst1q_f32(ptr, _p0);
92 vst1q_f32(ptr + 4, _p1);
93 vst1q_f32(ptr + 8, _p2);
94 vst1q_f32(ptr + 12, _p3);
95 ptr += 16;
96#endif // NCNN_GNU_INLINE_ASM
97 }
98 for (; i + 7 < size; i += 8)
99 {
100 float32x4_t _p0 = vld1q_f32(ptr);
101 float32x4_t _p1 = vld1q_f32(ptr + 4);
102 _p0 = vmulq_f32(_p0, _scale);
103 _p1 = vmulq_f32(_p1, _scale);
104 vst1q_f32(ptr, _p0);
105 vst1q_f32(ptr + 4, _p1);
106 ptr += 8;
107 }
108 for (; i + 3 < size; i += 4)
109 {
110 float32x4_t _p = vld1q_f32(ptr);
111 _p = vmulq_f32(_p, _scale);
112 vst1q_f32(ptr, _p);
113 ptr += 4;
114 }
115#endif // __ARM_NEON
116 for (; i < size; i++)
117 {
118 *ptr = *ptr * scale;
119
120 ptr++;
121 }
122 }
123
124 return 0;
125}
126
127} // namespace ncnn