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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import apex
|
| 4 |
+
if True:
|
| 5 |
+
print("using setup tools")
|
| 6 |
+
import syncbn
|
| 7 |
+
else:
|
| 8 |
+
print("using jit")
|
| 9 |
+
from torch.utils.cpp_extension import load
|
| 10 |
+
syncbn = load(name='syncbn', sources=['../../csrc/syncbn.cpp', '../../csrc/welford.cu'])
|
| 11 |
+
|
| 12 |
+
def compare(desc, inp1, inp2, error):
|
| 13 |
+
a = inp1.clone().detach().cpu().numpy()
|
| 14 |
+
b = inp2.clone().detach().cpu().numpy()
|
| 15 |
+
close = np.allclose(a,b, error, error)
|
| 16 |
+
if not close:
|
| 17 |
+
print(desc, close)
|
| 18 |
+
z = a - b
|
| 19 |
+
index = (np.abs(z) >= error + error * np.abs(b)).nonzero()
|
| 20 |
+
print("dif : ", z[index])
|
| 21 |
+
print("inp1 : ", a[index])
|
| 22 |
+
print("inp2 : ", b[index])
|
| 23 |
+
return close
|
| 24 |
+
|
| 25 |
+
feature_size = 10
|
| 26 |
+
space_size = 16
|
| 27 |
+
batch_size = 5
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
error = 1e-5
|
| 31 |
+
|
| 32 |
+
np.random.seed(1)
|
| 33 |
+
dtype = np.float32
|
| 34 |
+
inp = (np.random.randn(batch_size, feature_size, space_size, space_size)).astype(dtype)
|
| 35 |
+
grad = (np.random.randn(batch_size, feature_size, space_size, space_size)).astype(dtype)
|
| 36 |
+
weight = (np.random.randn(feature_size)).astype(dtype)
|
| 37 |
+
bias = (np.random.randn(feature_size)).astype(dtype)
|
| 38 |
+
count = torch.cuda.IntTensor([batch_size*space_size**2])
|
| 39 |
+
|
| 40 |
+
type_tensor = torch.cuda.FloatTensor
|
| 41 |
+
ref_tensor = torch.cuda.DoubleTensor
|
| 42 |
+
|
| 43 |
+
inp_t = type_tensor(inp)
|
| 44 |
+
weight_t = type_tensor(weight)
|
| 45 |
+
bias_t = type_tensor(bias)
|
| 46 |
+
|
| 47 |
+
inp_r = ref_tensor(inp.transpose(1, 0, 2, 3).reshape(feature_size, -1))
|
| 48 |
+
inp2_r = ref_tensor(inp)
|
| 49 |
+
weight_r = ref_tensor(weight).view(-1, 1, 1)
|
| 50 |
+
bias_r = ref_tensor(bias).view(-1, 1, 1)
|
| 51 |
+
|
| 52 |
+
grad_output_t = type_tensor(grad)
|
| 53 |
+
|
| 54 |
+
m = inp_r.mean(1)
|
| 55 |
+
b_v = inp_r.var(1, unbiased=False)
|
| 56 |
+
unb_v = inp_r.var(1, unbiased=True)
|
| 57 |
+
|
| 58 |
+
eps = 1e-5
|
| 59 |
+
|
| 60 |
+
#mean, var, var_biased = syncbn.welford_mean_var(inp_t)
|
| 61 |
+
mean, var_biased = syncbn.welford_mean_var(inp_t)
|
| 62 |
+
inv_std = 1.0 / torch.sqrt(var_biased + eps)
|
| 63 |
+
|
| 64 |
+
bn = torch.nn.BatchNorm2d(feature_size).cuda()
|
| 65 |
+
bn.momentum = 1.0
|
| 66 |
+
bn.weight.data = weight_t.clone()
|
| 67 |
+
bn.bias.data = bias_t.clone()
|
| 68 |
+
inp_bn = inp_t.clone().requires_grad_()
|
| 69 |
+
grad_bn = grad_output_t.clone().detach()
|
| 70 |
+
out_bn = bn(inp_bn)
|
| 71 |
+
out_bn.backward(grad_bn)
|
| 72 |
+
|
| 73 |
+
sbn = apex.parallel.SyncBatchNorm(feature_size).cuda()
|
| 74 |
+
sbn.momentum = 1.0
|
| 75 |
+
sbn.weight.data = weight_t.clone()
|
| 76 |
+
sbn.bias.data = bias_t.clone()
|
| 77 |
+
inp_sbn = inp_t.clone().requires_grad_()
|
| 78 |
+
grad_sbn = grad_output_t.clone().detach()
|
| 79 |
+
out_sbn = sbn(inp_sbn)
|
| 80 |
+
out_sbn.backward(grad_sbn)
|
| 81 |
+
|
| 82 |
+
sbn_c_last = apex.parallel.SyncBatchNorm(feature_size, channel_last=True).cuda()
|
| 83 |
+
sbn_c_last.momentum = 1.0
|
| 84 |
+
sbn_c_last.weight.data = weight_t.clone()
|
| 85 |
+
sbn_c_last.bias.data = bias_t.clone()
|
| 86 |
+
inp_sbn_c_last = inp_t.clone().transpose(-1, 1).contiguous().requires_grad_()
|
| 87 |
+
grad_sbn_c_last = grad_output_t.clone().transpose(-1, 1).contiguous().detach()
|
| 88 |
+
out_sbn_c_last = sbn_c_last(inp_sbn_c_last)
|
| 89 |
+
out_sbn_c_last.backward(grad_sbn_c_last)
|
| 90 |
+
|
| 91 |
+
sbn_result = True
|
| 92 |
+
sbn_result_c_last = True
|
| 93 |
+
bn_result = True
|
| 94 |
+
|
| 95 |
+
sbn_result = compare("comparing mean: ", mean, m, error) and sbn_result
|
| 96 |
+
#sbn_result = compare("comparing variance: ", var, unb_v, error) and sbn_result
|
| 97 |
+
sbn_result = compare("comparing biased variance: ", var_biased, b_v, error) and sbn_result
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
out = syncbn.batchnorm_forward(inp_t, mean, inv_std, weight_t, bias_t)
|
| 101 |
+
out_r = weight_r * (inp2_r - m.view(-1, 1, 1)) * torch.rsqrt(b_v.view(-1,1,1) + eps) + bias_r
|
| 102 |
+
|
| 103 |
+
sbn_result = compare("comparing output: ", out, out_r, error) and sbn_result
|
| 104 |
+
compare("comparing bn output: ", out_bn, out_r, error)
|
| 105 |
+
|
| 106 |
+
grad_output_t = type_tensor(grad)
|
| 107 |
+
|
| 108 |
+
grad_output_r = ref_tensor(grad.transpose(1, 0, 2, 3).reshape(feature_size, -1))
|
| 109 |
+
grad_output2_r = ref_tensor(grad)
|
| 110 |
+
|
| 111 |
+
grad_bias_r = grad_output_r.sum(1)
|
| 112 |
+
grad_weight_r = ((inp2_r - m.view(-1, 1, 1)) * torch.rsqrt(b_v.view(-1,1,1) + eps) * grad_output2_r).transpose(1,0).contiguous().view(feature_size, -1).sum(1)
|
| 113 |
+
|
| 114 |
+
sum_dy_r = grad_output_r.sum(1)
|
| 115 |
+
mean_dy_r = grad_output_r.mean(1)
|
| 116 |
+
sum_dy_xmu_r = ((inp2_r - m.view(-1, 1, 1)) * grad_output2_r).transpose(1,0).contiguous().view(feature_size, -1).sum(1)
|
| 117 |
+
mean_dy_xmu_r = ((inp2_r - m.view(-1, 1, 1)) * grad_output2_r).transpose(1,0).contiguous().view(feature_size, -1).mean(1)
|
| 118 |
+
|
| 119 |
+
grad_input_r = (grad_output2_r - mean_dy_r.view(-1, 1, 1) - (inp2_r - m.view(-1, 1, 1)) / (b_v.view(-1,1,1) + eps) * mean_dy_xmu_r.view(-1, 1, 1) ) * torch.rsqrt(b_v.view(-1,1,1) + eps) * weight_r.view(-1,1,1)
|
| 120 |
+
|
| 121 |
+
sum_dy, sum_dy_xmu, grad_weight, grad_bias = syncbn.reduce_bn(grad_output_t, inp_t, mean, inv_std, weight_t)
|
| 122 |
+
grad_input = syncbn.batchnorm_backward(grad_output_t, inp_t, mean, inv_std, weight_t, sum_dy, sum_dy_xmu, count)
|
| 123 |
+
sbn_result = compare("comparing bias grad: ", grad_bias, grad_bias_r, error) and sbn_result
|
| 124 |
+
sbn_result = compare("comparing weight grad: ", grad_weight, grad_weight_r, error) and sbn_result
|
| 125 |
+
sbn_result = compare("comparing sum_dy grad: ", sum_dy, sum_dy_r, error) and sbn_result
|
| 126 |
+
sbn_result = compare("comparing sum_dy_xmu grad: ", sum_dy_xmu, sum_dy_xmu_r, error) and sbn_result
|
| 127 |
+
sbn_result = compare("comparing input grad: ", grad_input, grad_input_r, error) and sbn_result
|
| 128 |
+
compare("comparing bn input grad: ", inp_bn.grad, grad_input_r, error)
|
| 129 |
+
sbn_result = compare("comparing sbn input grad: ", inp_sbn.grad, grad_input_r, error) and sbn_result
|
| 130 |
+
|
| 131 |
+
compare("comparing bn/sbn output: ", out_bn, out_sbn, error)
|
| 132 |
+
sbn_result = compare("comparing running_mean: ", bn.running_mean.data, sbn.running_mean.data, error) and sbn_result
|
| 133 |
+
sbn_result = compare("comparing running_variance: ", bn.running_var.data, sbn.running_var.data, error) and sbn_result
|
| 134 |
+
compare("comparing grad_input: ", inp_bn.grad, inp_sbn.grad, error)
|
| 135 |
+
compare("comparing grad_bias: ", bn.bias.grad, sbn.bias.grad, error)
|
| 136 |
+
compare("comparing grad_bias bn to ref: ", bn.bias.grad, grad_bias_r, error)
|
| 137 |
+
sbn_result = compare("comparing grad_bias sbn to ref: ", sbn.bias.grad, grad_bias_r, error) and sbn_result
|
| 138 |
+
compare("comparing grad_weight: ", bn.weight.grad, sbn.weight.grad, error)
|
| 139 |
+
compare("comparing grad_weight bn to ref: ", bn.weight.grad, grad_weight_r, error)
|
| 140 |
+
sbn_result = compare("comparing grad_weight sbn to ref: ", sbn.weight.grad, grad_weight_r, error) and sbn_result
|
| 141 |
+
|
| 142 |
+
compare("comparing channel last bn/sbn output: ", out_bn, out_sbn_c_last.transpose(-1, 1).contiguous(), error)
|
| 143 |
+
sbn_result_c_last = compare("comparing channel last running_mean: ", bn.running_mean.data, sbn_c_last.running_mean.data, error) and sbn_result_c_last
|
| 144 |
+
sbn_result_c_last = compare("comparing channel last running_variance: ", bn.running_var.data, sbn_c_last.running_var.data, error) and sbn_result_c_last
|
| 145 |
+
compare("comparing channel last grad_input: ", inp_bn.grad, inp_sbn_c_last.grad.transpose(-1, 1).contiguous(), error)
|
| 146 |
+
compare("comparing channel last grad_bias: ", bn.bias.grad, sbn_c_last.bias.grad, error)
|
| 147 |
+
sbn_result_c_last = compare("comparing channel last grad_bias sbn to ref: ", sbn_c_last.bias.grad, grad_bias_r, error) and sbn_result_c_last
|
| 148 |
+
compare("comparing channel last grad_weight: ", bn.weight.grad, sbn_c_last.weight.grad, error)
|
| 149 |
+
sbn_result_c_last = compare("comparing channel last grad_weight sbn to ref: ", sbn_c_last.weight.grad, grad_weight_r, error) and sbn_result_c_last
|
| 150 |
+
|
| 151 |
+
if sbn_result:
|
| 152 |
+
print("====SBN single gpu passed tests")
|
| 153 |
+
else:
|
| 154 |
+
print("*SBN single gpu failed*")
|
| 155 |
+
|
| 156 |
+
if sbn_result_c_last:
|
| 157 |
+
print("====SBN channel last single gpu passed tests")
|
| 158 |
+
else:
|
| 159 |
+
print("*SBN channel last single gpu failed*")
|