-
Notifications
You must be signed in to change notification settings - Fork 87
/
MetalNeuralNetworkShaders.metal
381 lines (317 loc) · 14.3 KB
/
MetalNeuralNetworkShaders.metal
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// MetalNeuralNetworkShaders.metal
// AIToolbox
//
// Created by Kevin Coble on 1/7/16.
// Copyright © 2016 Kevin Coble. All rights reserved.
//
// This is duplicated as a string in the .swift version of the file
// This file is left as a way to test the compile of the shaders at build time
#include <metal_stdlib>
using namespace metal;
kernel void sumForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Fet the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// No activation function, copy sum (z) to activation (σ)
activationOutVector[id] = sum;
}
kernel void sigmoidForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Fet the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// This calculates sigmoid for the node
activationOutVector[id] = 1.0 / (1.0 + exp(-sum));
}
kernel void tanhForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Fet the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// This calculates hyperbolic tangent for the node
activationOutVector[id] = tanh(sum);
}
kernel void rectLinearForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Fet the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// This calculates rectified linear value for the node
activationOutVector[id] = sum;
if (sum < 0.0) activationOutVector[id] = 0.0;
}
kernel void softSignForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Fet the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// This calculates soft sign for the node
activationOutVector[id] = sum / (1.0 + abs(sum));
}
kernel void softMaxForward(const device float *inMatrix [[ buffer(0) ]],
const device float *inInputs [[ buffer(1)]],
device float *sumOutVector [[ buffer(2) ]],
device float *activationOutVector [[ buffer(3) ]],
device int *sizingArray [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
float sum = 0.0;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
// Get the weighted sum
for (index = 0; index < numWeights; index++) {
sum += inMatrix[weightOffset] * inInputs[index];
weightOffset++;
}
sumOutVector[id] = sum;
// This calculates the exponent for the node (must be summed across all nodes later
activationOutVector[id] = exp(sum);
}
kernel void sumFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
outDelta[id] = 2.0 * (inSigma[id] - inExpected[id]);
}
kernel void tanhFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
outDelta[id] = 2.0 * (inSigma[id] - inExpected[id]) * (1.0 - inSigma[id] * inSigma[id]);
}
kernel void sigmoidFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
outDelta[id] = 2.0 * (inSigma[id] - inExpected[id]) * (inSigma[id] - inSigma[id] * inSigma[id]);
}
kernel void sigmoidCrossEntropyFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
outDelta[id] = (inSigma[id] - inExpected[id]);
}
kernel void rectLinearFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
if (inSigma[id] < 0.0) {
outDelta[id] = 0.0;
}
else {
outDelta[id] = 2.0 * (inSigma[id] - inExpected[id]);
}
}
kernel void softSignFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
float result;
result = (1.0 - abs(inSigma[id]));
result *= result;
result *= 2.0 * (inSigma[id] - inExpected[id]);
outDelta[id] = result;
}
kernel void softMaxFinal(const device float *inSigma [[ buffer(0) ]],
const device float *inExpected [[ buffer(1)]],
device float *outDelta [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]])
{
outDelta[id] = (inSigma[id] - inExpected[id]);
}
void layerDelta(uint id, int numNodesThisLayer, int numNodesNextLayer,
const device float *nextWeights, const device float *nextDelta, device float *outDelta);
kernel void sumDelta(const device float *nextLayerWeights [[ buffer(0) ]],
const device float *nextLayerDelta [[ buffer(1) ]],
const device int *sizingArray [[ buffer(2) ]],
const device float *inSigma [[ buffer(3) ]],
device float *outDelta [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
// Extract the sizes from the sizing array
int numNodesNextLayer = sizingArray[0];
int numNodesThisLayer = sizingArray[1];
// Calculate the delta for this layer
layerDelta(id, numNodesThisLayer, numNodesNextLayer, nextLayerWeights, nextLayerDelta, outDelta);
// Multiply the delta by the non-linearity - but there is none for sum
}
kernel void tanhDelta(const device float *nextLayerWeights [[ buffer(0) ]],
const device float *nextLayerDelta [[ buffer(1) ]],
const device int *sizingArray [[ buffer(2) ]],
const device float *inSigma [[ buffer(3) ]],
device float *outDelta [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
// Extract the sizes from the sizing array
int numNodesNextLayer = sizingArray[0];
int numNodesThisLayer = sizingArray[1];
// Calculate the delta for this layer
layerDelta(id, numNodesThisLayer, numNodesNextLayer, nextLayerWeights, nextLayerDelta, outDelta);
// Multiply the delta by the non-linearity
outDelta[id] *= (1 - inSigma[id] * inSigma[id]);
}
kernel void sigmoidDelta(const device float *nextLayerWeights [[ buffer(0) ]],
const device float *nextLayerDelta [[ buffer(1) ]],
const device int *sizingArray [[ buffer(2) ]],
const device float *inSigma [[ buffer(3) ]],
device float *outDelta [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
// Extract the sizes from the sizing array
int numNodesNextLayer = sizingArray[0];
int numNodesThisLayer = sizingArray[1];
// Calculate the delta for this layer
layerDelta(id, numNodesThisLayer, numNodesNextLayer, nextLayerWeights, nextLayerDelta, outDelta);
// Multiply the delta by the non-linearity
outDelta[id] *= (inSigma[id] - inSigma[id] * inSigma[id]);
}
kernel void rectLinearDelta(const device float *nextLayerWeights [[ buffer(0) ]],
const device float *nextLayerDelta [[ buffer(1) ]],
const device int *sizingArray [[ buffer(2) ]],
const device float *inSigma [[ buffer(3) ]],
device float *outDelta [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
// Extract the sizes from the sizing array
int numNodesNextLayer = sizingArray[0];
int numNodesThisLayer = sizingArray[1];
// Calculate the delta for this layer
layerDelta(id, numNodesThisLayer, numNodesNextLayer, nextLayerWeights, nextLayerDelta, outDelta);
// Multiply the delta by the non-linearity
outDelta[id] = inSigma[id] < 0.0 ? 0.0 : outDelta[id];
}
kernel void softSignDelta(const device float *nextLayerWeights [[ buffer(0) ]],
const device float *nextLayerDelta [[ buffer(1) ]],
const device int *sizingArray [[ buffer(2) ]],
const device float *inSigma [[ buffer(3) ]],
device float *outDelta [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
// Extract the sizes from the sizing array
int numNodesNextLayer = sizingArray[0];
int numNodesThisLayer = sizingArray[1];
// Calculate the delta for this layer
layerDelta(id, numNodesThisLayer, numNodesNextLayer, nextLayerWeights, nextLayerDelta, outDelta);
// Multiply the delta by the non-linearity
if (inSigma[id] < 0) outDelta[id] *= -1.0;
outDelta[id] /= (1.0 + inSigma[id]) * (1.0 + inSigma[id]);
}
void layerDelta(uint thisLayerNode, int numNodesThisLayer, int numNodesNextLayer,
const device float *nextWeights, const device float *nextDelta, device float *outDelta)
{
int nextLayerNode;
int weightOffset;
// Reset delta
outDelta[thisLayerNode] = 0.0;
// Add each portion from the nodes in the next forward layer
for (nextLayerNode = 0; nextLayerNode < numNodesNextLayer; nextLayerNode++) {
weightOffset = (numNodesThisLayer + 1) * nextLayerNode + thisLayerNode;
outDelta[thisLayerNode] += nextWeights[weightOffset] * nextDelta[nextLayerNode];
}
}
kernel void updateWeights(const device float *inputs [[ buffer(0) ]],
const device float *delta [[ buffer(1) ]],
const device float *parameters [[ buffer(2) ]],
const device int *sizingArray [[ buffer(3) ]],
device float *weights [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]])
{
int index;
// Get the number of weights for each node
int numWeights = sizingArray[1];
// Get the weight offset for this node
int weightOffset = numWeights * id;
float weightDecay = parameters[1];
if (weightDecay < 1.0) {
for (index = 0; index < numWeights; index++) {
weights[weightOffset+index] *= weightDecay;
}
}
// weights = weights + delta * inputs * training rate
float trainingWeight = parameters[0];
for (index = 0; index < numWeights; index++) {
weights[weightOffset] -= delta[id] * inputs[index] * trainingWeight;
weightOffset++;
}
}