forked from flexflow/flexflow-train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda_helper.cu
74 lines (65 loc) · 1.55 KB
/
cuda_helper.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
#include "cuda_helper.h"
__global__
void scale_kernel(float* ptr, coord_t size, float a, float b)
{
CUDA_KERNEL_LOOP(i, size)
{
ptr[i] = (b - a) * ptr[i] + a;
}
}
__global__
void ones_kernel(float* ptr, coord_t size)
{
CUDA_KERNEL_LOOP(i, size)
{
ptr[i] = 1.0f;
}
}
__global__
void assign_kernel(float* ptr, coord_t size, float value)
{
CUDA_KERNEL_LOOP(i, size)
{
ptr[i] = value;
}
}
__global__
void reluBackward(float *grad_ptr, const float *input, int n)
{
CUDA_KERNEL_LOOP(i, n)
{
grad_ptr[i] = (input[i] > 0.0f) ? grad_ptr[i] : 0;
}
}
__global__
void apply_add(float *data_ptr, const float *replica_ptr, size_t size)
{
CUDA_KERNEL_LOOP(i, size)
{
data_ptr[i] += replica_ptr[i];
}
}
__global__
void apply_add_with_scale(float *data_ptr, const float *grad_ptr,
size_t size, float scale)
{
CUDA_KERNEL_LOOP(i, size)
{
data_ptr[i] += grad_ptr[i] * scale;
}
}
__host__
void updateGAS(float* para_ptr, const float* grad_ptr, size_t replica_size,
int num_replica, float learning_rate)
{
// Step 1: gater gradients to the first replica
for (int i = 1; i < num_replica; i++) {
const float *replica = grad_ptr + i * replica_size;
apply_add<<<GET_BLOCKS(replica_size), CUDA_NUM_THREADS>>>(
(float*)grad_ptr, replica, replica_size);
}
// Step 2: scale the first replica
float scale_factor = 1.0f / num_replica * (-learning_rate);
apply_add_with_scale<<<GET_BLOCKS(replica_size), CUDA_NUM_THREADS>>>(
para_ptr, grad_ptr, replica_size, scale_factor);
}