-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconv.cpp
More file actions
267 lines (231 loc) · 12.8 KB
/
Copy pathconv.cpp
File metadata and controls
267 lines (231 loc) · 12.8 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
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
#include <iostream>
#include <vector>
#include <cmath>
#include "tensor.h"
namespace tensor {
Tensor *conv1d(Tensor *input, Tensor *kernel, Tensor *bias, int stride, int padding) {
std::vector<int> inputShape = input->getShape();
std::vector<int> kernelShape = kernel->getShape();
std::vector<int> biasShape = bias->getShape();
if (inputShape.size() != 3 || kernelShape.size() != 3 || biasShape.size() != 1) {
throw std::invalid_argument("Invalid input shape");
}
int batchSize = inputShape[0];
int inputChannels = inputShape[1];
int inputWidth = inputShape[2];
int kernelChannels = kernelShape[0];
int outputChannels = kernelShape[1];
int kernelWidth = kernelShape[2];
if (inputChannels != kernelChannels) {
throw std::invalid_argument("Input and kernel channels do not match");
}
if (biasShape[0] != outputChannels) {
throw std::invalid_argument("Bias shape does not match output channels");
}
int paddedWidth = inputWidth + 2 * padding;
Tensor *paddedInput = Tensor::zeros({batchSize, inputChannels, paddedWidth});
for (int b = 0; b < batchSize; b++) {
for (int c = 0; c < inputChannels; c++) {
for (int w = 0; w < inputWidth; w++) {
paddedInput->data[b * inputChannels * paddedWidth + c * paddedWidth + (w + padding)] =
input->data[b * inputChannels * inputWidth + c * inputWidth + w];
}
}
}
int outputWidth = (paddedWidth - kernelWidth) / stride + 1;
std::vector<int> outputShape = {batchSize, outputChannels, outputWidth};
Tensor *result = Tensor::zeros(outputShape);
for (int b = 0; b < batchSize; b++) {
for (int oc = 0; oc < outputChannels; oc++) {
for (int ow = 0; ow < outputWidth; ow++) {
float sum = 0.0f;
for (int ic = 0; ic < inputChannels; ic++) {
for (int kw = 0; kw < kernelWidth; kw++) {
int inputIndex = b * inputChannels * paddedWidth + ic * paddedWidth + (ow * stride + kw);
int kernelIndex = ic * outputChannels * kernelWidth + oc * kernelWidth + kw;
sum += paddedInput->data[inputIndex] * kernel->data[kernelIndex];
}
}
result->data[b * outputChannels * outputWidth + oc * outputWidth + ow] = sum + bias->data[oc];
}
}
}
if (result->isGradNode()) {
result->addToPrevious(input);
result->addToPrevious(kernel);
result->addToPrevious(bias);
result->gradFn = [result, input, kernel, bias, stride, padding, inputShape, kernelShape, biasShape, outputWidth, paddedInput]() mutable {
Tensor *gradInput = Tensor::zeros(inputShape);
Tensor *gradKernel = Tensor::zeros(kernelShape);
Tensor *gradBias = Tensor::zeros(biasShape);
int batchSize = inputShape[0];
int inputChannels = inputShape[1];
int inputWidth = inputShape[2];
int outputChannels = kernelShape[1];
int kernelWidth = kernelShape[2];
int paddedWidth = inputWidth + 2 * padding;
Tensor *paddedGradInput = Tensor::zeros({batchSize, inputChannels, paddedWidth});
for (int b = 0; b < batchSize; b++) {
for (int oc = 0; oc < outputChannels; oc++) {
for (int ow = 0; ow < outputWidth; ow++) {
float gradOutput = result->grad[b * outputChannels * outputWidth + oc * outputWidth + ow];
for (int ic = 0; ic < inputChannels; ic++) {
for (int kw = 0; kw < kernelWidth; kw++) {
int inputIndex = b * inputChannels * paddedWidth + ic * paddedWidth + (ow * stride + kw);
int kernelIndex = ic * outputChannels * kernelWidth + oc * kernelWidth + kw;
paddedGradInput->data[inputIndex] += kernel->data[kernelIndex] * gradOutput;
gradKernel->data[kernelIndex] += paddedInput->data[inputIndex] * gradOutput;
}
}
gradBias->data[oc] += gradOutput;
}
}
}
for (int b = 0; b < batchSize; b++) {
for (int c = 0; c < inputChannels; c++) {
for (int w = 0; w < inputWidth; w++) {
gradInput->data[b * inputChannels * inputWidth + c * inputWidth + w] =
paddedGradInput->data[b * inputChannels * paddedWidth + c * paddedWidth + (w + padding)];
}
}
}
if (input->isGradNode()) {
for (int i = 0; i < batchSize * inputChannels * inputWidth; i++) {
input->grad[i] += gradInput->data[i];
}
} else {
input->grad = gradInput->data;
}
if (kernel->isGradNode()) {
for (int i = 0; i < inputChannels * outputChannels * kernelWidth; i++) {
kernel->grad[i] += gradKernel->data[i];
}
} else {
kernel->grad = gradKernel->data;
}
if (bias->isGradNode()) {
for (int i = 0; i < outputChannels; i++) {
bias->grad[i] += gradBias->data[i];
}
} else {
bias->grad = gradBias->data;
}
};
}
return result;
}
Tensor* conv2d(Tensor* input, Tensor* kernel, Tensor* bias, int stride_H, int stride_W, int padding_H, int padding_W)
{
std::vector<int> inputShape = input->getShape();
std::vector<int> kernelShape = kernel->getShape();
std::vector<int> biasShape = bias->getShape();
if (inputShape.size() != 4 || kernelShape.size() != 4 || biasShape.size() != 1) {
throw std::invalid_argument("Invalid input shape");
}
int batchSize = inputShape[0];
int inputChannels = inputShape[1];
int inputHeight = inputShape[2];
int inputWidth = inputShape[3];
int kernelChannels = kernelShape[0];
int outputChannels = kernelShape[1];
int kernelHeight = kernelShape[2];
int kernelWidth = kernelShape[3];
if (inputChannels != kernelChannels) {
throw std::invalid_argument("Input and kernel channels do not match");
}
if (biasShape[0] != outputChannels) {
throw std::invalid_argument("Bias shape does not match output channels");
}
int paddedHeight = inputHeight + 2 * padding_H;
int paddedWidth = inputWidth + 2 * padding_W;
Tensor *paddedInput = Tensor::zeros({batchSize, inputChannels, paddedHeight, paddedWidth});
for (int b = 0; b < batchSize; b++) {
for (int c = 0; c < inputChannels; c++) {
for (int h = 0; h < inputHeight; h++) {
for (int w = 0; w < inputWidth; w++) {
paddedInput->data[b * inputChannels * paddedHeight * paddedWidth + c * paddedHeight * paddedWidth + (h + padding_H) * paddedWidth + (w + padding_W)] =
input->data[b * inputChannels * inputHeight * inputWidth + c * inputHeight * inputWidth + h * inputWidth + w];
}
}
}
}
int outputHeight = (paddedHeight - kernelHeight) / stride_H + 1;
int outputWidth = (paddedWidth - kernelWidth) / stride_W + 1;
std::vector<int> outputShape = {batchSize, outputChannels, outputHeight, outputWidth};
Tensor *result = new Tensor(outputShape);
for(int batch =0 ; batch < batchSize; batch++)
{
for(int channel =0 ; channel < inputChannels; channel++)
{
for(int outputChannel =0 ; outputChannel < outputChannels; outputChannel++)
{
for(int outputH =0 ; outputH < outputHeight; outputH++)
{
for(int outputW =0 ; outputW < outputWidth; outputW++)
{
float sum = 0.0f;
for(int kernelH =0 ; kernelH < kernelHeight; kernelH+=stride_H)
{
for(int kernelW =0 ; kernelW < kernelWidth; kernelW+=stride_W)
{
int inputIndex = batch * input->getStrides()[0] + channel * paddedHeight * paddedWidth + (outputH * stride_H + kernelH) * paddedWidth + (outputW * stride_W + kernelW);
int kernelIndex = channel * outputChannels * kernelHeight * kernelWidth + outputChannel * kernelHeight * kernelWidth + kernelH * kernelWidth + kernelW;
sum += paddedInput->data[inputIndex] * kernel->data[kernelIndex];
}
}
result->data[batch * outputChannels * outputHeight * outputWidth + outputChannel * outputHeight * outputWidth + outputH * outputWidth + outputW] = sum + bias->data[outputChannel];
}
}
}
}
}
if(result->isGradNode())
{
result->addToPrevious(input);
result->addToPrevious(kernel);
result->addToPrevious(bias);
result->gradFn = [result, input, kernel, bias, stride_H, stride_W, padding_H, padding_W, inputShape, kernelShape, biasShape, outputHeight, outputWidth, paddedInput]() mutable {
Tensor* gradInput = Tensor::zeros(inputShape);
Tensor* gradKernel = Tensor::zeros(kernelShape);
Tensor* gradBias = Tensor::zeros(biasShape);
int batchSize = inputShape[0];
int inputChannels = inputShape[1];
int inputHeight = inputShape[2];
int inputWidth = inputShape[3];
int outputChannels = kernelShape[1];
int kernelHeight = kernelShape[2];
int kernelWidth = kernelShape[3];
int paddedHeight = inputHeight + 2 * padding_H;
int paddedWidth = inputWidth + 2 * padding_W;
Tensor* paddedGradInput = Tensor::zeros({batchSize, inputChannels, paddedHeight, paddedWidth});
for(int batch =0 ; batch < batchSize; batch++)
{
for(int outputChannel =0 ; outputChannel < outputChannels; outputChannel++)
{
for(int outputH =0 ; outputH < outputHeight; outputH++)
{
for(int outputW =0 ; outputW < outputWidth; outputW++)
{
float gradOutput = result->grad[batch * outputChannels * outputHeight * outputWidth + outputChannel * outputHeight * outputWidth + outputH * outputWidth + outputW];
for(int channel =0 ; channel < inputChannels; channel++)
{
for(int kernelH =0 ; kernelH < kernelHeight; kernelH+=stride_H)
{
for(int kernelW =0 ; kernelW < kernelWidth; kernelW+=stride_W)
{
int inputIndex = batch * inputChannels * paddedHeight * paddedWidth + channel * paddedHeight * paddedWidth + (outputH * stride_H + kernelH) * paddedWidth + (outputW * stride_W + kernelW);
int kernelIndex = channel * outputChannels * kernelHeight * kernelWidth + outputChannel * kernelHeight * kernelWidth + kernelH * kernelWidth + kernelW;
paddedGradInput->data[inputIndex] += kernel->data[kernelIndex] * gradOutput;
gradKernel->data[kernelIndex] += paddedInput->data[inputIndex] * gradOutput;
}
}
}
}
}
}
}
};
}
return result;
}
};