Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
torch-spline-conv
Commits
850bb170
"megatron/git@developer.sourcefind.cn:OpenDAS/megatron-lm.git" did not exist on "fb3a13458da1f26872cb992fba76a6e211ed8322"
Commit
850bb170
authored
Mar 07, 2018
by
Jan Eric Lenssen
Browse files
tests working for bp_to_adj and attentionpooling
parent
2de0f11f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
6 deletions
+56
-6
spline_conv_gpu.py
spline_conv_gpu.py
+56
-6
No files found.
spline_conv_gpu.py
View file @
850bb170
...
@@ -324,6 +324,9 @@ const long* kernel_size, const long* is_open_spline, int num_threads) {
...
@@ -324,6 +324,9 @@ const long* kernel_size, const long* is_open_spline, int num_threads) {
}
}
'''
'''
# This is the efficient version which uses amount but may divide by 0 and
# may be numerically unstable.
# No solution for this yet, use the less efficient version 2.
_spline_kernel_linear_backward
=
kernel_loop
+
'''
_spline_kernel_linear_backward
=
kernel_loop
+
'''
extern "C"
extern "C"
__global__ void spline_kernel(
__global__ void spline_kernel(
...
@@ -342,6 +345,7 @@ int num_threads) {
...
@@ -342,6 +345,7 @@ int num_threads) {
${Dtype} grad_out = 0.0;
${Dtype} grad_out = 0.0;
int quotient = (int)pow(2.0,(double)d_idx);
int quotient = (int)pow(2.0,(double)d_idx);
value = input[e_idx * ${dim} + d_idx];
value = input[e_idx * ${dim} + d_idx];
value *= kernel_size[d_idx] - is_open_spline[d_idx];
value *= kernel_size[d_idx] - is_open_spline[d_idx];
frac = value - floor(value);
frac = value - floor(value);
...
@@ -349,31 +353,77 @@ int num_threads) {
...
@@ -349,31 +353,77 @@ int num_threads) {
for (int k_idx = 0; k_idx < ${k_max}; k_idx++) {
for (int k_idx = 0; k_idx < ${k_max}; k_idx++) {
k_idx_mod = (k_idx/quotient) % 2;
k_idx_mod = (k_idx/quotient) % 2;
${Dtype} residual = (1 - k_idx_mod) * (frac - 1) + k_idx_mod * frac;
int a_idx = e_idx*${k_max} + k_idx;
int a_idx = e_idx*${k_max} + k_idx;
${Dtype} residual = - (1 - k_idx_mod) * (1 - frac) + k_idx_mod * frac;
grad_out += grad_amount[a_idx]*amount[a_idx]/residual;
grad_out += grad_amount[a_idx]*amount[a_idx]/residual;
}
}
grad_adj[idx] = grad_out*(kernel_size[d_idx] - is_open_spline[d_idx]);
grad_adj[idx] = grad_out*(kernel_size[d_idx] - is_open_spline[d_idx]);
}
}
}
}
/*
/*
${Dtype} a = -(1 - k_idx_mod) + k_idx_mod;
for (int d_it = 0; d_it < ${dim}; d_it++) {
if(d_it!=d_idx)
{
value = input[e_idx * ${dim} + d_it];
value *= kernel_size[d_it] - is_open_spline[d_it];
frac = value - floor(value);
a *= (1 - k_idx_mod) * (1 - frac) + k_idx_mod * frac;
}
}
grad_out += a*grad_amount[a_idx];
*/
'''
# This is the inefficient version with gradient computation without using amount
_spline_kernel_linear_backward2
=
kernel_loop
+
'''
extern "C"
__global__ void spline_kernel(
const ${Dtype}* input, const ${Dtype}* grad_amount, ${Dtype}* amount,
${Dtype}* grad_adj, const long* kernel_size, const long* is_open_spline,
int num_threads) {
CUDA_KERNEL_LOOP(idx, num_threads) {
const int e_idx = idx / ${dim};
int d_idx = idx % ${dim};
int k_idx_mod;
${Dtype} value;
${Dtype} frac;
${Dtype} grad_out = 0.0;
int quotient = (int)pow(2.0,(double)d_idx);
for (int k_idx = 0; k_idx < ${k_max}; k_idx++) {
k_idx_mod = (k_idx/quotient) % 2;
int a_idx = e_idx*${k_max} + k_idx;
${Dtype} a = -(1 - k_idx_mod) + k_idx_mod;
${Dtype} a = -(1 - k_idx_mod) + k_idx_mod;
for (int d_it = 0; d_it < ${dim}; d_it++) {
for (int d_it = 0; d_it < ${dim}; d_it++) {
if(d_it!=d_idx)
if(d_it!=d_idx)
{
{
int quotient = (int)pow(2.0,(double)d_it);
k_idx_mod = (k_idx/quotient) % 2;
value = input[e_idx * ${dim} + d_it];
value = input[e_idx * ${dim} + d_it];
value *= kernel_size[d_it] - is_open_spline[d_it];
value *= kernel_size[d_it] - is_open_spline[d_it];
frac = value - floor(value);
frac = value - floor(value);
a *= (1 - k_idx_mod) * (1 - frac) + k_idx_mod * frac;
a *= (1 - k_idx_mod) * (1 - frac) + k_idx_mod * frac;
}
}
}
}
grad_out += a*grad_amount[a_idx];
grad_out += a*grad_amount[a_idx];
*/
}
grad_adj[idx] = grad_out*(kernel_size[d_idx] - is_open_spline[d_idx]);
}
}
'''
'''
...
@@ -403,7 +453,7 @@ def get_basis_backward_kernel(k_max, K, dim, degree, dtype='float'):
...
@@ -403,7 +453,7 @@ def get_basis_backward_kernel(k_max, K, dim, degree, dtype='float'):
elif
degree
==
2
:
elif
degree
==
2
:
raise
NotImplementedError
raise
NotImplementedError
else
:
else
:
_spline_kernel
=
_spline_kernel_linear_backward
_spline_kernel
=
_spline_kernel_linear_backward
2
cuda_tensor
=
torch
.
FloatTensor
([
1
]).
cuda
()
cuda_tensor
=
torch
.
FloatTensor
([
1
]).
cuda
()
with
torch
.
cuda
.
device_of
(
cuda_tensor
):
with
torch
.
cuda
.
device_of
(
cuda_tensor
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment