Commit 66529500 authored by yan.yan's avatar yan.yan
Browse files

backup code

parent 863080a0
......@@ -49,3 +49,5 @@ else:
BOOST_ROOT = Path(_BOOST_ROOT)
assert BOOST_ROOT.exists(), "you provide BOOST_ROOT, but it not exists"
assert (BOOST_ROOT / "boost" / "geometry").exists(), "you provide BOOST_ROOT, but BOOST_ROOT/boost/geometry not exists"
SPCONV_DEBUG_WEIGHT = False
\ No newline at end of file
......@@ -32,7 +32,7 @@ from spconv.pytorch import ops
from spconv.cppconstants import CPU_ONLY_BUILD
from spconv.pytorch.core import IndiceData, SparseConvTensor, ImplicitGemmIndiceData, expand_nd
from spconv.pytorch.modules import SparseModule
from spconv.constants import SAVED_WEIGHT_LAYOUT, ALL_WEIGHT_IS_KRSC
from spconv.constants import SAVED_WEIGHT_LAYOUT, ALL_WEIGHT_IS_KRSC, SPCONV_DEBUG_WEIGHT
from spconv.utils import nullcontext
from torch.nn.init import calculate_gain
......@@ -222,7 +222,11 @@ class SparseConvolution(SparseModule):
return tensor.uniform_(-bound, bound)
def reset_parameters(self):
self._custom_kaiming_uniform_(self.weight, a=math.sqrt(5))
if SPCONV_DEBUG_WEIGHT:
self._custom_kaiming_uniform_(self.weight, a=math.sqrt(0.005))
else:
self._custom_kaiming_uniform_(self.weight, a=math.sqrt(5))
if self.bias is not None:
fan_in, _ = self._calculate_fan_in_and_fan_out()
bound = 1 / math.sqrt(fan_in)
......
......@@ -210,8 +210,9 @@ class SparseConvTester:
def _test_impgemm_conv_cuda(subm: bool):
ndim = 3
np.random.seed(50005)
dtype_to_tol = {
np.float32: (1e-4, 1e-4),
np.float32: (1e-2, 1e-2),
np.float16: (1e-2, 1e-2),
np.int8: (1e-4, 1e-4),
}
......@@ -220,8 +221,9 @@ def _test_impgemm_conv_cuda(subm: bool):
batchsizes = [1]
dtypes = [np.float32, np.float16]
test_case = TestCase()
in_channels = [32, 47]
out_channels = [32, 48, 62]
in_channels = [512]
out_channels = [512]
multiple_base = 16
if subm:
ksizes = [3]
strides = [1]
......@@ -233,7 +235,7 @@ def _test_impgemm_conv_cuda(subm: bool):
paddings = [0, 1]
dilations = [1, 2]
algos = [
ConvAlgo.MaskSplitImplicitGemm,
# ConvAlgo.MaskSplitImplicitGemm,
ConvAlgo.MaskImplicitGemm,
]
arch = torch.cuda.get_device_capability()
......@@ -243,9 +245,10 @@ def _test_impgemm_conv_cuda(subm: bool):
strides, paddings, dilations, algos, dtypes)):
shape_prod = np.prod(shape)
num_batch = np.random.randint(int(0.2 * shape_prod), int(0.7 * shape_prod))
C = np.random.randint(int(0.3 * C), int(0.7 * C))
K = np.random.randint(int(0.3 * K), int(0.7 * K))
# C = np.random.randint(int(0.3 * C), int(0.7 * C))
# K = np.random.randint(int(0.3 * K), int(0.7 * K))
multipler = max(C, K) / multiple_base
multipler = max(multipler, 1.0)
# print(num_batch)
tester = SparseConvTester(algo, subm, shape, bs, dtype, num_batch, K, C, k, s, p, d)
atol, rtol = dtype_to_tol[dtype]
......@@ -257,6 +260,7 @@ def _test_impgemm_conv_cuda(subm: bool):
for op_type in op_types:
inp_tv, weight_tv, output_tv = tester.get_operands(op_type)
avail_desps = CONV.get_all_available(inp_tv, weight_tv, output_tv, NHWC, NHWC, NHWC, arch, op_type, -1)
print(avail_desps)
for desp in avail_desps:
if not subm:
if op_type == ConvOpType.kForward:
......@@ -370,9 +374,9 @@ def _test_impgemm_conv_cuda(subm: bool):
test_case.assertAllClose(out_ref, out_my, atol=atol, rtol=rtol)
else:
error_norm = np.linalg.norm(out_ref.reshape(-1) - out_my.reshape(-1))
if (error_norm > 5):
print(f"{desp}, Error={error_norm}")
assert error_norm < 10
# if (error_norm > 5):
print(f"{desp}, Error={error_norm}")
assert error_norm < 10 * multipler
# print(desp, )
else:
din_my = inp_tv.cpu().numpy()
......@@ -380,7 +384,7 @@ def _test_impgemm_conv_cuda(subm: bool):
test_case.assertAllClose(din_ref, din_my, atol=atol, rtol=rtol)
else:
error_norm = np.linalg.norm(din_ref.reshape(-1) - din_my.reshape(-1))
assert error_norm < 10, f"{desp}, {error_norm}, {k}, {s}, {p}, {d}"
assert error_norm < 10 * multipler, f"{desp}, {error_norm}, {k}, {s}, {p}, {d}"
inp_tv, weight_tv, output_tv = tester.get_operands(ConvOpType.kBackwardWeight)
for spk in [1, 4, 16, 64]:
......@@ -450,8 +454,8 @@ def _test_impgemm_conv_cuda(subm: bool):
error_norm = np.linalg.norm(dw_ref.reshape(-1) - dw_my.reshape(-1))
# print(desp, error_norm)
if (error_norm > 5):
print(f"{desp}, Error={error_norm}")
assert error_norm < 10
print(f"{desp}, Error={error_norm}, {spk}")
assert error_norm < 10 * multipler
def _test_native_conv_cuda(subm: bool):
ndim = 3
......@@ -477,6 +481,8 @@ def _test_native_conv_cuda(subm: bool):
strides = [1, 2, 3]
paddings = [0, 1]
dilations = [1, 2]
multiple_base = 128
arch = torch.cuda.get_device_capability()
stream = get_current_stream()
for shape, bs, C, K, k, s, p, d, dtype in tqdm.tqdm(params_grid(
......@@ -484,6 +490,8 @@ def _test_native_conv_cuda(subm: bool):
strides, paddings, dilations, dtypes)):
tester = SparseConvTester(ConvAlgo.Native, subm, shape, bs, dtype, 1500, K, C, k, s, p, d)
atol, rtol = dtype_to_tol[dtype]
multipler = max(C, K) / multiple_base
multipler = max(multipler, 1.0)
kv_center = tester.kv // 2
kv = tester.kv
......@@ -556,7 +564,7 @@ def _test_native_conv_cuda(subm: bool):
test_case.assertAllClose(out_ref, out_my, atol=atol, rtol=rtol)
else:
error_norm = np.linalg.norm(out_ref.reshape(-1) - out_my.reshape(-1))
assert error_norm < 10
assert error_norm < 10 * multipler
elif op_type == ConvOpType.kBackwardInput:
a = output_tv
......@@ -607,7 +615,7 @@ def _test_native_conv_cuda(subm: bool):
else:
error_norm = np.linalg.norm(din_ref.reshape(-1) - din_my.reshape(-1))
assert error_norm < 10
assert error_norm < 10 * multipler
else:
a = output_tv
......@@ -651,7 +659,7 @@ def _test_native_conv_cuda(subm: bool):
dw_my = weight_tv.cpu().numpy()
if dtype != np.float16:
error_norm = np.linalg.norm(dw_ref.reshape(-1) - dw_my.reshape(-1))
assert error_norm < 1
assert error_norm < 1 * multipler
# test_case.assertAllClose(dw_ref, dw_my, atol=atol, rtol=rtol)
# print(desp, error_norm)
......@@ -659,15 +667,15 @@ def _test_native_conv_cuda(subm: bool):
else:
error_norm = np.linalg.norm(dw_ref.reshape(-1) - dw_my.reshape(-1))
# print(desp, error_norm)
assert error_norm < 10
assert error_norm < 10 * multipler
def test_all_algo_unit():
# for i in range(5):
_test_impgemm_conv_cuda(True)
_test_impgemm_conv_cuda(False)
_test_native_conv_cuda(True)
_test_native_conv_cuda(False)
# _test_impgemm_conv_cuda(False)
# _test_native_conv_cuda(True)
# _test_native_conv_cuda(False)
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment