Repository navigation
Expand file tree
/
Copy pathCudaNetwork.cpp
More file actions
373 lines (312 loc) · 14.3 KB
/
Copy pathCudaNetwork.cpp
File metadata and controls
373 lines (312 loc) · 14.3 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
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
#include "CudaNetwork.hpp"
#include <iostream>
#include <memory>
#include <vector>
namespace nn {
namespace cuda {
namespace {
constexpr uint32_t c_checkpointMagic = 0x43434B50; // 'CCKP'
constexpr uint32_t c_checkpointVersion = 1;
struct CheckpointHeader
{
uint32_t magic;
uint32_t version;
uint32_t numLayers;
uint32_t padding;
uint64_t numTrainingVectorsPassed;
};
// Repeated per layer, so a checkpoint written for a different architecture is rejected instead of
// being read as garbage.
struct CheckpointLayerHeader
{
uint32_t inputSize;
uint32_t outputSize;
uint32_t numVariants;
uint32_t padding;
uint64_t adamStep;
};
} // namespace
CudaNeuralNetwork::CudaNeuralNetwork()
{
CUDA_CHECK(cudaEventCreateWithFlags(&m_ftGradConsumedEvent, cudaEventDisableTiming));
CUDA_CHECK(cudaEventCreateWithFlags(&m_ftGradClearedEvent, cudaEventDisableTiming));
CUDA_CHECK(cudaEventCreateWithFlags(&m_trainConsumedEvent, cudaEventDisableTiming));
CUDA_CHECK(cudaEventCreateWithFlags(&m_copyDoneEvent, cudaEventDisableTiming));
// Unlike the synchronization-only events above, these must keep timing enabled.
CUDA_CHECK(cudaEventCreate(&m_iterationStartEvent));
CUDA_CHECK(cudaEventCreate(&m_iterationEndEvent));
// Pre-record so the first iteration's cross-stream waits are already satisfied.
CUDA_CHECK(cudaEventRecord(m_ftGradConsumedEvent, m_stream.Get()));
CUDA_CHECK(cudaEventRecord(m_ftGradClearedEvent, m_auxStream.Get()));
CUDA_CHECK(cudaEventRecord(m_trainConsumedEvent, m_stream.Get()));
CUDA_CHECK(cudaEventRecord(m_copyDoneEvent, m_copyStream.Get()));
}
CudaNeuralNetwork::~CudaNeuralNetwork()
{
cudaEventDestroy(m_ftGradConsumedEvent);
cudaEventDestroy(m_ftGradClearedEvent);
cudaEventDestroy(m_trainConsumedEvent);
cudaEventDestroy(m_copyDoneEvent);
cudaEventDestroy(m_iterationStartEvent);
cudaEventDestroy(m_iterationEndEvent);
}
void CudaNeuralNetwork::BeginIterationTiming()
{
CUDA_CHECK(cudaEventRecord(m_iterationStartEvent, m_stream.Get()));
}
float CudaNeuralNetwork::EndIterationTimingMs()
{
CUDA_CHECK(cudaEventRecord(m_iterationEndEvent, m_stream.Get()));
CUDA_CHECK(cudaEventSynchronize(m_iterationEndEvent));
float elapsedMs = 0.0f;
CUDA_CHECK(cudaEventElapsedTime(&elapsedMs, m_iterationStartEvent, m_iterationEndEvent));
return elapsedMs;
}
void CudaNeuralNetwork::InitRandomWeights(uint32_t seed)
{
// scaled so that the accumulator of a ~32 feature position lands in the CReLU range
m_featureTransformerWeights->Init(seed, 0.1f);
#if USE_FACTORIZER
// the factorizer starts at zero, so the effective weights are just the bucket weights
CUDA_CHECK(cudaMemset(
m_featureTransformerWeights->m_weights.Get() + c_numNetworkInputs * c_accumulatorSize,
0, FactorizerInputs * c_accumulatorSize * sizeof(float)));
#endif // USE_FACTORIZER
InitRandomOutputSubnetWeights(seed);
}
void CudaNeuralNetwork::InitRandomOutputSubnetWeights(uint32_t seed)
{
// He initialization, appropriate for the (clipped) ReLU activations of the output subnet
m_l1Weights->Init(seed + 1, sqrtf(2.0f / (float)c_l1InputSize));
m_l2Weights->Init(seed + 2, sqrtf(2.0f / (float)c_l1Size));
m_l3Weights->Init(seed + 3, sqrtf(2.0f / (float)c_l2Size));
}
void CudaNeuralNetwork::Init(
const nn::WeightsStoragePtr& featureTransformerWeights,
const nn::WeightsStoragePtr& l1Weights,
const nn::WeightsStoragePtr& l2Weights,
const nn::WeightsStoragePtr& l3Weights)
{
// Create CUDA weight storages based on host network
m_featureTransformerWeights = std::make_shared<CudaWeightsStorage>(
FeatureTransformerInputs, c_accumulatorSize, 1
);
#if USE_FACTORIZER
m_featureTransformerWeights->m_factorizerFirstWeight = c_numNetworkInputs * c_accumulatorSize;
m_featureTransformerWeights->m_factorizerRange = FactorizerWeightRange;
#endif // USE_FACTORIZER
m_l1Weights = std::make_shared<CudaWeightsStorage>(c_l1InputSize, c_l1Size, c_numVariants);
m_l2Weights = std::make_shared<CudaWeightsStorage>(c_l1Size, c_l2Size, c_numVariants);
m_l3Weights = std::make_shared<CudaWeightsStorage>(c_l2Size, 1, c_numVariants);
CopyWeightsFromHost(featureTransformerWeights, l1Weights, l2Weights, l3Weights);
// Quantization-aware training: weights/biases are fake-quantized on read in the forward/backward
// kernels using the same scales as the final pack step.
m_featureTransformerWeights->m_weightQuantScale = nn::InputLayerWeightQuantizationScale;
m_featureTransformerWeights->m_biasQuantScale = nn::InputLayerBiasQuantizationScale;
m_l1Weights->m_weightQuantScale = nn::HiddenLayerWeightQuantizationScale;
m_l1Weights->m_biasQuantScale = nn::HiddenLayerBiasQuantizationScale;
m_l2Weights->m_weightQuantScale = nn::HiddenLayerWeightQuantizationScale;
m_l2Weights->m_biasQuantScale = nn::HiddenLayerBiasQuantizationScale;
m_l3Weights->m_weightQuantScale = nn::OutputLayerWeightQuantizationScale;
m_l3Weights->m_biasQuantScale = nn::OutputLayerBiasQuantizationScale;
}
void CudaNeuralNetwork::SetWeightDecay(float featureTransformerDecay, float outputSubnetDecay)
{
m_featureTransformerWeights->m_weightDecay = featureTransformerDecay;
m_l1Weights->m_weightDecay = outputSubnetDecay;
m_l2Weights->m_weightDecay = outputSubnetDecay;
m_l3Weights->m_weightDecay = outputSubnetDecay;
}
void CudaNeuralNetwork::SetAdamBetas(float beta1, float beta2)
{
for (CudaWeightsStorage* weights : { m_featureTransformerWeights.get(), m_l1Weights.get(), m_l2Weights.get(), m_l3Weights.get() })
{
weights->m_beta1 = beta1;
weights->m_beta2 = beta2;
}
}
void CudaNeuralNetwork::SetFeatureTransformerFrozen(bool frozen)
{
m_featureTransformerWeights->m_updateWeights = !frozen;
}
void CudaNeuralNetwork::CopyWeightsFromHost(
const nn::WeightsStoragePtr& featureTransformerWeights,
const nn::WeightsStoragePtr& l1Weights,
const nn::WeightsStoragePtr& l2Weights,
const nn::WeightsStoragePtr& l3Weights)
{
m_featureTransformerWeights->CopyFromHost(*featureTransformerWeights);
m_l1Weights->CopyFromHost(*l1Weights);
m_l2Weights->CopyFromHost(*l2Weights);
m_l3Weights->CopyFromHost(*l3Weights);
}
bool CudaNeuralNetwork::SaveCheckpoint(const char* path, uint64_t numTrainingVectorsPassed) const
{
const CudaWeightsStoragePtr layers[] = { m_featureTransformerWeights, m_l1Weights, m_l2Weights, m_l3Weights };
FILE* file = fopen(path, "wb");
if (!file)
{
std::cerr << "Failed to save checkpoint: cannot open " << path << std::endl;
return false;
}
CheckpointHeader header{};
header.magic = c_checkpointMagic;
header.version = c_checkpointVersion;
header.numLayers = (uint32_t)std::size(layers);
header.numTrainingVectorsPassed = numTrainingVectorsPassed;
bool ok = fwrite(&header, sizeof(header), 1, file) == 1;
std::vector<float> weights, moment1, moment2;
for (const CudaWeightsStoragePtr& layer : layers)
{
if (!ok) break;
CheckpointLayerHeader layerHeader{};
layerHeader.inputSize = layer->m_inputSize;
layerHeader.outputSize = layer->m_outputSize;
layerHeader.numVariants = layer->m_numVariants;
layerHeader.adamStep = layer->m_adamStep;
layer->CopyStateToHost(weights, moment1, moment2);
ok = fwrite(&layerHeader, sizeof(layerHeader), 1, file) == 1
&& fwrite(weights.data(), sizeof(float), weights.size(), file) == weights.size()
&& fwrite(moment1.data(), sizeof(float), moment1.size(), file) == moment1.size()
&& fwrite(moment2.data(), sizeof(float), moment2.size(), file) == moment2.size();
}
fclose(file);
if (!ok)
std::cerr << "Failed to save checkpoint: cannot write " << path << std::endl;
return ok;
}
bool CudaNeuralNetwork::LoadCheckpoint(const char* path, uint64_t& outNumTrainingVectorsPassed)
{
const CudaWeightsStoragePtr layers[] = { m_featureTransformerWeights, m_l1Weights, m_l2Weights, m_l3Weights };
FILE* file = fopen(path, "rb");
if (!file)
{
std::cerr << "Failed to load checkpoint: cannot open " << path << std::endl;
return false;
}
CheckpointHeader header{};
if (fread(&header, sizeof(header), 1, file) != 1 ||
header.magic != c_checkpointMagic ||
header.version != c_checkpointVersion ||
header.numLayers != (uint32_t)std::size(layers))
{
fclose(file);
std::cerr << "Failed to load checkpoint: " << path << " is not a compatible checkpoint" << std::endl;
return false;
}
std::vector<float> weights, moment1, moment2;
for (const CudaWeightsStoragePtr& layer : layers)
{
CheckpointLayerHeader layerHeader{};
if (fread(&layerHeader, sizeof(layerHeader), 1, file) != 1 ||
layerHeader.inputSize != layer->m_inputSize ||
layerHeader.outputSize != layer->m_outputSize ||
layerHeader.numVariants != layer->m_numVariants)
{
fclose(file);
std::cerr << "Failed to load checkpoint: " << path << " was written for a different architecture" << std::endl;
return false;
}
weights.resize(layer->m_totalWeights);
moment1.resize(layer->m_totalWeights);
moment2.resize(layer->m_totalWeights);
const size_t count = layer->m_totalWeights;
if (fread(weights.data(), sizeof(float), count, file) != count ||
fread(moment1.data(), sizeof(float), count, file) != count ||
fread(moment2.data(), sizeof(float), count, file) != count)
{
fclose(file);
std::cerr << "Failed to load checkpoint: " << path << " is truncated" << std::endl;
return false;
}
layer->CopyStateFromHost(weights, moment1, moment2);
layer->m_adamStep = (size_t)layerHeader.adamStep;
}
fclose(file);
outNumTrainingVectorsPassed = header.numTrainingVectorsPassed;
return true;
}
void CudaNeuralNetwork::CopyWeightsToHost(
const nn::WeightsStoragePtr& featureTransformerWeights,
const nn::WeightsStoragePtr& l1Weights,
const nn::WeightsStoragePtr& l2Weights,
const nn::WeightsStoragePtr& l3Weights) const
{
m_featureTransformerWeights->CopyToHost(*featureTransformerWeights);
m_l1Weights->CopyToHost(*l1Weights);
m_l2Weights->CopyToHost(*l2Weights);
m_l3Weights->CopyToHost(*l3Weights);
}
static DeviceLayer GetDeviceLayer(const CudaWeightsStorage& weights)
{
return
{
weights.m_weights.Get(),
{ weights.m_weightQuantScale, weights.m_biasQuantScale, 1.0f / weights.m_weightQuantScale, 1.0f / weights.m_biasQuantScale }
};
}
void CudaNeuralNetwork::Forward(CudaBatchData& batch)
{
const cudaStream_t stream = m_stream.Get();
// Clear the FT gradient buffer on a separate stream so it overlaps the forward pass.
// Wait until the previous iteration's FT Adam finished reading the buffer, clear it, then
// signal completion for the backward FT-gradient accumulation to wait on.
CUDA_CHECK(cudaStreamWaitEvent(m_auxStream.Get(), m_ftGradConsumedEvent, 0));
batch.featureTransformerGradients.ClearAsync(m_auxStream.Get());
CUDA_CHECK(cudaEventRecord(m_ftGradClearedEvent, m_auxStream.Get()));
// The forward pass reads the training vectors; wait for this batch's copy to complete.
CUDA_CHECK(cudaStreamWaitEvent(stream, m_copyDoneEvent, 0));
LaunchSparseBinaryInput(batch, GetDeviceLayer(*m_featureTransformerWeights), stream);
LaunchL1Forward(batch, GetDeviceLayer(*m_l1Weights), stream);
LaunchL2Forward(batch, GetDeviceLayer(*m_l2Weights), stream);
LaunchL3Forward(batch, GetDeviceLayer(*m_l3Weights), stream);
LaunchSigmoidActivation(batch, stream);
}
void CudaNeuralNetwork::Backward(CudaBatchData& batch, float learningRate)
{
const cudaStream_t stream = m_stream.Get();
// The dense gradient kernels accumulate atomically across batch slices
batch.l1Gradients.ClearAsync(stream);
batch.l2Gradients.ClearAsync(stream);
batch.l3Gradients.ClearAsync(stream);
LaunchSigmoidDerivative(batch, stream);
LaunchL3Backward(batch, GetDeviceLayer(*m_l3Weights), stream);
LaunchL2Backward(batch, GetDeviceLayer(*m_l2Weights), stream);
LaunchL1Gradients(batch, stream);
// The L1 -> feature transformer backprop and the FT weight gradients are only needed to update
// the feature transformer, so both are skipped when it is frozen.
if (m_featureTransformerWeights->m_updateWeights)
{
// The buffer was cleared on the aux stream (overlapping the forward pass); wait for that
// clear to complete before accumulating.
CUDA_CHECK(cudaStreamWaitEvent(stream, m_ftGradClearedEvent, 0));
LaunchFeatureTransformerGradients(batch, GetDeviceLayer(*m_l1Weights), stream);
}
// The training-vectors buffer is no longer read after the FT gradient accumulation (the Adam
// updates below don't touch it), so signal that the next batch's copy may overwrite it.
CUDA_CHECK(cudaEventRecord(m_trainConsumedEvent, stream));
// Update output subnet weights
m_l3Weights->UpdateAdam(batch.l3Gradients.Get(), learningRate, stream);
m_l2Weights->UpdateAdam(batch.l2Gradients.Get(), learningRate, stream);
m_l1Weights->UpdateAdam(batch.l1Gradients.Get(), learningRate, stream);
// Update feature transformer weights
m_featureTransformerWeights->UpdateAdam(
batch.featureTransformerGradients.Get(),
learningRate,
stream
);
// Signal that the FT gradient buffer is no longer needed on the main stream, so the next
// iteration's overlapped clear (on the aux stream) may proceed.
CUDA_CHECK(cudaEventRecord(m_ftGradConsumedEvent, stream));
}
void CudaNeuralNetwork::CopyTrainingBatchAsync(CudaBatchData& batch, const TrainingEntry* hostSrc, uint32_t count)
{
// Wait until the previous batch's last reader of the training-vectors buffer
// (FeatureTransformerGradientsKernel) has finished, then copy on the dedicated copy stream so
// it overlaps the previous batch's Adam updates. Forward waits on m_copyDoneEvent before use.
CUDA_CHECK(cudaStreamWaitEvent(m_copyStream.Get(), m_trainConsumedEvent, 0));
batch.trainingVectors.CopyFromHost(hostSrc, count, m_copyStream.Get());
CUDA_CHECK(cudaEventRecord(m_copyDoneEvent, m_copyStream.Get()));
}
} // namespace cuda
} // namespace nn