Skip to content

Replace pytest's paremeterization with parameterized #1156

@mthrok

Description

@mthrok

If you are interested working on this, please leave a comment here (so that other people will not do a duplicated work), then create a PR and tag @mthrok.


For historical and technical reasons, torchaudio's test suite cannot use pytest as a module. (we still use it as a runner in CircleCI, which is fine.)

There are couple of pytest utilities used in torchaudio's test code.

@pytest.mark.parametrize('complex_tensor', [
torch.randn(1, 2, 1025, 400, 2),
torch.randn(1025, 400, 2)
])
@pytest.mark.parametrize('power', [1, 2, 0.7])
def test_complex_norm(complex_tensor, power):
expected_norm_tensor = complex_tensor.pow(2).sum(-1).pow(power / 2)
norm_tensor = F.complex_norm(complex_tensor, power)
torch.testing.assert_allclose(norm_tensor, expected_norm_tensor, atol=1e-5, rtol=1e-5)
@pytest.mark.parametrize('specgram', [
torch.randn(2, 1025, 400),
torch.randn(1, 201, 100)
])
@pytest.mark.parametrize('mask_param', [100])
@pytest.mark.parametrize('mask_value', [0., 30.])
@pytest.mark.parametrize('axis', [1, 2])
def test_mask_along_axis(specgram, mask_param, mask_value, axis):
mask_specgram = F.mask_along_axis(specgram, mask_param, mask_value, axis)
other_axis = 1 if axis == 2 else 2
masked_columns = (mask_specgram == mask_value).sum(other_axis)
num_masked_columns = (masked_columns == mask_specgram.size(other_axis)).sum()
num_masked_columns //= mask_specgram.size(0)
assert mask_specgram.size() == specgram.size()
assert num_masked_columns < mask_param
@pytest.mark.parametrize('mask_param', [100])
@pytest.mark.parametrize('mask_value', [0., 30.])
@pytest.mark.parametrize('axis', [2, 3])
def test_mask_along_axis_iid(mask_param, mask_value, axis):
torch.random.manual_seed(42)
specgrams = torch.randn(4, 2, 1025, 400)
mask_specgrams = F.mask_along_axis_iid(specgrams, mask_param, mask_value, axis)
other_axis = 2 if axis == 3 else 3
masked_columns = (mask_specgrams == mask_value).sum(other_axis)
num_masked_columns = (masked_columns == mask_specgrams.size(other_axis)).sum(-1)
assert mask_specgrams.size() == specgrams.size()
assert (num_masked_columns < mask_param).sum() == num_masked_columns.numel()

We would like to replace pytest.mark.parametrize with parameterized module like the following

@parameterized.expand([
('q', 'quarter_sine'),
('h', 'half_sine'),
('t', 'linear'),
])
def test_fade(self, fade_shape_sox, fade_shape):

Steps

  1. Convert the test function into test class using TorchaudioTestCase
  2. In test method, use parameterized.expand
  3. Additionally, when generating a tensor with random values, do it in the method and set the seed value first so that the test is more deterministic.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions