Add FX backward tests back

pull/1014/head
Ross Wightman 3 years ago
parent 834a9ec721
commit cd059cbe9c

@ -386,41 +386,41 @@ def test_model_forward_fx(model_name, batch_size):
assert not torch.isnan(outputs).any(), 'Output included NaNs' assert not torch.isnan(outputs).any(), 'Output included NaNs'
if 'GITHUB_ACTIONS' not in os.environ: @pytest.mark.timeout(120)
# FIXME this test is causing GitHub actions to run out of RAM and abruptly kill the test process @pytest.mark.parametrize('model_name', list_models(
exclude_filters=EXCLUDE_FILTERS + EXCLUDE_FX_FILTERS, name_matches_cfg=True))
@pytest.mark.parametrize('batch_size', [2])
def test_model_backward_fx(model_name, batch_size):
"""Symbolically trace each model and run single backward pass through the resulting GraphModule"""
if not has_fx_feature_extraction:
pytest.skip("Can't test FX. Torch >= 1.10 and Torchvision >= 0.11 are required.")
@pytest.mark.timeout(120) input_size = _get_input_size(model_name=model_name, target=TARGET_BWD_FX_SIZE)
@pytest.mark.parametrize('model_name', list_models( if max(input_size) > MAX_BWD_FX_SIZE:
exclude_filters=EXCLUDE_FILTERS + EXCLUDE_FX_FILTERS, name_matches_cfg=True)) pytest.skip("Fixed input size model > limit.")
@pytest.mark.parametrize('batch_size', [2])
def test_model_backward_fx(model_name, batch_size):
"""Symbolically trace each model and run single backward pass through the resulting GraphModule"""
if not has_fx_feature_extraction:
pytest.skip("Can't test FX. Torch >= 1.10 and Torchvision >= 0.11 are required.")
input_size = _get_input_size(model_name=model_name, target=TARGET_BWD_FX_SIZE) model = create_model(model_name, pretrained=False, num_classes=42)
if max(input_size) > MAX_BWD_FX_SIZE: model.train()
pytest.skip("Fixed input size model > limit.") num_params = sum([x.numel() for x in model.parameters()])
if 'GITHUB_ACTIONS' in os.environ and num_params > 100e6:
pytest.skip("Skipping FX backward test on model with more than 100M params.")
model = create_model(model_name, pretrained=False, num_classes=42) model = _create_fx_model(model, train=True)
model.train() outputs = tuple(model(torch.randn((batch_size, *input_size))).values())
num_params = sum([x.numel() for x in model.parameters()]) if isinstance(outputs, tuple):
if 'GITHUB_ACTIONS' in os.environ and num_params > 100e6: outputs = torch.cat(outputs)
pytest.skip("Skipping FX backward test on model with more than 100M params.") outputs.mean().backward()
for n, x in model.named_parameters():
assert x.grad is not None, f'No gradient for {n}'
num_grad = sum([x.grad.numel() for x in model.parameters() if x.grad is not None])
model = _create_fx_model(model, train=True) assert outputs.shape[-1] == 42
outputs = tuple(model(torch.randn((batch_size, *input_size))).values()) assert num_params == num_grad, 'Some parameters are missing gradients'
if isinstance(outputs, tuple): assert not torch.isnan(outputs).any(), 'Output included NaNs'
outputs = torch.cat(outputs)
outputs.mean().backward()
for n, x in model.named_parameters():
assert x.grad is not None, f'No gradient for {n}'
num_grad = sum([x.grad.numel() for x in model.parameters() if x.grad is not None])
assert outputs.shape[-1] == 42
assert num_params == num_grad, 'Some parameters are missing gradients'
assert not torch.isnan(outputs).any(), 'Output included NaNs'
if 'GITHUB_ACTIONS' not in os.environ:
# FIXME this test is causing GitHub actions to run out of RAM and abruptly kill the test process
# reason: model is scripted after fx tracing, but beit has torch.jit.is_scripting() control flow # reason: model is scripted after fx tracing, but beit has torch.jit.is_scripting() control flow
EXCLUDE_FX_JIT_FILTERS = [ EXCLUDE_FX_JIT_FILTERS = [

Loading…
Cancel
Save