-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimdMixer.hpp
159 lines (147 loc) · 4.79 KB
/
SimdMixer.hpp
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
#ifndef PAQ8GEN_SIMDMIXER_HPP
#define PAQ8GEN_SIMDMIXER_HPP
#include "UpdateBroadcaster.hpp"
#include "Ilog.hpp"
#include "Mixer.hpp"
#include "Squash.hpp"
template<SIMD simd>
class SIMDMixer : public Mixer {
private:
SIMDMixer *mp; /**< points to a Mixer to combine results */
/**
* Define padding requirements.
*/
[[nodiscard]] constexpr inline auto simdWidth() const -> int {
if( simd == SIMD_AVX2 ) {
return 32 / sizeof(short); // 256 bit (32 byte) data size
}
else if( simd == SIMD_SSE2 || simd == SIMD_SSSE3 || simd == SIMD_NEON ) {
return 16 / sizeof(short); // 128 bit (16 byte) data size
}
else if( simd == SIMD_NONE ) {
return 4 / sizeof(short); // Processes 2 shorts at once -> width is 4 bytes
}
assert(false);
}
public:
SIMDMixer(const Shared* const sh, const int n, const int m, const int s) : Mixer(sh, ((n + (simdWidth() - 1)) & -(simdWidth())), m, s) {
assert((this->n & (simdWidth() - 1)) == 0);
assert(this->m > 0);
assert(this->s > 0);
mp = (s > 1) ? new SIMDMixer<simd>(sh, s, 1, 1) : nullptr;
}
~SIMDMixer() override {
delete mp;
}
void setScaleFactor(const int sf0, const int sf1) override {
scaleFactor = sf0;
if( mp ) {
mp->setScaleFactor(sf1, 0);
}
}
void promote(int x) override {
if (mp != nullptr)
mp->add(x);
}
/**
* Adjust weights to minimize coding cost of last prediction.
* Trains the network where the expected output is the last bit (in the shared variable y).
*/
void update() override {
INJECT_SHARED_y
const int target = y << 12;
if( nx > 0 ) {
for( uint64_t i = 0; i < numContexts; ++i ) {
if (cxt[i] != UINT32_MAX) {
const int err = target - pr[i];
int rate = rates[i];
if (mp == nullptr) {
if (rate > MIN_LEARNING_RATE_S1) rate--;
rates[i] = rate;
}
else {
if (rate > MIN_LEARNING_RATE_SN) rate--;
rates[i] = rate;
}
if (simd == SIMD_NONE) {
trainSimdNone(&tx[0], &wx[cxt[i] * n], nx, (err * rate) >> 16);
}
else if (simd == SIMD_SSE2 || simd == SIMD_SSSE3) {
trainSimdSse2(&tx[0], &wx[cxt[i] * n], nx, (err * rate) >> 16);
}
else if (simd == SIMD_AVX2) {
trainSimdAvx2(&tx[0], &wx[cxt[i] * n], nx, (err * rate) >> 16);
}
else if (simd == SIMD_NEON) {
trainSimdNeon(&tx[0], &wx[cxt[i] * n], nx, (err * rate) >> 16);
}
}
}
}
reset();
}
/**
* Predict next bit
* @return prediction
*/
auto p() -> int override {
shared->GetUpdateBroadcaster()->subscribe(this);
assert(scaleFactor > 0);
//if(mp)printf("nx: %d, numContexts: %d, base: %d\n",nx, numContexts, base); //for debugging: how many inputs do we have?
while( nx & (simdWidth() - 1)) {
tx[nx++] = 0; // pad
}
if( mp ) { // combine outputs
for( uint64_t i = 0; i < numContexts; ++i ) {
int dp = 0;
if (cxt[i] != UINT32_MAX) { // valid mixer context (not to skip)
if (simd == SIMD_NONE) {
dp = dotProductSimdNone(&tx[0], &wx[cxt[i] * n], nx);
}
else if (simd == SIMD_SSE2 || simd == SIMD_SSSE3) {
dp = dotProductSimdSse2(&tx[0], &wx[cxt[i] * n], nx);
}
else if (simd == SIMD_AVX2) {
dp = dotProductSimdAvx2(&tx[0], &wx[cxt[i] * n], nx);
}
else if (simd == SIMD_NEON) {
dp = dotProductSimdNeon(&tx[0], &wx[cxt[i] * n], nx);
}
else {
static_assert("Unknown SIMD parameter");
}
dp = (dp * scaleFactor) >> 16;
if (dp < -2047) {
dp = -2047;
}
else if (dp > 2047) {
dp = 2047;
}
}
mp->add(dp);
pr[i] = squash(dp);
}
mp->set(0, 1);
return mp->p();
} // s=1 context
int dp;
if( simd == SIMD_NONE ) {
dp = dotProductSimdNone(&tx[0], &wx[cxt[0] * n], nx);
}
else if( simd == SIMD_SSE2 || simd == SIMD_SSSE3 ) {
dp = dotProductSimdSse2(&tx[0], &wx[cxt[0] * n], nx);
}
else if( simd == SIMD_AVX2 ) {
dp = dotProductSimdAvx2(&tx[0], &wx[cxt[0] * n], nx);
}
else if (simd == SIMD_NEON) {
dp = dotProductSimdNeon(&tx[0], &wx[cxt[0] * n], nx);
}
else {
static_assert("Unknown SIMD parameter");
}
dp = (dp * scaleFactor) >> 16;
return pr[0] = squash(dp);
}
};
#endif //PAQ8GEN_SIMDMIXER_HPP