forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIMDFunctions.h
165 lines (139 loc) · 4.28 KB
/
SIMDFunctions.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <stddef.h>
#include <stdint.h>
namespace paddle {
namespace simd {
namespace naive {
template <typename Type>
inline void addTo(Type* a, const Type* b, size_t len) {
for (size_t i = 0; i < len; ++i) {
a[i] += b[i];
}
}
template <typename Type>
inline void batchAddTo(Type* a, const Type* b[], int batch, size_t len) {
for (int i = 0; i < batch; ++i) {
for (size_t j = 0; j < len; ++j) {
a[j] += b[i][j];
}
}
}
/**
* @note this method is unused in paddle.
*/
template <typename Type>
inline void colMax(Type* result, const Type* data, int dim, int numSamples) {
for (int d = 0; d < dim; ++d) {
Type sm = data[d];
for (int i = 1; i < numSamples; ++i) {
sm = sm > data[i * dim + d] ? sm : data[i * dim + d];
}
result[d] = sm;
}
}
template <typename Type>
inline void decayL1(Type* dst, Type* src, Type* lr, Type lambda, size_t len) {
for (size_t i = 0; i < len; ++i) {
Type& src_val = src[i];
float nlambda = lr[i] * lambda;
if (src_val > 0) {
dst[i] = ((src_val > nlambda) ? (src_val - nlambda) : 0);
} else {
dst[i] = ((-src_val > nlambda) ? (src_val + nlambda) : 0);
}
}
}
template <class Type>
inline void decayL1(Type* dst, Type* src, Type lambda, size_t len) {
for (size_t i = 0; i < len; ++i) {
Type& src_val = src[i];
if (src_val > 0) {
dst[i] = ((src_val > lambda) ? (src_val - lambda) : 0);
} else {
dst[i] = ((-src_val > lambda) ? (src_val + lambda) : 0);
}
}
}
} // namespace naive
template <typename Type>
inline void addTo(Type* a, const Type* b, size_t len) {
naive::addTo(a, b, len);
}
template <typename Type>
inline void batchAddTo(Type* a, const Type* b[], int batch, size_t len) {
naive::batchAddTo(a, b, batch, len);
}
template <typename Type>
inline void colMax(Type* result, const Type* data, int dim, int numSamples) {
naive::colMax(result, data, dim, numSamples);
}
template <typename Type>
inline void decayL1(Type* dst, Type* src, Type* lr, Type lambda, size_t len) {
naive::decayL1(dst, src, lr, lambda, len);
}
template <typename Type>
inline void decayL1(Type* dst, Type* src, Type lambda, size_t len) {
naive::decayL1(dst, src, lambda, len);
}
template <size_t AlignSize>
inline bool isPointerAlign(void* ptr) {
return reinterpret_cast<uintptr_t>(ptr) % AlignSize == 0;
}
inline bool vec_check(size_t len) {
#ifdef __AVX__
return len % 8 == 0;
#else
return len % 4 == 0;
#endif
}
namespace internal {
void addToImpl(float* a, const float* b, size_t len);
void batchAddToImpl(float* a, const float* b[], int batch, size_t len);
void colMaxImpl(float* result, const float* data, int dim, int numSamples);
#ifdef __AVX__
void decayL1AvxImpl(float* dst, float* src, float lambda, size_t len);
void decayL1AvxImpl(
float* dst, float* src, float* lr, float lambda, size_t len);
#endif
} // namespace internal
template <>
inline void addTo(float* a, const float* b, size_t len) {
internal::addToImpl(a, b, len);
}
template <>
inline void batchAddTo(float* a, const float* b[], int batch, size_t len) {
internal::batchAddToImpl(a, b, batch, len);
}
template <>
inline void colMax(float* result, const float* data, int dim, int numSamples) {
internal::colMaxImpl(result, data, dim, numSamples);
}
template <>
inline void decayL1(float* dst, float* src, float lambda, size_t len) {
#ifdef __AVX__
internal::decayL1AvxImpl(dst, src, lambda, len);
#else
naive::decayL1(dst, src, lambda, len);
#endif
}
template <>
inline void decayL1(
float* dst, float* src, float* lr, float lambda, size_t len) {
#ifdef __AVX__
internal::decayL1AvxImpl(dst, src, lr, lambda, len);
#else
naive::decayL1(dst, src, lr, lambda, len);
#endif
}
} // namespace simd
} // namespace paddle