forked from DeepLearningKit/DeepLearningKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaders.metal
More file actions
224 lines (186 loc) · 7.86 KB
/
Shaders.metal
File metadata and controls
224 lines (186 loc) · 7.86 KB
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
//
// Shaders.metal
//
// Created by Torb Morland & Amund Tveit on 12/12/15.
// Copyright © 2015 Memkite. All rights reserved.
//
#include <metal_stdlib>
#include <metal_common>
#include <metal_math>
#include <metal_graphics>
#include <metal_matrix>
#include <metal_geometric>
#include <metal_texture>
#include <simd/simd.h>
using namespace metal;
////////////////////////////////
// DATA TYPES
////////////////////////////////
struct MetalComplexNumberType { // needs to map to float2 in Metal
float real;
float imag;
};
struct MetalShaderParameters {
float image_xdim;
float image_ydim;
float num_images;
float filter_xdim; // e.g. 5
float filter_ydim; // e.g. 5
float num_filters; // e.g. 20 -> W = 20*1*5*5
float conv_xdim; // image_xdim - filter_xdim + 1 without striding
float conv_ydim; // image_ydim - filter_ydim + 1 without striding
float pool_xdim;
float pool_ydim;
float b;
};
struct MetalMatrixVectorParameters {
float x_xdim;
float x_ydim;
float w_xdim;
float w_ydim;
float b_xdim;
float b_ydim;
float result_xdim;
float result_ydim;
};
struct MetalPoolingParameters {
float kernel_size;
float pool_stride;
float pad;
};
struct MetalTensorDimensions {
float n;
float channels;
float width;
float height;
};
struct MetalConvolutionParameters {
float pad;
float kernel_size;
float stride;
};
////////////////////////////////
// SHADER FUNCTIONS
////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// Returns max(0, X[id])
kernel void rectifier_linear(device float* X [[ buffer(0)]],
uint id [[ thread_position_in_grid ]]) {
X[id] = fmax(0.0, X[id]);
}
////////////////////////////////////////////////////////////////////////////////////////////
kernel void max_pool(device float* result [[ buffer(0) ]],
const device float* input [[ buffer(1) ]],
const device MetalShaderParameters* size_params [[ buffer(2) ]],
const device MetalPoolingParameters* pooling_params [[ buffer(3) ]],
uint id [[ thread_position_in_grid ]]) {
int channels = int(size_params[0].num_filters);
int in_width = int(size_params[0].image_xdim);
int in_height = int(size_params[0].image_ydim);
float kernel_size = float(pooling_params[0].kernel_size);
int pool_stride = int(pooling_params[0].pool_stride);
int pad = int(pooling_params[0].pad);
int out_width = int(size_params[0].pool_xdim);
int out_height = int(size_params[0].pool_ydim);
int i = (id / out_height) % out_width;
int j = id % out_height;
int n = id / (channels * out_width * out_height);
int c = (id / (out_width * out_height)) % channels;
int wstart = i * pool_stride - pad;
int hstart = j * pool_stride - pad;
int wend = fmin(wstart + kernel_size, in_width + pad);
int hend = fmin(hstart + kernel_size, in_height + pad);
wstart = fmax(0.0, wstart);
hstart = fmax(0.0, hstart);
thread float pool = -100000.0;
for (int ii = wstart; ii < wend; ++ii) {
for (int jj = hstart; jj < hend; ++jj) {
pool = fmax(pool, input[(n * channels * in_height + c * in_height + ii) * in_width + jj]);
}
}
result[id] = pool;
}
////////////////////////////////////////////////////////////////////////////////////////////
kernel void avg_pool(device float* result [[ buffer(0) ]],
const device float* input [[ buffer(1) ]],
const device MetalShaderParameters* size_params [[ buffer(2) ]],
const device MetalPoolingParameters* pooling_params [[ buffer(3) ]],
uint id [[ thread_position_in_grid ]]) {
int channels = int(size_params[0].num_filters);
int in_width = int(size_params[0].image_xdim);
int in_height = int(size_params[0].image_ydim);
float kernel_size = float(pooling_params[0].kernel_size);
int pool_stride = int(pooling_params[0].pool_stride);
int pad = int(pooling_params[0].pad);
int out_width = int(size_params[0].pool_xdim);
int out_height = int(size_params[0].pool_ydim);
int i = (id / out_height) % out_width;
int j = id % out_height;
int n = id / (channels * out_width * out_height);
int c = (id / (out_width * out_height)) % channels;
int wstart = i * pool_stride - pad;
int hstart = j * pool_stride - pad;
int wend = fmin(wstart + kernel_size, in_width + pad);
int hend = fmin(hstart + kernel_size, in_height + pad);
float pool_size = (hend - hstart) * (wend - wstart);
wstart = fmax(0.0, wstart);
hstart = fmax(0.0, hstart);
thread float pool = 0.0;
for (int ii = wstart; ii < wend; ++ii) {
for (int jj = hstart; jj < hend; ++jj) {
pool += input[(n * channels * in_height + c * in_height + ii) * in_width + jj]/pool_size;
}
}
result[id] = pool;
}
////////////////////////////////////////////////////////////////////////////////////////////
kernel void im2col(const device float* convolution_input [[ buffer(0)]],
const device MetalTensorDimensions* tensor_dimensions [[ buffer(1) ]],
const device MetalConvolutionParameters* convolution_params [[ buffer(2) ]],
device float* col_output [[ buffer(3) ]],
uint id [[ thread_position_in_grid ]]) {
int channels_in = int(tensor_dimensions[0].channels);
int in_width = int(tensor_dimensions[0].width);
int in_height = int(tensor_dimensions[0].height);
int channels_col = int(tensor_dimensions[2].channels);
int width_col = int(tensor_dimensions[2].width);
int height_col = int(tensor_dimensions[2].height);
// 1. do an im2col transformation
int pad = int(convolution_params[0].pad);
int kernel_size = int(convolution_params[0].kernel_size);
int n = id / (channels_col * height_col * width_col);
int c = (id / (width_col * height_col)) % channels_col;
int h = (id / width_col) % height_col;
int w = id % width_col;
int w_offset = c % kernel_size;
int h_offset = (c / kernel_size) % kernel_size;
int h_pad = h - pad + h_offset;
int w_pad = w - pad + w_offset;
int c_im = c / (kernel_size * kernel_size);
if (h_pad >= 0 && h_pad < in_height && w_pad >= 0 && w_pad < in_width) {
col_output[id] = convolution_input[(n * channels_in * in_height + c_im * in_height + h_pad) * in_width + w_pad];
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Tensor dimensions = { input_dimensions, weight_dimensions, col_dimensions, result_dimensions }
kernel void convolution_layer(device float* result [[ buffer(0) ]],
const device float* weights [[ buffer(1)]],
const device MetalTensorDimensions* tensor_dimensions [[ buffer(2) ]],
const device float* col_output [[ buffer(3) ]],
const device float* bias [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]]) {
int channels_col = int(tensor_dimensions[2].channels);
int width_col = int(tensor_dimensions[2].width);
int height_col = int(tensor_dimensions[2].height);
int channels_out = int(tensor_dimensions[3].channels);
int width_out = int(tensor_dimensions[3].width);
int height_out = int(tensor_dimensions[3].height);
int n = id / (channels_out * height_out * width_out);
int a = (id / (height_out * width_out)) % channels_out;
int b = id % (height_out * width_out);
thread float tmp_result = bias[a];
for (int c = 0; c < channels_col; ++c) {
tmp_result += weights[a * channels_col + c] * col_output[(n * channels_col * width_col + c * width_col) * height_col + b];
}
result[id] = tmp_result;
}