-
Notifications
You must be signed in to change notification settings - Fork 1
/
hypercomplex_multiply_op_gpu.cu.cc
162 lines (150 loc) · 5.64 KB
/
hypercomplex_multiply_op_gpu.cu.cc
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
#ifdef GOOGLE_CUDA
#define EIGEN_USE_GPU
#include "tensorflow/core/user_ops/hypercomplex_multiply_op.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/types.h"
namespace tensorflow {
typedef Eigen::GpuDevice GPUDevice;
__device__ bool* quick_conjugate_gpu(
bool* input,
const int length
) {
for (int i = 1; i < length; i++) {
input[i] = !input[i];
} return input;
}
template<typename T>
__device__ T partial_cayley_dickson_gpu(
const T* left,
const T* right,
bool* left_sign,
bool* right_sign,
const int target,
const int length
) {
if (length == 1) {
if (left_sign[0] != right_sign[0]) {
return -left[0] * right[0];
} else {
return left[0] * right[0];
}
} else {
bool is_left = (((float)target) /
((float)(length))) < 0.5f;
if (is_left) {
return partial_cayley_dickson_gpu<T>(
&(left[0]),
&(right[0]),
&(left_sign[0]),
&(right_sign[0]),
target,
(length / 2)
) - partial_cayley_dickson_gpu<T>(
&(right[(length / 2)]),
&(left[(length / 2)]),
quick_conjugate_gpu(
&(right_sign[(length / 2)]),
(length / 2)
),
&(left_sign[(length / 2)]),
target,
(length / 2)
);
} else {
return partial_cayley_dickson_gpu<T>(
&(right[(length / 2)]),
&(left[0]),
&(right_sign[(length / 2)]),
&(left_sign[0]),
(target - (length / 2)),
(length / 2)
) + partial_cayley_dickson_gpu<T>(
&(left[(length / 2)]),
&(right[0]),
&(left_sign[(length / 2)]),
quick_conjugate_gpu(
&(right_sign[0]),
(length / 2)
),
(target - (length / 2)),
(length / 2)
);
}
}
} // __device__ T* partial_cayley_dickson_gpu
template<typename T>
__global__ void HypercomplexMultiplyCudaKernel(
const T* in_tensor_left,
const T* in_tensor_right,
bool* sign_left,
bool* sign_right,
T* out_tensor,
const int hypercomplex_size,
const int remaining_size
) {
for (
int i = blockIdx.x * blockDim.x + threadIdx.x;
i < hypercomplex_size * remaining_size;
i += blockDim.x * gridDim.x
) {
int repositioned_index = (int)(i / hypercomplex_size) * hypercomplex_size;
const T* repositioned_tensor_left = &(in_tensor_left[repositioned_index]);
const T* repositioned_tensor_right = &(in_tensor_right[repositioned_index]);
bool* repositioned_sign_left = &(sign_left[i * hypercomplex_size]);
bool* repositioned_sign_right = &(sign_right[i * hypercomplex_size]);
out_tensor[i] = partial_cayley_dickson_gpu<T>(
repositioned_tensor_left,
repositioned_tensor_right,
repositioned_sign_left,
repositioned_sign_right,
(i % hypercomplex_size),
hypercomplex_size);
}
} // __global__ void HypercomplexMultiplyCudaKernel
namespace functor {
template<typename T>
struct HypercomplexMultiply<GPUDevice, T> {
void operator()(
const GPUDevice& device,
const T* in_tensor_left,
const T* in_tensor_right,
T* out_tensor,
const int hypercomplex_size,
const int remaining_size
) {
size_t memory_size = sizeof(bool) * hypercomplex_size * hypercomplex_size * remaining_size;
bool* sign_left_buffer;
bool* sign_right_buffer;
cudaMalloc(&sign_left_buffer, memory_size);
cudaMalloc(&sign_right_buffer, memory_size);
cudaMemset(sign_left_buffer, 0, memory_size);
cudaMemset(sign_right_buffer, 0, memory_size);
int block_count = 1024;
int thread_per_block = 20;
HypercomplexMultiplyCudaKernel<T><<<
block_count,
thread_per_block,
0,
device.stream()
>>>(
in_tensor_left,
in_tensor_right,
sign_left_buffer,
sign_right_buffer,
out_tensor,
hypercomplex_size,
remaining_size
);
cudaFree(sign_left_buffer);
cudaFree(sign_right_buffer);
}
};
} // namespace functor
template struct functor::HypercomplexMultiply<GPUDevice, uint8>;
template struct functor::HypercomplexMultiply<GPUDevice, int8>;
template struct functor::HypercomplexMultiply<GPUDevice, int16>;
template struct functor::HypercomplexMultiply<GPUDevice, int32>;
template struct functor::HypercomplexMultiply<GPUDevice, float>;
template struct functor::HypercomplexMultiply<GPUDevice, double>;
} // namespace tensorflow
#endif // GOOGLE_CUDA