Skip to content

Commit

Permalink
[Fix] Fix unittest of ncnn. (open-mmlab#309)
Browse files Browse the repository at this point in the history
* fix test_pytorch_functions

* fix test_mmocr_models
  • Loading branch information
hanrui1sensetime committed Dec 21, 2021
1 parent c1ed41c commit 33bde90
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
8 changes: 5 additions & 3 deletions tests/test_codebase/test_mmocr/test_mmocr_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_bidirectionallstm(backend: Backend):
wrapped_model=wrapped_model,
model_inputs=rewrite_inputs,
deploy_cfg=deploy_cfg,
run_with_backend=False)
run_with_backend=True)
if is_backend_output:
model_output = model_outputs.cpu().numpy()
rewrite_output = rewrite_outputs[0].cpu().numpy()
Expand Down Expand Up @@ -200,7 +200,8 @@ def test_simple_test_of_single_stage_text_detector(backend: Backend):
rewrite_outputs, is_backend_output = get_rewrite_outputs(
wrapped_model=wrapped_model,
model_inputs=rewrite_inputs,
deploy_cfg=deploy_cfg)
deploy_cfg=deploy_cfg,
run_with_backend=True)

if is_backend_output:
rewrite_outputs = rewrite_outputs[0]
Expand Down Expand Up @@ -254,7 +255,8 @@ def test_crnndecoder(backend: Backend, rnn_flag: bool):
wrapped_model=wrapped_model,
model_inputs=rewrite_inputs,
deploy_cfg=deploy_cfg,
run_with_backend=False)
run_with_backend=True)
rewrite_outputs = [rewrite_outputs[-1]]
if is_backend_output:
for model_output, rewrite_output in zip(model_outputs,
rewrite_outputs):
Expand Down
29 changes: 14 additions & 15 deletions tests/test_pytorch/test_pytorch_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def test_get_attribute():
def model_func(tensor):
x = tensor.size()
assert isinstance(x[0], int) and not isinstance(x[0], torch.Tensor)
return x[0] * tensor
return torch.tensor(x)

input = torch.zeros([1, 2, 3, 4])
wrapped_func = WrapFunction(model_func)
rewrite_outputs, _ = get_rewrite_outputs(
wrapped_func,
model_inputs={'tensor': input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)
run_with_backend=True)

assert rewrite_outputs is not None, 'Got unexpected rewrite '
'outputs: {}'.format(rewrite_outputs)
Expand All @@ -71,9 +71,9 @@ def group_norm_caller(input):
wrapped_func,
model_inputs={'input': input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)
run_with_backend=True)

assert np.allclose(model_output, rewrite_output, rtol=1e-03, atol=1e-05)
assert np.allclose(model_output, rewrite_output[0], rtol=1e-03, atol=1e-05)


@backend_checker(Backend.NCNN)
Expand All @@ -89,9 +89,9 @@ def interpolate_caller(*arg, **kwargs):
wrapped_func,
model_inputs={'input': input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)
run_with_backend=True)

assert np.allclose(model_output, rewrite_output, rtol=1e-03, atol=1e-05)
assert np.allclose(model_output, rewrite_output[0], rtol=1e-03, atol=1e-05)


@backend_checker(Backend.NCNN)
Expand All @@ -109,9 +109,9 @@ def linear_caller(*arg, **kwargs):
wrapped_func,
model_inputs={'input': input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)
run_with_backend=True)

assert np.allclose(model_output, rewrite_output, rtol=1e-03, atol=1e-05)
assert np.allclose(model_output, rewrite_output[0], rtol=1e-03, atol=1e-05)


@backend_checker(Backend.TENSORRT)
Expand All @@ -127,10 +127,10 @@ def model_func(input):

deploy_cfg = get_trt_config(['output'], [1])

rewrite_output, is_backend_ouptut = get_rewrite_outputs(
rewrite_output, is_backend_output = get_rewrite_outputs(
wrapped_func, model_inputs={'input': input}, deploy_cfg=deploy_cfg)

if is_backend_ouptut:
if is_backend_output:
rewrite_output = rewrite_output[0].detach().cpu()

assert np.allclose(
Expand All @@ -145,15 +145,15 @@ def test_size_of_tensor_static():
def model_func(input):
x = torch.Tensor.size(input)
assert isinstance(x[0], int) and not isinstance(x[0], torch.Tensor)
return x[0] * input
return torch.tensor(x)

input = torch.zeros([1, 2, 3, 4])
wrapped_func = WrapFunction(model_func)
rewrite_outputs, _ = get_rewrite_outputs(
wrapped_func,
model_inputs={'input': input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)
run_with_backend=True)

assert rewrite_outputs is not None, 'Got unexpected rewrite '
'outputs: {}'.format(rewrite_outputs)
Expand Down Expand Up @@ -181,9 +181,8 @@ def model_func(input):
wrapped_func,
model_inputs={'input': TestTopk.input},
deploy_cfg=deploy_cfg_ncnn,
run_with_backend=False)

assert np.allclose(model_output, output[1], rtol=1e-03, atol=1e-05)
run_with_backend=True)
assert np.allclose(model_output, output[0], rtol=1e-03, atol=1e-05)

@backend_checker(Backend.TENSORRT)
@pytest.mark.parametrize('k', [1, 3, 4])
Expand Down

0 comments on commit 33bde90

Please sign in to comment.