-
Notifications
You must be signed in to change notification settings - Fork 2
/
TsetlinMachineKernels.cu
256 lines (201 loc) · 7.63 KB
/
TsetlinMachineKernels.cu
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright (c) 2019 Ole-Christoffer Granmo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This code implements a multiclass version of the Tsetlin Machine from paper arXiv:1804.01508
https://arxiv.org/abs/1804.01508
*/
#include "TsetlinMachineConfig.cuh"
#include "TsetlinMachine.cuh"
#include <cuda.h>
#include <curand.h>
#include <curand_kernel.h>
#include <stdio.h>
// Increment the states of each of those 32 Tsetlin Automata flagged in the active bit vector.
__device__ inline void inc(int *ta_state, int clause, int chunk, unsigned int active)
{
unsigned int carry, carry_next;
int id = clause*LA_CHUNKS*STATE_BITS + chunk*STATE_BITS;
carry = active;
for (int b = 0; b < STATE_BITS; ++b) {
if (carry == 0)
break;
carry_next = ta_state[id + b] & carry; // Sets carry bits (overflow) passing on to next bit
ta_state[id + b] = ta_state[id + b] ^ carry; // Performs increments with XOR
carry = carry_next;
}
if (carry > 0) {
for (int b = 0; b < STATE_BITS; ++b) {
ta_state[id + b] |= carry;
}
}
}
// Decrement the states of each of those 32 Tsetlin Automata flagged in the active bit vector.
__device__ inline void dec(int *ta_state, int clause, int chunk, unsigned int active)
{
unsigned int carry, carry_next;
int id = clause*LA_CHUNKS*STATE_BITS + chunk*STATE_BITS;
carry = active;
for (int b = 0; b < STATE_BITS; ++b) {
if (carry == 0)
break;
carry_next = (~ta_state[id + b]) & carry; // Sets carry bits (overflow) passing on to next bit
ta_state[id + b] = ta_state[id + b] ^ carry; // Performs increments with XOR
carry = carry_next;
}
if (carry > 0) {
for (int b = 0; b < STATE_BITS; ++b) {
ta_state[id + b] &= ~carry;
}
}
}
__global__ void type_i_feedback(curandState *state, int *ta_state, int *clause_feedback, int *clause_output, int *Xi, float s)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
/* Copy state to local memory for efficiency */
curandState localState = state[index];
for (int i = index; i < CLAUSES*LA_CHUNKS; i += stride) {
int clause = i / LA_CHUNKS;
if (clause_feedback[clause] != 1) {
continue;
}
int la_chunk = i % LA_CHUNKS;
// Generate random bit values
int la_feedback;
for (int b = 0; b < INT_SIZE; ++b) {
if (curand_uniform(&localState) <= 1.0/S) {
la_feedback |= (1 << b);
} else {
la_feedback &= ~(1 << b);
}
}
if (clause_output[clause]) {
#ifdef BOOST_TRUE_POSITIVE_FEEDBACK
inc(ta_state, clause, la_chunk, Xi[la_chunk]);
#else
inc(ta_state, clause, la_chunk, Xi[la_chunk] & (~la_feedback));
#endif
dec(ta_state, clause, la_chunk, (~Xi[la_chunk]) & la_feedback);
} else {
dec(ta_state, clause, la_chunk, la_feedback);
}
}
state[index] = localState;
}
__global__ void type_ii_feedback(int *ta_state, int *clause_feedback, int *clause_output, int *Xi)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < CLAUSES*LA_CHUNKS; i += stride) {
int clause = i / LA_CHUNKS;
if (clause_feedback[clause] != -1 || clause_output[clause] == 0) {
continue;
}
int la_chunk = i % LA_CHUNKS;
int id = clause*LA_CHUNKS*STATE_BITS + la_chunk*STATE_BITS + STATE_BITS - 1;
inc(ta_state, clause, la_chunk, (~Xi[la_chunk]) & (~ta_state[id]));
}
}
__global__ void generate_clause_feedback(curandState *state, int *clause_feedback, int class_sum, int target)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
/* Copy state to local memory for efficiency */
curandState localState = state[index];
for (int j = index; j < CLAUSES; j += stride) {
int sign = 1 - 2 * (j & 1);
if (target) {
if (curand_uniform(&localState) > (1.0/(THRESHOLD*2))*(THRESHOLD - class_sum)) {
clause_feedback[j] = 0;
} else {
clause_feedback[j] = sign;
}
} else {
if (curand_uniform(&localState) > (1.0/(THRESHOLD*2))*(THRESHOLD + class_sum)) {
clause_feedback[j] = 0;
} else {
clause_feedback[j] = -1*sign;
}
}
}
state[index] = localState;
}
__global__ void initialize_clause_output(int *clause_output)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
// Initialize clause output
for (int j = index; j < CLAUSES; j += stride) {
clause_output[j] = 1;
}
}
__global__ void calculate_clause_output(int *ta_state, int *clause_output, int *Xi)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < CLAUSES*LA_CHUNKS; i += stride) {
int clause = i / LA_CHUNKS;
int la_chunk = i % LA_CHUNKS;
int id = clause*LA_CHUNKS*STATE_BITS + la_chunk*STATE_BITS + STATE_BITS - 1;
if (la_chunk < LA_CHUNKS-1 && ((ta_state[id] & Xi[la_chunk]) != ta_state[id])) {
clause_output[clause] = 0;
} else if (la_chunk == LA_CHUNKS-1 && ((ta_state[id] & Xi[LA_CHUNKS-1] & FILTER) != (ta_state[id] & FILTER))) {
clause_output[clause] = 0;
}
}
}
__global__ void initialize_clause_output_predict(int *clause_output, int *all_exclude)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
// Initialize clause output
for (int j = index; j < CLAUSES; j += stride) {
clause_output[j] = 1;
all_exclude[j] = 1;
}
}
__global__ void calculate_clause_output_predict(int *ta_state, int *clause_output, int *all_exclude, int *Xi)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < CLAUSES*LA_CHUNKS; i += stride) {
int clause = i / LA_CHUNKS;
int la_chunk = i % LA_CHUNKS;
int id = clause*LA_CHUNKS*STATE_BITS + la_chunk*STATE_BITS + STATE_BITS - 1;
if ((la_chunk < LA_CHUNKS - 1) && ((ta_state[id] & Xi[la_chunk]) != ta_state[id])) {
clause_output[clause] = 0;
} else if ((la_chunk == LA_CHUNKS - 1) && ((ta_state[id] & Xi[LA_CHUNKS-1] & FILTER) != (ta_state[id] & FILTER))) {
clause_output[clause] = 0;
}
if ((la_chunk < LA_CHUNKS - 1) && ((ta_state[id] & Xi[la_chunk]) > 0)) {
all_exclude[clause] = 0;
} else if ((la_chunk == LA_CHUNKS - 1) && ((ta_state[id] & Xi[LA_CHUNKS-1] & FILTER) > 0)) {
all_exclude[clause] = 0;
}
}
}
__global__ void update_with_all_exclude(int *clause_output, int *all_exclude)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
// Initialize clause output
for (int j = index; j < CLAUSES; j += stride) {
if (all_exclude[j] == 1) {
clause_output[j] = 0;
}
}
}